$ sudo port install ImageMagick
$ mogrify -format png /path/*.jpg
Tag Archives: MacPorts
Mac OS X 上 gfwlist2privoxy 的使用
本文中的软件使用 MacPorts(2.3.4) 安装。
安装 Privoxy(3.0.21)、py-pip(8.1.2)、gfwlist2privoxy(1.0.3)
$ sudo port install privoxy py-pip $ sudo pip install gfwlist2privoxy
查找 gfwlist2privoxy 的安装路径
$ find /opt -iname 'gfwlist2privoxy' /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/gfwlist2privoxy /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gfwlist2privoxy /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gfwlist2privoxy-1.0.3-py2.7.egg-info
配置 Privoxy
生成空文件:
$ sudo touch /opt/local/etc/privoxy/gfwlist.action
修改文件所有者:
$ sudo chown privoxy:privoxy /opt/local/etc/privoxy/gfwlist.action
修改文件权限:
$ sudo chmod 660 /opt/local/etc/privoxy/gfwlist.action
允许当前用户修改该文件,便于后续使用当前用户的 crontab
定时生成该文件:
$ sudo chmod +a "${USER} allow read,write" /opt/local/etc/privoxy/gfwlist.action
查看文件权限:
$ ls -le /opt/local/etc/privoxy/gfwlist.action -rw-rw----+ 1 privoxy privoxy 62526 Sep 6 00:00 /opt/local/etc/privoxy/gfwlist.action 0: user:YOURNAME allow read,write
在文件 /opt/local/etc/privoxy/config
的行 actionsfile user.action
下一行追加 actionsfile gfwlist.action
,原文件会备份为 /opt/local/etc/privoxy/config.bak
。注意这个命令就是写作三行的:
$ sudo sed -i '.bak' '/actionsfile user.action/a\ \ actionsfile gfwlist.action\ ' /opt/local/etc/privoxy/config
对比原文件和修改后的文件,确保上述命令被正确执行:
$ sudo diff -ruN /opt/local/etc/privoxy/config.bak /opt/local/etc/privoxy/config --- /opt/local/etc/privoxy/config.bak +++ /opt/local/etc/privoxy/config @@ -370,6 +370,7 @@ actionsfile match-all.action # Actions that are applied to all sites and maybe overruled later on. actionsfile default.action # Main actions file actionsfile user.action # User customizations +actionsfile gfwlist.action # # # 2.5. filterfile
删除备份文件
$ sudo rm /opt/local/etc/privoxy/config.bak
启动 Privoxy
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.Privoxy.plist
生成 gfwlist.action 文件
下载 gfwlist
并生成 gfwlist.action
文件。其中 127.0.0.1:1080
是监听在本地 1080 端口的 socks5 代理服务器,至于如何建立一个 socks5 代理服务器并不在本文讨论的范畴。/opt/local/etc/privoxy/gfwlist.action
修改后不需要重启 Privoxy
,Privoxy
会自动应用修改。
$ mkdir -p ~/bin/ $ echo "\!this is a user defined rule" >> ~/bin/user_rule.txt $ echo "ip.cn" >> ~/bin/user_rule.txt $ /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/gfwlist2privoxy -f /opt/local/etc/privoxy/gfwlist.action -p '127.0.0.1:1080' -t socks5 --user-rule ~/bin/user_rule.txt
可以将这个命令加入 crontab
中定时执行,使用命令crontab -e
编辑当前用户的 crontab
,
添加一条记录,这里的例子是每天0点执行一次:
#minute hour mday month wday command 0 0 * * * /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/gfwlist2privoxy -f /opt/local/etc/privoxy/gfwlist.action -p '127.0.0.1:1080' -t socks5 --user-rule ~/bin/user_rule.txt > /dev/null 2>&1
测试
测试 socks5 代理服务器正在监听 1080 端口:
$ telnet 127.0.0.1 1080 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
测试通过 socks5 代理服务器访问 ip.cn
:
curl -x 'socks5://127.0.0.1:1080' ip.cn 当前 IP:x.x.x.x 来自:xxxxxx xxx
测试通过 Privoxy 访问 ip.cn
分流到了 socks5 代理服务器。我们已经把 ip.cn
添加到了 user_rule.txt
,因此下述命令输出结果应该和上述命令一致:
$ curl -x 'http://127.0.0.1:8118' ip.cn 当前 IP:x.x.x.x 来自:xxxxxx xxx
使用
在操作系统的系统偏好设置
(系统偏好设置->网络->高级…->代理)里设置 Web 代理(HTTP)
和安全 Web 代理(HTTPS)
为 Privoxy 监听的地址 127.0.0.1:8118
。
Mac OS X 上安装 tor 并配置为自动启动
安装
$ sudo port install tor
自动启动
由于该 port 并未提供自动启动脚本,因此我们只能自己编写。
编写文件 /Library/LaunchDaemons/org.example.tor.plist
$ sudo vi /Library/LaunchDaemons/org.example.tor.plist
内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>org.example.tor</string> <key>ProgramArguments</key> <array> <string>/opt/local/bin/tor</string> <string>-f</string> <string>/opt/local/etc/tor/torrc</string> <string>--PidFile</string> <string>/tmp/tor.pid</string> <string>--DataDirectory</string> <string>/opt/local/var/lib/tor</string> </array> <!-- <key>UserName</key> <string>privoxy</string> --> <key>KeepAlive</key> <true/> <key>RunAtLoad</key> <true/> <key>StandardOutPath</key> <string>/tmp/org.example.tor-out.log</string> <key>StandardErrorPath</key> <string>/tmp/org.example.tor-err.log</string> </dict> </plist>
注意上述文件内容中的 UserName 那段被注释掉了,被注释掉后 tor 将以 root 身份运行。
由于我的 Mac OS X 上通过 MacPorts 安装了 Privoxy, 所以有 privoxy 这个用户,所以我就使用 privoxy 用户来运行 tor。
如果使用非 root 用户运行 tor,那么需要预先创建数据目录 /opt/local/var/lib/tor 并对 UserName 所指的用户授予读写权限。
比如我这里手动创建该目录并授权用户 privoxy 读写权限:
$ sudo mkdir -p /opt/local/var/lib/tor
$ sudo chown -R privoxy:privoxy /opt/local/var/lib/tor
$ sudo chmod 700 /opt/local/var/lib/tor
启动后台驻留程序(Daemon):
$ sudo launchctl load /Library/LaunchDaemons/org.example.tor.plist
停止后台驻留程序(Daemon):
$ sudo launchctl unload /Library/LaunchDaemons/org.example.tor.plist
可以通过查看日志文件 /tmp/net.oxerr.tor-out.log 来查看运行状态,如果启动成功并成功连上 tor 网络,那么它默认会监听在 127.0.0.1:9050 端口,可使用 telnet 命令测试是否成功连接:
$ telnet 127.0.0.1 9050
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
使用方法
默认情况下我们会得到一个工作在本机 9050 端口的 socks 代理服务器,我们可以在浏览器等应用程序里设置代理服务器为 socks 127.0.0.1:9050。
或者使用命令 torsocks,比如:
$ torsocks curl ip.cn
当前 IP:85.25.103.69 来自:法国
Tor 的配置文件 /opt/local/etc/tor/torrc
安装完成后会有一个示例配置文件 /opt/local/etc/tor/torrc.sample,复制到/opt/local/etc/tor/torrc:
$ sudo cp /opt/local/etc/tor/torrc.sample /opt/local/etc/tor/torrc
使用 man tor 命令可以查阅 tor 的配置帮助文档。
常用配置项
给 Tor 配置 socks 代理服务器,比如:
Socks5Proxy 127.0.0.1:1080
卸载
停止后台驻留程序(Daemon):
$ sudo launchctl unload /Library/LaunchDaemons/org.example.tor.plist
删除手动创建的文件:
$ sudo rm /Library/LaunchDaemons/org.example.tor.plist
$ sudo rm /opt/local/etc/tor/torrc
删除数据文件目录:
$ sudo rm -r /opt/local/var/lib/tor
卸载 port:
$ sudo port uninstall tor
在 Mac OS X 上给 Telegram 单独设置代理服务器
由于众所周知的原因,并不是在世界上每一个国家都能顺利地使用 Telegram,那么我们不得不通过给 Telegram 设置代理服务器等途径来收发消息。
在 Mac OS X 的系统设置里可以设置 SOCKS Proxy,但是这样的缺点是所有其它应用程序也会被应用该代理设置。
本文将介绍如何使用 dsocks 软件来给 Telegram 应用程序单独设置代理服务器。
在开始本文前,你需要有一个 SOCKS 代理服务器,至于如何获得或者架设 SOCKS 代理服务器,并不是本文所要涵盖的内容。
安装 dsocks
使用 MacPorts 或者其它包管理工具都可以安装 dsocks。
$ port search dsocks
dsocks @1.6 (net)
SOCKS client wrapper for *BSD / Mac OS X
$ sudo port install dsocks
编写启动脚本
假设你的 SOCKS 代理服务器运行在 127.0.0.1 的 1080 端口,那么将下面这段脚本存储为文件 ~/bin/telegram.sh
并 chmod +x ~/bin/telegram.sh
#!/bin/sh env DSOCKS_PROXY="127.0.0.1:1080" dsocks.sh \ /Applications/Telegram.app/Contents/MacOS/Telegram \ >/dev/null 2>&1 &
注意上述脚本中的 dsocks.sh,对于使用 MacPorts 来安装 dsocks 的情况下,它实际位于:
$ which dsocks.sh
/opt/local/bin/dsocks.sh
启动 Telegram
要启动 Telegram,则改为使用上述脚本来启动: ~/bin/telegram.sh
顺便说一下,给 Telegram 应用程序设置 HTTP/HTTPS 代理也能收发消息,但是延迟非常严重。