|  | @@ -1,4 +1,4 @@
 | 
											
												
													
														|  | -package server
 |  | 
 | 
											
												
													
														|  | 
 |  | +package dns
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg server -path App,DNS,Server
 |  |  //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg server -path App,DNS,Server
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -10,7 +10,6 @@ import (
 | 
											
												
													
														|  |  	dnsmsg "github.com/miekg/dns"
 |  |  	dnsmsg "github.com/miekg/dns"
 | 
											
												
													
														|  |  	"v2ray.com/core/app"
 |  |  	"v2ray.com/core/app"
 | 
											
												
													
														|  |  	"v2ray.com/core/app/dispatcher"
 |  |  	"v2ray.com/core/app/dispatcher"
 | 
											
												
													
														|  | -	"v2ray.com/core/app/dns"
 |  | 
 | 
											
												
													
														|  |  	"v2ray.com/core/common"
 |  |  	"v2ray.com/core/common"
 | 
											
												
													
														|  |  	"v2ray.com/core/common/net"
 |  |  	"v2ray.com/core/common/net"
 | 
											
												
													
														|  |  )
 |  |  )
 | 
											
										
											
												
													
														|  | @@ -41,7 +40,7 @@ type CacheServer struct {
 | 
											
												
													
														|  |  	servers []NameServer
 |  |  	servers []NameServer
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -func NewCacheServer(ctx context.Context, config *dns.Config) (*CacheServer, error) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +func NewCacheServer(ctx context.Context, config *Config) (*CacheServer, error) {
 | 
											
												
													
														|  |  	space := app.SpaceFromContext(ctx)
 |  |  	space := app.SpaceFromContext(ctx)
 | 
											
												
													
														|  |  	if space == nil {
 |  |  	if space == nil {
 | 
											
												
													
														|  |  		return nil, newError("no space in context")
 |  |  		return nil, newError("no space in context")
 | 
											
										
											
												
													
														|  | @@ -79,10 +78,11 @@ func NewCacheServer(ctx context.Context, config *dns.Config) (*CacheServer, erro
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func (*CacheServer) Interface() interface{} {
 |  |  func (*CacheServer) Interface() interface{} {
 | 
											
												
													
														|  | -	return (*dns.Server)(nil)
 |  | 
 | 
											
												
													
														|  | 
 |  | +	return (*CacheServer)(nil)
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -func (*CacheServer) Start() error {
 |  | 
 | 
											
												
													
														|  | 
 |  | +func (s *CacheServer) Start() error {
 | 
											
												
													
														|  | 
 |  | +	net.RegisterIPResolver(s)
 | 
											
												
													
														|  |  	return nil
 |  |  	return nil
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -116,15 +116,15 @@ func (s *CacheServer) tryCleanup() {
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -func (s *CacheServer) Get(domain string) []net.IP {
 |  | 
 | 
											
												
													
														|  | 
 |  | +func (s *CacheServer) LookupIP(domain string) ([]net.IP, error) {
 | 
											
												
													
														|  |  	if ip, found := s.hosts[domain]; found {
 |  |  	if ip, found := s.hosts[domain]; found {
 | 
											
												
													
														|  | -		return []net.IP{ip}
 |  | 
 | 
											
												
													
														|  | 
 |  | +		return []net.IP{ip}, nil
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	domain = dnsmsg.Fqdn(domain)
 |  |  	domain = dnsmsg.Fqdn(domain)
 | 
											
												
													
														|  |  	ips := s.GetCached(domain)
 |  |  	ips := s.GetCached(domain)
 | 
											
												
													
														|  |  	if ips != nil {
 |  |  	if ips != nil {
 | 
											
												
													
														|  | -		return ips
 |  | 
 | 
											
												
													
														|  | 
 |  | +		return ips, nil
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	s.tryCleanup()
 |  |  	s.tryCleanup()
 | 
											
										
											
												
													
														|  | @@ -144,17 +144,16 @@ func (s *CacheServer) Get(domain string) []net.IP {
 | 
											
												
													
														|  |  			}
 |  |  			}
 | 
											
												
													
														|  |  			s.Unlock()
 |  |  			s.Unlock()
 | 
											
												
													
														|  |  			newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug().WriteToLog()
 |  |  			newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug().WriteToLog()
 | 
											
												
													
														|  | -			return a.IPs
 |  | 
 | 
											
												
													
														|  | 
 |  | +			return a.IPs, nil
 | 
											
												
													
														|  |  		case <-time.After(QueryTimeout):
 |  |  		case <-time.After(QueryTimeout):
 | 
											
												
													
														|  |  		}
 |  |  		}
 | 
											
												
													
														|  |  	}
 |  |  	}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	newError("returning nil for domain ", domain).AtDebug().WriteToLog()
 |  | 
 | 
											
												
													
														|  | -	return nil
 |  | 
 | 
											
												
													
														|  | 
 |  | +	return nil, newError("returning nil for domain ", domain)
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func init() {
 |  |  func init() {
 | 
											
												
													
														|  | -	common.Must(common.RegisterConfig((*dns.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
 |  | 
 | 
											
												
													
														|  | -		return NewCacheServer(ctx, config.(*dns.Config))
 |  | 
 | 
											
												
													
														|  | 
 |  | +	common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
 | 
											
												
													
														|  | 
 |  | +		return NewCacheServer(ctx, config.(*Config))
 | 
											
												
													
														|  |  	}))
 |  |  	}))
 | 
											
												
													
														|  |  }
 |  |  }
 |