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

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

14
 	"github.com/9seconds/mtg/obfuscated2"
14
 	"github.com/9seconds/mtg/obfuscated2"
15
 	"github.com/9seconds/mtg/proxy"
15
 	"github.com/9seconds/mtg/proxy"
16
 	"github.com/9seconds/mtg/stats"
16
 	"github.com/9seconds/mtg/stats"
17
-	"github.com/9seconds/mtg/telegram"
18
 	"github.com/9seconds/mtg/utils"
17
 	"github.com/9seconds/mtg/utils"
19
 )
18
 )
20
 
19
 
79
 	app := &proxy.Proxy{
78
 	app := &proxy.Proxy{
80
 		Logger:  zap.S().Named("proxy"),
79
 		Logger:  zap.S().Named("proxy"),
81
 		Context: ctx,
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
 	app.Serve(proxyListener)
91
 	app.Serve(proxyListener)
92
 
92
 

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

20
 const clientProtocolHandshakeTimeout = 10 * time.Second
20
 const clientProtocolHandshakeTimeout = 10 * time.Second
21
 
21
 
22
 type ClientProtocol struct {
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
 func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error) {
40
 func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error) {
46
 	magic := decryptedFrame.Magic()
60
 	magic := decryptedFrame.Magic()
47
 	switch {
61
 	switch {
48
 	case bytes.Equal(magic, conntypes.ConnectionTagAbridged):
62
 	case bytes.Equal(magic, conntypes.ConnectionTagAbridged):
49
-		c.ConnectionType = conntypes.ConnectionTypeAbridged
63
+		c.connectionType = conntypes.ConnectionTypeAbridged
50
 	case bytes.Equal(magic, conntypes.ConnectionTagIntermediate):
64
 	case bytes.Equal(magic, conntypes.ConnectionTagIntermediate):
51
-		c.ConnectionType = conntypes.ConnectionTypeIntermediate
65
+		c.connectionType = conntypes.ConnectionTypeIntermediate
52
 	case bytes.Equal(magic, conntypes.ConnectionTagSecure):
66
 	case bytes.Equal(magic, conntypes.ConnectionTagSecure):
53
-		c.ConnectionType = conntypes.ConnectionTypeSecure
67
+		c.connectionType = conntypes.ConnectionTypeSecure
54
 	default:
68
 	default:
55
 		return nil, errors.New("Unknown connection type")
69
 		return nil, errors.New("Unknown connection type")
56
 	}
70
 	}
57
 
71
 
58
-	c.ConnectionProtocol = conntypes.ConnectionProtocolIPv4
72
+	c.connectionProtocol = conntypes.ConnectionProtocolIPv4
59
 	if socket.LocalAddr().IP.To4() == nil {
73
 	if socket.LocalAddr().IP.To4() == nil {
60
-		c.ConnectionProtocol = conntypes.ConnectionProtocolIPv6
74
+		c.connectionProtocol = conntypes.ConnectionProtocolIPv6
61
 	}
75
 	}
62
 
76
 
63
 	buf := bytes.NewReader(decryptedFrame.DC())
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
 	antiReplayKey := decryptedFrame.Unique()
82
 	antiReplayKey := decryptedFrame.Unique()

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

10
 	"github.com/9seconds/mtg/wrappers"
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
 func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wrap, error) {
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
 		req.Cancel,
17
 		req.Cancel,
22
-		req.ClientProtocol.GetDC(),
23
-		req.ClientProtocol.GetConnectionProtocol())
18
+		req.ClientProtocol.DC(),
19
+		req.ClientProtocol.ConnectionProtocol())
24
 	if err != nil {
20
 	if err != nil {
25
 		return nil, fmt.Errorf("cannot dial to telegram: %w", err)
21
 		return nil, fmt.Errorf("cannot dial to telegram: %w", err)
26
 	}
22
 	}
43
 	return wrappers.NewObfuscated2(socket, encryptor, decryptor), nil
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
 func generateFrame(cp protocol.ClientProtocol) (fm Frame) {
46
 func generateFrame(cp protocol.ClientProtocol) (fm Frame) {
70
 			continue
64
 			continue
71
 		}
65
 		}
72
 
66
 
73
-		copy(fm.Magic(), cp.GetConnectionType().Tag())
67
+		copy(fm.Magic(), cp.ConnectionType().Tag())
74
 
68
 
75
 		return
69
 		return
76
 	}
70
 	}

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

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
 
2
 
3
 import (
3
 import (
4
 	"github.com/9seconds/mtg/conntypes"
4
 	"github.com/9seconds/mtg/conntypes"
5
-	"github.com/9seconds/mtg/telegram"
6
 	"github.com/9seconds/mtg/wrappers"
5
 	"github.com/9seconds/mtg/wrappers"
7
 )
6
 )
8
 
7
 
9
 type ClientProtocol interface {
8
 type ClientProtocol interface {
10
 	Handshake(wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error)
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
 type TelegramProtocol interface {
15
 type TelegramProtocol interface {
19
 	Handshake(*TelegramRequest) (wrappers.Wrap, error)
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
 	"github.com/9seconds/mtg/conntypes"
12
 	"github.com/9seconds/mtg/conntypes"
13
 	"github.com/9seconds/mtg/protocol"
13
 	"github.com/9seconds/mtg/protocol"
14
 	"github.com/9seconds/mtg/stats"
14
 	"github.com/9seconds/mtg/stats"
15
-	"github.com/9seconds/mtg/telegram"
16
 	"github.com/9seconds/mtg/utils"
15
 	"github.com/9seconds/mtg/utils"
17
 	"github.com/9seconds/mtg/wrappers"
16
 	"github.com/9seconds/mtg/wrappers"
18
 )
17
 )
24
 	Context               context.Context
23
 	Context               context.Context
25
 	ClientProtocolMaker   protocol.ClientProtocolMaker
24
 	ClientProtocolMaker   protocol.ClientProtocolMaker
26
 	TelegramProtocolMaker protocol.TelegramProtocolMaker
25
 	TelegramProtocolMaker protocol.TelegramProtocolMaker
27
-	TelegramDialer        telegram.Telegram
28
 }
26
 }
29
 
27
 
30
 func (p *Proxy) Serve(listener net.Listener) {
28
 func (p *Proxy) Serve(listener net.Listener) {
77
 	}
75
 	}
78
 	defer wrappedConn.Close()
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
 	logger.Infow("Client connected", "addr", conn.RemoteAddr())
80
 	logger.Infow("Client connected", "addr", conn.RemoteAddr())
83
 
81
 
84
 	req := &protocol.TelegramRequest{
82
 	req := &protocol.TelegramRequest{
100
 }
98
 }
101
 
99
 
102
 func (p *Proxy) acceptDirectConnection(request *protocol.TelegramRequest) error {
100
 func (p *Proxy) acceptDirectConnection(request *protocol.TelegramRequest) error {
103
-	telegramProtocol := p.TelegramProtocolMaker(p.TelegramDialer)
101
+	telegramProtocol := p.TelegramProtocolMaker()
104
 	telegramConnRaw, err := telegramProtocol.Handshake(request)
102
 	telegramConnRaw, err := telegramProtocol.Handshake(request)
105
 	if err != nil {
103
 	if err != nil {
106
 		return err
104
 		return err

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

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

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

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

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