-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.go
More file actions
166 lines (137 loc) · 3.51 KB
/
server.go
File metadata and controls
166 lines (137 loc) · 3.51 KB
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
// Package socks implements a socks version 5 server (RFC 1928).
// Currently supports CONNECT for IPv4 and IPv6.
package socks
import (
"errors"
"fmt"
"io"
"net"
"strconv"
"time"
)
// The socks version that we support
const VERSION = 0x5
// SocksConn represents the client connection that we should read and reply Requests to
type SocksConn struct {
io.ReadWriteCloser
Dialer
}
// Handshake is the first step
// here we negotiate available authentication methods and verify the protocol version
func (s *SocksConn) Handshake() error {
buf := make([]byte, 2)
s.Read(buf)
ver, nmethods := buf[0], buf[1]
if ver != VERSION {
return errors.New("Invalid socks version specified by client")
}
if nmethods > 255 {
return errors.New("Too many methods specified")
}
buf = make([]byte, nmethods)
if nread, err := s.Read(buf); err != nil {
return err
} else {
// Look through methods to find one that suits us
for b := range buf[:nread] {
if b == 0x0 {
if _, err := s.Write([]byte{VERSION, 0x0}); err != nil {
return err
}
return nil
}
}
}
return errors.New("Could not find a suitable method for authentication")
}
// FailHandshake is ran when we want to tell the client we did not understand their Handshake
func (s *SocksConn) FailHandshake() {
s.Write([]byte{VERSION, 0xFF})
}
// FailRequest is a generic request failure reply
func (s *SocksConn) FailRequest() {
s.Write(Marshal(
&Request{Ver: VERSION, Cmd: 0x1, Atyp: 0x0, Addr: []byte{0x0}, Port: []byte{0x0, 0x0}}))
}
// HandleRequest reads the request and issues the requested command.
// Currently only CONNECT is implemented.
func (s *SocksConn) HandleRequest() error {
buf := make([]byte, 261)
nread, err := io.ReadAtLeast(s, buf, 4)
if err != nil {
return err
}
buf = buf[:nread]
r, err := Unmarshal(buf)
if err != nil {
return err
}
switch r.Cmd {
case CONNECT:
if err := s.Dial(r.Addr, r.Atyp, r.Port); err != nil {
return err
}
}
return nil
}
type Dialer interface {
Dial([]byte, AddrType, []byte) error
}
type defaultDialer struct {
client io.ReadWriteCloser
}
// Dial will connect to an address of specified type (IPV4 or 6) on behalf of the client.
// Writes and reads are copied between the client and the new connection until EOF of either end.
func (d defaultDialer) Dial(addr []byte, aTyp AddrType, port []byte) error {
sport := strconv.Itoa(int(BytesToInt(port)))
var saddr string
switch aTyp {
case DOMAIN:
saddr = string(addr)
case IPV4:
saddr = net.IP(addr).String()
case IPV6:
saddr = fmt.Sprintf("[%s]", net.IP(addr))
}
dialstring := fmt.Sprintf("%s:%s", saddr, sport)
conn, err := net.DialTimeout("tcp", dialstring, 30*time.Second)
if err != nil {
return err
}
laddr := conn.LocalAddr().(*net.TCPAddr)
if err != nil {
return err
}
if _, err := NewReply(0x0, laddr.IP, uint16(laddr.Port)).WriteTo(d.client); err != nil {
return err
}
done := make(chan bool)
go func() {
io.Copy(d.client, conn)
d.client.Close()
done <- true
}()
io.Copy(conn, d.client)
conn.Close()
<-done
return nil
}
// Serve will Handshake and HandleRequest for an established net.Conn
// or any other implementation of io.ReadWriteCloser. Connections will be proxied using specified
// dialer.
func Serve(c io.ReadWriteCloser, d Dialer) error {
defer c.Close()
if d == nil {
d = defaultDialer{c}
}
sock := SocksConn{c, d}
if err := sock.Handshake(); err != nil {
sock.FailHandshake()
return err
}
if err := sock.HandleRequest(); err != nil {
sock.FailRequest()
return err
}
return nil
}