Sfoglia il codice sorgente

Refactor cipherrwc to wrappers

tags/0.9
9seconds 8 anni fa
parent
commit
4486fbb8f4
4 ha cambiato i file con 62 aggiunte e 78 eliminazioni
  1. 6
    20
      obfuscated2/obfuscated2.go
  2. 0
    56
      proxy/cipherrwc.go
  3. 3
    2
      proxy/server.go
  4. 53
    0
      wrappers/streamcipherrwc.go

+ 6
- 20
obfuscated2/obfuscated2.go Vedi File

@@ -11,22 +11,8 @@ import (
11 11
 // Obfuscated2 contains AES CTR encryption and decryption streams
12 12
 // for telegram connection.
13 13
 type Obfuscated2 struct {
14
-	decryptor cipher.Stream
15
-	encryptor cipher.Stream
16
-}
17
-
18
-// Encrypt encrypts given data.
19
-func (o *Obfuscated2) Encrypt(data []byte) []byte {
20
-	buf := make([]byte, len(data))
21
-	o.encryptor.XORKeyStream(buf, data)
22
-	return buf
23
-}
24
-
25
-// Decrypt decrypts given data.
26
-func (o *Obfuscated2) Decrypt(data []byte) []byte {
27
-	buf := make([]byte, len(data))
28
-	o.decryptor.XORKeyStream(buf, data)
29
-	return buf
14
+	Decryptor cipher.Stream
15
+	Encryptor cipher.Stream
30 16
 }
31 17
 
32 18
 // ParseObfuscated2ClientFrame parses client frame. Please check this link for
@@ -54,8 +40,8 @@ func ParseObfuscated2ClientFrame(secret, data []byte) (*Obfuscated2, int16, erro
54 40
 	}
55 41
 
56 42
 	obfs := &Obfuscated2{
57
-		decryptor: decryptor,
58
-		encryptor: encryptor,
43
+		Decryptor: decryptor,
44
+		Encryptor: encryptor,
59 45
 	}
60 46
 
61 47
 	return obfs, decryptedFrame.DC(), nil
@@ -77,8 +63,8 @@ func MakeTelegramObfuscated2Frame() (*Obfuscated2, Frame) {
77 63
 	copy(frame, copyFrame)
78 64
 
79 65
 	obfs := &Obfuscated2{
80
-		decryptor: decryptor,
81
-		encryptor: encryptor,
66
+		Decryptor: decryptor,
67
+		Encryptor: encryptor,
82 68
 	}
83 69
 
84 70
 	return obfs, frame

+ 0
- 56
proxy/cipherrwc.go Vedi File

@@ -1,56 +0,0 @@
1
-package proxy
2
-
3
-import (
4
-	"bytes"
5
-	"io"
6
-)
7
-
8
-// Cipher is an interface to anything which can encrypt and decrypt
9
-type Cipher interface {
10
-	Encrypt([]byte) []byte
11
-	Decrypt([]byte) []byte
12
-}
13
-
14
-// CipherReadWriteCloser wraps connection for transparent encryption
15
-type CipherReadWriteCloser struct {
16
-	crypt Cipher
17
-	conn  io.ReadWriteCloser
18
-	rest  *bytes.Buffer
19
-}
20
-
21
-// Read reads from connection
22
-func (c *CipherReadWriteCloser) Read(p []byte) (n int, err error) {
23
-	n, err = c.conn.Read(p)
24
-	copy(p, c.crypt.Decrypt(p[:n]))
25
-	return
26
-}
27
-
28
-// Write writes into connection.
29
-func (c *CipherReadWriteCloser) Write(p []byte) (int, error) {
30
-	encrypted := c.crypt.Encrypt(p)
31
-	allWritten := 0
32
-
33
-	for len(encrypted) > 0 {
34
-		n, err := c.conn.Write(encrypted)
35
-		allWritten += n
36
-		if err != nil {
37
-			return allWritten, err
38
-		}
39
-		encrypted = encrypted[n:]
40
-	}
41
-
42
-	return allWritten, nil
43
-}
44
-
45
-// Close closes underlying connection.
46
-func (c *CipherReadWriteCloser) Close() error {
47
-	return c.conn.Close()
48
-}
49
-
50
-func newCipherReadWriteCloser(conn io.ReadWriteCloser, crypt Cipher) *CipherReadWriteCloser {
51
-	return &CipherReadWriteCloser{
52
-		conn:  conn,
53
-		crypt: crypt,
54
-		rest:  &bytes.Buffer{},
55
-	}
56
-}

+ 3
- 2
proxy/server.go Vedi File

@@ -9,6 +9,7 @@ import (
9 9
 	"time"
10 10
 
11 11
 	"github.com/9seconds/mtg/obfuscated2"
12
+	"github.com/9seconds/mtg/wrappers"
12 13
 	"github.com/juju/errors"
13 14
 	uuid "github.com/satori/go.uuid"
14 15
 	"go.uber.org/zap"
@@ -124,7 +125,7 @@ func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc,
124 125
 	}
125 126
 
126 127
 	wConn = newLogReadWriteCloser(wConn, s.logger, socketID, "client")
127
-	wConn = newCipherReadWriteCloser(wConn, obfs2)
128
+	wConn = wrappers.NewStreamCipherRWC(wConn, obfs2.Encryptor, obfs2.Decryptor)
128 129
 	wConn = newCtxReadWriteCloser(ctx, cancel, wConn)
129 130
 
130 131
 	return wConn, dc, nil
@@ -144,7 +145,7 @@ func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFun
144 145
 	}
145 146
 
146 147
 	wConn = newLogReadWriteCloser(wConn, s.logger, socketID, "telegram")
147
-	wConn = newCipherReadWriteCloser(wConn, obfs2)
148
+	wConn = wrappers.NewStreamCipherRWC(wConn, obfs2.Encryptor, obfs2.Decryptor)
148 149
 	wConn = newCtxReadWriteCloser(ctx, cancel, wConn)
149 150
 
150 151
 	return wConn, nil

+ 53
- 0
wrappers/streamcipherrwc.go Vedi File

@@ -0,0 +1,53 @@
1
+package wrappers
2
+
3
+import (
4
+	"bytes"
5
+	"crypto/cipher"
6
+	"io"
7
+)
8
+
9
+type StreamCipherReadWriteCloser struct {
10
+	encryptor cipher.Stream
11
+	decryptor cipher.Stream
12
+	conn      io.ReadWriteCloser
13
+	rest      *bytes.Buffer
14
+}
15
+
16
+// Read reads from connection
17
+func (c *StreamCipherReadWriteCloser) Read(p []byte) (n int, err error) {
18
+	n, err = c.conn.Read(p)
19
+	c.decryptor.XORKeyStream(p, p[:n])
20
+	return
21
+}
22
+
23
+// Write writes into connection.
24
+func (c *StreamCipherReadWriteCloser) Write(p []byte) (int, error) {
25
+	encrypted := make([]byte, len(p))
26
+	c.encryptor.XORKeyStream(encrypted, p)
27
+	allWritten := 0
28
+
29
+	for len(encrypted) > 0 {
30
+		n, err := c.conn.Write(encrypted)
31
+		allWritten += n
32
+		if err != nil {
33
+			return allWritten, err
34
+		}
35
+		encrypted = encrypted[n:]
36
+	}
37
+
38
+	return allWritten, nil
39
+}
40
+
41
+// Close closes underlying connection.
42
+func (c *StreamCipherReadWriteCloser) Close() error {
43
+	return c.conn.Close()
44
+}
45
+
46
+func NewStreamCipherRWC(conn io.ReadWriteCloser, encryptor, decryptor cipher.Stream) io.ReadWriteCloser {
47
+	return &StreamCipherReadWriteCloser{
48
+		conn:      conn,
49
+		encryptor: encryptor,
50
+		decryptor: decryptor,
51
+		rest:      &bytes.Buffer{},
52
+	}
53
+}

Loading…
Annulla
Salva