mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-02 00:11:56 +03:00
Refactor timeoutrwc to wrappers
This commit is contained in:
+2
-2
@@ -112,7 +112,7 @@ func (s *Server) makeSocketID() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc, conn net.Conn, socketID string) (io.ReadWriteCloser, int16, error) {
|
func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc, conn net.Conn, socketID string) (io.ReadWriteCloser, int16, error) {
|
||||||
wConn := newTimeoutReadWriteCloser(conn, s.readTimeout, s.writeTimeout)
|
wConn := wrappers.NewTimeoutRWC(conn, s.readTimeout, s.writeTimeout)
|
||||||
wConn = newTrafficReadWriteCloser(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
wConn = newTrafficReadWriteCloser(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
||||||
frame, err := obfuscated2.ExtractFrame(wConn)
|
frame, err := obfuscated2.ExtractFrame(wConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -136,7 +136,7 @@ func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFun
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot dial")
|
return nil, errors.Annotate(err, "Cannot dial")
|
||||||
}
|
}
|
||||||
wConn := newTimeoutReadWriteCloser(socket, s.readTimeout, s.writeTimeout)
|
wConn := wrappers.NewTimeoutRWC(socket, s.readTimeout, s.writeTimeout)
|
||||||
wConn = newTrafficReadWriteCloser(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
wConn = newTrafficReadWriteCloser(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
||||||
|
|
||||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
package proxy
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TimeoutReadWriteCloser sets timeouts for read/write into underlying
|
|
||||||
// network connection.
|
|
||||||
type TimeoutReadWriteCloser struct {
|
|
||||||
conn net.Conn
|
|
||||||
readTimeout time.Duration
|
|
||||||
writeTimeout time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read reads from connection
|
|
||||||
func (t *TimeoutReadWriteCloser) Read(p []byte) (int, error) {
|
|
||||||
t.conn.SetReadDeadline(time.Now().Add(t.readTimeout)) // nolint: errcheck, gas
|
|
||||||
return t.conn.Read(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write writes into connection.
|
|
||||||
func (t *TimeoutReadWriteCloser) Write(p []byte) (int, error) {
|
|
||||||
t.conn.SetWriteDeadline(time.Now().Add(t.writeTimeout)) // nolint: errcheck, gas
|
|
||||||
return t.conn.Write(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes underlying connection.
|
|
||||||
func (t *TimeoutReadWriteCloser) Close() error {
|
|
||||||
return t.conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTimeoutReadWriteCloser(conn net.Conn, readTimeout, writeTimeout time.Duration) io.ReadWriteCloser {
|
|
||||||
return &TimeoutReadWriteCloser{
|
|
||||||
conn: conn,
|
|
||||||
readTimeout: readTimeout,
|
|
||||||
writeTimeout: writeTimeout,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user