-
Notifications
You must be signed in to change notification settings - Fork 0
/
tproxy.go
91 lines (78 loc) · 1.81 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
package inbound
import (
"fmt"
C "github.com/qauzy/aiat/constant"
"github.com/qauzy/aiat/listener/tproxy"
"github.com/qauzy/aiat/log"
)
type TProxyOption struct {
BaseOption
UDP bool `inbound:"udp,omitempty"`
}
func (o TProxyOption) Equal(config C.InboundConfig) bool {
return optionToString(o) == optionToString(config)
}
type TProxy struct {
*Base
config *TProxyOption
lUDP *tproxy.UDPListener
lTCP *tproxy.Listener
udp bool
}
func NewTProxy(options *TProxyOption) (*TProxy, error) {
base, err := NewBase(&options.BaseOption)
if err != nil {
return nil, err
}
return &TProxy{
Base: base,
config: options,
udp: options.UDP,
}, nil
}
// Config implements constant.InboundListener
func (t *TProxy) Config() C.InboundConfig {
return t.config
}
// Address implements constant.InboundListener
func (t *TProxy) Address() string {
return t.lTCP.Address()
}
// Listen implements constant.InboundListener
func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
var err error
t.lTCP, err = tproxy.New(t.RawAddress(), tcpIn, t.Additions()...)
if err != nil {
return err
}
if t.udp {
t.lUDP, err = tproxy.NewUDP(t.RawAddress(), udpIn, natTable, t.Additions()...)
if err != nil {
return err
}
}
log.Infoln("TProxy[%s] proxy listening at: %s", t.Name(), t.Address())
return nil
}
// Close implements constant.InboundListener
func (t *TProxy) Close() error {
var tcpErr error
var udpErr error
if t.lTCP != nil {
tcpErr = t.lTCP.Close()
}
if t.lUDP != nil {
udpErr = t.lUDP.Close()
}
if tcpErr != nil && udpErr != nil {
return fmt.Errorf("tcp close err: %s and udp close err: %s", tcpErr, udpErr)
}
if tcpErr != nil {
return tcpErr
}
if udpErr != nil {
return udpErr
}
return nil
}
var _ C.InboundListener = (*TProxy)(nil)