为什么我的脚本跟在/ etc/hosts之后,但是当有SOCKS代理时浏览器没有?

ohh*_*hho 5 dns proxy hosts socks osx-mountain-lion

我办公室里的MacBook没有上网功能.所以我通过SSH建立了一个即时SOCKS代理来访问stackoverflow.我主要使用我的MacBook进行开发,所以我依靠/etc/hosts和虚拟主机在本地测试一些网站.

但是,当我尝试添加条目时/etc/hosts,浏览器不会转到我期望的网站...

我的MacBook的WIFI已关闭并连接到公司LAN:

 IP address:  192.168.8.250
 Subnet mask: 255.255.255.0
 Router:      192.168.8.1
 DNS server:  8.8.8.8
Run Code Online (Sandbox Code Playgroud)

默认情况下,没有互联网访问权限.

有一个Linux开发盒(192.168.12.128)可以访问互联网,所以我设置了一个即时的SOCKS代理来获取我的MacBook的互联网访问:

 ssh -fND localhost:30000 ohho@192.168.12.128
Run Code Online (Sandbox Code Playgroud)

然后在我的MacBook的系统偏好设置>网络>代理中

 (Enable) SOCKS Proxy
 SOCKS Proxy sever: 127.0.0.1:30000
 Bypass proxy settings for these Hosts & Domains:
   *.local, 169.254/16, 127.0.0.1
Run Code Online (Sandbox Code Playgroud)

现在我可以上网了,到目前为止一切都很好.

为了开发,我/etc/hosts为虚拟主机设置了一些条目:

 127.0.0.1 air.company.com
Run Code Online (Sandbox Code Playgroud)

bash:

 $ ping air.company.com
 PING air.ohho.es (127.0.0.1): 56 data bytes
 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.046 ms

 $ curl air.company.com
 <html>OK</html>
Run Code Online (Sandbox Code Playgroud)

它看起来不错,并且很好地curl返回了内容index.html.

但是,如果我尝试打开网站:http://air.company.com在浏览器(Safari/Chrome/Firefox)中,它们都没有返回结果curl.Chrome会出错:

此网页不可用http://air.company.com/上的网页 可能暂时关闭,或者可能已永久移动到新的网址.错误120(net :: ERR_SOCKS_CONNECTION_FAILED):未知错误.

如果我添加另一个条目/etc/hosts:

 127.0.0.1 www.microsoft.com
Run Code Online (Sandbox Code Playgroud)

bash,它看起来不错:

 $ ping www.microsoft.com
 PING www.microsoft.com (127.0.0.1): 56 data bytes
 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.047 ms
 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.128 ms
 ^C
 --- www.microsoft.com ping statistics ---
 2 packets transmitted, 2 packets received, 0.0% packet loss
 round-trip min/avg/max/stddev = 0.047/0.087/0.128/0.041 ms

 $ curl www.microsoft.com
 <html>OK</html>
Run Code Online (Sandbox Code Playgroud)

我也尝试使用ruby脚本:

 require 'socket'
 require 'curl'

 ip = IPSocket.getaddress('www.microsoft.com')
 puts ip

 puts

 http = Curl.get("http://www.microsoft.com/")
 puts http.body_str
Run Code Online (Sandbox Code Playgroud)

输出看起来没问题:

 $ bundle exec ruby openweb.rb 
 127.0.0.1

 <html>OK</html>
Run Code Online (Sandbox Code Playgroud)

但是,当我使用浏览器时,浏览器会从真实 Microsoft网站的Web服务器返回内容,而不是MacBook(127.0.01)中的内容.为什么?

PS:

如果我禁用SOCKS代理,浏览器将正确返回127.0.0.1中的内容.

如果我断开LAN电缆,浏览器将正确返回127.0.0.1中的内容.

Car*_*ano 1

我会尽力回答这个问题,因为我已经遇到了这个问题。

当您使用Socks5连接时,您的IP解析将转发到socks服务器。

在版本 5 中,SOCKS 实现可以接收完整的域名作为目标和源地址[1](第 4 页)。

在 Mac OS 上,默认值为 SOCKS 5,如[2] (NSStreamSOCKSProxyVersionKey) 中所示。

因此,默认情况下,mac 客户端将使用 SOCKS5 并转发 DNS 请求。

我希望这能回答您的问题:“当有 SOCKS 代理时,为什么我的脚本遵循 /etc/hosts 而浏览器却不遵循?” --> 因为 SOCKS5 实现转发 dns 请求:)

带着敬意

1 - http://www.ietf.org/rfc/rfc1928.txt

2 - https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSStream_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSStreamSOCKSProxyConfigurationKey