Add Fake DNS support (#309)

Co-authored-by: Xiaokang Wang <xiaokangwang@outlook.com>
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
Co-authored-by: kslr <kslrwang@gmail.com>
This commit is contained in:
yuhan6665 2021-03-06 23:39:50 -05:00 committed by GitHub
parent db32ce6fd9
commit f50eff5ebb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 1351 additions and 301 deletions

65
infra/conf/fakedns.go Normal file
View file

@ -0,0 +1,65 @@
package conf
import (
"github.com/golang/protobuf/proto"
"github.com/xtls/xray-core/app/dns/fakedns"
)
type FakeDNSConfig struct {
IPPool string `json:"ipPool"`
LruSize int64 `json:"poolSize"`
}
func (f FakeDNSConfig) Build() (proto.Message, error) {
return &fakedns.FakeDnsPool{
IpPool: f.IPPool,
LruSize: f.LruSize,
}, nil
}
type FakeDNSPostProcessingStage struct{}
func (FakeDNSPostProcessingStage) Process(conf *Config) error {
var fakeDNSInUse bool
if conf.DNSConfig != nil {
for _, v := range conf.DNSConfig.Servers {
if v.Address.Family().IsDomain() {
if v.Address.Domain() == "fakedns" {
fakeDNSInUse = true
}
}
}
}
if fakeDNSInUse {
if conf.FakeDNS == nil {
// Add a Fake DNS Config if there is none
conf.FakeDNS = &FakeDNSConfig{
IPPool: "240.0.0.0/8",
LruSize: 65535,
}
}
found := false
// Check if there is a Outbound with necessary sniffer on
var inbounds []InboundDetourConfig
if len(conf.InboundConfigs) > 0 {
inbounds = append(inbounds, conf.InboundConfigs...)
}
for _, v := range inbounds {
if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
for _, dov := range *v.SniffingConfig.DestOverride {
if dov == "fakedns" {
found = true
}
}
}
}
if !found {
newError("Defined Fake DNS but haven't enabled fake dns sniffing at any inbound.").AtWarning().WriteToLog()
}
}
return nil
}

5
infra/conf/init.go Normal file
View file

@ -0,0 +1,5 @@
package conf
func init() {
RegisterConfigureFilePostProcessingStage("FakeDNS", &FakeDNSPostProcessingStage{})
}

23
infra/conf/lint.go Normal file
View file

@ -0,0 +1,23 @@
package conf
type ConfigureFilePostProcessingStage interface {
Process(conf *Config) error
}
var configureFilePostProcessingStages map[string]ConfigureFilePostProcessingStage
func RegisterConfigureFilePostProcessingStage(name string, stage ConfigureFilePostProcessingStage) {
if configureFilePostProcessingStages == nil {
configureFilePostProcessingStages = make(map[string]ConfigureFilePostProcessingStage)
}
configureFilePostProcessingStages[name] = stage
}
func PostProcessConfigureFile(conf *Config) error {
for k, v := range configureFilePostProcessingStages {
if err := v.Process(conf); err != nil {
return newError("Rejected by Postprocessing Stage ", k).AtError().Base(err)
}
}
return nil
}

View file

@ -62,6 +62,7 @@ type SniffingConfig struct {
Enabled bool `json:"enabled"`
DestOverride *StringList `json:"destOverride"`
DomainsExcluded *StringList `json:"domainsExcluded"`
MetadataOnly bool `json:"metadataOnly"`
}
// Build implements Buildable.
@ -74,6 +75,8 @@ func (c *SniffingConfig) Build() (*proxyman.SniffingConfig, error) {
p = append(p, "http")
case "tls", "https", "ssl":
p = append(p, "tls")
case "fakedns":
p = append(p, "fakedns")
default:
return nil, newError("unknown protocol: ", protocol)
}
@ -91,6 +94,7 @@ func (c *SniffingConfig) Build() (*proxyman.SniffingConfig, error) {
Enabled: c.Enabled,
DestinationOverride: p,
DomainsExcluded: d,
MetadataOnly: c.MetadataOnly,
}, nil
}
@ -395,6 +399,7 @@ type Config struct {
API *APIConfig `json:"api"`
Stats *StatsConfig `json:"stats"`
Reverse *ReverseConfig `json:"reverse"`
FakeDNS *FakeDNSConfig `json:"fakeDns"`
}
func (c *Config) findInboundTag(tag string) int {
@ -448,6 +453,10 @@ func (c *Config) Override(o *Config, fn string) {
c.Reverse = o.Reverse
}
if o.FakeDNS != nil {
c.FakeDNS = o.FakeDNS
}
// deprecated attrs... keep them for now
if o.InboundConfig != nil {
c.InboundConfig = o.InboundConfig
@ -519,6 +528,10 @@ func applyTransportConfig(s *StreamConfig, t *TransportConfig) {
// Build implements Buildable.
func (c *Config) Build() (*core.Config, error) {
if err := PostProcessConfigureFile(c); err != nil {
return nil, err
}
config := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&dispatcher.Config{}),
@ -585,6 +598,14 @@ func (c *Config) Build() (*core.Config, error) {
config.App = append(config.App, serial.ToTypedMessage(r))
}
if c.FakeDNS != nil {
r, err := c.FakeDNS.Build()
if err != nil {
return nil, err
}
config.App = append(config.App, serial.ToTypedMessage(r))
}
var inbounds []InboundDetourConfig
if c.InboundConfig != nil {