1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

Static hosts in DNS server

This commit is contained in:
v2ray 2016-05-22 22:30:08 +02:00
parent 3d6200dc64
commit bb503c6954
4 changed files with 27 additions and 2 deletions

View File

@ -1,9 +1,12 @@
package dns package dns
import ( import (
"net"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
) )
type Config struct { type Config struct {
Hosts map[string]net.IP
NameServers []v2net.Destination NameServers []v2net.Destination
} }

View File

@ -4,6 +4,8 @@ package dns
import ( import (
"encoding/json" "encoding/json"
"errors"
"net"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
) )
@ -11,6 +13,7 @@ import (
func (this *Config) UnmarshalJSON(data []byte) error { func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct { type JsonConfig struct {
Servers []v2net.AddressJson `json:"servers"` Servers []v2net.AddressJson `json:"servers"`
Hosts map[string]v2net.AddressJson `json:"hosts"`
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
@ -21,5 +24,15 @@ func (this *Config) UnmarshalJSON(data []byte) error {
this.NameServers[idx] = v2net.UDPDestination(server.Address, v2net.Port(53)) this.NameServers[idx] = v2net.UDPDestination(server.Address, v2net.Port(53))
} }
if jsonConfig.Hosts != nil {
this.Hosts = make(map[string]net.IP)
for domain, ip := range jsonConfig.Hosts {
if ip.Address.IsDomain() {
return errors.New(ip.Address.String() + " is not an IP.")
}
this.Hosts[domain] = ip.Address.IP()
}
}
return nil return nil
} }

View File

@ -23,6 +23,7 @@ type DomainRecord struct {
type CacheServer struct { type CacheServer struct {
sync.RWMutex sync.RWMutex
space app.Space space app.Space
hosts map[string]net.IP
records map[string]*DomainRecord records map[string]*DomainRecord
servers []NameServer servers []NameServer
} }
@ -31,6 +32,7 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer {
server := &CacheServer{ server := &CacheServer{
records: make(map[string]*DomainRecord), records: make(map[string]*DomainRecord),
servers: make([]NameServer, len(config.NameServers)), servers: make([]NameServer, len(config.NameServers)),
hosts: config.Hosts,
} }
space.InitializeApplication(func() error { space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) { if !space.HasApp(dispatcher.APP_ID) {
@ -46,6 +48,9 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer {
server.servers[idx] = NewUDPNameServer(ns, dispatcher) server.servers[idx] = NewUDPNameServer(ns, dispatcher)
} }
} }
if len(config.NameServers) == 0 {
server.servers = append(server.servers, &LocalNameServer{})
}
return nil return nil
}) })
return server return server
@ -67,6 +72,10 @@ func (this *CacheServer) GetCached(domain string) []net.IP {
} }
func (this *CacheServer) Get(domain string) []net.IP { func (this *CacheServer) Get(domain string) []net.IP {
if ip, found := this.hosts[domain]; found {
return []net.IP{ip}
}
domain = dns.Fqdn(domain) domain = dns.Fqdn(domain)
ips := this.GetCached(domain) ips := this.GetCached(domain)
if ips != nil { if ips != nil {

View File

@ -22,7 +22,7 @@ func TestDnsAdd(t *testing.T) {
space := app.NewSpace() space := app.NewSpace()
outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager() outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
outboundHandlerManager.SetDefaultHandler(&freedom.FreedomConnection{}) outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))