-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
103 lines (85 loc) · 2.23 KB
/
base.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
package inbound
import (
"encoding/json"
"net"
"net/netip"
"strconv"
"github.com/qauzy/netat/adapter/inbound"
C "github.com/qauzy/netat/constant"
)
type Base struct {
config *BaseOption
name string
specialRules string
listenAddr netip.Addr
port int
}
func NewBase(options *BaseOption) (*Base, error) {
if options.Listen == "" {
options.Listen = "0.0.0.0"
}
addr, err := netip.ParseAddr(options.Listen)
if err != nil {
return nil, err
}
return &Base{
name: options.Name(),
listenAddr: addr,
specialRules: options.SpecialRules,
port: options.Port,
config: options,
}, nil
}
// Config implements constant.InboundListener
func (b *Base) Config() C.InboundConfig {
return b.config
}
// Address implements constant.InboundListener
func (b *Base) Address() string {
return b.RawAddress()
}
// Close implements constant.InboundListener
func (*Base) Close() error {
return nil
}
// Name implements constant.InboundListener
func (b *Base) Name() string {
return b.name
}
// RawAddress implements constant.InboundListener
func (b *Base) RawAddress() string {
return net.JoinHostPort(b.listenAddr.String(), strconv.Itoa(int(b.port)))
}
// Listen implements constant.InboundListener
func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
return nil
}
func (b *Base) Additions() []inbound.Addition {
return b.config.Additions()
}
var _ C.InboundListener = (*Base)(nil)
type BaseOption struct {
NameStr string `inbound:"name"`
Listen string `inbound:"listen,omitempty"`
Port int `inbound:"port,omitempty"`
SpecialRules string `inbound:"rule,omitempty"`
SpecialProxy string `inbound:"proxy,omitempty"`
}
func (o BaseOption) Name() string {
return o.NameStr
}
func (o BaseOption) Equal(config C.InboundConfig) bool {
return optionToString(o) == optionToString(config)
}
func (o BaseOption) Additions() []inbound.Addition {
return []inbound.Addition{
inbound.WithInName(o.NameStr),
inbound.WithSpecialRules(o.SpecialRules),
inbound.WithSpecialProxy(o.SpecialProxy),
}
}
var _ C.InboundConfig = (*BaseOption)(nil)
func optionToString(option any) string {
str, _ := json.Marshal(option)
return string(str)
}