forked from shawwwn/Gole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.go
180 lines (167 loc) · 4.47 KB
/
crypt.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
package gole
import (
"crypto/sha1"
"errors"
"net"
"syscall"
"time"
xor "github.com/templexxx/xorsimd"
"golang.org/x/crypto/pbkdf2"
)
type EConnXor struct {
conn net.Conn
key []byte
key_ri int // key read index
key_wi int // key write index
}
func (econn *EConnXor) Conn() net.Conn {
return econn.conn
}
func (econn *EConnXor) Read(b []byte) (n int, err error) {
n, err = econn.conn.Read(b)
// stream decrypt
sz := n
for i := 0; i < sz; {
ct := xor.Bytes(b[i:n], b[i:n], econn.key[econn.key_ri:])
if ct == 0 {
break
}
econn.key_ri = (econn.key_ri + ct) % len(econn.key)
i += ct
}
return n, err
}
func (econn *EConnXor) Write(b []byte) (n int, err error) {
// stream encrypt
sz := len(b)
for i := 0; i < sz; {
ct := xor.Bytes(b[i:], b[i:], econn.key[econn.key_wi:])
if ct == 0 {
break
}
econn.key_wi = (econn.key_wi + ct) % len(econn.key)
i += ct
}
return econn.conn.Write(b)
}
func (econn *EConnXor) Close() error {
return econn.conn.Close()
}
func (econn *EConnXor) LocalAddr() net.Addr {
return econn.conn.LocalAddr()
}
func (econn *EConnXor) RemoteAddr() net.Addr {
return econn.conn.RemoteAddr()
}
func (econn *EConnXor) SetDeadline(t time.Time) error {
return econn.conn.SetDeadline(t)
}
func (econn *EConnXor) SetReadDeadline(t time.Time) error {
return econn.conn.SetReadDeadline(t)
}
func (econn *EConnXor) SetWriteDeadline(t time.Time) error {
return econn.conn.SetWriteDeadline(t)
}
func (econn *EConnXor) SetReadBuffer(bytes int) error {
if nc, ok := econn.conn.(*net.TCPConn); ok {
return nc.SetReadBuffer(bytes)
}
return errors.New("not implemented")
}
func (econn *EConnXor) SetWriteBuffer(bytes int) error {
if nc, ok := econn.conn.(*net.TCPConn); ok {
return nc.SetWriteBuffer(bytes)
}
return errors.New("not implemented")
}
func (econn *EConnXor) SyscallConn() (syscall.RawConn, error) {
if nc, ok := econn.conn.(*net.TCPConn); ok {
return nc.SyscallConn()
}
return nil, errors.New("not implemented")
}
type EPacketConnXor struct {
conn net.PacketConn
key []byte
}
func (econn *EPacketConnXor) Conn() net.Conn {
if nc, ok := econn.conn.(net.Conn); ok {
return nc
}
return nil
}
func (econn *EPacketConnXor) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
n, addr, err = econn.conn.ReadFrom(b)
xor.Bytes(b, b[:n], []byte(econn.key))
return n, addr, err
}
func (econn *EPacketConnXor) WriteTo(b []byte, addr net.Addr) (n int, err error) {
xor.Bytes(b, b, []byte(econn.key))
return econn.conn.WriteTo(b, addr)
}
func (econn *EPacketConnXor) Close() error {
return econn.conn.Close()
}
func (econn *EPacketConnXor) LocalAddr() net.Addr {
return econn.conn.LocalAddr()
}
func (econn *EPacketConnXor) SetDeadline(t time.Time) error {
return econn.conn.SetDeadline(t)
}
func (econn *EPacketConnXor) SetReadDeadline(t time.Time) error {
return econn.conn.SetReadDeadline(t)
}
func (econn *EPacketConnXor) SetWriteDeadline(t time.Time) error {
return econn.conn.SetWriteDeadline(t)
}
func (econn *EPacketConnXor) SetReadBuffer(bytes int) error {
if nc, ok := econn.conn.(*net.UDPConn); ok {
return nc.SetReadBuffer(bytes)
}
return errors.New("not implemented")
}
func (econn *EPacketConnXor) SetWriteBuffer(bytes int) error {
if nc, ok := econn.conn.(*net.UDPConn); ok {
return nc.SetWriteBuffer(bytes)
}
return errors.New("not implemented")
}
func (econn *EPacketConnXor) SyscallConn() (syscall.RawConn, error) {
if nc, ok := econn.conn.(*net.UDPConn); ok {
return nc.SyscallConn()
}
return nil, errors.New("not implemented")
}
// for compatibility with net.Conn
func (econn *EPacketConnXor) Read(b []byte) (n int, err error) {
conn, ok := interface{}(econn.conn).(net.Conn)
if ok {
n, err = conn.Read(b)
xor.Bytes(b, b[:n], []byte(econn.key))
return n, err
}
return 0, errors.New("not implemented")
}
func (econn *EPacketConnXor) Write(b []byte) (n int, err error) {
conn, ok := interface{}(econn.conn).(net.Conn)
if ok {
xor.Bytes(b, b, []byte(econn.key))
return conn.Write(b)
}
return 0, errors.New("not implemented")
}
func (econn *EPacketConnXor) RemoteAddr() net.Addr {
conn, ok := interface{}(econn.conn).(net.Conn)
if ok {
return conn.RemoteAddr()
}
return nil
}
func NewEConn(conn net.Conn, enc, key string) net.Conn {
k := pbkdf2.Key([]byte(key), []byte("saltybiscuit"), 64, 4096, sha1.New)
return &EConnXor{conn, k, 0, 0}
}
func NewEPacketConn(conn net.PacketConn, enc, key string) net.PacketConn {
k := pbkdf2.Key([]byte(key), []byte("saltybiscuit"), 64, 4096, sha1.New)
return &EPacketConnXor{conn, k}
}