|
|
@@ -25,6 +25,7 @@ type Server struct {
|
|
25
|
25
|
ctx context.Context
|
|
26
|
26
|
readTimeout time.Duration
|
|
27
|
27
|
writeTimeout time.Duration
|
|
|
28
|
+ stats *Stats
|
|
28
|
29
|
}
|
|
29
|
30
|
|
|
30
|
31
|
func (s *Server) Serve() error {
|
|
|
@@ -50,6 +51,8 @@ func (s *Server) Addr() string {
|
|
50
|
51
|
|
|
51
|
52
|
func (s *Server) accept(conn net.Conn) {
|
|
52
|
53
|
defer conn.Close()
|
|
|
54
|
+ defer s.stats.closeConnection()
|
|
|
55
|
+ s.stats.newConnection()
|
|
53
|
56
|
|
|
54
|
57
|
ctx, cancel := context.WithCancel(context.Background())
|
|
55
|
58
|
socketID := s.makeSocketID()
|
|
|
@@ -102,6 +105,7 @@ func (s *Server) makeSocketID() string {
|
|
102
|
105
|
|
|
103
|
106
|
func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, int16, error) {
|
|
104
|
107
|
wConn := newTimeoutReadWriteCloser(conn, s.readTimeout, s.writeTimeout)
|
|
|
108
|
+ wConn = newTrafficReadWriteCloser(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
|
105
|
109
|
frame, err := obfuscated2.ExtractFrame(wConn)
|
|
106
|
110
|
if err != nil {
|
|
107
|
111
|
return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
|
|
@@ -113,7 +117,7 @@ func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel cont
|
|
113
|
117
|
}
|
|
114
|
118
|
|
|
115
|
119
|
wConn = newLogReadWriteCloser(wConn, s.logger, socketID, "client")
|
|
116
|
|
- wConn = newCipherReadWriteCloser(conn, obfs2)
|
|
|
120
|
+ wConn = newCipherReadWriteCloser(wConn, obfs2)
|
|
117
|
121
|
wConn = newCtxReadWriteCloser(wConn, ctx, cancel)
|
|
118
|
122
|
|
|
119
|
123
|
return wConn, dc, nil
|
|
|
@@ -125,13 +129,14 @@ func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context
|
|
125
|
129
|
return nil, errors.Annotate(err, "Cannot dial")
|
|
126
|
130
|
}
|
|
127
|
131
|
wConn := newTimeoutReadWriteCloser(socket, s.readTimeout, s.writeTimeout)
|
|
|
132
|
+ wConn = newTrafficReadWriteCloser(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
|
128
|
133
|
|
|
129
|
134
|
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
|
130
|
135
|
if n, err := socket.Write(frame); err != nil || n != len(frame) {
|
|
131
|
136
|
return nil, errors.Annotate(err, "Cannot write hadnshake frame")
|
|
132
|
137
|
}
|
|
133
|
138
|
|
|
134
|
|
- wConn = newLogReadWriteCloser(socket, s.logger, socketID, "telegram")
|
|
|
139
|
+ wConn = newLogReadWriteCloser(wConn, s.logger, socketID, "telegram")
|
|
135
|
140
|
wConn = newCipherReadWriteCloser(wConn, obfs2)
|
|
136
|
141
|
wConn = newCtxReadWriteCloser(wConn, ctx, cancel)
|
|
137
|
142
|
|
|
|
@@ -140,13 +145,11 @@ func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context
|
|
140
|
145
|
|
|
141
|
146
|
func (s *Server) pipe(wait *sync.WaitGroup, reader io.Reader, writer io.Writer) {
|
|
142
|
147
|
defer wait.Done()
|
|
143
|
|
-
|
|
144
|
|
- buf := make([]byte, bufferSize)
|
|
145
|
|
- io.CopyBuffer(writer, reader, buf)
|
|
|
148
|
+ io.Copy(writer, reader)
|
|
146
|
149
|
}
|
|
147
|
150
|
|
|
148
|
151
|
func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
|
|
149
|
|
- readTimeout, writeTimeout time.Duration) *Server {
|
|
|
152
|
+ readTimeout, writeTimeout time.Duration, stat *Stats) *Server {
|
|
150
|
153
|
return &Server{
|
|
151
|
154
|
ip: ip,
|
|
152
|
155
|
port: port,
|
|
|
@@ -155,5 +158,6 @@ func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
|
|
155
|
158
|
logger: logger,
|
|
156
|
159
|
readTimeout: readTimeout,
|
|
157
|
160
|
writeTimeout: writeTimeout,
|
|
|
161
|
+ stats: stat,
|
|
158
|
162
|
}
|
|
159
|
163
|
}
|