|
|
@@ -2,7 +2,7 @@ package server
|
|
2
|
2
|
|
|
3
|
3
|
import (
|
|
4
|
4
|
"context"
|
|
5
|
|
- "fmt"
|
|
|
5
|
+ "io"
|
|
6
|
6
|
"net"
|
|
7
|
7
|
"strconv"
|
|
8
|
8
|
"sync"
|
|
|
@@ -13,6 +13,8 @@ import (
|
|
13
|
13
|
"go.uber.org/zap"
|
|
14
|
14
|
)
|
|
15
|
15
|
|
|
|
16
|
+const bufferSize = 4096
|
|
|
17
|
+
|
|
16
|
18
|
type Server struct {
|
|
17
|
19
|
ip net.IP
|
|
18
|
20
|
port int
|
|
|
@@ -79,42 +81,8 @@ func (s *Server) accept(conn net.Conn) {
|
|
79
|
81
|
|
|
80
|
82
|
wait := &sync.WaitGroup{}
|
|
81
|
83
|
wait.Add(2)
|
|
82
|
|
- go func() {
|
|
83
|
|
- defer wait.Done()
|
|
84
|
|
- buf := make([]byte, 128)
|
|
85
|
|
-
|
|
86
|
|
- for {
|
|
87
|
|
- fmt.Println("client loop")
|
|
88
|
|
- n, err := clientConn.Read(buf)
|
|
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)
|
|
96
|
|
- return
|
|
97
|
|
- }
|
|
98
|
|
- }
|
|
99
|
|
- }()
|
|
100
|
|
- go func() {
|
|
101
|
|
- defer wait.Done()
|
|
102
|
|
- buf := make([]byte, 128)
|
|
103
|
|
-
|
|
104
|
|
- for {
|
|
105
|
|
- fmt.Println("tg loop")
|
|
106
|
|
- n, err := tgConn.Read(buf)
|
|
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)
|
|
114
|
|
- return
|
|
115
|
|
- }
|
|
116
|
|
- }
|
|
117
|
|
- }()
|
|
|
84
|
+ go s.pipe(wait, clientConn, tgConn)
|
|
|
85
|
+ go s.pipe(wait, tgConn, clientConn)
|
|
118
|
86
|
<-ctx.Done()
|
|
119
|
87
|
wait.Wait()
|
|
120
|
88
|
|
|
|
@@ -143,12 +111,6 @@ func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel cont
|
|
143
|
111
|
wConn := newCipherReadWriteCloser(conn, obfs2)
|
|
144
|
112
|
|
|
145
|
113
|
return wConn, dc, nil
|
|
146
|
|
-
|
|
147
|
|
- // cipherConn := newCipherReadWriteCloser(conn, obfs2)
|
|
148
|
|
- // ctxConn := newCtxReadWriteCloser(cipherConn, ctx, cancel)
|
|
149
|
|
- // // logConn := newLogReadWriteCloser(ctxConn, s.logger, socketID, "client")
|
|
150
|
|
-
|
|
151
|
|
- // return ctxConn, dc, nil
|
|
152
|
114
|
}
|
|
153
|
115
|
|
|
154
|
116
|
func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (*CipherReadWriteCloser, error) {
|
|
|
@@ -165,12 +127,14 @@ func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context
|
|
165
|
127
|
wConn := newCipherReadWriteCloser(socket, obfs2)
|
|
166
|
128
|
|
|
167
|
129
|
return wConn, nil
|
|
|
130
|
+}
|
|
|
131
|
+
|
|
|
132
|
+func (s *Server) pipe(wait *sync.WaitGroup, reader io.Reader, writer io.Writer) {
|
|
|
133
|
+ defer wait.Done()
|
|
168
|
134
|
|
|
169
|
|
- // cipherConn := newCipherReadWriteCloser(socket, obfs2)
|
|
170
|
|
- // ctxConn := newCtxReadWriteCloser(cipherConn, ctx, cancel)
|
|
171
|
|
- // logConn := newLogReadWriteCloser(ctxConn, s.logger, socketID, "telegram")
|
|
|
135
|
+ buf := make([]byte, bufferSize)
|
|
|
136
|
+ io.CopyBuffer(writer, reader, buf)
|
|
172
|
137
|
|
|
173
|
|
- // return logConn, nil
|
|
174
|
138
|
}
|
|
175
|
139
|
|
|
176
|
140
|
func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger) *Server {
|