|
|
@@ -10,18 +10,19 @@ import (
|
|
10
|
10
|
uuid "github.com/satori/go.uuid"
|
|
11
|
11
|
"go.uber.org/zap"
|
|
12
|
12
|
|
|
|
13
|
+ "github.com/9seconds/mtg/client"
|
|
13
|
14
|
"github.com/9seconds/mtg/config"
|
|
14
|
|
- "github.com/9seconds/mtg/obfuscated2"
|
|
15
|
15
|
"github.com/9seconds/mtg/telegram"
|
|
16
|
16
|
"github.com/9seconds/mtg/wrappers"
|
|
17
|
17
|
)
|
|
18
|
18
|
|
|
19
|
19
|
// Server is an insgtance of MTPROTO proxy.
|
|
20
|
20
|
type Server struct {
|
|
21
|
|
- conf *config.Config
|
|
22
|
|
- logger *zap.SugaredLogger
|
|
23
|
|
- stats *Stats
|
|
24
|
|
- tg telegram.Telegram
|
|
|
21
|
+ conf *config.Config
|
|
|
22
|
+ logger *zap.SugaredLogger
|
|
|
23
|
+ stats *Stats
|
|
|
24
|
+ tg telegram.Telegram
|
|
|
25
|
+ clientInit client.Init
|
|
25
|
26
|
}
|
|
26
|
27
|
|
|
27
|
28
|
// Serve does MTPROTO proxying.
|
|
|
@@ -59,7 +60,7 @@ func (s *Server) accept(conn net.Conn) {
|
|
59
|
60
|
"socketid", socketID,
|
|
60
|
61
|
)
|
|
61
|
62
|
|
|
62
|
|
- clientConn, dc, err := s.getClientStream(ctx, cancel, conn, socketID)
|
|
|
63
|
+ dc, clientConn, err := s.getClientStream(ctx, cancel, conn, socketID)
|
|
63
|
64
|
if err != nil {
|
|
64
|
65
|
s.logger.Warnw("Cannot initialize client connection",
|
|
65
|
66
|
"addr", conn.RemoteAddr().String(),
|
|
|
@@ -99,24 +100,17 @@ func (s *Server) accept(conn net.Conn) {
|
|
99
|
100
|
)
|
|
100
|
101
|
}
|
|
101
|
102
|
|
|
102
|
|
-func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc, conn net.Conn, socketID string) (io.ReadWriteCloser, int16, error) {
|
|
103
|
|
- wConn := wrappers.NewTimeoutRWC(conn, s.conf.TimeoutRead, s.conf.TimeoutWrite)
|
|
104
|
|
- wConn = wrappers.NewTrafficRWC(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
|
105
|
|
- frame, err := obfuscated2.ExtractFrame(wConn)
|
|
|
103
|
+func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc, conn net.Conn, socketID string) (int16, io.ReadWriteCloser, error) {
|
|
|
104
|
+ dc, socket, err := s.clientInit(conn, s.conf)
|
|
106
|
105
|
if err != nil {
|
|
107
|
|
- return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
|
106
|
+ return 0, nil, errors.Annotate(err, "Cannot init client connection")
|
|
108
|
107
|
}
|
|
109
|
108
|
|
|
110
|
|
- obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(s.conf.Secret, frame)
|
|
111
|
|
- if err != nil {
|
|
112
|
|
- return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
113
|
|
- }
|
|
114
|
|
-
|
|
115
|
|
- wConn = wrappers.NewLogRWC(wConn, s.logger, socketID, "client")
|
|
116
|
|
- wConn = wrappers.NewStreamCipherRWC(wConn, obfs2.Encryptor, obfs2.Decryptor)
|
|
117
|
|
- wConn = wrappers.NewCtxRWC(ctx, cancel, wConn)
|
|
|
109
|
+ socket = wrappers.NewTrafficRWC(socket, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
|
|
110
|
+ socket = wrappers.NewLogRWC(socket, s.logger, socketID, "client")
|
|
|
111
|
+ socket = wrappers.NewCtxRWC(ctx, cancel, socket)
|
|
118
|
112
|
|
|
119
|
|
- return wConn, dc, nil
|
|
|
113
|
+ return dc, socket, nil
|
|
120
|
114
|
}
|
|
121
|
115
|
|
|
122
|
116
|
func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFunc, dc int16, socketID string) (io.ReadWriteCloser, error) {
|
|
|
@@ -140,9 +134,10 @@ func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFun
|
|
140
|
134
|
// NewServer creates new instance of MTPROTO proxy.
|
|
141
|
135
|
func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
|
|
142
|
136
|
return &Server{
|
|
143
|
|
- conf: conf,
|
|
144
|
|
- logger: logger,
|
|
145
|
|
- stats: stat,
|
|
146
|
|
- tg: telegram.NewDirectTelegram(conf),
|
|
|
137
|
+ conf: conf,
|
|
|
138
|
+ logger: logger,
|
|
|
139
|
+ stats: stat,
|
|
|
140
|
+ tg: telegram.NewDirectTelegram(conf),
|
|
|
141
|
+ clientInit: client.DirectInit,
|
|
147
|
142
|
}
|
|
148
|
143
|
}
|