Przeglądaj źródła

Propagate to middle client

tags/0.9
9seconds 7 lat temu
rodzic
commit
a7eb29cc94
4 zmienionych plików z 45 dodań i 27 usunięć
  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 Wyświetl plik

@@ -0,0 +1,25 @@
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 Wyświetl plik

@@ -4,7 +4,6 @@ package main
4 4
 
5 5
 import (
6 6
 	"encoding/json"
7
-	"fmt"
8 7
 	"io"
9 8
 	"math/rand"
10 9
 	"os"
@@ -16,8 +15,7 @@ import (
16 15
 	kingpin "gopkg.in/alecthomas/kingpin.v2"
17 16
 
18 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 19
 	"github.com/juju/errors"
22 20
 )
23 21
 
@@ -115,29 +113,15 @@ func main() {
115 113
 		atom,
116 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 127
 func setRLimit() (err error) {

+ 10
- 2
proxy/server.go Wyświetl plik

@@ -139,11 +139,19 @@ func (s *Server) pipe(dst io.Writer, src io.Reader, wait *sync.WaitGroup) {
139 139
 
140 140
 // NewServer creates new instance of MTPROTO proxy.
141 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 150
 	return &Server{
143 151
 		conf:       conf,
144 152
 		logger:     logger,
145 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 Wyświetl plik

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

Ładowanie…
Anuluj
Zapisz