2015年6月3日星期三

Shadowsocks @Pi

自從天朝加強封鎖GOOGLE的服務後,為我等依賴Google相關服務的人帶來許多不必要的麻煩,只好被逼“科學上網”了。其中shadowsocks是一個十分穩定的方法。對比其他“科學上網”的方法, SS最大的好處是在網絡斷線後不用再次連接,而且感覺反應速度也不錯。
但長期用免費的SS 帳號總覺有點安全問題,而且也增加提供者的壓力。既然有條件,便自己架設一個SS好了。

要準備的東西:

1. Raspberry Pi 一部 或者買個VPS (本文以Pi為例,其實操作差不多)

舊的B版便足夠,不一定要2.0的新版。

2. 牆外的寛頻上網

好了,可以開工了。

一. 安裝Pi的系統


下載RespBian後,用工具寫入MicroSD卡便可。
接好顯示器(HDMI),USB keybroad mouse,LAN線及電源後,便Boot機。按指示便可安裝完成所需的基本功能。
建議在上述安裝過程中,選擇安裝SSH server,方便日後遠程登入設置,不必再接keybroad mouse.

注意:
一般購入的Pi未必包火牛及MicroSD卡。要問清楚。其實可用一般電話的火牛便可。Pi的輸入是5V+1A. 其實用800mA也可以。插頭是MicroUSB。 

二.安裝 SS防火牆

參考:
本想安裝C的版本,但安裝時出現問題,最後決定用Python的版本。

1. 確定Python是2.6或是 2.7

python --version
Python 2.6.8

2. 安裝必需的軟件

為方便,建議用root來執行安裝過程
sudo su -
開始安裝
apt-get install python-pip python-gevent python-m2crypto
更新 pip
    pip install --upgrade pip

3. 安裝 SS

pip install shadowsocks
如無意外,安裝便完成。接下來要配置參數。

4. 配置參數

以下是2個ports登入的設置:
nano /etc/shadowsocks.json
{
    "server":"0.0.0.0",
    "local_address": "127.0.0.1",
    "local_port":1080,
    "port_password": {
       "443":"password1",
       "8889":"password2"
     },
    "timeout":600,
    "method":"aes-256-cfb",
    "fast_open": false,
    "workers": 2
}

登入ports的數值并沒有限定,一般低於1000的ports可能已有服務需用。有部分ports可能會被家用寛頻封掉,如port 25。Port 443一般是https 服務用的,有機會家用寛頻會封掉。
完成以上後安裝客戶端,填上密碼等資料,便可翻牆了。但為免被人輕鬆入侵,建議設備防火牆 iptables。最簡單就是只開放指定幾個ports,其他不接受連接。

5.設置iptables

以下是一個只開放port 443,8889及22的例子:
把以下儲存為 iptables.save
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [672:84130]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 8889 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -j DROP
COMMIT

然後執行
iptables-restore < iptables.save

將上述命令加入/etc/rc.local (在exit 0之前)
簡單的防火牆便完成了。

三. 測試及報行

ssserver -c /etc/shadowsocks.json 
如無錯誤,使完成安裝了。

更改/etc/rc.local,使之在重啟後在背景報行:
sudo nano /etc/rc.local
在exit 0之前加入以下
sudo nohup ssserver -c /etc/shadowsocks.json > /dev/null 2>&1 &

四. 客戶端

ref: 
http://shadowsocks.org/en/config/quick-guide.html

Linux

創建配置 abc.json
nano abc.json
{
    "server":"abc.no-ip.org",
    "server_port":443,
    "local_port":8888,
    "password":"password",
    "timeout":600,
    "method":"aes-256-cfb"
}
執行:
sslocal -c hksp.json > /dev/null 2>&1 &

將瀏覽器或系統的proxy設置成
ip: 127.0.0.1
port:8888
socks5

完成,可科學上網了。

其他客戶端

http://shadowsocks.org/en/download/clients.html



用Command Line 匯入P12證書到Firefox中




轉至/home/UserName/.mozilla/firefox
前題是Firefox曾執行過,否則 ".mozilla" 不存在。

在此目錄下,有一個xxxxxxxx.default的目錄,進入去,執行以下命令匯入P12證書到Firefox中
pk12util -i abc_cert.p12 -d ./

Enter password for PKCS12 file:   <==如有密碼
pk12util: PKCS12 IMPORT SUCCESSFUL

完成後,證書已被匯入,以下檔案會被更新。
cert8.db
key3.db


------------------------------------------------------------------------------------------------------------
參考:
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Reference/NSS_tools_:_pk12util

-i p12file
Import keys and certificates from a PKCS#12 file into a security database.

-d [sql:]directory
Specify the database directory into which to import to or export from certificates and keys.

pk12util supports two types of databases: the legacy security databases (cert8.db, key3.db, and secmod.db) and new SQLite databases (cert9.db, key4.db, and pkcs11.txt). If the prefix sql: is not used, then the tool assumes that the given databases are in the old format.

-n certname
Specify the nickname of the cert and private key to export.