-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (123 loc) · 2.85 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
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
package main
import (
"github.com/armon/go-socks5"
"github.com/coreos/go-systemd/daemon"
"golang.org/x/net/context"
"log"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
type ruleset struct {
config *config
}
func ipToNet(ip net.IP) net.IPNet {
n := net.IPNet{
IP: ip,
Mask: make(net.IPMask, len(ip)),
}
for i := range ip {
n.Mask[i] = 0xff
}
return n
}
func (self *ruleset) Allow(ctx context.Context, req *socks5.Request) (context.Context, bool) {
src := &NetPort{ipToNet(req.RemoteAddr.IP), req.RemoteAddr.Port}
dst := &NetPort{ipToNet(req.DestAddr.IP), req.DestAddr.Port}
allowed := self.config.allow.Match("allow", src, dst)
denied := self.config.deny.Match("deny", src, dst)
ok := allowed && !denied
log.Printf("access-check src=%s dst=%s ok=%v", req.RemoteAddr.Address(), req.DestAddr.Address(), ok)
return ctx, ok
}
func MustSdNotify(state string) {
if _, err := daemon.SdNotify(false, state); err != nil {
panic(err)
}
}
func main() {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)
MustSdNotify("READY=1")
if watchdogTimeout, err := daemon.SdWatchdogEnabled(false); err != nil && watchdogTimeout > 0 {
go func() {
const watchdogState = "WATCHDOG=1"
for {
time.Sleep(watchdogTimeout / 2)
MustSdNotify(watchdogState)
}
}()
}
conf := newConfig()
conf.MustParse()
log.Printf("config: %s", conf.String())
socksConf := &socks5.Config{
Rules: &ruleset{config: conf},
Logger: log.New(os.Stderr, "", log.LstdFlags),
}
server, err := socks5.New(socksConf)
if err != nil {
panic(err)
}
connch := make(chan net.Conn, 8)
var listener net.Listener
if listener, err = net.Listen("tcp", conf.addrListen); err != nil {
panic(err)
}
wg := sync.WaitGroup{}
donech := make(chan struct{})
stopListen := make(chan struct{}, 1)
terminate := func(timeout time.Duration, exitCode int) {
begin := time.Now()
MustSdNotify("STOPPING=1")
stopListen <- struct{}{}
listener.Close()
go func() { wg.Wait(); donech <- struct{}{} }()
select {
case <-donech:
case <-time.After(timeout):
}
cleanDuration := time.Since(begin)
log.Printf("remaining work finished in %s", cleanDuration)
os.Exit(exitCode)
}
go func() {
for {
conn, err := listener.Accept()
select {
case <-stopListen:
return
default:
if err == nil {
log.Printf("accept() remote=%s", conn.RemoteAddr().String())
wg.Add(1)
connch <- conn
} else {
log.Fatal(err)
}
}
}
}()
for {
select {
case conn := <-connch:
go func() {
server.ServeConn(conn)
wg.Done()
}()
case sig := <-sigch:
if sig == os.Interrupt {
log.Printf("caught SIGINT")
terminate(conf.interruptTimeout, 1)
}
if sig == syscall.SIGTERM {
log.Printf("caught SIGTERM")
terminate(conf.gracefulTimeout, 0)
}
log.Fatalf("unexpected signal: %v", sig)
}
}
}