-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtproxy.go
124 lines (102 loc) · 2.51 KB
/
tproxy.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
package ttproxy
import (
"fmt"
"io"
"log/slog"
"net"
"github.com/imgk/go-tproxy"
)
func (srv Server) ServeTProxyTCP() error {
addr, err := net.ResolveTCPAddr("tcp", srv.TProxyAddr)
if err != nil {
return err
}
ln, err := tproxy.ListenTCP("tcp", addr)
if err != nil {
return err
}
defer ln.Close()
slog.Info(fmt.Sprintf("receive new tproxy connnection at %s", ln.Addr().String()))
for {
conn, err := ln.AcceptTProxy()
if err != nil {
break
}
slog.Info(fmt.Sprintf("receive new tproxy TCP connection %s <---> %s", conn.RemoteAddr().String(), conn.LocalAddr().String()))
go srv.relay(conn)
}
return nil
}
func (srv Server) relay(conn tproxy.Conn) {
defer conn.Close()
rc, err := srv.Dial("tcp", conn.LocalAddr().String())
if err != nil {
// slog.Error(fmt.Sprintf("dial TCP connnection to ---> %s, error: %s", conn.LocalAddr().String(), err.Error()))
return
}
defer rc.Close()
done := make(chan struct{})
go func() {
io.Copy(rc, conn)
// conn.WriteTo(rc)
// copyBuffer(rc, conn, make([]byte, 1024*16))
if cw, ok := rc.(interface{ CloseWrite() error }); ok {
cw.CloseWrite()
}
done <- struct{}{}
}()
io.Copy(conn, rc)
// conn.ReadFrom(rc)
// copyBuffer(conn, rc, make([]byte, 1024*16))
conn.CloseWrite()
<-done
}
func (srv Server) ServeTProxyUDP() error {
addr, err := net.ResolveUDPAddr("udp", srv.TProxyAddr)
if err != nil {
return err
}
ln, err := tproxy.ListenUDP("udp", addr)
if err != nil {
return err
}
defer ln.Close()
nm := newNatMap(srv.Timeout)
bb := make([]byte, 2048)
for {
n, addr, raddr, err := ln.ReadFromUDPAddrPortTProxy(bb)
if err != nil {
break
}
pc, ok := nm.Get(addr)
if ok {
// write packet to remote connection if found
if _, err := pc.WriteToUDPAddrPort(bb[:n], raddr); err != nil {
slog.Error(fmt.Sprintf("write from: %v to: %v error: %v", addr, raddr, err))
}
continue
}
// create new connection for new address
slog.Info(fmt.Sprintf("receive new tproxy UDP connection %s <---> %s", addr.String(), raddr.String()))
conn, err := srv.Dial("udp", raddr.String())
if err != nil {
// slog.Error(fmt.Sprintf("dial UDP connnection to ---> %s, error: %s", raddr.String(), err.Error()))
continue
}
pc = newPacketConn(conn)
// enable firewall for default
// only transport packets with context id
err = pc.SetFirewall(true)
if err != nil {
pc.Close()
continue
}
_, err = pc.WriteToUDPAddrPort(bb[:n], raddr)
if err != nil {
pc.Close()
continue
}
nm.Add(addr, pc)
}
return nil
}