|
|
@@ -6,6 +6,7 @@ import (
|
|
6
|
6
|
"net"
|
|
7
|
7
|
"strconv"
|
|
8
|
8
|
"sync"
|
|
|
9
|
+ "time"
|
|
9
|
10
|
|
|
10
|
11
|
"github.com/9seconds/mtg/obfuscated2"
|
|
11
|
12
|
"github.com/juju/errors"
|
|
|
@@ -16,12 +17,14 @@ import (
|
|
16
|
17
|
const bufferSize = 4096
|
|
17
|
18
|
|
|
18
|
19
|
type Server struct {
|
|
19
|
|
- ip net.IP
|
|
20
|
|
- port int
|
|
21
|
|
- secret []byte
|
|
22
|
|
- logger *zap.SugaredLogger
|
|
23
|
|
- lsock net.Listener
|
|
24
|
|
- ctx context.Context
|
|
|
20
|
+ ip net.IP
|
|
|
21
|
+ port int
|
|
|
22
|
+ secret []byte
|
|
|
23
|
+ logger *zap.SugaredLogger
|
|
|
24
|
+ lsock net.Listener
|
|
|
25
|
+ ctx context.Context
|
|
|
26
|
+ readTimeout time.Duration
|
|
|
27
|
+ writeTimeout time.Duration
|
|
25
|
28
|
}
|
|
26
|
29
|
|
|
27
|
30
|
func (s *Server) Serve() error {
|
|
|
@@ -98,7 +101,8 @@ func (s *Server) makeSocketID() string {
|
|
98
|
101
|
}
|
|
99
|
102
|
|
|
100
|
103
|
func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, int16, error) {
|
|
101
|
|
- frame, err := obfuscated2.ExtractFrame(conn)
|
|
|
104
|
+ wConn := newTimeoutReadWriteCloser(conn, s.readTimeout, s.writeTimeout)
|
|
|
105
|
+ frame, err := obfuscated2.ExtractFrame(wConn)
|
|
102
|
106
|
if err != nil {
|
|
103
|
107
|
return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
104
|
108
|
}
|
|
|
@@ -108,7 +112,7 @@ func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel cont
|
|
108
|
112
|
return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
109
|
113
|
}
|
|
110
|
114
|
|
|
111
|
|
- wConn := newLogReadWriteCloser(conn, s.logger, socketID, "client")
|
|
|
115
|
+ wConn = newLogReadWriteCloser(wConn, s.logger, socketID, "client")
|
|
112
|
116
|
wConn = newCipherReadWriteCloser(conn, obfs2)
|
|
113
|
117
|
wConn = newCtxReadWriteCloser(wConn, ctx, cancel)
|
|
114
|
118
|
|
|
|
@@ -116,17 +120,18 @@ func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel cont
|
|
116
|
120
|
}
|
|
117
|
121
|
|
|
118
|
122
|
func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, error) {
|
|
119
|
|
- socket, err := dialToTelegram(dc)
|
|
|
123
|
+ socket, err := dialToTelegram(dc, s.readTimeout)
|
|
120
|
124
|
if err != nil {
|
|
121
|
125
|
return nil, errors.Annotate(err, "Cannot dial")
|
|
122
|
126
|
}
|
|
|
127
|
+ wConn := newTimeoutReadWriteCloser(socket, s.readTimeout, s.writeTimeout)
|
|
123
|
128
|
|
|
124
|
129
|
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
|
125
|
130
|
if n, err := socket.Write(frame); err != nil || n != len(frame) {
|
|
126
|
131
|
return nil, errors.Annotate(err, "Cannot write hadnshake frame")
|
|
127
|
132
|
}
|
|
128
|
133
|
|
|
129
|
|
- wConn := newLogReadWriteCloser(socket, s.logger, socketID, "telegram")
|
|
|
134
|
+ wConn = newLogReadWriteCloser(socket, s.logger, socketID, "telegram")
|
|
130
|
135
|
wConn = newCipherReadWriteCloser(wConn, obfs2)
|
|
131
|
136
|
wConn = newCtxReadWriteCloser(wConn, ctx, cancel)
|
|
132
|
137
|
|
|
|
@@ -138,15 +143,17 @@ func (s *Server) pipe(wait *sync.WaitGroup, reader io.Reader, writer io.Writer)
|
|
138
|
143
|
|
|
139
|
144
|
buf := make([]byte, bufferSize)
|
|
140
|
145
|
io.CopyBuffer(writer, reader, buf)
|
|
141
|
|
-
|
|
142
|
146
|
}
|
|
143
|
147
|
|
|
144
|
|
-func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger) *Server {
|
|
|
148
|
+func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
|
|
|
149
|
+ readTimeout, writeTimeout time.Duration) *Server {
|
|
145
|
150
|
return &Server{
|
|
146
|
|
- ip: ip,
|
|
147
|
|
- port: port,
|
|
148
|
|
- secret: secret,
|
|
149
|
|
- ctx: context.Background(),
|
|
150
|
|
- logger: logger,
|
|
|
151
|
+ ip: ip,
|
|
|
152
|
+ port: port,
|
|
|
153
|
+ secret: secret,
|
|
|
154
|
+ ctx: context.Background(),
|
|
|
155
|
+ logger: logger,
|
|
|
156
|
+ readTimeout: readTimeout,
|
|
|
157
|
+ writeTimeout: writeTimeout,
|
|
151
|
158
|
}
|
|
152
|
159
|
}
|