-
Notifications
You must be signed in to change notification settings - Fork 16
/
socks.go
executable file
·77 lines (64 loc) · 1.28 KB
/
socks.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
package socks
import (
"io"
"log"
"net"
"time"
)
const (
socks4Version = 0x04
socks5Version = 0x05
cmdConnect = 0x01
addrTypeIPv4 = 0x01
addrTypeDomain = 0x03
addrTypeIPv6 = 0x04
)
// DialFunc the function dial to remote
type DialFunc func(network, addr string) (net.Conn, error)
// Conn present a socks connection
type Conn struct {
net.Conn
// the function to dial to upstream server
// when nil, use net.Dial
Dial DialFunc
// Auth the auth service to authenticate the user for socks5
Auth AuthService
}
// Serve serve the client
func (s *Conn) Serve() {
buf := make([]byte, 512)
// read version
n, err := io.ReadAtLeast(s.Conn, buf, 1)
if err != nil {
log.Println(err)
return
}
dial := s.Dial
if s.Dial == nil {
d := net.Dialer{Timeout: 10 * time.Second}
dial = d.Dial
}
switch buf[0] {
case socks4Version:
s4 := socks4Conn{clientConn: s.Conn, dial: dial}
s4.Serve(buf, n)
case socks5Version:
s5 := socks5Conn{clientConn: s.Conn, dial: dial, auth: s.Auth}
s5.Serve(buf, n)
default:
log.Printf("unknown socks version 0x%x", buf[0])
s.Conn.Close()
}
}
func forward(c1, c2 io.ReadWriter) {
c := make(chan struct{}, 2)
go func() {
io.Copy(c1, c2)
c <- struct{}{}
}()
go func() {
io.Copy(c2, c1)
c <- struct{}{}
}()
<-c
}