wenlong1423
2/1/2015 - 9:46 AM

Mac 下命令行使用代理并分流.md

使用路由表实现分流
proxychains 最多只支持 64 条路由规则
因此使用 tsocks 而不是 proxychains

由于 brew tap adamv/alt 已不能使用,需要手动创建 Formula

创建 /usr/local/Library/Formula/tsocks.rb 内容如下

require 'formula'

class Tsocks < Formula
  # The original is http://tsocks.sourceforge.net/
  # This GitHub repo is a maintained fork with OSX support
  homepage 'http://github.com/pc/tsocks'
  head 'https://github.com/pc/tsocks.git'

  depends_on 'autoconf' => :build if MacOS.xcode_version.to_f >= 4.3

  def install
    system "autoconf", "-v"
    system "./configure", "--prefix=#{prefix}", "--disable-debug", "--disable-dependency-tracking", "--with-conf=#{config_file}"

    inreplace("tsocks") { |bin| bin.change_make_var! "LIBDIR", lib }

    system "make"
    system "make install"

    etc.install "tsocks.conf.simple.example" => "tsocks.conf" unless config_file.exist?
  end

  def test
    puts 'Your current public ip is:'
    ohai `curl -sS ifconfig.me 2>&1`.chomp
    puts "If your correctly configured #{config_file}, this should show the ip you have trough the proxy"
    puts 'Your ip through the proxy is:'
    ohai `tsocks curl -sS ifconfig.me 2>&1`.chomp
  end

  def config_file
    etc / 'tsocks.conf'
  end
end

然后进行安装

brew install --HEAD tsocks

编辑配置文件 /usr/local/etc/tsocks.conf

local = 192.168.0.0/255.255.255.0
local = 1.0.1.0/255.255.255.0
local = 1.0.2.0/255.255.254.0
local = 1.0.8.0/255.255.248.0
local = 1.0.32.0/255.255.224.0
local = 1.1.0.0/255.255.255.0
local = 1.1.2.0/255.255.254.0
local = 1.1.4.0/255.255.252.0
local = 1.1.8.0/255.255.248.0
#.....太长,略,使用 chnroute.py 生成
server = 127.0.0.1
server_type = 5
server_port = 1080

####测试

tsocks curl ip.cn
tsocks curl ifconfig.me

结果应该一个为国内 IP 一个为代理服务器 IP (当然你的 DNS 得是国内的或者是分流的)