-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackets.go
161 lines (128 loc) · 3.51 KB
/
packets.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
package main
import (
"errors"
"fmt"
"io"
"net"
)
type MethodRequest struct {
version byte
methods []byte
}
type MethodReply struct {
version byte
method byte
}
type SocksRequest struct {
version byte
command byte
addressType byte
address net.IP
domain string
port int
}
type SocksReply struct {
version byte
reply byte
addressType byte
address net.IP
domain string
port int
}
func (s MethodRequest) ReadBinary(r io.Reader) error {
b := make([]byte, 2)
if n, _ := r.Read(b); n != 2 {
return errors.New("MethodRequest packet is too short")
}
if s.version = b[0]; s.version != 0x05 {
return fmt.Errorf("Invalid version: %d", s.version)
}
numMethods := int(b[1])
if numMethods == 0 {
return errors.New("Invalid number of methods: 0")
} else {
s.methods = make([]byte, numMethods)
}
if n, _ := r.Read(s.methods); n != numMethods {
return errors.New("MethodRequest packet is too short")
}
return nil
}
func (s MethodReply) WriteBinary(w io.Writer) error {
out := []byte{s.version, s.method}
if _, err := w.Write(out); err != nil {
return fmt.Errorf("Error writing MethodReply: %v", err)
}
return nil
}
func (s SocksRequest) ReadBinary(r io.Reader) error {
b := make([]byte, 4)
if n, _ := r.Read(b); n != 4 {
return errors.New("SocksRequest input too short")
}
if s.version = b[0]; s.version != 0x05 {
return fmt.Errorf("Invalid version: %d", b[0])
}
if s.command = b[1]; b[1] == 0 || b[1] > 3 {
return fmt.Errorf("Invalid command: %d", s.command)
}
if s.addressType = b[3]; b[3] != 1 && b[3] != 3 && b[3] != 4 {
return fmt.Errorf("Invalid address type: %d", b[3])
}
if s.addressType == 1 {
address := make([]byte, 4)
if n, _ := r.Read(address); n != 4 {
return errors.New("SocksRequest address is too short")
}
s.address = address
} else if s.addressType == 4 {
address := make([]byte, 16)
if n, _ := r.Read(address); n != 16 {
return errors.New("SocksRequest address is too short")
}
s.address = address
} else if s.addressType == 3 {
length := make([]byte, 1)
if n, _ := r.Read(length); n != 1 {
return errors.New("SocksRequest domain length missing")
}
domainLength := int(length[0])
domain := make([]byte, domainLength)
if n, _ := r.Read(domain); n < domainLength {
return errors.New("SocksRequest domain too short")
}
s.domain = string(domain)
}
portBytes := make([]byte, 2)
if n, _ := r.Read(portBytes); n != 2 {
return errors.New("SocksRequest missing port")
}
//Horrible, but it works
s.port = (int(portBytes[0]) << 8) + int(portBytes[1])
return nil
}
func (s SocksReply) WriteBinary(w io.Writer) error {
var b []byte
if s.addressType == 0 || s.addressType == 1 {
b = make([]byte, 0, 10)
b = append(b, s.version, s.reply, 0x00, s.addressType)
b = append(b, s.address[0:4]...)
b = append(b, byte((s.port&0xFF00)>>8), byte(s.port&0xFF))
} else if s.addressType == 4 {
b = make([]byte, 0, 22)
b = append(b, s.version, s.reply, 0x00, s.addressType)
b = append(b, s.address[0:16]...)
b = append(b, byte((s.port&0xFF00)>>8), byte(s.port&0xFF))
} else if s.addressType == 3 {
b = make([]byte, 0, 7+len(s.domain))
b = append(b, s.version, s.reply, 0x00, s.addressType, byte(len(s.domain)))
b = append(b, s.domain...)
b = append(b, byte((s.port&0xFF00)>>8), byte(s.port&0xFF))
} else {
return errors.New("Cannot write SocksReply, Invalid address type")
}
if _, err := w.Write(b); err != nil {
return fmt.Errorf("Error writing SocksReply: %v", err)
}
return nil
}