-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
156 lines (132 loc) · 3.2 KB
/
server.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package srcproxy
import (
"fmt"
"github.com/haruue-net/srcproxy/utils"
"github.com/lunixbochs/struc"
"net"
"time"
)
type Server struct {
CheckDstFunc CheckAddrFunc
CheckSrcFunc CheckAddrFunc
Logger ServerLogger
Timeout time.Duration
DialerCreatedFunc func(dialer *net.Dialer)
}
type CheckAddrFunc = func(origAddr *net.TCPAddr, auth []byte) (modifiedAddr *net.TCPAddr, err error)
var sockopts = utils.LinuxSockopts{FreeBind: true}
type ServerLogger interface {
LogConn(reqAddr net.Addr, src, dst *net.TCPAddr)
LogError(reqAddr net.Addr, src, dst *net.TCPAddr, err error)
}
func (s *Server) logConn(conn net.Conn, src, dst *net.TCPAddr) {
if s.Logger != nil {
s.Logger.LogConn(conn.RemoteAddr(), src, dst)
}
}
func (s *Server) logError(conn net.Conn, src, dst *net.TCPAddr, err error) {
if s.Logger != nil {
s.Logger.LogError(conn.RemoteAddr(), src, dst, err)
}
}
func (s *Server) checkSrc(origSrc *net.TCPAddr, auth []byte) (modifiedSrc *net.TCPAddr, err error) {
if s.CheckSrcFunc != nil {
return s.CheckSrcFunc(origSrc, auth)
}
modifiedSrc = origSrc
return
}
func (s *Server) checkDst(origDst *net.TCPAddr, auth []byte) (modifiedDst *net.TCPAddr, err error) {
if s.CheckDstFunc != nil {
return s.CheckDstFunc(origDst, auth)
}
modifiedDst = origDst
return
}
func (s *Server) ServeListener(listener utils.TCPListener) error {
for {
c, err := listener.Accept()
if err != nil {
return err
}
go func() {
defer c.Close()
s.handleConn(c)
}()
}
}
func (s *Server) handleConn(conn net.Conn) {
var err error
defer func() {
if err != nil {
s.logError(conn, nil, nil, err)
}
}()
var req RelayRequest
err = struc.Unpack(conn, &req)
if err != nil {
return
}
if req.Version != Version {
err = fmt.Errorf("unsupported version: %d", req.Version)
return
}
switch req.OpCode {
case OpCodeRelayRequest:
s.handleRelayRequest(conn, &req)
default:
err = fmt.Errorf("unsupported opcode: %d", req.OpCode)
}
}
func (s *Server) handleRelayRequest(conn net.Conn, req *RelayRequest) {
var err error
var src, dst *net.TCPAddr
defer func() {
if err != nil {
s.logError(conn, src, dst, err)
}
}()
if req.SrcAddrLen != net.IPv4len && req.SrcAddrLen != net.IPv6len {
err = fmt.Errorf("invalid src addr len: %d", req.SrcAddrLen)
return
}
if req.DstAddrLen != net.IPv4len && req.DstAddrLen != net.IPv6len {
err = fmt.Errorf("invalid dst addr len: %d", req.DstAddrLen)
return
}
src = &net.TCPAddr{
IP: req.SrcAddr,
Port: int(req.SrcPort),
}
msrc, err := s.checkSrc(src, req.Auth)
if err != nil {
err = fmt.Errorf("rejected by src checker: %w", err)
return
}
dst = &net.TCPAddr{
IP: req.DstAddr,
Port: int(req.DstPort),
}
mdst, err := s.checkDst(dst, req.Auth)
if err != nil {
err = fmt.Errorf("rejected by dst checker: %w", err)
return
}
src = msrc
dst = mdst
dialer := &net.Dialer{
LocalAddr: src,
Timeout: s.Timeout,
Control: sockopts.Control,
}
if s.DialerCreatedFunc != nil {
s.DialerCreatedFunc(dialer)
}
dstConn, err := dialer.Dial("tcp", dst.String())
if err != nil {
return
}
defer dstConn.Close()
s.logConn(conn, src, dst)
err = utils.PipePairWithTimeout(conn, dstConn, s.Timeout)
}