-
Notifications
You must be signed in to change notification settings - Fork 34
/
redirect_linux.go
180 lines (150 loc) · 4.18 KB
/
redirect_linux.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//go:build linux
/**
* File: redirect_linux.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Wednesday, July 6th 2022, 11:47:00 am
* Last Modified: Thursday, July 7th 2022, 6:32:42 pm
*
* http://www.opensource.org/licenses/MIT
*/
package socks5lb
import (
"errors"
"fmt"
"github.com/LiamHaworth/go-tproxy"
log "github.com/sirupsen/logrus"
"github.com/txthinking/socks5"
"net"
"sync"
"syscall"
"time"
)
// getOriginalDstAddr to get the original address from the socket
// this function is referenced from
// https://github.com/ginuerzh/gost/blob/0247b941ac31344f0d7b3c547941a051188ba202/redirect.go#L72
func getOriginalDstAddr(conn *net.TCPConn) (addr net.Addr, c *net.TCPConn, err error) {
defer conn.Close()
fc, err := conn.File()
if err != nil {
return
}
defer fc.Close()
mreq, err := syscall.GetsockoptIPv6Mreq(int(fc.Fd()), syscall.IPPROTO_IP, 80)
if err != nil {
return
}
// only ipv4 support
ip := net.IPv4(mreq.Multiaddr[4], mreq.Multiaddr[5], mreq.Multiaddr[6], mreq.Multiaddr[7])
port := uint16(mreq.Multiaddr[2])<<8 + uint16(mreq.Multiaddr[3])
addr, err = net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", ip.String(), port))
if err != nil {
return
}
cc, err := net.FileConn(fc)
if err != nil {
return
}
c, ok := cc.(*net.TCPConn)
if !ok {
err = errors.New("sorry, this is not a TCP connection")
}
return
}
var (
currentSocks5Client *socks5.Client
updateClientLock sync.Mutex
)
// updateSocks5Client to connect to a proxy server
func (s *Server) updateSocks5Client() (client *socks5.Client, err error) {
updateClientLock.Lock()
defer func() {
if err != nil {
log.Error(err)
return
}
if client != nil && currentSocks5Client != client {
if currentSocks5Client != nil && (currentSocks5Client.TCPConn != nil || currentSocks5Client.UDPConn != nil) {
log.Tracef("close pervious client before update the current socks5 client")
_ = currentSocks5Client.Close()
currentSocks5Client = nil
}
log.Infof("markup current socks5 proxy client %v", client.Server)
currentSocks5Client = client
}
// lock the update client
updateClientLock.Unlock()
}()
// found a available backend
backend := s.Pool.Next()
if backend != nil {
return backend.socks5Client(0)
}
err = errors.New("sorry, we don't have healthy backend, so close the connection")
return
}
// ListenTProxy is listening the local tcp port on the given address
// Deprecated: this feature will be disabled in the future
func (s *Server) ListenTProxy(addr string) (err error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
log.Error(err)
return
}
s.tproxyListener, err = tproxy.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Error(err)
return
}
defer s.tproxyListener.Close()
// connect to available socks5 proxy server
selectTimeInterval := SecFromEnv("SELECT_TIME_INTERVAL", 300)
log.Infof("auto select the socks5 proxy server every %v", selectTimeInterval)
timer := time.NewTicker(selectTimeInterval)
defer timer.Stop()
go func() {
for ; true; <-timer.C {
log.Tracef("start to update current socks5 proxy client")
if _, err := s.updateSocks5Client(); err != nil {
log.Error(err)
}
}
}()
for {
if s.tproxyListener == nil {
return fmt.Errorf("transparent socket listenser is closed")
}
tproxyConn, err := s.tproxyListener.Accept()
if err != nil {
log.Error(err)
continue
}
go func() {
defer tproxyConn.Close()
if currentSocks5Client == nil {
log.Error("not found any suitable socks5 clients")
return
}
log.Tracef("using connected socks5 proxy client: %v", currentSocks5Client.Server)
connect, ok := tproxyConn.(*tproxy.Conn)
if !ok {
log.Error("[red-tcp] not a TCP connection")
return
}
srcAddr := connect.RemoteAddr()
dstAddr, orgDstConn, err := getOriginalDstAddr(connect.TCPConn)
if err != nil {
log.Errorf("[red-tcp] %s -> %s : %s", srcAddr, dstAddr, err)
return
}
defer orgDstConn.Close()
log.Tracef("[red-tcp] %s -> %s", srcAddr, dstAddr)
socks5Conn, err := currentSocks5Client.Dial("tcp", dstAddr.String())
if err != nil {
log.Error(err)
}
defer socks5Conn.Close()
s.Transport(orgDstConn, socks5Conn)
}()
}
}