我在办公室的 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
默认情况下,没有互联网访问。
有一个可以访问互联网的 Linux 开发盒(192.168.12.128),所以我设置了一个即时 SOCKS 代理来为我的 MacBook 获得互联网访问权限:
ssh -fND localhost:30000 ohho@192.168.12.128
然后在我的 MacBook 的 System Preferences > Network > Proxies
(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
现在我可以上网了,到目前为止一切顺利。
/etc/hosts
为了开发,我为虚拟主机设置了一些条目:
127.0.0.1 air.company.com
在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>
它看起来不错,并且很好地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
在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>
我也尝试使用 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
输出看起来不错:
$ bundle exec ruby openweb.rb
127.0.0.1
<html>OK</html>
但是,当我使用浏览器时,浏览器会返回来自真实Microsoft 站点的 Web 服务器的内容,而不是来自我的 MacBook (127.0.01) 的内容。为什么?
ps:
如果我禁用 SOCKS 代理,浏览器会正确返回 127.0.0.1 的内容。
如果我断开 LAN 电缆,浏览器会正确返回 127.0.0.1 的内容。