-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconn_socks4.go
53 lines (48 loc) · 1.04 KB
/
conn_socks4.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
package proxy
import (
"io"
"net"
)
type socks4Conn struct {
localConn net.Conn
conf *SOCKSConf
}
func (c *socks4Conn) Serve() (err error) {
request, err := readSocks4Request(c.localConn)
if err != nil {
c.sendReply(request, socks4StatusRejected)
return err
}
switch request.command {
case commandConnect:
c.sendReply(request, socks4StatusGranted)
err = c.handleConnect(request.Address())
default:
err = errCommandNotSupported
}
if err != nil {
c.sendReply(request, socks4StatusRejected)
}
return
}
func (c *socks4Conn) handleConnect(host string) (err error) {
remoteConn, err := c.conf.Dial("tcp", host)
if err != nil {
return err
}
go io.Copy(c.localConn, remoteConn)
go io.Copy(remoteConn, c.localConn)
return
}
func (c *socks4Conn) sendReply(request *socks4Request, status byte) {
response := &socks4Response{
status:status,
port: make([]byte, 2),
ip: make([]byte, 4),
}
if request.IsSOCKS4A() {
response.port = request.port
response.ip = request.ip
}
c.localConn.Write(response.ToPacket())
}