add hosts

This commit is contained in:
ginuerzh
2021-12-31 11:32:06 +08:00
parent 9769efe33c
commit 4bf754b83b
9 changed files with 229 additions and 66 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -47,6 +47,33 @@ type BypassConfig struct {
Reverse bool `yaml:",omitempty"`
Matchers []string
}
type NameserverConfig struct {
Addr string
Chain string
Prefer string
ClientIP string
Hostname string
TTL time.Duration
Timeout time.Duration
}
type ResolverConfig struct {
Name string
Nameservers []NameserverConfig
}
type HostConfig struct {
IP string
Hostname string
Aliases []string
}
type HostsConfig struct {
Name string
Entries []HostConfig
}
type ListenerConfig struct {
Type string
Metadata map[string]interface{} `yaml:",omitempty"`
@ -78,6 +105,8 @@ type ServiceConfig struct {
Addr string `yaml:",omitempty"`
Chain string `yaml:",omitempty"`
Bypass string `yaml:",omitempty"`
Resolver string `yaml:",omitempty"`
Hosts string `yaml:",omitempty"`
Listener *ListenerConfig `yaml:",omitempty"`
Handler *HandlerConfig `yaml:",omitempty"`
Forwarder *ForwarderConfig `yaml:",omitempty"`
@ -105,12 +134,14 @@ type NodeConfig struct {
}
type Config struct {
Log *LogConfig `yaml:",omitempty"`
Profiling *ProfilingConfig `yaml:",omitempty"`
TLS *TLSConfig `yaml:",omitempty"`
Log *LogConfig `yaml:",omitempty"`
Profiling *ProfilingConfig `yaml:",omitempty"`
TLS *TLSConfig `yaml:",omitempty"`
Bypasses []*BypassConfig `yaml:",omitempty"`
Resolvers []*ResolverConfig `yaml:",omitempty"`
Hosts []*HostsConfig `yaml:",omitempty"`
Chains []*ChainConfig `yaml:",omitempty"`
Services []*ServiceConfig
Chains []*ChainConfig `yaml:",omitempty"`
Bypasses []*BypassConfig `yaml:",omitempty"`
}
func (c *Config) Load() error {

56
pkg/hosts/hosts.go Normal file
View File

@ -0,0 +1,56 @@
package hosts
import (
"net"
)
// Host is a static mapping from hostname to IP.
type Host struct {
IP net.IP
Hostname string
Aliases []string
}
// NewHost creates a Host.
func NewHost(ip net.IP, hostname string, aliases ...string) Host {
return Host{
IP: ip,
Hostname: hostname,
Aliases: aliases,
}
}
// Hosts is a static table lookup for hostnames.
// For each host a single line should be present with the following information:
// IP_address canonical_hostname [aliases...]
// Fields of the entry are separated by any number of blanks and/or tab characters.
// Text from a "#" character until the end of the line is a comment, and is ignored.
type Hosts struct {
hosts []Host
}
// AddHost adds host(s) to the host table.
func (h *Hosts) AddHost(host ...Host) {
h.hosts = append(h.hosts, host...)
}
// Lookup searches the IP address corresponds to the given host from the host table.
func (h *Hosts) Lookup(host string) (ip net.IP) {
if h == nil || host == "" {
return
}
for _, h := range h.hosts {
if h.Hostname == host {
ip = h.IP
break
}
for _, alias := range h.Aliases {
if alias == host {
ip = h.IP
break
}
}
}
return
}

View File

@ -65,7 +65,10 @@ func NewResolver(nameservers []NameServer, opts ...ResolverOption) (resolverpkg.
}
ex, err := exchanger.NewExchanger(
addr,
exchanger.ChainOption(server.Chain),
exchanger.RouterOption(&chain.Router{
Chain: server.Chain,
Logger: options.logger,
}),
exchanger.TimeoutOption(server.Timeout),
exchanger.LoggerOption(options.logger),
)