9seconds 6 лет назад
Родитель
Сommit
691954bd16

+ 8
- 8
cli/proxy.go Просмотреть файл

@@ -14,7 +14,6 @@ import (
14 14
 	"github.com/9seconds/mtg/obfuscated2"
15 15
 	"github.com/9seconds/mtg/proxy"
16 16
 	"github.com/9seconds/mtg/stats"
17
-	"github.com/9seconds/mtg/telegram"
18 17
 	"github.com/9seconds/mtg/utils"
19 18
 )
20 19
 
@@ -79,14 +78,15 @@ func Proxy() error {
79 78
 	app := &proxy.Proxy{
80 79
 		Logger:  zap.S().Named("proxy"),
81 80
 		Context: ctx,
81
+		ClientProtocolMaker: obfuscated2.MakeClientProtocol,
82
+		TelegramProtocolMaker: obfuscated2.MakeTelegramProtocol,
82 83
 	}
83
-	if len(config.C.AdTag) == 0 {
84
-		app.TelegramProtocolMaker = obfuscated2.MakeTelegramProtocol
85
-		app.TelegramDialer = telegram.NewDirectTelegram()
86
-	}
87
-	if config.C.SecretMode != config.SecretModeTLS {
88
-		app.ClientProtocolMaker = obfuscated2.MakeClientProtocol
89
-	}
84
+	// if len(config.C.AdTag) == 0 {
85
+	// 	app.TelegramProtocolMaker = obfuscated2.MakeTelegramProtocol
86
+	// }
87
+	// if config.C.SecretMode != config.SecretModeTLS {
88
+	// 	app.ClientProtocolMaker = obfuscated2.MakeClientProtocol
89
+	// }
90 90
 
91 91
 	app.Serve(proxyListener)
92 92
 

+ 22
- 8
obfuscated2/client_protocol.go Просмотреть файл

@@ -20,7 +20,21 @@ import (
20 20
 const clientProtocolHandshakeTimeout = 10 * time.Second
21 21
 
22 22
 type ClientProtocol struct {
23
-	protocol.BaseProtocol
23
+	connectionType     conntypes.ConnectionType
24
+	connectionProtocol conntypes.ConnectionProtocol
25
+	dc                 conntypes.DC
26
+}
27
+
28
+func (c *ClientProtocol) ConnectionType() conntypes.ConnectionType {
29
+	return c.connectionType
30
+}
31
+
32
+func (c *ClientProtocol) ConnectionProtocol() conntypes.ConnectionProtocol {
33
+	return c.connectionProtocol
34
+}
35
+
36
+func (c *ClientProtocol) DC() conntypes.DC {
37
+	return c.dc
24 38
 }
25 39
 
26 40
 func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error) {
@@ -46,23 +60,23 @@ func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrapp
46 60
 	magic := decryptedFrame.Magic()
47 61
 	switch {
48 62
 	case bytes.Equal(magic, conntypes.ConnectionTagAbridged):
49
-		c.ConnectionType = conntypes.ConnectionTypeAbridged
63
+		c.connectionType = conntypes.ConnectionTypeAbridged
50 64
 	case bytes.Equal(magic, conntypes.ConnectionTagIntermediate):
51
-		c.ConnectionType = conntypes.ConnectionTypeIntermediate
65
+		c.connectionType = conntypes.ConnectionTypeIntermediate
52 66
 	case bytes.Equal(magic, conntypes.ConnectionTagSecure):
53
-		c.ConnectionType = conntypes.ConnectionTypeSecure
67
+		c.connectionType = conntypes.ConnectionTypeSecure
54 68
 	default:
55 69
 		return nil, errors.New("Unknown connection type")
56 70
 	}
57 71
 
58
-	c.ConnectionProtocol = conntypes.ConnectionProtocolIPv4
72
+	c.connectionProtocol = conntypes.ConnectionProtocolIPv4
59 73
 	if socket.LocalAddr().IP.To4() == nil {
60
-		c.ConnectionProtocol = conntypes.ConnectionProtocolIPv6
74
+		c.connectionProtocol = conntypes.ConnectionProtocolIPv6
61 75
 	}
62 76
 
63 77
 	buf := bytes.NewReader(decryptedFrame.DC())
64
-	if err := binary.Read(buf, binary.LittleEndian, &c.DC); err != nil {
65
-		c.DC = conntypes.DCDefaultIdx
78
+	if err := binary.Read(buf, binary.LittleEndian, &c.dc); err != nil {
79
+		c.dc = conntypes.DCDefaultIdx
66 80
 	}
67 81
 
68 82
 	antiReplayKey := decryptedFrame.Unique()

+ 7
- 13
obfuscated2/telegram_protocol.go Просмотреть файл

@@ -10,17 +10,13 @@ import (
10 10
 	"github.com/9seconds/mtg/wrappers"
11 11
 )
12 12
 
13
-type TelegramProtocol struct {
14
-	protocol.BaseProtocol
15
-
16
-	dialer telegram.Telegram
17
-}
13
+type TelegramProtocol struct{}
18 14
 
19 15
 func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wrap, error) {
20
-	socket, err := t.dialer.Dial(req.Ctx,
16
+	socket, err := telegram.Direct.Dial(req.Ctx,
21 17
 		req.Cancel,
22
-		req.ClientProtocol.GetDC(),
23
-		req.ClientProtocol.GetConnectionProtocol())
18
+		req.ClientProtocol.DC(),
19
+		req.ClientProtocol.ConnectionProtocol())
24 20
 	if err != nil {
25 21
 		return nil, fmt.Errorf("cannot dial to telegram: %w", err)
26 22
 	}
@@ -43,10 +39,8 @@ func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wr
43 39
 	return wrappers.NewObfuscated2(socket, encryptor, decryptor), nil
44 40
 }
45 41
 
46
-func MakeTelegramProtocol(dialer telegram.Telegram) protocol.TelegramProtocol {
47
-	return &TelegramProtocol{
48
-		dialer: dialer,
49
-	}
42
+func MakeTelegramProtocol() protocol.TelegramProtocol {
43
+	return &TelegramProtocol{}
50 44
 }
51 45
 
52 46
 func generateFrame(cp protocol.ClientProtocol) (fm Frame) {
@@ -70,7 +64,7 @@ func generateFrame(cp protocol.ClientProtocol) (fm Frame) {
70 64
 			continue
71 65
 		}
72 66
 
73
-		copy(fm.Magic(), cp.GetConnectionType().Tag())
67
+		copy(fm.Magic(), cp.ConnectionType().Tag())
74 68
 
75 69
 		return
76 70
 	}

+ 0
- 21
protocol/base_protocol.go Просмотреть файл

@@ -1,21 +0,0 @@
1
-package protocol
2
-
3
-import "github.com/9seconds/mtg/conntypes"
4
-
5
-type BaseProtocol struct {
6
-	ConnectionType     conntypes.ConnectionType
7
-	ConnectionProtocol conntypes.ConnectionProtocol
8
-	DC                 conntypes.DC
9
-}
10
-
11
-func (b *BaseProtocol) GetConnectionType() conntypes.ConnectionType {
12
-	return b.ConnectionType
13
-}
14
-
15
-func (b *BaseProtocol) GetConnectionProtocol() conntypes.ConnectionProtocol {
16
-	return b.ConnectionProtocol
17
-}
18
-
19
-func (b *BaseProtocol) GetDC() conntypes.DC {
20
-	return b.DC
21
-}

+ 5
- 7
protocol/interfaces.go Просмотреть файл

@@ -2,21 +2,19 @@ package protocol
2 2
 
3 3
 import (
4 4
 	"github.com/9seconds/mtg/conntypes"
5
-	"github.com/9seconds/mtg/telegram"
6 5
 	"github.com/9seconds/mtg/wrappers"
7 6
 )
8 7
 
9 8
 type ClientProtocol interface {
10 9
 	Handshake(wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error)
11
-	GetConnectionType() conntypes.ConnectionType
12
-	GetConnectionProtocol() conntypes.ConnectionProtocol
13
-	GetDC() conntypes.DC
10
+	ConnectionType() conntypes.ConnectionType
11
+	ConnectionProtocol() conntypes.ConnectionProtocol
12
+	DC() conntypes.DC
14 13
 }
15 14
 
16
-type ClientProtocolMaker func() ClientProtocol
17
-
18 15
 type TelegramProtocol interface {
19 16
 	Handshake(*TelegramRequest) (wrappers.Wrap, error)
20 17
 }
21 18
 
22
-type TelegramProtocolMaker func(telegram.Telegram) TelegramProtocol
19
+type ClientProtocolMaker func() ClientProtocol
20
+type TelegramProtocolMaker func() TelegramProtocol

+ 3
- 5
proxy/proxy.go Просмотреть файл

@@ -12,7 +12,6 @@ import (
12 12
 	"github.com/9seconds/mtg/conntypes"
13 13
 	"github.com/9seconds/mtg/protocol"
14 14
 	"github.com/9seconds/mtg/stats"
15
-	"github.com/9seconds/mtg/telegram"
16 15
 	"github.com/9seconds/mtg/utils"
17 16
 	"github.com/9seconds/mtg/wrappers"
18 17
 )
@@ -24,7 +23,6 @@ type Proxy struct {
24 23
 	Context               context.Context
25 24
 	ClientProtocolMaker   protocol.ClientProtocolMaker
26 25
 	TelegramProtocolMaker protocol.TelegramProtocolMaker
27
-	TelegramDialer        telegram.Telegram
28 26
 }
29 27
 
30 28
 func (p *Proxy) Serve(listener net.Listener) {
@@ -77,8 +75,8 @@ func (p *Proxy) accept(conn net.Conn) {
77 75
 	}
78 76
 	defer wrappedConn.Close()
79 77
 
80
-	stats.S.ClientConnected(clientProtocol.GetConnectionType(), wrappedConn.RemoteAddr())
81
-	defer stats.S.ClientDisconnected(clientProtocol.GetConnectionType(), wrappedConn.RemoteAddr())
78
+	stats.S.ClientConnected(clientProtocol.ConnectionType(), wrappedConn.RemoteAddr())
79
+	defer stats.S.ClientDisconnected(clientProtocol.ConnectionType(), wrappedConn.RemoteAddr())
82 80
 	logger.Infow("Client connected", "addr", conn.RemoteAddr())
83 81
 
84 82
 	req := &protocol.TelegramRequest{
@@ -100,7 +98,7 @@ func (p *Proxy) accept(conn net.Conn) {
100 98
 }
101 99
 
102 100
 func (p *Proxy) acceptDirectConnection(request *protocol.TelegramRequest) error {
103
-	telegramProtocol := p.TelegramProtocolMaker(p.TelegramDialer)
101
+	telegramProtocol := p.TelegramProtocolMaker()
104 102
 	telegramConnRaw, err := telegramProtocol.Handshake(request)
105 103
 	if err != nil {
106 104
 		return err

+ 3
- 1
telegram/direct.go Просмотреть файл

@@ -8,6 +8,8 @@ import (
8 8
 	"github.com/9seconds/mtg/wrappers"
9 9
 )
10 10
 
11
+var Direct = newDirectTelegram()
12
+
11 13
 const (
12 14
 	directV4DefaultIdx conntypes.DC = 1
13 15
 	directV6DefaultIdx conntypes.DC = 1
@@ -48,7 +50,7 @@ func (d *directTelegram) Dial(ctx context.Context,
48 50
 	return d.baseTelegram.dial(ctx, cancel, dc-1, protocol)
49 51
 }
50 52
 
51
-func NewDirectTelegram() Telegram {
53
+func newDirectTelegram() Telegram {
52 54
 	return &directTelegram{
53 55
 		baseTelegram: baseTelegram{
54 56
 			dialer:      net.Dialer{Timeout: telegramDialTimeout},

+ 2
- 0
telegram/middle.go Просмотреть файл

@@ -16,6 +16,8 @@ import (
16 16
 
17 17
 const middleTelegramBackgroundUpdateEvery = time.Hour
18 18
 
19
+var Middle = NewMiddleTelegram()
20
+
19 21
 type middleTelegram struct {
20 22
 	baseTelegram
21 23
 

Загрузка…
Отмена
Сохранить