-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
85 lines (72 loc) · 1.71 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"net"
"strings"
"syscall"
"github.com/c-robinson/iplib"
"tailscale.com/net/socks5"
)
func main() {
if err := loadConfig(); err != nil {
panic(err)
}
go checkIPsLoop()
subnet := iplib.NewNet6(net.ParseIP(cfg.Subnet), cfg.SubnetMask, 0)
server := &socks5.Server{
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
// Split host and port
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, fmt.Errorf("failed to split host and port: %w", err)
}
ip, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil, fmt.Errorf("failed to resolve IP: %w", err)
}
_, ok := whitelist[ip.String()]
if !ok {
for _, whost := range cfg.Whitelist {
if strings.EqualFold(host, whost) {
ok = true
break
}
}
}
// Valid dest
if ok {
newAddr := subnet.RandomIP()
log.Println("Dialing", network, addr, "from", newAddr)
dialer := &net.Dialer{
Control: func(network, address string, c syscall.RawConn) error {
var operr error
if err := c.Control(func(fd uintptr) {
operr = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_FREEBIND, 1)
}); err != nil {
return err
}
return operr
},
LocalAddr: &net.TCPAddr{
IP: newAddr,
},
}
conn, err := dialer.DialContext(ctx, network, addr)
if err != nil {
log.Println("Failed to dial:", err)
}
return conn, err
}
return nil, fmt.Errorf("ip %s is not in the whitelist", ip.IP.String())
},
}
ln, err := net.Listen("tcp", cfg.Listen)
if err != nil {
panic(err)
}
if err := server.Serve(ln); err != nil {
panic(err)
}
}