Explorar el Código

Add client package

tags/0.9
9seconds hace 7 años
padre
commit
daca606058
Se han modificado 5 ficheros con 60 adiciones y 28 borrados
  1. 10
    0
      client/client.go
  2. 29
    0
      client/direct.go
  3. 1
    1
      obfuscated2/frame_test.go
  4. 1
    3
      obfuscated2/obfuscated2.go
  5. 19
    24
      proxy/server.go

+ 10
- 0
client/client.go Ver fichero

@@ -0,0 +1,10 @@
1
+package client
2
+
3
+import (
4
+	"io"
5
+	"net"
6
+
7
+	"github.com/9seconds/mtg/config"
8
+)
9
+
10
+type Init func(net.Conn, *config.Config) (int16, io.ReadWriteCloser, error)

+ 29
- 0
client/direct.go Ver fichero

@@ -0,0 +1,29 @@
1
+package client
2
+
3
+import (
4
+	"io"
5
+	"net"
6
+
7
+	"github.com/juju/errors"
8
+
9
+	"github.com/9seconds/mtg/config"
10
+	"github.com/9seconds/mtg/obfuscated2"
11
+	"github.com/9seconds/mtg/wrappers"
12
+)
13
+
14
+func DirectInit(conn net.Conn, conf *config.Config) (int16, io.ReadWriteCloser, error) {
15
+	socket := wrappers.NewTimeoutRWC(conn, conf.TimeoutRead, conf.TimeoutWrite)
16
+	frame, err := obfuscated2.ExtractFrame(socket)
17
+	if err != nil {
18
+		return 0, nil, errors.Annotate(err, "Cannot extract frame")
19
+	}
20
+
21
+	obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
22
+	if err != nil {
23
+		return 0, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
24
+	}
25
+
26
+	socket = wrappers.NewStreamCipherRWC(socket, obfs2.Encryptor, obfs2.Decryptor)
27
+
28
+	return dc, socket, nil
29
+}

+ 1
- 1
obfuscated2/frame_test.go Ver fichero

@@ -34,7 +34,7 @@ func TestFrameMagic(t *testing.T) {
34 34
 }
35 35
 
36 36
 func TestFrameDC(t *testing.T) {
37
-	assert.Equal(t, int16(770), makeFrame().DC())
37
+	assert.Equal(t, int16(771), makeFrame().DC())
38 38
 }
39 39
 
40 40
 func TestFrameValid(t *testing.T) {

+ 1
- 3
obfuscated2/obfuscated2.go Ver fichero

@@ -19,9 +19,7 @@ type Obfuscated2 struct {
19 19
 // details: http://telegra.ph/telegram-blocks-wtf-05-26
20 20
 //
21 21
 // Beware, link above is in russian.
22
-func ParseObfuscated2ClientFrame(secret, data []byte) (*Obfuscated2, int16, error) {
23
-	frame := Frame(data)
24
-
22
+func ParseObfuscated2ClientFrame(secret []byte, frame Frame) (*Obfuscated2, int16, error) {
25 23
 	decHasher := sha256.New()
26 24
 	decHasher.Write(frame.Key()) // nolint: errcheck
27 25
 	decHasher.Write(secret)      // nolint: errcheck

+ 19
- 24
proxy/server.go Ver fichero

@@ -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
 }

Loading…
Cancelar
Guardar