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 ip match feature for dns #2037

Merged
merged 4 commits into from
Nov 21, 2019
Merged

add ip match feature for dns #2037

merged 4 commits into from
Nov 21, 2019

Conversation

weaving118
Copy link
Contributor

背景

  • 在透明代理的模式下,会有一个独立DNS服务提供服务,按现在geosite:cn匹配模式,因为不能完全枚举国内的域名,不能完全解决DNS就近访问的问题
  • 对不想再搭建chinadns或者overture的,可以直接使用v2ray dns的方案解决上面的场景

添加的功能逻辑

  • 在DNS配置中,新增了ip配置项,配置格式与路由中的IP配置方式一致
  • 如果有IP配置项,当解析到域名返回的IP都不在当前IP段时,继续下一个DNS解析
  • 如果没有IP配置项,和原来逻辑一致

添加配置示例:

{
  "dns": {
    "servers": [
      {
        "address": "119.29.29.29",
        "port": 53,
        "domains": [
          "geosite:cn"
        ],
        "ip": [   新增配置项
          "geoip:cn"
        ]
      },
      {
        "address": "8.8.8.8",
        "port": 53,
        "domains": [
          "ext:h2y.dat:gfw"
        ]
      }
    ]
  }
}

逻辑参考

相关issue

PS

  • 第一次提交,有配置命名需要优化等,可以一起讨论下:)
  • dns查询没有做并发,后面感觉可以优化下.

@kslr
Copy link
Contributor

kslr commented Nov 18, 2019

Reviewers @vcptr

@vcptr
Copy link
Contributor

vcptr commented Nov 19, 2019

按照工作逻辑,这项应该叫做skipIP,或者ignoreIP好一点,不然容易产生歧义误解

又重新理解了一下,应该叫 exptectIP

@weaving118
Copy link
Contributor Author

weaving118 commented Nov 19, 2019

是的,找了几个相关的

  • reliableIP 可信IP
  • devotedIP忠诚的IP
  • predictIP 预期内的ip
  • intendedIP 预期的ip
  • expectIP 预期IP

expectIP感觉可以:)

@weaving118
Copy link
Contributor Author

@vcptr 配置已经修改为expectIPs,帮忙review下是否OK

@vcptr
Copy link
Contributor

vcptr commented Nov 20, 2019

@weaving118
app/dns/a.json 这个示例文件要去掉,如果测试用例里面用到的文件应放到一个data子目录,go会自动忽略;
另外还烦请撰写手册 https://github.com/v2ray/manual 里面/zh_cn/chapter_02/04_dns.md 相应文本和实例,另发PR。

实现的代码比较清晰,基本OK的~

@weaving118
Copy link
Contributor Author

weaving118 commented Nov 20, 2019

@vcptr

app/dns/server.go Outdated Show resolved Hide resolved
app/dns/server.go Outdated Show resolved Hide resolved
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
if len(s.tag) > 0 {
ctx = session.ContextWithInbound(ctx, &session.Inbound{
Tag: s.tag,
})
}
ips, err := client.QueryIP(ctx, domain, option)
ips, err = s.Match(idx, client, domain, ips)
cancel()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里判断应该在cancle()后调用,Match返回后可直接返回;
然后在调用queryIPTimeout的循环内,判断err == ErrExpectedIPNonMatch可马上执行continue,或者进一步判断虑如果用户没有配置下一个服务器情况下,强行使用本次结果。

Copy link
Contributor Author

@weaving118 weaving118 Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 这里在queryIPTimeout应该是单个DNS查询处理结果,对是否继续下一个,应该还是需要放在lookupIPInternal中处理了。
  • 现在DNS策略是如果都配置了expectIPs,且都没有不匹配,返回nil,这个也比较符合预期,强行返回可能返回被墙的IP或者非当前就近IP(一般推荐是最后一个不填写expectIPs,这样就不会匹配了)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

再提交了commit,这里也再优化了下,按这几天事件跑的情况,如果第一个DNS返回是empty response,基本第二个也是nil,所有如果是empty response等错误,直接返回了

@weaving118
Copy link
Contributor Author

weaving118 commented Nov 20, 2019

  • 又测试下代码逻辑,新增了一个判断,如果域名直接解析的DNS失败,使用其他DNS解析时,默认跳过失败的DNS。减少重试时间。

@weaving118
Copy link
Contributor Author

@vcptr 有空帮忙再看看,多谢。

@kslr kslr merged commit e52d3fa into v2ray:master Nov 21, 2019
vcptr added a commit to vcptr/v2ray-core-raymaster that referenced this pull request Nov 22, 2019
vcptr added a commit to vcptr/v2ray-core-raymaster that referenced this pull request Nov 22, 2019
@wysnxzm
Copy link

wysnxzm commented Jan 2, 2020

有一个类似功能的软件 DnsChooser,可以参考下
https://www.jianshu.com/p/ad63fe00858c

@qin-nz
Copy link

qin-nz commented Jan 13, 2020

@vcptr

这个流程图里面的 expectIPs 没有“否”的情况

https://github.com/weaving118/manual/raw/master/resources/dns_flowchart.svg?sanitize=true

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

5 participants