Kaynağa Gözat

Return back timeoutrwc

tags/0.9
9seconds 7 yıl önce
ebeveyn
işleme
0e2898f6be
4 değiştirilmiş dosya ile 40 ekleme ve 8 silme
  1. 3
    4
      client/direct.go
  2. 3
    0
      config/config.go
  3. 3
    4
      telegram/dialer.go
  4. 31
    0
      wrappers/timeoutrwc.go

+ 3
- 4
client/direct.go Dosyayı Görüntüle

13
 	"github.com/9seconds/mtg/wrappers"
13
 	"github.com/9seconds/mtg/wrappers"
14
 )
14
 )
15
 
15
 
16
-const (
17
-	handshakeTimeout = 10 * time.Second
18
-)
16
+const handshakeTimeout = 10 * time.Second
19
 
17
 
20
 // DirectInit initializes client to access Telegram bypassing middleproxies.
18
 // DirectInit initializes client to access Telegram bypassing middleproxies.
21
 func DirectInit(conn net.Conn, conf *config.Config) (*mtproto.ConnectionOpts, io.ReadWriteCloser, error) {
19
 func DirectInit(conn net.Conn, conf *config.Config) (*mtproto.ConnectionOpts, io.ReadWriteCloser, error) {
36
 	}
34
 	}
37
 	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
35
 	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
38
 
36
 
39
-	socket := wrappers.NewStreamCipherRWC(conn, obfs2.Encryptor, obfs2.Decryptor)
37
+	socket := wrappers.NewTimeoutRWC(conn)
38
+	socket = wrappers.NewStreamCipherRWC(socket, obfs2.Encryptor, obfs2.Decryptor)
40
 
39
 
41
 	return connOpts, socket, nil
40
 	return connOpts, socket, nil
42
 }
41
 }

+ 3
- 0
config/config.go Dosyayı Görüntüle

16
 	BufferReadSize  = 32 * 1024
16
 	BufferReadSize  = 32 * 1024
17
 	BufferSizeCopy  = 32 * 1024
17
 	BufferSizeCopy  = 32 * 1024
18
 
18
 
19
+	TimeoutRead  = time.Minute
20
+	TimeoutWrite = time.Minute
21
+
19
 	keepAlivePeriod = 20 * time.Second
22
 	keepAlivePeriod = 20 * time.Second
20
 )
23
 )
21
 
24
 

+ 3
- 4
telegram/dialer.go Dosyayı Görüntüle

8
 	"github.com/juju/errors"
8
 	"github.com/juju/errors"
9
 
9
 
10
 	"github.com/9seconds/mtg/config"
10
 	"github.com/9seconds/mtg/config"
11
+	"github.com/9seconds/mtg/wrappers"
11
 )
12
 )
12
 
13
 
13
-const (
14
-	telegramDialTimeout = 10 * time.Second
15
-)
14
+const telegramDialTimeout = 10 * time.Second
16
 
15
 
17
 type tgDialer struct {
16
 type tgDialer struct {
18
 	net.Dialer
17
 	net.Dialer
36
 		return nil, err
35
 		return nil, err
37
 	}
36
 	}
38
 
37
 
39
-	return conn, nil
38
+	return wrappers.NewTimeoutRWC(conn), nil
40
 }
39
 }

+ 31
- 0
wrappers/timeoutrwc.go Dosyayı Görüntüle

1
+package wrappers
2
+
3
+import (
4
+	"io"
5
+	"net"
6
+	"time"
7
+
8
+	"github.com/9seconds/mtg/config"
9
+)
10
+
11
+type TimeoutReadWriteCloser struct {
12
+	conn net.Conn
13
+}
14
+
15
+func (t *TimeoutReadWriteCloser) Read(p []byte) (int, error) {
16
+	t.conn.SetReadDeadline(time.Now().Add(config.TimeoutRead))
17
+	return t.conn.Read(p)
18
+}
19
+
20
+func (t *TimeoutReadWriteCloser) Write(p []byte) (int, error) {
21
+	t.conn.SetWriteDeadline(time.Now().Add(config.TimeoutWrite))
22
+	return t.conn.Write(p)
23
+}
24
+
25
+func (t *TimeoutReadWriteCloser) Close() error {
26
+	return t.conn.Close()
27
+}
28
+
29
+func NewTimeoutRWC(conn net.Conn) io.ReadWriteCloser {
30
+	return &TimeoutReadWriteCloser{conn}
31
+}

Loading…
İptal
Kaydet