|
|
@@ -3,7 +3,6 @@ package server
|
|
3
|
3
|
import (
|
|
4
|
4
|
"context"
|
|
5
|
5
|
"fmt"
|
|
6
|
|
- "io"
|
|
7
|
6
|
"net"
|
|
8
|
7
|
"strconv"
|
|
9
|
8
|
"sync"
|
|
|
@@ -56,65 +55,66 @@ func (s *Server) accept(conn net.Conn) {
|
|
56
|
55
|
"socketid", socketID,
|
|
57
|
56
|
)
|
|
58
|
57
|
|
|
59
|
|
- clientCipher, dc := s.getClientStream(conn, ctx, cancel, socketID)
|
|
60
|
|
- // if err != nil {
|
|
61
|
|
- // s.logger.Warnw("Cannot initialize client connection",
|
|
62
|
|
- // "secret", s.secret,
|
|
63
|
|
- // "addr", conn.RemoteAddr().String(),
|
|
64
|
|
- // "socketid", socketID,
|
|
65
|
|
- // "error", err,
|
|
66
|
|
- // )
|
|
67
|
|
- // return
|
|
68
|
|
- // }
|
|
69
|
|
-
|
|
70
|
|
- tgConn, tgCipher := s.getTelegramStream(dc, ctx, cancel, socketID)
|
|
71
|
|
- // if err != nil {
|
|
72
|
|
- // s.logger.Warnw("Cannot initialize Telegram connection",
|
|
73
|
|
- // "socketid", socketID,
|
|
74
|
|
- // "error", err,
|
|
75
|
|
- // )
|
|
76
|
|
- // return
|
|
77
|
|
- // }
|
|
|
58
|
+ clientConn, dc, err := s.getClientStream(conn, ctx, cancel, socketID)
|
|
|
59
|
+ if err != nil {
|
|
|
60
|
+ s.logger.Warnw("Cannot initialize client connection",
|
|
|
61
|
+ "secret", s.secret,
|
|
|
62
|
+ "addr", conn.RemoteAddr().String(),
|
|
|
63
|
+ "socketid", socketID,
|
|
|
64
|
+ "error", err,
|
|
|
65
|
+ )
|
|
|
66
|
+ return
|
|
|
67
|
+ }
|
|
|
68
|
+ defer clientConn.Close()
|
|
|
69
|
+
|
|
|
70
|
+ tgConn, err := s.getTelegramStream(dc, ctx, cancel, socketID)
|
|
|
71
|
+ if err != nil {
|
|
|
72
|
+ s.logger.Warnw("Cannot initialize Telegram connection",
|
|
|
73
|
+ "socketid", socketID,
|
|
|
74
|
+ "error", err,
|
|
|
75
|
+ )
|
|
|
76
|
+ return
|
|
|
77
|
+ }
|
|
78
|
78
|
defer tgConn.Close()
|
|
79
|
79
|
|
|
80
|
80
|
wait := &sync.WaitGroup{}
|
|
81
|
81
|
wait.Add(2)
|
|
82
|
82
|
go func() {
|
|
|
83
|
+ defer wait.Done()
|
|
83
|
84
|
buf := make([]byte, 128)
|
|
|
85
|
+
|
|
84
|
86
|
for {
|
|
85
|
|
- n, err := conn.Read(buf)
|
|
|
87
|
+ fmt.Println("client loop")
|
|
|
88
|
+ n, err := clientConn.Read(buf)
|
|
86
|
89
|
if err != nil {
|
|
|
90
|
+ fmt.Println("client read error", err)
|
|
|
91
|
+ return
|
|
|
92
|
+ }
|
|
|
93
|
+ _, err = tgConn.Write(buf[:n])
|
|
|
94
|
+ if err != nil {
|
|
|
95
|
+ fmt.Println("tgConn write error", err)
|
|
87
|
96
|
return
|
|
88
|
97
|
}
|
|
89
|
|
- decrypted := clientCipher.Decrypt(buf[:n])
|
|
90
|
|
- encrypted := tgCipher.Encrypt(decrypted)
|
|
91
|
|
- tgConn.Write(encrypted)
|
|
92
|
98
|
}
|
|
93
|
99
|
}()
|
|
94
|
100
|
go func() {
|
|
|
101
|
+ defer wait.Done()
|
|
95
|
102
|
buf := make([]byte, 128)
|
|
|
103
|
+
|
|
96
|
104
|
for {
|
|
|
105
|
+ fmt.Println("tg loop")
|
|
97
|
106
|
n, err := tgConn.Read(buf)
|
|
98
|
107
|
if err != nil {
|
|
|
108
|
+ fmt.Println("tgConn read error", err)
|
|
|
109
|
+ return
|
|
|
110
|
+ }
|
|
|
111
|
+ _, err = clientConn.Write(buf[:n])
|
|
|
112
|
+ if err != nil {
|
|
|
113
|
+ fmt.Println("client write error", err)
|
|
99
|
114
|
return
|
|
100
|
115
|
}
|
|
101
|
|
- decrypted := tgCipher.Decrypt(buf[:n])
|
|
102
|
|
- encrypted := clientCipher.Encrypt(decrypted)
|
|
103
|
|
- conn.Write(encrypted)
|
|
104
|
116
|
}
|
|
105
|
117
|
}()
|
|
106
|
|
- // go func() {
|
|
107
|
|
- // buf := make([]byte, 128)
|
|
108
|
|
- // for {
|
|
109
|
|
- // n, err := conn.
|
|
110
|
|
- // }
|
|
111
|
|
- // defer wait.Done()
|
|
112
|
|
- // io.Copy(tgConn, clientConn)
|
|
113
|
|
- // }()
|
|
114
|
|
- // go func() {
|
|
115
|
|
- // defer wait.Done()
|
|
116
|
|
- // io.Copy(clientConn, tgConn)
|
|
117
|
|
- // }()
|
|
118
|
118
|
<-ctx.Done()
|
|
119
|
119
|
wait.Wait()
|
|
120
|
120
|
|
|
|
@@ -129,20 +129,20 @@ func (s *Server) makeSocketID() string {
|
|
129
|
129
|
return uuid.NewV4().String()
|
|
130
|
130
|
}
|
|
131
|
131
|
|
|
132
|
|
-func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel context.CancelFunc, socketID string) (Cipher, int16) {
|
|
|
132
|
+func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel context.CancelFunc, socketID string) (*CipherReadWriteCloser, int16, error) {
|
|
133
|
133
|
frame, err := obfuscated2.ExtractFrame(conn)
|
|
134
|
134
|
if err != nil {
|
|
135
|
|
- fmt.Println(err)
|
|
136
|
|
- // return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
|
135
|
+ return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
137
|
136
|
}
|
|
138
|
137
|
|
|
139
|
138
|
obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(s.secret, frame)
|
|
140
|
139
|
if err != nil {
|
|
141
|
|
- fmt.Println(err)
|
|
142
|
|
- // return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
|
140
|
+ return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
143
|
141
|
}
|
|
144
|
142
|
|
|
145
|
|
- return obfs2, dc
|
|
|
143
|
+ wConn := newCipherReadWriteCloser(conn, obfs2)
|
|
|
144
|
+
|
|
|
145
|
+ return wConn, dc, nil
|
|
146
|
146
|
|
|
147
|
147
|
// cipherConn := newCipherReadWriteCloser(conn, obfs2)
|
|
148
|
148
|
// ctxConn := newCtxReadWriteCloser(cipherConn, ctx, cancel)
|
|
|
@@ -151,20 +151,20 @@ func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel cont
|
|
151
|
151
|
// return ctxConn, dc, nil
|
|
152
|
152
|
}
|
|
153
|
153
|
|
|
154
|
|
-func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, Cipher) {
|
|
|
154
|
+func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (*CipherReadWriteCloser, error) {
|
|
155
|
155
|
socket, err := dialToTelegram(dc)
|
|
156
|
156
|
if err != nil {
|
|
157
|
|
- fmt.Println(err)
|
|
158
|
|
- // return nil, errors.Annotate(err, "Cannot dial")
|
|
|
157
|
+ return nil, errors.Annotate(err, "Cannot dial")
|
|
159
|
158
|
}
|
|
160
|
159
|
|
|
161
|
160
|
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
|
162
|
161
|
if n, err := socket.Write(frame); err != nil || n != len(frame) {
|
|
163
|
|
- fmt.Println(err)
|
|
164
|
|
- // return nil, errors.Annotate(err, "Cannot write hadnshake frame")
|
|
|
162
|
+ return nil, errors.Annotate(err, "Cannot write hadnshake frame")
|
|
165
|
163
|
}
|
|
166
|
164
|
|
|
167
|
|
- return socket, obfs2
|
|
|
165
|
+ wConn := newCipherReadWriteCloser(socket, obfs2)
|
|
|
166
|
+
|
|
|
167
|
+ return wConn, nil
|
|
168
|
168
|
|
|
169
|
169
|
// cipherConn := newCipherReadWriteCloser(socket, obfs2)
|
|
170
|
170
|
// ctxConn := newCtxReadWriteCloser(cipherConn, ctx, cancel)
|