update hosts

This commit is contained in:
ginuerzh 2022-01-01 22:49:38 +08:00
parent 4bf754b83b
commit 14537d16ea
6 changed files with 62 additions and 63 deletions

View File

@ -26,7 +26,7 @@ var (
chains = make(map[string]*chain.Chain) chains = make(map[string]*chain.Chain)
bypasses = make(map[string]bypass.Bypass) bypasses = make(map[string]bypass.Bypass)
resolvers = make(map[string]resolver.Resolver) resolvers = make(map[string]resolver.Resolver)
hosts = make(map[string]*hostspkg.Hosts) hosts = make(map[string]hostspkg.HostMapper)
) )
func buildService(cfg *config.Config) (services []*service.Service) { func buildService(cfg *config.Config) (services []*service.Service) {
@ -286,11 +286,11 @@ func resolverFromConfig(cfg *config.ResolverConfig) (resolver.Resolver, error) {
return resolver_impl.NewResolver(nameservers) return resolver_impl.NewResolver(nameservers)
} }
func hostsFromConfig(cfg *config.HostsConfig) *hostspkg.Hosts { func hostsFromConfig(cfg *config.HostsConfig) hostspkg.HostMapper {
if cfg == nil { if cfg == nil || len(cfg.Entries) == 0 {
return nil return nil
} }
hosts := &hostspkg.Hosts{} hosts := hostspkg.NewHosts()
for _, host := range cfg.Entries { for _, host := range cfg.Entries {
if host.IP == "" || host.Hostname == "" { if host.IP == "" || host.Hostname == "" {
@ -301,7 +301,7 @@ func hostsFromConfig(cfg *config.HostsConfig) *hostspkg.Hosts {
if ip == nil { if ip == nil {
continue continue
} }
hosts.AddHost(hostspkg.NewHost(ip, host.Hostname, host.Aliases...)) hosts.Map(ip, host.Hostname, host.Aliases...)
} }
return hosts return hosts
} }

View File

@ -34,7 +34,6 @@ services:
method: chacha20-ietf method: chacha20-ietf
password: gost password: gost
readTimeout: 5s readTimeout: 5s
retry: 3
udp: true udp: true
bufferSize: 4096 bufferSize: 4096
listener: listener:
@ -52,7 +51,6 @@ services:
auths: auths:
- gost:gost - gost:gost
readTimeout: 5s readTimeout: 5s
retry: 3
notls: true notls: true
bind: true bind: true
udp: true udp: true
@ -70,7 +68,6 @@ services:
auths: auths:
- gost:gost - gost:gost
readTimeout: 5s readTimeout: 5s
retry: 3
notls: true notls: true
# udpBufferSize: 1024 # udpBufferSize: 1024
listener: listener:
@ -94,7 +91,6 @@ services:
chain: chain-ss chain: chain-ss
metadata: metadata:
readTimeout: 5s readTimeout: 5s
retry: 3
listener: listener:
type: udp type: udp
metadata: metadata:
@ -109,7 +105,6 @@ services:
type: forward type: forward
metadata: metadata:
readTimeout: 5s readTimeout: 5s
retry: 3
listener: listener:
type: kcp type: kcp
metadata: metadata:
@ -125,7 +120,6 @@ services:
type: forward type: forward
metadata: metadata:
readTimeout: 5s readTimeout: 5s
retry: 3
listener: listener:
type: rtcp type: rtcp
metadata: metadata:
@ -145,7 +139,6 @@ services:
type: forward type: forward
metadata: metadata:
readTimeout: 5s readTimeout: 5s
retry: 3
listener: listener:
type: rudp type: rudp
chain: chain-socks5 chain: chain-socks5
@ -325,6 +318,21 @@ hosts:
- bar - bar
- baz - baz
probeResistance:
- name: pr-code404
type: code
value: 404
knock: www.example.com
- name: pr-web
type: web
value: http://example.com/page.html
- name: pr-host
type: host
value: example.com:80
- name: pr-file
type: file
value: /path/to/file
profiling: profiling:
addr: ":6060" addr: ":6060"
enabled: true enabled: true

View File

@ -15,7 +15,7 @@ import (
type Router struct { type Router struct {
Retries int Retries int
Chain *Chain Chain *Chain
Hosts *hosts.Hosts Hosts hosts.HostMapper
Resolver resolver.Resolver Resolver resolver.Resolver
Logger logger.Logger Logger logger.Logger
} }
@ -78,10 +78,12 @@ func (r *Router) resolve(ctx context.Context, addr string) (string, error) {
return "", err return "", err
} }
if r.Hosts != nil {
if ip := r.Hosts.Lookup(host); ip != nil { if ip := r.Hosts.Lookup(host); ip != nil {
r.Logger.Debugf("hit hosts: %s -> %s", host, ip) r.Logger.Debugf("hit hosts: %s -> %s", host, ip)
return net.JoinHostPort(ip.String(), port), nil return net.JoinHostPort(ip.String(), port), nil
} }
}
if r.Resolver != nil { if r.Resolver != nil {
ips, err := r.Resolver.Resolve(ctx, host) ips, err := r.Resolver.Resolve(ctx, host)

View File

@ -139,22 +139,6 @@ func (h *httpHandler) handleRequest(ctx context.Context, conn net.Conn, req *htt
resp.Header = http.Header{} resp.Header = http.Header{}
} }
/*
if !Can("tcp", host, h.options.Whitelist, h.options.Blacklist) {
log.Logf("[http] %s - %s : Unauthorized to tcp connect to %s",
conn.RemoteAddr(), conn.LocalAddr(), host)
resp.StatusCode = http.StatusForbidden
if Debug {
dump, _ := httputil.DumpResponse(resp, false)
log.Logf("[http] %s <- %s\n%s", conn.RemoteAddr(), conn.LocalAddr(), string(dump))
}
resp.Write(conn)
return
}
*/
if h.bypass != nil && h.bypass.Contains(addr) { if h.bypass != nil && h.bypass.Contains(addr) {
resp.StatusCode = http.StatusForbidden resp.StatusCode = http.StatusForbidden

View File

@ -4,34 +4,37 @@ import (
"net" "net"
) )
// Host is a static mapping from hostname to IP. // HostMapper is a mapping from hostname to IP.
type Host struct { type HostMapper interface {
Lookup(host string) net.IP
}
type host struct {
IP net.IP IP net.IP
Hostname string Hostname string
Aliases []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. // Hosts is a static table lookup for hostnames.
// For each host a single line should be present with the following information: // For each host a single line should be present with the following information:
// IP_address canonical_hostname [aliases...] // IP_address canonical_hostname [aliases...]
// Fields of the entry are separated by any number of blanks and/or tab characters. // 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. // Text from a "#" character until the end of the line is a comment, and is ignored.
type Hosts struct { type Hosts struct {
hosts []Host mappings []host
} }
// AddHost adds host(s) to the host table. func NewHosts() *Hosts {
func (h *Hosts) AddHost(host ...Host) { return &Hosts{}
h.hosts = append(h.hosts, host...) }
// Map maps ip to hostname or aliases.
func (h *Hosts) Map(ip net.IP, hostname string, aliases ...string) {
h.mappings = append(h.mappings, host{
IP: ip,
Hostname: hostname,
Aliases: aliases,
})
} }
// Lookup searches the IP address corresponds to the given host from the host table. // Lookup searches the IP address corresponds to the given host from the host table.
@ -40,7 +43,7 @@ func (h *Hosts) Lookup(host string) (ip net.IP) {
return return
} }
for _, h := range h.hosts { for _, h := range h.mappings {
if h.Hostname == host { if h.Hostname == host {
ip = h.IP ip = h.IP
break break

View File

@ -51,22 +51,12 @@ func (c *Cache) Load(key CacheKey) *dns.Msg {
return nil return nil
} }
elapsed := time.Since(item.ts) if time.Since(item.ts) > item.ttl {
if item.ttl > 0 {
if elapsed > item.ttl {
c.m.Delete(key) c.m.Delete(key)
return nil return nil
} }
} else {
for _, rr := range item.msg.Answer {
if elapsed > time.Duration(rr.Header().Ttl)*time.Second {
c.m.Delete(key)
return nil
}
}
}
c.logger.Debugf("resolver cache hit %s", key) c.logger.Debugf("resolver cache hit: %s", key)
return item.msg.Copy() return item.msg.Copy()
} }
@ -76,11 +66,23 @@ func (c *Cache) Store(key CacheKey, mr *dns.Msg, ttl time.Duration) {
return return
} }
if ttl == 0 {
for _, answer := range mr.Answer {
v := time.Duration(answer.Header().Ttl) * time.Second
if ttl == 0 || ttl > v {
ttl = v
}
}
}
if ttl == 0 {
ttl = 30 * time.Second
}
c.m.Store(key, &cacheItem{ c.m.Store(key, &cacheItem{
msg: mr.Copy(), msg: mr.Copy(),
ts: time.Now(), ts: time.Now(),
ttl: ttl, ttl: ttl,
}) })
c.logger.Debugf("resolver cache store %s", key) c.logger.Debugf("resolver cache store: %s, ttl: %v", key, ttl)
} }