Return back timeoutrwc

This commit is contained in:
9seconds
2018-07-02 08:09:22 +03:00
parent 5e82dce22e
commit 0e2898f6be
4 changed files with 40 additions and 8 deletions
+3 -4
View File
@@ -13,9 +13,7 @@ import (
"github.com/9seconds/mtg/wrappers"
)
const (
handshakeTimeout = 10 * time.Second
)
const handshakeTimeout = 10 * time.Second
// DirectInit initializes client to access Telegram bypassing middleproxies.
func DirectInit(conn net.Conn, conf *config.Config) (*mtproto.ConnectionOpts, io.ReadWriteCloser, error) {
@@ -36,7 +34,8 @@ func DirectInit(conn net.Conn, conf *config.Config) (*mtproto.ConnectionOpts, io
}
connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
socket := wrappers.NewStreamCipherRWC(conn, obfs2.Encryptor, obfs2.Decryptor)
socket := wrappers.NewTimeoutRWC(conn)
socket = wrappers.NewStreamCipherRWC(socket, obfs2.Encryptor, obfs2.Decryptor)
return connOpts, socket, nil
}
+3
View File
@@ -16,6 +16,9 @@ const (
BufferReadSize = 32 * 1024
BufferSizeCopy = 32 * 1024
TimeoutRead = time.Minute
TimeoutWrite = time.Minute
keepAlivePeriod = 20 * time.Second
)
+3 -4
View File
@@ -8,11 +8,10 @@ import (
"github.com/juju/errors"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/wrappers"
)
const (
telegramDialTimeout = 10 * time.Second
)
const telegramDialTimeout = 10 * time.Second
type tgDialer struct {
net.Dialer
@@ -36,5 +35,5 @@ func (t *tgDialer) dialRWC(addr string) (io.ReadWriteCloser, error) {
return nil, err
}
return conn, nil
return wrappers.NewTimeoutRWC(conn), nil
}
+31
View File
@@ -0,0 +1,31 @@
package wrappers
import (
"io"
"net"
"time"
"github.com/9seconds/mtg/config"
)
type TimeoutReadWriteCloser struct {
conn net.Conn
}
func (t *TimeoutReadWriteCloser) Read(p []byte) (int, error) {
t.conn.SetReadDeadline(time.Now().Add(config.TimeoutRead))
return t.conn.Read(p)
}
func (t *TimeoutReadWriteCloser) Write(p []byte) (int, error) {
t.conn.SetWriteDeadline(time.Now().Add(config.TimeoutWrite))
return t.conn.Write(p)
}
func (t *TimeoutReadWriteCloser) Close() error {
return t.conn.Close()
}
func NewTimeoutRWC(conn net.Conn) io.ReadWriteCloser {
return &TimeoutReadWriteCloser{conn}
}