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
import (
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type Config struct {
Hosts map[string]net.IP
NameServers []v2net.Destination
}

View File

@ -4,6 +4,8 @@ package dns
import (
"encoding/json"
"errors"
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
@ -11,6 +13,7 @@ import (
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Servers []v2net.AddressJson `json:"servers"`
Hosts map[string]v2net.AddressJson `json:"hosts"`
}
jsonConfig := new(JsonConfig)
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))
}
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
}

View File

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

View File

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