Просмотр исходного кода

Use ReadFull in blockcipher

tags/1.0^2
9seconds 6 лет назад
Родитель
Сommit
1a7eee444e
2 измененных файлов: 32 добавлений и 58 удалений
  1. 20
    0
      utils/read_full.go
  2. 12
    58
      wrappers/blockcipher.go

+ 20
- 0
utils/read_full.go Просмотреть файл

1
+package utils
2
+
3
+import "io"
4
+
5
+const readFullBufferSize = 1024 + 1 // +1 because telegram opreates with blocks mod 4
6
+
7
+func ReadFull(src io.Reader) (rv []byte, err error) {
8
+	buf := make([]byte, readFullBufferSize)
9
+	n := readFullBufferSize
10
+
11
+	for n == len(buf) {
12
+		n, err = src.Read(buf)
13
+		if err != nil {
14
+			return nil, err
15
+		}
16
+		rv = append(rv, buf[:n]...)
17
+	}
18
+
19
+	return rv, nil
20
+}

+ 12
- 58
wrappers/blockcipher.go Просмотреть файл

4
 	"bytes"
4
 	"bytes"
5
 	"crypto/aes"
5
 	"crypto/aes"
6
 	"crypto/cipher"
6
 	"crypto/cipher"
7
-	"errors"
8
 	"fmt"
7
 	"fmt"
9
 	"net"
8
 	"net"
10
 	"time"
9
 	"time"
12
 	"go.uber.org/zap"
11
 	"go.uber.org/zap"
13
 
12
 
14
 	"github.com/9seconds/mtg/conntypes"
13
 	"github.com/9seconds/mtg/conntypes"
14
+	"github.com/9seconds/mtg/utils"
15
 )
15
 )
16
 
16
 
17
-const blockCipherReadCurrentDataBufferSize = 1024 + 1 // +1 because telegram operates with blocks mod 4
18
-
19
 type wrapperBlockCipher struct {
17
 type wrapperBlockCipher struct {
20
 	buf bytes.Buffer
18
 	buf bytes.Buffer
21
 
19
 
41
 }
39
 }
42
 
40
 
43
 func (w *wrapperBlockCipher) Read(p []byte) (int, error) {
41
 func (w *wrapperBlockCipher) Read(p []byte) (int, error) {
44
-	return w.read(p, readAll)
45
-
46
-}
47
-
48
-func (w *wrapperBlockCipher) ReadTimeout(p []byte, timeout time.Duration) (int, error) {
49
-	return w.read(p, readAllTimeout(timeout))
50
-}
51
-
52
-func (w *wrapperBlockCipher) read(p []byte,
53
-	reader func(conntypes.StreamReadWriteCloser) ([]byte, error)) (int, error) {
54
 	if w.buf.Len() > 0 {
42
 	if w.buf.Len() > 0 {
55
 		return w.flush(p)
43
 		return w.flush(p)
56
 	}
44
 	}
57
 
45
 
58
-	var buf []byte
59
-	for len(buf) == 0 || len(buf)%aes.BlockSize != 0 {
60
-		rv, err := reader(w.parent)
46
+	var currentBuffer []byte
47
+	for len(currentBuffer) == 0 || len(currentBuffer)%aes.BlockSize != 0 {
48
+		rv, err := utils.ReadFull(w.parent)
61
 		if err != nil {
49
 		if err != nil {
62
-			return 0, fmt.Errorf("cannot read from socket: %w", err)
50
+			return 0, fmt.Errorf("cannot read data: %w", err)
63
 		}
51
 		}
64
-		buf = append(buf, rv...)
52
+		currentBuffer = append(currentBuffer, rv...)
65
 	}
53
 	}
66
 
54
 
67
-	w.decryptor.CryptBlocks(buf, buf)
68
-	w.buf.Write(buf)
55
+	w.decryptor.CryptBlocks(currentBuffer, currentBuffer)
56
+	w.buf.Write(currentBuffer)
69
 
57
 
70
 	return w.flush(p)
58
 	return w.flush(p)
71
 }
59
 }
72
 
60
 
61
+func (w *wrapperBlockCipher) ReadTimeout(p []byte, timeout time.Duration) (int, error) {
62
+	return w.Read(p)
63
+}
64
+
73
 func (w *wrapperBlockCipher) flush(p []byte) (int, error) {
65
 func (w *wrapperBlockCipher) flush(p []byte) (int, error) {
74
 	if w.buf.Len() > len(p) {
66
 	if w.buf.Len() > len(p) {
75
 		return w.buf.Read(p)
67
 		return w.buf.Read(p)
93
 	return encrypted, nil
85
 	return encrypted, nil
94
 }
86
 }
95
 
87
 
96
-func readAll(src conntypes.StreamReadWriteCloser) (rv []byte, err error) {
97
-	buf := make([]byte, blockCipherReadCurrentDataBufferSize)
98
-	n := blockCipherReadCurrentDataBufferSize
99
-
100
-	for n == len(buf) {
101
-		n, err = src.Read(buf)
102
-		if err != nil {
103
-			return nil, err
104
-		}
105
-		rv = append(rv, buf[:n]...)
106
-	}
107
-
108
-	return rv, nil
109
-}
110
-
111
-func readAllTimeout(timeout time.Duration) func(conntypes.StreamReadWriteCloser) ([]byte, error) {
112
-	return func(src conntypes.StreamReadWriteCloser) (rv []byte, err error) {
113
-		tmo := timeout
114
-		buf := make([]byte, blockCipherReadCurrentDataBufferSize)
115
-		n := blockCipherReadCurrentDataBufferSize
116
-
117
-		for n == len(buf) {
118
-			if tmo <= 0 {
119
-				return nil, errors.New("timeout")
120
-			}
121
-			startTime := time.Now()
122
-			n, err = src.ReadTimeout(buf, tmo)
123
-			if err != nil {
124
-				return nil, err
125
-			}
126
-			rv = append(rv, buf[:n]...)
127
-			tmo -= time.Since(startTime)
128
-		}
129
-
130
-		return rv, nil
131
-	}
132
-}
133
-
134
 func (w *wrapperBlockCipher) Close() error {
88
 func (w *wrapperBlockCipher) Close() error {
135
 	return w.parent.Close()
89
 	return w.parent.Close()
136
 }
90
 }

Загрузка…
Отмена
Сохранить