Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

关于dns转发的疑问 #272

Closed
chenfengfeng opened this issue Sep 30, 2019 · 15 comments
Closed

关于dns转发的疑问 #272

chenfengfeng opened this issue Sep 30, 2019 · 15 comments
Labels

Comments

@chenfengfeng
Copy link

先说下我目前的配置情况:

proxySettings,没有配置,不走http代理
NEDNSSettings(servers: ["8.8.8.8"])设置了dns配置
TUNInterface只配置了udp和tcp,没有配置DNSServer
udp类是改的一个类,这个类可以发送udp数据到代理服务器,然后代理服务器返回数据写入在iOS

目前的情况:

打开网页的时候,需要解析域名,那么就需要通过dns,dns会发送udp包,这个时候,udp类会收到包并且做了代理服务器的握手协议,转发到代理服务器,代理服务器再发送udp给APP,写入到iOS中
按照情况来说dns解析了域名拿到IP,开始走tcp包来访问,但是目前的情况是没有走。

ps:在进行udp的普通转发,比如游戏使用了udp,是正常的,能够正常转发接收并且应用也能解析。
目前不清楚问题出在哪里,希望大佬能够给点思路或者探讨下,我不清楚是否我的这个流程有没有问题

@zhuhaow
Copy link
Owner

zhuhaow commented Sep 30, 2019 via email

@chenfengfeng
Copy link
Author

抱歉没看到,我目前的路由配置如下:

let ipv4Settings = NEIPv4Settings(addresses: ["192.169.89.1"], subnetMasks: ["255.255.255.0"])
ipv4Settings.includedRoutes = [NEIPv4Route.default()]
ipv4Settings.excludedRoutes = [
NEIPv4Route(destinationAddress: "10.0.0.0", subnetMask: "255.0.0.0"),
NEIPv4Route(destinationAddress: "100.64.0.0", subnetMask: "255.192.0.0"),
NEIPv4Route(destinationAddress: "127.0.0.0", subnetMask: "255.0.0.0"),
NEIPv4Route(destinationAddress: "169.254.0.0", subnetMask: "255.255.0.0"),
NEIPv4Route(destinationAddress: "172.16.0.0", subnetMask: "255.240.0.0"),
NEIPv4Route(destinationAddress: "192.168.0.0", subnetMask: "255.255.0.0"),
NEIPv4Route(destinationAddress: "17.0.0.0", subnetMask: "255.0.0.0"),
]
networkSettings.ipv4Settings = ipv4Settings

@zhuhaow
Copy link
Owner

zhuhaow commented Oct 1, 2019 via email

@chenfengfeng
Copy link
Author

这是我startTunnel方法的实现代码:

override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
        
        let s5AdapterFactory = SOCKS5AdapterFactory(serverHost: "192.168.1.111", serverPort: 9287)
        let allRule = AllRule(adapterFactory: s5AdapterFactory)
        RuleManager.currentManager = RuleManager(fromRules: [allRule], appendDirect: false)

        let networkSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "8.8.8.8")
        networkSettings.mtu = 1500

        let ipv4Settings = NEIPv4Settings(addresses: ["192.169.89.1"], subnetMasks: ["255.255.255.0"])
        ipv4Settings.includedRoutes = [NEIPv4Route.default()]
        ipv4Settings.excludedRoutes = [
            NEIPv4Route(destinationAddress: "10.0.0.0", subnetMask: "255.0.0.0"),
            NEIPv4Route(destinationAddress: "100.64.0.0", subnetMask: "255.192.0.0"),
            NEIPv4Route(destinationAddress: "127.0.0.0", subnetMask: "255.0.0.0"),
            NEIPv4Route(destinationAddress: "169.254.0.0", subnetMask: "255.255.0.0"),
            NEIPv4Route(destinationAddress: "172.16.0.0", subnetMask: "255.240.0.0"),
            NEIPv4Route(destinationAddress: "192.168.0.0", subnetMask: "255.255.0.0"),
            NEIPv4Route(destinationAddress: "17.0.0.0", subnetMask: "255.0.0.0"),
        ]
        networkSettings.ipv4Settings = ipv4Settings


        if enablePacketProcessing {
            let DNSSettings = NEDNSSettings(servers: ["8.8.8.8"])
            DNSSettings.matchDomains = [""]
            DNSSettings.matchDomainsNoSearch = false
            networkSettings.dnsSettings = DNSSettings
            
            RawSocketFactory.TunnelProvider = self
        }

        setTunnelNetworkSettings(networkSettings) { error in
            guard error == nil else {
                completionHandler(error)
                return
            }

            if !self.started {
                // 下面的观察者要检查网络变化并重启服务
//                self.addObserver(self, forKeyPath: "defaultPath", options: .initial, context: nil)
                if #available(iOSApplicationExtension 10.0, *) {
                    DDLog.add(DDOSLogger.sharedInstance, with: DDLogLevel.verbose)
                    DDOSLogger.sharedInstance.logFormatter = LogFormatter()
                }
                Opt.MAXNWTCPSocketReadDataSize = 60 * 1024 - 1
            }

            if self.started {
                self.httpProxy.stop()
                try! self.httpProxy.start()
            } else {
                self.httpProxy = GCDHTTPProxyServer(address: IPAddress(fromString: "127.0.0.1"), port: 6152)
                try! self.httpProxy.start()
            }

            completionHandler(nil)

            if self.enablePacketProcessing {
                if self.started {
                    self.interface.stop()
                }
                if !self.started {
                    self.interface = TUNInterface(packetFlow: self.packetFlow)

                    let udpStack = UDPAssociateStack(serverHost: "192.168.1.111", serverPort: 9287)
                    self.interface.register(stack: udpStack)

                    let tcpStack = TCPStack.stack
                    tcpStack.proxyServer = self.httpProxy
                    self.interface.register(stack:tcpStack)

                    self.interface.start()
                }
            }
            self.started = true
        }
    }

