/
io.go
194 lines (154 loc) · 3.66 KB
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package proxy
import (
. "../config"
"../logg"
"bytes"
"io"
"net"
"net/http"
"sync"
"time"
)
const sslRecordMax = 18 * 1024 // 18kb
func getIOCipher(dst io.Writer, src io.Reader, key string, throttle bool) *IOCopyCipher {
iocc := &IOCopyCipher{
Dst: dst,
Src: src,
Key: ReverseRandomKey(key),
Partial: *G_PartialEncrypt,
}
if throttle {
iocc.Throttling = NewTokenBucket(int64(*G_Throttling), int64(*G_ThrottlingMax))
}
return iocc
}
func copyAndClose(dst, src *net.TCPConn, key string, throttle bool) {
ts := time.Now()
if _, err := getIOCipher(dst, src, key, throttle).DoCopy(); err != nil {
logg.E("[COPY] ", time.Now().Sub(ts).Seconds(), "s - ", err)
}
dst.CloseWrite()
src.CloseRead()
}
func copyOrWarn(dst io.Writer, src io.Reader, key string, throttle bool, wg *sync.WaitGroup) {
ts := time.Now()
if _, err := getIOCipher(dst, src, key, throttle).DoCopy(); err != nil {
logg.E("[COPYW] ", time.Now().Sub(ts).Seconds(), "s - ", err)
}
wg.Done()
}
func TwoWayBridge(target, source net.Conn, key string, throttleTargetToSource bool) {
targetTCP, targetOK := target.(*net.TCPConn)
sourceTCP, sourceOK := source.(*net.TCPConn)
if targetOK && sourceOK {
// copy from source, decrypt, to target
go copyAndClose(targetTCP, sourceTCP, key, false)
// copy from target, encrypt, to source
go copyAndClose(sourceTCP, targetTCP, key, throttleTargetToSource)
} else {
go func() {
var wg sync.WaitGroup
wg.Add(2)
go copyOrWarn(target, source, key, false, &wg)
go copyOrWarn(source, target, key, throttleTargetToSource, &wg)
wg.Wait()
source.Close()
target.Close()
}()
}
}
type IOCopyCipher struct {
Dst io.Writer
Src io.Reader
Key []byte
Throttling *TokenBucket
Partial bool
}
func (cc *IOCopyCipher) DoCopy() (written int64, err error) {
defer func() {
if r := recover(); r != nil {
logg.E("[WTF] - ", r)
}
}()
buf := make([]byte, 32*1024)
ctr := GetCipherStream(cc.Key)
encrypted := 0
for {
nr, er := cc.Src.Read(buf)
if nr > 0 {
xbuf := buf[0:nr]
if cc.Partial && encrypted == sslRecordMax {
goto direct_transmission
}
if ctr != nil {
xs := 0
if bytes.HasPrefix(xbuf, OK200) {
xs = len(OK200)
}
if encrypted+nr > sslRecordMax && cc.Partial {
ctr.XorBuffer(xbuf[:sslRecordMax-encrypted])
encrypted = sslRecordMax
// we are done, the traffic coming later will be transfered as is
} else {
ctr.XorBuffer(xbuf[xs:])
encrypted += (nr - xs)
}
}
direct_transmission:
if cc.Throttling != nil {
cc.Throttling.Consume(int64(len(xbuf)))
}
nw, ew := cc.Dst.Write(xbuf)
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return written, err
}
type IOReaderCipher struct {
Src io.Reader
Key []byte
ctr *InplaceCTR
}
func (rc *IOReaderCipher) Init() *IOReaderCipher {
rc.ctr = GetCipherStream(rc.Key)
return rc
}
func (rc *IOReaderCipher) Read(p []byte) (n int, err error) {
n, err = rc.Src.Read(p)
if n > 0 && rc.ctr != nil {
// cp := make([]byte, n)
// copy(cp, p[:n])
//rc.ctr.XORKeyStream(cp, p[:n])
// copy(p, cp)
rc.ctr.XorBuffer(p[:n])
}
return
}
func XorWrite(w http.ResponseWriter, r *http.Request, p []byte, code int) (n int, err error) {
key := ReverseRandomKey(SafeGetHeader(r, rkeyHeader))
if ctr := GetCipherStream(key); ctr != nil {
// cp := make([]byte, len(p))
// copy(cp, p)
// ctr.XORKeyStream(cp, p)
// copy(p, cp)
ctr.XorBuffer(p)
}
w.WriteHeader(code)
return w.Write(p)
}