add hosts
This commit is contained in:
@ -12,16 +12,16 @@ func (c *Chain) AddNodeGroup(group *NodeGroup) {
|
||||
c.groups = append(c.groups, group)
|
||||
}
|
||||
|
||||
func (c *Chain) GetRoute() (r *Route) {
|
||||
func (c *Chain) GetRoute() (r *route) {
|
||||
return c.GetRouteFor("tcp", "")
|
||||
}
|
||||
|
||||
func (c *Chain) GetRouteFor(network, address string) (r *Route) {
|
||||
func (c *Chain) GetRouteFor(network, address string) (r *route) {
|
||||
if c == nil || len(c.groups) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
r = &Route{}
|
||||
r = &route{}
|
||||
for _, group := range c.groups {
|
||||
node := group.Next()
|
||||
if node == nil {
|
||||
@ -36,7 +36,7 @@ func (c *Chain) GetRouteFor(network, address string) (r *Route) {
|
||||
WithRoute(r)
|
||||
node = node.Copy().
|
||||
WithTransport(tr)
|
||||
r = &Route{}
|
||||
r = &route{}
|
||||
}
|
||||
|
||||
r.AddNode(node)
|
||||
|
@ -15,15 +15,15 @@ var (
|
||||
ErrEmptyRoute = errors.New("empty route")
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
type route struct {
|
||||
nodes []*Node
|
||||
}
|
||||
|
||||
func (r *Route) AddNode(node *Node) {
|
||||
func (r *route) AddNode(node *Node) {
|
||||
r.nodes = append(r.nodes, node)
|
||||
}
|
||||
|
||||
func (r *Route) connect(ctx context.Context) (conn net.Conn, err error) {
|
||||
func (r *route) connect(ctx context.Context) (conn net.Conn, err error) {
|
||||
if r.IsEmpty() {
|
||||
return nil, ErrEmptyRoute
|
||||
}
|
||||
@ -67,7 +67,7 @@ func (r *Route) connect(ctx context.Context) (conn net.Conn, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Route) Dial(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
func (r *route) Dial(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
if r.IsEmpty() {
|
||||
return r.dialDirect(ctx, network, address)
|
||||
}
|
||||
@ -85,7 +85,7 @@ func (r *Route) Dial(ctx context.Context, network, address string) (net.Conn, er
|
||||
return cc, nil
|
||||
}
|
||||
|
||||
func (r *Route) dialDirect(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
func (r *route) dialDirect(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
switch network {
|
||||
case "udp", "udp4", "udp6":
|
||||
if address == "" {
|
||||
@ -98,7 +98,7 @@ func (r *Route) dialDirect(ctx context.Context, network, address string) (net.Co
|
||||
return d.DialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
func (r *Route) Bind(ctx context.Context, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||
func (r *route) Bind(ctx context.Context, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||
if r.IsEmpty() {
|
||||
return r.bindLocal(ctx, network, address, opts...)
|
||||
}
|
||||
@ -117,18 +117,18 @@ func (r *Route) Bind(ctx context.Context, network, address string, opts ...conne
|
||||
return ln, nil
|
||||
}
|
||||
|
||||
func (r *Route) IsEmpty() bool {
|
||||
func (r *route) IsEmpty() bool {
|
||||
return r == nil || len(r.nodes) == 0
|
||||
}
|
||||
|
||||
func (r *Route) Last() *Node {
|
||||
func (r *route) Last() *Node {
|
||||
if r.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
return r.nodes[len(r.nodes)-1]
|
||||
}
|
||||
|
||||
func (r *Route) Path() (path []*Node) {
|
||||
func (r *route) Path() (path []*Node) {
|
||||
if r == nil || len(r.nodes) == 0 {
|
||||
return nil
|
||||
}
|
||||
@ -142,7 +142,7 @@ func (r *Route) Path() (path []*Node) {
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Route) bindLocal(ctx context.Context, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||
func (r *route) bindLocal(ctx context.Context, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||
options := connector.BindOptions{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
|
@ -3,11 +3,11 @@ package chain
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/hosts"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
"github.com/go-gost/gost/pkg/resolver"
|
||||
)
|
||||
@ -15,6 +15,7 @@ import (
|
||||
type Router struct {
|
||||
Retries int
|
||||
Chain *Chain
|
||||
Hosts *hosts.Hosts
|
||||
Resolver resolver.Resolver
|
||||
Logger logger.Logger
|
||||
}
|
||||
@ -77,11 +78,10 @@ func (r *Router) resolve(ctx context.Context, addr string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
/*
|
||||
if ip := hosts.Lookup(host); ip != nil {
|
||||
return net.JoinHostPort(ip.String(), port)
|
||||
}
|
||||
*/
|
||||
if ip := r.Hosts.Lookup(host); ip != nil {
|
||||
r.Logger.Debugf("hit hosts: %s -> %s", host, ip)
|
||||
return net.JoinHostPort(ip.String(), port), nil
|
||||
}
|
||||
|
||||
if r.Resolver != nil {
|
||||
ips, err := r.Resolver.Resolve(ctx, host)
|
||||
@ -89,7 +89,7 @@ func (r *Router) resolve(ctx context.Context, addr string) (string, error) {
|
||||
r.Logger.Error(err)
|
||||
}
|
||||
if len(ips) == 0 {
|
||||
return "", errors.New("domain not exists")
|
||||
return "", fmt.Errorf("resolver: domain %s does not exists", host)
|
||||
}
|
||||
return net.JoinHostPort(ips[0].String(), port), nil
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type Transport struct {
|
||||
addr string
|
||||
route *Route
|
||||
route *route
|
||||
dialer dialer.Dialer
|
||||
connector connector.Connector
|
||||
}
|
||||
@ -82,7 +82,7 @@ func (tr *Transport) Multiplex() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (tr *Transport) WithRoute(r *Route) *Transport {
|
||||
func (tr *Transport) WithRoute(r *route) *Transport {
|
||||
tr.route = r
|
||||
return tr
|
||||
}
|
||||
|
Reference in New Issue
Block a user