improve config
This commit is contained in:
@ -23,15 +23,15 @@ func (c *Chain) GetRouteFor(network, address string) (r *route) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
if node.bypass != nil && node.bypass.Contains(address) {
|
||||
if node.Bypass != nil && node.Bypass.Contains(address) {
|
||||
break
|
||||
}
|
||||
|
||||
if node.transport.Multiplex() {
|
||||
tr := node.transport.Copy().
|
||||
if node.Transport.Multiplex() {
|
||||
tr := node.Transport.Copy().
|
||||
WithRoute(r)
|
||||
node = node.Copy().
|
||||
WithTransport(tr)
|
||||
node = node.Copy()
|
||||
node.Transport = tr
|
||||
r = &route{}
|
||||
}
|
||||
|
||||
|
@ -5,44 +5,18 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/bypass"
|
||||
"github.com/go-gost/gost/pkg/hosts"
|
||||
"github.com/go-gost/gost/pkg/resolver"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
name string
|
||||
addr string
|
||||
transport *Transport
|
||||
bypass bypass.Bypass
|
||||
marker *FailMarker
|
||||
}
|
||||
|
||||
func NewNode(name, addr string) *Node {
|
||||
return &Node{
|
||||
name: name,
|
||||
addr: addr,
|
||||
marker: &FailMarker{},
|
||||
}
|
||||
}
|
||||
|
||||
func (node *Node) Name() string {
|
||||
return node.name
|
||||
}
|
||||
|
||||
func (node *Node) Addr() string {
|
||||
return node.addr
|
||||
}
|
||||
|
||||
func (node *Node) Marker() *FailMarker {
|
||||
return node.marker
|
||||
}
|
||||
|
||||
func (node *Node) WithTransport(tr *Transport) *Node {
|
||||
node.transport = tr
|
||||
return node
|
||||
}
|
||||
|
||||
func (node *Node) WithBypass(bp bypass.Bypass) *Node {
|
||||
node.bypass = bp
|
||||
return node
|
||||
Name string
|
||||
Addr string
|
||||
Transport *Transport
|
||||
Bypass bypass.Bypass
|
||||
Resolver resolver.Resolver
|
||||
Hosts hosts.HostMapper
|
||||
Marker *FailMarker
|
||||
}
|
||||
|
||||
func (node *Node) Copy() *Node {
|
||||
|
@ -29,35 +29,35 @@ func (r *route) connect(ctx context.Context) (conn net.Conn, err error) {
|
||||
}
|
||||
|
||||
node := r.nodes[0]
|
||||
cc, err := node.transport.Dial(ctx, r.nodes[0].Addr())
|
||||
cc, err := node.Transport.Dial(ctx, r.nodes[0].Addr)
|
||||
if err != nil {
|
||||
node.Marker().Mark()
|
||||
node.Marker.Mark()
|
||||
return
|
||||
}
|
||||
|
||||
cn, err := node.transport.Handshake(ctx, cc)
|
||||
cn, err := node.Transport.Handshake(ctx, cc)
|
||||
if err != nil {
|
||||
cc.Close()
|
||||
node.Marker().Mark()
|
||||
node.Marker.Mark()
|
||||
return
|
||||
}
|
||||
node.Marker().Reset()
|
||||
node.Marker.Reset()
|
||||
|
||||
preNode := node
|
||||
for _, node := range r.nodes[1:] {
|
||||
cc, err = preNode.transport.Connect(ctx, cn, "tcp", node.Addr())
|
||||
cc, err = preNode.Transport.Connect(ctx, cn, "tcp", node.Addr)
|
||||
if err != nil {
|
||||
cn.Close()
|
||||
node.Marker().Mark()
|
||||
node.Marker.Mark()
|
||||
return
|
||||
}
|
||||
cc, err = node.transport.Handshake(ctx, cc)
|
||||
cc, err = node.Transport.Handshake(ctx, cc)
|
||||
if err != nil {
|
||||
cn.Close()
|
||||
node.Marker().Mark()
|
||||
node.Marker.Mark()
|
||||
return
|
||||
}
|
||||
node.Marker().Reset()
|
||||
node.Marker.Reset()
|
||||
|
||||
cn = cc
|
||||
preNode = node
|
||||
@ -77,7 +77,7 @@ func (r *route) Dial(ctx context.Context, network, address string) (net.Conn, er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cc, err := r.Last().transport.Connect(ctx, conn, network, address)
|
||||
cc, err := r.Last().Transport.Connect(ctx, conn, network, address)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
@ -108,7 +108,7 @@ func (r *route) Bind(ctx context.Context, network, address string, opts ...conne
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ln, err := r.Last().transport.Bind(ctx, conn, network, address, opts...)
|
||||
ln, err := r.Last().Transport.Bind(ctx, conn, network, address, opts...)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
@ -134,8 +134,8 @@ func (r *route) Path() (path []*Node) {
|
||||
}
|
||||
|
||||
for _, node := range r.nodes {
|
||||
if node.transport != nil && node.transport.route != nil {
|
||||
path = append(path, node.transport.route.Path()...)
|
||||
if node.Transport != nil && node.Transport.route != nil {
|
||||
path = append(path, node.Transport.route.Path()...)
|
||||
}
|
||||
path = append(path, node)
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func (r *Router) dial(ctx context.Context, network, address string) (conn net.Co
|
||||
if r.Logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
buf := bytes.Buffer{}
|
||||
for _, node := range route.Path() {
|
||||
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
|
||||
fmt.Fprintf(&buf, "%s@%s > ", node.Name, node.Addr)
|
||||
}
|
||||
fmt.Fprintf(&buf, "%s", address)
|
||||
r.Logger.Debugf("route(retry=%d) %s", i, buf.String())
|
||||
@ -114,7 +114,7 @@ func (r *Router) Bind(ctx context.Context, network, address string, opts ...conn
|
||||
if r.Logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
buf := bytes.Buffer{}
|
||||
for _, node := range route.Path() {
|
||||
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
|
||||
fmt.Fprintf(&buf, "%s@%s > ", node.Name, node.Addr)
|
||||
}
|
||||
fmt.Fprintf(&buf, "%s", address)
|
||||
r.Logger.Debugf("route(retry=%d) %s", i, buf.String())
|
||||
|
@ -141,8 +141,8 @@ func (f *failFilter) Filter(nodes ...*Node) []*Node {
|
||||
}
|
||||
var nl []*Node
|
||||
for _, node := range nodes {
|
||||
if node.Marker().FailCount() < int64(maxFails) ||
|
||||
time.Since(time.Unix(node.Marker().FailTime(), 0)) >= failTimeout {
|
||||
if node.Marker.FailCount() < int64(maxFails) ||
|
||||
time.Since(time.Unix(node.Marker.FailTime(), 0)) >= failTimeout {
|
||||
nl = append(nl, node)
|
||||
}
|
||||
}
|
||||
@ -161,7 +161,7 @@ func InvalidFilter() Filter {
|
||||
func (f *invalidFilter) Filter(nodes ...*Node) []*Node {
|
||||
var nl []*Node
|
||||
for _, node := range nodes {
|
||||
_, sport, _ := net.SplitHostPort(node.Addr())
|
||||
_, sport, _ := net.SplitHostPort(node.Addr)
|
||||
if port, _ := strconv.Atoi(sport); port > 0 {
|
||||
nl = append(nl, node)
|
||||
}
|
||||
|
Reference in New Issue
Block a user