ソースを参照

Return back timeoutrwc

tags/0.9
9seconds 7年前
コミット
0e2898f6be
4個のファイルの変更40行の追加8行の削除
  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 ファイルの表示

@@ -13,9 +13,7 @@ import (
13 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 18
 // DirectInit initializes client to access Telegram bypassing middleproxies.
21 19
 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
36 34
 	}
37 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 40
 	return connOpts, socket, nil
42 41
 }

+ 3
- 0
config/config.go ファイルの表示

@@ -16,6 +16,9 @@ const (
16 16
 	BufferReadSize  = 32 * 1024
17 17
 	BufferSizeCopy  = 32 * 1024
18 18
 
19
+	TimeoutRead  = time.Minute
20
+	TimeoutWrite = time.Minute
21
+
19 22
 	keepAlivePeriod = 20 * time.Second
20 23
 )
21 24
 

+ 3
- 4
telegram/dialer.go ファイルの表示

@@ -8,11 +8,10 @@ import (
8 8
 	"github.com/juju/errors"
9 9
 
10 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 16
 type tgDialer struct {
18 17
 	net.Dialer
@@ -36,5 +35,5 @@ func (t *tgDialer) dialRWC(addr string) (io.ReadWriteCloser, error) {
36 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 ファイルの表示

@@ -0,0 +1,31 @@
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
+}

読み込み中…
キャンセル
保存