我能够在udp的input方法,收到dns要发送的udp包,我把包转发到代理服务器,代理服务器也返回udp包了。

@zhuhaow
Copy link
Owner

zhuhaow commented Oct 1, 2019 via email

@chenfengfeng
Copy link
Author

你是说用电脑的dig命令吗?

fengMacmini:~ feng$ dig @8.8.8.8 www.google.com A

; <<>> DiG 9.10.6 <<>> @8.8.8.8 www.google.com A
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12285
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.google.com.			IN	A

;; ANSWER SECTION:
www.google.com.		145	IN	A	31.13.85.16

;; Query time: 62 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Oct 03 14:07:35 CST 2019
;; MSG SIZE  rcvd: 48

@zhuhaow
Copy link
Owner

zhuhaow commented Oct 3, 2019 via email

@zhuhaow
Copy link
Owner

zhuhaow commented Oct 3, 2019 via email

@chenfengfeng
Copy link
Author

好~我先试试

@chenfengfeng
Copy link
Author

@zhuhaow
Copy link
Owner

zhuhaow commented Oct 11, 2019

不要指定8.8.8.8

@chenfengfeng
Copy link
Author

dig没有指定8.8.8.8,但是NEDNSSettings有设定

@chenfengfeng
Copy link
Author

chenfengfeng commented Oct 11, 2019

大佬,我有几个关于dns的疑问,先抛开之前说的,希望能够解惑。

1.NEDNSSettings的作用是不是起到了把系统的dns更改为我设定的dns?如果不是的话,是用来干嘛?

2.如果我设置了NEProxySettings,也就是http代理,并且TUNInterface里面注册了DNSServer,那么我访问网页连接请求的时候,域名解析的dns会走系统的dns还是会被TUNInterface劫持,用DNSServer的设定去解析呢?

3.如果我用了DNSServer但是不用ip池,会有什么影响吗?文档上说的是为了进行反向查找,但是我想知道如果不用的话会有什么影响。
代码是这样的:

self.interface = TUNInterface(packetFlow: self.packetFlow)
let dnsServer = DNSServer(address: IPAddress(fromString: "198.18.0.1")!, port: NEKit.Port(port: 53))
let resolver = UDPDNSResolver(address: IPAddress(fromString: "8.8.8.8")!, port: NEKit.Port(port: 53))
dnsServer.registerResolver(resolver)
self.interface.register(stack: dnsServer)

@qyb
Copy link

qyb commented Oct 12, 2019

请参考 https://forums.developer.apple.com/thread/35027 ,这里是最权威的回答了

@stale
Copy link

stale bot commented Oct 27, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Oct 27, 2019
@stale stale bot closed this as completed Oct 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants