|
|
@@ -1,7 +1,6 @@
|
|
1
|
1
|
package proxy
|
|
2
|
2
|
|
|
3
|
3
|
import (
|
|
4
|
|
- "context"
|
|
5
|
4
|
"io"
|
|
6
|
5
|
"net"
|
|
7
|
6
|
"sync"
|
|
|
@@ -52,15 +51,14 @@ func (p *Proxy) accept(conn net.Conn) {
|
|
52
|
51
|
|
|
53
|
52
|
log.Infow("Client connected", "addr", conn.RemoteAddr())
|
|
54
|
53
|
|
|
55
|
|
- ctx, cancel := context.WithCancel(context.Background())
|
|
56
|
|
- client, opts, err := p.clientInit(ctx, cancel, conn, connID, p.conf)
|
|
|
54
|
+ client, opts, err := p.clientInit(conn, connID, p.conf)
|
|
57
|
55
|
if err != nil {
|
|
58
|
56
|
log.Errorw("Cannot initialize client connection", "error", err)
|
|
59
|
57
|
return
|
|
60
|
58
|
}
|
|
61
|
59
|
defer client.(io.Closer).Close()
|
|
62
|
60
|
|
|
63
|
|
- server, err := p.getTelegramConn(ctx, cancel, opts, connID)
|
|
|
61
|
+ server, err := p.getTelegramConn(opts, connID)
|
|
64
|
62
|
if err != nil {
|
|
65
|
63
|
log.Errorw("Cannot initialize server connection", "error", err)
|
|
66
|
64
|
return
|
|
|
@@ -82,19 +80,16 @@ func (p *Proxy) accept(conn net.Conn) {
|
|
82
|
80
|
go p.directPipe(serverStream, clientStream, wait)
|
|
83
|
81
|
}
|
|
84
|
82
|
|
|
85
|
|
- <-ctx.Done()
|
|
86
|
83
|
wait.Wait()
|
|
87
|
84
|
|
|
88
|
85
|
log.Infow("Client disconnected", "addr", conn.RemoteAddr())
|
|
89
|
86
|
}
|
|
90
|
87
|
|
|
91
|
|
-func (p *Proxy) getTelegramConn(ctx context.Context, cancel context.CancelFunc, opts *mtproto.ConnectionOpts,
|
|
92
|
|
- connID string) (wrappers.Wrap, error) {
|
|
|
88
|
+func (p *Proxy) getTelegramConn(opts *mtproto.ConnectionOpts, connID string) (wrappers.Wrap, error) {
|
|
93
|
89
|
streamConn, err := p.tg.Dial(connID, opts)
|
|
94
|
90
|
if err != nil {
|
|
95
|
91
|
return nil, errors.Annotate(err, "Cannot dial to Telegram")
|
|
96
|
92
|
}
|
|
97
|
|
- streamConn = wrappers.NewCtx(ctx, cancel, streamConn)
|
|
98
|
93
|
|
|
99
|
94
|
packetConn, err := p.tg.Init(opts, streamConn)
|
|
100
|
95
|
if err != nil {
|
|
|
@@ -104,8 +99,13 @@ func (p *Proxy) getTelegramConn(ctx context.Context, cancel context.CancelFunc,
|
|
104
|
99
|
return packetConn, nil
|
|
105
|
100
|
}
|
|
106
|
101
|
|
|
107
|
|
-func (p *Proxy) middlePipe(src wrappers.PacketReader, dst wrappers.PacketWriter, wait *sync.WaitGroup, hacks *mtproto.Hacks) {
|
|
108
|
|
- defer wait.Done()
|
|
|
102
|
+func (p *Proxy) middlePipe(src wrappers.PacketReadCloser, dst wrappers.PacketWriteCloser, wait *sync.WaitGroup, hacks *mtproto.Hacks) {
|
|
|
103
|
+ defer func() {
|
|
|
104
|
+ src.Close()
|
|
|
105
|
+ dst.Close()
|
|
|
106
|
+ wait.Done()
|
|
|
107
|
+ }()
|
|
|
108
|
+
|
|
109
|
109
|
for {
|
|
110
|
110
|
hacks.SimpleAck = false
|
|
111
|
111
|
hacks.QuickAck = false
|
|
|
@@ -120,10 +120,14 @@ func (p *Proxy) middlePipe(src wrappers.PacketReader, dst wrappers.PacketWriter,
|
|
120
|
120
|
}
|
|
121
|
121
|
}
|
|
122
|
122
|
|
|
123
|
|
-func (p *Proxy) directPipe(src io.Reader, dst io.Writer, wait *sync.WaitGroup) {
|
|
124
|
|
- defer wait.Done()
|
|
125
|
|
- io.Copy(dst, src)
|
|
|
123
|
+func (p *Proxy) directPipe(src io.ReadCloser, dst io.WriteCloser, wait *sync.WaitGroup) {
|
|
|
124
|
+ defer func() {
|
|
|
125
|
+ src.Close()
|
|
|
126
|
+ dst.Close()
|
|
|
127
|
+ wait.Done()
|
|
|
128
|
+ }()
|
|
126
|
129
|
|
|
|
130
|
+ io.Copy(dst, src)
|
|
127
|
131
|
}
|
|
128
|
132
|
|
|
129
|
133
|
func NewProxy(conf *config.Config) *Proxy {
|