forked from go-gost/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
159 lines (134 loc) · 2.91 KB
/
node.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
package chain
import (
"regexp"
"github.com/dolfly/core/auth"
"github.com/dolfly/core/bypass"
"github.com/dolfly/core/hosts"
"github.com/dolfly/core/metadata"
"github.com/dolfly/core/resolver"
"github.com/dolfly/core/selector"
)
type HTTPURLRewriteSetting struct {
Pattern *regexp.Regexp
Replacement string
}
type HTTPNodeSettings struct {
Host string
Header map[string]string
Auther auth.Authenticator
Rewrite []HTTPURLRewriteSetting
}
type TLSNodeSettings struct {
ServerName string
Secure bool
Options struct {
MinVersion string
MaxVersion string
CipherSuites []string
}
}
type NodeOptions struct {
Transport *Transport
Bypass bypass.Bypass
Resolver resolver.Resolver
HostMapper hosts.HostMapper
Metadata metadata.Metadata
Host string
Network string
Protocol string
Path string
HTTP *HTTPNodeSettings
TLS *TLSNodeSettings
}
type NodeOption func(*NodeOptions)
func TransportNodeOption(tr *Transport) NodeOption {
return func(o *NodeOptions) {
o.Transport = tr
}
}
func BypassNodeOption(bp bypass.Bypass) NodeOption {
return func(o *NodeOptions) {
o.Bypass = bp
}
}
func ResoloverNodeOption(resolver resolver.Resolver) NodeOption {
return func(o *NodeOptions) {
o.Resolver = resolver
}
}
func HostMapperNodeOption(m hosts.HostMapper) NodeOption {
return func(o *NodeOptions) {
o.HostMapper = m
}
}
func HostNodeOption(host string) NodeOption {
return func(o *NodeOptions) {
o.Host = host
}
}
func NetworkNodeOption(network string) NodeOption {
return func(o *NodeOptions) {
o.Network = network
}
}
func ProtocolNodeOption(protocol string) NodeOption {
return func(o *NodeOptions) {
o.Protocol = protocol
}
}
func PathNodeOption(path string) NodeOption {
return func(o *NodeOptions) {
o.Path = path
}
}
func MetadataNodeOption(md metadata.Metadata) NodeOption {
return func(o *NodeOptions) {
o.Metadata = md
}
}
func HTTPNodeOption(httpSettings *HTTPNodeSettings) NodeOption {
return func(o *NodeOptions) {
o.HTTP = httpSettings
}
}
func TLSNodeOption(tlsSettings *TLSNodeSettings) NodeOption {
return func(o *NodeOptions) {
o.TLS = tlsSettings
}
}
type Node struct {
Name string
Addr string
marker selector.Marker
options NodeOptions
}
func NewNode(name string, addr string, opts ...NodeOption) *Node {
var options NodeOptions
for _, opt := range opts {
if opt != nil {
opt(&options)
}
}
return &Node{
Name: name,
Addr: addr,
marker: selector.NewFailMarker(),
options: options,
}
}
func (node *Node) Options() *NodeOptions {
return &node.options
}
// Metadata implements metadadta.Metadatable interface.
func (node *Node) Metadata() metadata.Metadata {
return node.options.Metadata
}
// Marker implements selector.Markable interface.
func (node *Node) Marker() selector.Marker {
return node.marker
}
func (node *Node) Copy() *Node {
n := &Node{}
*n = *node
return n
}