Pārlūkot izejas kodu

Add client package

tags/0.9
9seconds 8 gadus atpakaļ
vecāks
revīzija
daca606058
5 mainītis faili ar 60 papildinājumiem un 28 dzēšanām
  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 Parādīt failu

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 Parādīt failu

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 Parādīt failu

34
 }
34
 }
35
 
35
 
36
 func TestFrameDC(t *testing.T) {
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
 func TestFrameValid(t *testing.T) {
40
 func TestFrameValid(t *testing.T) {

+ 1
- 3
obfuscated2/obfuscated2.go Parādīt failu

19
 // details: http://telegra.ph/telegram-blocks-wtf-05-26
19
 // details: http://telegra.ph/telegram-blocks-wtf-05-26
20
 //
20
 //
21
 // Beware, link above is in russian.
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
 	decHasher := sha256.New()
23
 	decHasher := sha256.New()
26
 	decHasher.Write(frame.Key()) // nolint: errcheck
24
 	decHasher.Write(frame.Key()) // nolint: errcheck
27
 	decHasher.Write(secret)      // nolint: errcheck
25
 	decHasher.Write(secret)      // nolint: errcheck

+ 19
- 24
proxy/server.go Parādīt failu

10
 	uuid "github.com/satori/go.uuid"
10
 	uuid "github.com/satori/go.uuid"
11
 	"go.uber.org/zap"
11
 	"go.uber.org/zap"
12
 
12
 
13
+	"github.com/9seconds/mtg/client"
13
 	"github.com/9seconds/mtg/config"
14
 	"github.com/9seconds/mtg/config"
14
-	"github.com/9seconds/mtg/obfuscated2"
15
 	"github.com/9seconds/mtg/telegram"
15
 	"github.com/9seconds/mtg/telegram"
16
 	"github.com/9seconds/mtg/wrappers"
16
 	"github.com/9seconds/mtg/wrappers"
17
 )
17
 )
18
 
18
 
19
 // Server is an insgtance of MTPROTO proxy.
19
 // Server is an insgtance of MTPROTO proxy.
20
 type Server struct {
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
 // Serve does MTPROTO proxying.
28
 // Serve does MTPROTO proxying.
59
 		"socketid", socketID,
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
 	if err != nil {
64
 	if err != nil {
64
 		s.logger.Warnw("Cannot initialize client connection",
65
 		s.logger.Warnw("Cannot initialize client connection",
65
 			"addr", conn.RemoteAddr().String(),
66
 			"addr", conn.RemoteAddr().String(),
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
 	if err != nil {
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
 func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFunc, dc int16, socketID string) (io.ReadWriteCloser, error) {
116
 func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFunc, dc int16, socketID string) (io.ReadWriteCloser, error) {
140
 // NewServer creates new instance of MTPROTO proxy.
134
 // NewServer creates new instance of MTPROTO proxy.
141
 func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
135
 func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
142
 	return &Server{
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
 }

Notiek ielāde…
Atcelt
Saglabāt