-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixed.go
89 lines (77 loc) · 1.83 KB
/
mixed.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
package inbound
import (
"fmt"
C "github.com/qauzy/netat/constant"
"github.com/qauzy/netat/log"
"github.com/qauzy/netat/listener/mixed"
"github.com/qauzy/netat/listener/socks"
)
type MixedOption struct {
BaseOption
UDP bool `inbound:"udp,omitempty"`
}
func (o MixedOption) Equal(config C.InboundConfig) bool {
return optionToString(o) == optionToString(config)
}
type Mixed struct {
*Base
config *MixedOption
l *mixed.Listener
lUDP *socks.UDPListener
udp bool
}
func NewMixed(options *MixedOption) (*Mixed, error) {
base, err := NewBase(&options.BaseOption)
if err != nil {
return nil, err
}
return &Mixed{
Base: base,
config: options,
udp: options.UDP,
}, nil
}
// Config implements constant.InboundListener
func (m *Mixed) Config() C.InboundConfig {
return m.config
}
// Address implements constant.InboundListener
func (m *Mixed) Address() string {
return m.l.Address()
}
// Listen implements constant.InboundListener
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
var err error
m.l, err = mixed.New(m.RawAddress(), tcpIn, m.Additions()...)
if err != nil {
return err
}
if m.udp {
m.lUDP, err = socks.NewUDP(m.RawAddress(), udpIn, m.Additions()...)
if err != nil {
return err
}
}
log.Infoln("Mixed(http+socks)[%s] proxy listening at: %s", m.Name(), m.Address())
return nil
}
// Close implements constant.InboundListener
func (m *Mixed) Close() error {
var err error
if m.l != nil {
if tcpErr := m.l.Close(); tcpErr != nil {
err = tcpErr
}
}
if m.udp && m.lUDP != nil {
if udpErr := m.lUDP.Close(); udpErr != nil {
if err == nil {
err = udpErr
} else {
return fmt.Errorf("close tcp err: %s, close udp err: %s", err.Error(), udpErr.Error())
}
}
}
return err
}
var _ C.InboundListener = (*Mixed)(nil)