Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fake dns #395

Closed
wants to merge 3 commits into from
Closed

Add fake dns #395

wants to merge 3 commits into from

Conversation

yuhan6665
Copy link
Contributor

基于v2ray/v2ray-core#2237 并大幅精简的Fake DNS/IP

  "dns": {
    "fake": {}
  }

参考了一些v2ray/v2ray-core#2233 里面的讨论,与原pr区别如下:

  • v2ray 路由所发出的 dns 不再受 fake DNS 影响。因为内部路由如果用户不需要可以通过 "domainStrategy": "AsIs" 关掉,既然想用 ip 分流,得到 fake IP 没有意义而且用户会报 bug
  • 基于隐私和安全考量,暂未加入持久化存储。一些更复杂的配置后面会根据用户意见增加
  • 发现在安卓上返回 224.x.x.x 有一些问题,改用240
  • 使用sync.map

提交原文:

A new config object "fake" in DnsObject for toggling fake dns function

Compare with sniffing, fake dns is not limited to http and tls traffic.
It works across all inbounds. For example, when dns request come
from one inbound, the local DNS server of v2ray will response with a
unique fake IP for every unique domain name. Then later on v2ray
received a request to one of the fake IP from any inbounds, it will
override the request destination with the previously saved domain.

By default, v2ray cache up to 65535 addresses. The old records will
be discarded bases on LRU. The fake IP will be 240.x.x.x

@kslr
Copy link
Contributor

kslr commented Nov 8, 2020

酷 不过你得先处理下 lint 的问题,更多稍后讨论

@yuhan6665
Copy link
Contributor Author

Fixed. Looks like the other failed test is not related.

A new config object "fake" in DnsObject for toggling fake dns function

Compare with sniffing, fake dns is not limited to http and tls traffic.
It works across all inbounds. For example, when dns request come
from one inbound, the local DNS server of v2ray will response with a
unique fake IP for every unique domain name. Then later on v2ray
received a request to one of the fake IP from any inbounds, it will
override the request destination with the previously saved domain.

By default, v2ray cache up to 65535 addresses. The old records will
be discarded bases on LRU. The fake IP will be 240.x.x.x
@xiaokangwang
Copy link
Contributor

感谢来自 @yuhan6665 的贡献!

经过V2Fly团队的内部讨论,这些代码可以合并入V2成为V2的新功能,但是在合并入V2之前需要完成以下修改。因为你所实现的功能是一个目前需求很高的功能,如果你不希望进行以下修改,这些修改可以由我完成后合并入V2。

  • 匹配并替换为域名部分需要在配置文件中启用后才能激活。这个是保证在极端情况下如果代码出现问题可以通过配置文件避开有问题的代码。即使现在的代码没有问题未来在进步修改和增强中也可能引入。

  • 应该允许用户自行设置IP段,并且使用更加符合代码规范和通用的方式分配Fake IP。现在这个写死地址加字符串操作的方法当然在没有错误,但是有些时候可能和用户的环境有冲突。

  • LRU 的代码应该复用其他人的代码或者保证本身可复用。可以将其移动移动到common包来允许其他代码复用,或者使用第三方的库。

请回复是否想自己完成上述修改。如果由我来实现的话,会把IP匹配这里做成一个sniffer,然后读取CIDR表示的FakeIP的IP段并通过BigInt和net.IP之间转换完成IP分配,之后将LRU的代码移动入common来保证其可复用。

@yuhan6665
Copy link
Contributor Author

@xiaokangwang 感谢大佬 review!

  • 写 LRU 的时候我已经注意了将来的复用,可以移到 common。没有引入外来库的原因一是实现起来并不复杂,二是我们这里有一个特殊需求,就是支持以 value 反查 key
  • 增加一个 Fake IP CIDR 的配置项也没有问题
  • 关于匹配替换这部分改成 sniffer 我有问题。目前我们有三个 sniffer,你的意思是说加一个 fakeDNS sniffer,然后只在 inbound 开 sniffing 的时候起作用?但是我觉得 fake IP 要特殊看待,等到 v2 收到一个 fake IP request 的时候,如果 inbound 没有开选项会失败(其实我再考虑一个更进一步的,不管有没有开 fake 和 sniffing,当收到 fake IP request,我们都应该尝试 sniffing,这样可以在不增加文件存储的情况下解决一些重启的问题)

我当然愿意改不过我周一到周五可能比较忙,我争取抽时间改 :)

@xiaokangwang
Copy link
Contributor

xiaokangwang commented Nov 9, 2020

我同意如果配置文件前后不匹配会出现无法将原请求映射回域名的情况,但是吧,这么做是为了保证没有启用Fake DNS的用户的代码不会因为Fake DNS部分的代码的错误而导致请求失败(即使现在没有错误未来也可能会有),而且并不是每个用户都会使用Fake DNS,要保证没有启用Fake DNS的用户完全不受到这些修改的影响(包括性能影响,未来可能会考虑增加更加更多的匹配机制)。
我想提出的解决方法是Fake DNS作为一个sniffer实现需要主动启用,但是如果在没有启用Fake DNS sniffer的情况下启用了Fake DNS的解析的话,在分析配置文件的时候就会报错。你觉得这个方法怎么样呢?

如果你同意的话这个修改就由我来完成,因为目前sniffer并不包含相应的接口来获取请求的IP,而配置文件解析的代码也没有检查配置的功能。改变接口的代码需要考虑比较多的事情,要和团队中其他的人沟通,并达成共识,而且这里的修改也基本都是维护者提出的要求,这几天我也稍微有点时间^_^。

@yuhan6665
Copy link
Contributor Author

@xiaokangwang 我改了两个小问题。
可以,既然大佬要亲自改,我就权当抛砖引玉了吧 :)

@xiaokangwang
Copy link
Contributor

好的,感谢你的贡献。我这就开始整合和改进。

@xiaokangwang
Copy link
Contributor

正在进行相应的整合,相关的代码正在 https://github.com/v2fly/v2ray-core/commits/dev-fakedns 分支被开发。

@yuhan6665
Copy link
Contributor Author

Close as work is moved to https://github.com/v2fly/v2ray-core/commits/dev-fakedns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants