Просмотр исходного кода

Propagate to middle client

tags/0.9
9seconds 7 лет назад
Родитель
Сommit
a7eb29cc94
4 измененных файлов: 45 добавлений и 27 удалений
  1. 25
    0
      client/middle.go
  2. 8
    24
      main.go
  3. 10
    2
      proxy/server.go
  4. 2
    1
      telegram/direct.go

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

1
+package client
2
+
3
+import (
4
+	"net"
5
+
6
+	"github.com/9seconds/mtg/config"
7
+	"github.com/9seconds/mtg/mtproto"
8
+	mtwrappers "github.com/9seconds/mtg/mtproto/wrappers"
9
+	"github.com/9seconds/mtg/wrappers"
10
+)
11
+
12
+func MiddleInit(conn net.Conn, conf *config.Config) (*mtproto.ConnectionOpts, wrappers.ReadWriteCloserWithAddr, error) {
13
+	opts, newConn, err := DirectInit(conn, conf)
14
+	if err != nil {
15
+		return nil, nil, err
16
+	}
17
+
18
+	if opts.ConnectionType == mtproto.ConnectionTypeAbridged {
19
+		newConn = mtwrappers.NewAbridgedRWC(newConn, opts)
20
+	} else {
21
+		newConn = mtwrappers.NewIntermediateRWC(newConn, opts)
22
+	}
23
+
24
+	return opts, newConn, nil
25
+}

+ 8
- 24
main.go Просмотреть файл

4
 
4
 
5
 import (
5
 import (
6
 	"encoding/json"
6
 	"encoding/json"
7
-	"fmt"
8
 	"io"
7
 	"io"
9
 	"math/rand"
8
 	"math/rand"
10
 	"os"
9
 	"os"
16
 	kingpin "gopkg.in/alecthomas/kingpin.v2"
15
 	kingpin "gopkg.in/alecthomas/kingpin.v2"
17
 
16
 
18
 	"github.com/9seconds/mtg/config"
17
 	"github.com/9seconds/mtg/config"
19
-	"github.com/9seconds/mtg/mtproto"
20
-	"github.com/9seconds/mtg/telegram"
18
+	"github.com/9seconds/mtg/proxy"
21
 	"github.com/juju/errors"
19
 	"github.com/juju/errors"
22
 )
20
 )
23
 
21
 
115
 		atom,
113
 		atom,
116
 	)).Sugar()
114
 	)).Sugar()
117
 
115
 
118
-	tg := telegram.NewMiddleTelegram(conf, logger)
119
-	connOpts := &mtproto.ConnectionOpts{
120
-		DC:              int16(1),
121
-		ConnectionType:  mtproto.ConnectionTypeAbridged,
122
-		ConnectionProto: mtproto.ConnectionProtocolIPv4,
123
-	}
116
+	stat := proxy.NewStats(conf)
117
+	go stat.Serve()
124
 
118
 
125
-	sock, err := tg.Dial(connOpts)
126
-	if err != nil {
127
-		panic(err)
128
-	}
129
-	_, err = tg.Init(connOpts, sock)
130
-	fmt.Println(err)
119
+	srv := proxy.NewServer(conf, logger, stat)
120
+	printURLs(conf.GetURLs())
131
 
121
 
132
-	// stat := proxy.NewStats(conf)
133
-	// go stat.Serve()
134
-
135
-	// srv := proxy.NewServer(conf, logger, stat)
136
-	// printURLs(conf.GetURLs())
137
-
138
-	// if err := srv.Serve(); err != nil {
139
-	// 	logger.Fatal(err.Error())
140
-	// }
122
+	if err := srv.Serve(); err != nil {
123
+		logger.Fatal(err.Error())
124
+	}
141
 }
125
 }
142
 
126
 
143
 func setRLimit() (err error) {
127
 func setRLimit() (err error) {

+ 10
- 2
proxy/server.go Просмотреть файл

139
 
139
 
140
 // NewServer creates new instance of MTPROTO proxy.
140
 // NewServer creates new instance of MTPROTO proxy.
141
 func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
141
 func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
142
+	clientInit := client.DirectInit
143
+	tg := telegram.NewDirectTelegram
144
+
145
+	if len(conf.AdTag) > 0 {
146
+		clientInit = client.MiddleInit
147
+		tg = telegram.NewMiddleTelegram
148
+	}
149
+
142
 	return &Server{
150
 	return &Server{
143
 		conf:       conf,
151
 		conf:       conf,
144
 		logger:     logger,
152
 		logger:     logger,
145
 		stats:      stat,
153
 		stats:      stat,
146
-		tg:         telegram.NewDirectTelegram(conf),
147
-		clientInit: client.DirectInit,
154
+		tg:         tg(conf, logger),
155
+		clientInit: clientInit,
148
 	}
156
 	}
149
 }
157
 }

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

4
 	"net"
4
 	"net"
5
 
5
 
6
 	"github.com/juju/errors"
6
 	"github.com/juju/errors"
7
+	"go.uber.org/zap"
7
 
8
 
8
 	"github.com/9seconds/mtg/config"
9
 	"github.com/9seconds/mtg/config"
9
 	"github.com/9seconds/mtg/mtproto"
10
 	"github.com/9seconds/mtg/mtproto"
55
 
56
 
56
 // NewDirectTelegram returns Telegram instance which connects directly
57
 // NewDirectTelegram returns Telegram instance which connects directly
57
 // to Telegram bypassing middleproxies.
58
 // to Telegram bypassing middleproxies.
58
-func NewDirectTelegram(conf *config.Config) Telegram {
59
+func NewDirectTelegram(conf *config.Config, _ *zap.SugaredLogger) Telegram {
59
 	return &directTelegram{baseTelegram{
60
 	return &directTelegram{baseTelegram{
60
 		dialer: tgDialer{
61
 		dialer: tgDialer{
61
 			Dialer: net.Dialer{Timeout: telegramDialTimeout},
62
 			Dialer: net.Dialer{Timeout: telegramDialTimeout},

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