2014年2月19日 星期三

[Linux] 使用 vnstat 監控網路流量,提供每小時、每日、每週、每月流量總報表 @ Ubuntu 12.04

vnstat

最近在用雲端服務,採用用多少資源付多少錢,其中的變動收費的就是網路流量,所以就裝一下 vnstat 來計算一下,實在很夠用!

$ sudo apt-get install vnstat

接著就可以很方便地用 command line 執行顯示了。

2014年2月18日 星期二

[Linux] MongoDB Replica Set with Auth & KeyFile @ Ubuntu 12.04

mongod shutdown error

建 cluster 不見得需要帳號管控,因為多數處在保護的網路環境,而 MongoDB 預設沒有帳密管控且帳密管控做的很簡單,分成 read-only 跟 read-write 模式。而使用認證的主因是為了遠端關掉機器,沒有認證機制的服務僅能透過 localhost 關掉服務,有一派說是透過 service mongodb stop 指令,又有一派說直接 kill process 即可,但有文章說如此容易造成問題?還是要用正式的關機流程比較妥( mongo> db.shutdownServer() )。

參考文件:
架設 Replica Set 的流程:
  1. 啟動多台 mongod --replSet set_name
  2. 挑選一台 mongod 登入
  3. 使用 rs.init() 初始化後,再用 rs.add() 加入其他 mongod 或是使用 db.runCommand({'replSetInitiate':{'members':[]}}) 一次新增
架設需要認證的 Replica Set 的流程:
  1. 用 default 模式啟動一台 mongod
  2. 建立帳號
  3. 建立 keyFile (檔案內容隨意,但需要多過6個字元)
  4. 關掉後以 --replSet set_name 與 --keyFile key_file_path 模式啟動多支 mongod
  5. 登入一台 mongod 以及完成帳號認證
  6. 使用 rs.init() 初始化後,再用 rs.add() 加入其他 mongod 或是使用 db.runCommand({'replSetInitiate':{'members':[]}}) 一次新增
上述兩者流程的差別在於後者要先建帳號後,再用 --replSet 跟 --keyFile 模式啟動。而後,為了測試就寫了隻 script 筆記一下:

未使用認證模式:

$ python replset_init.py --reset

Init DB(0), Port: 30000, Path: /home/id/data/mongodb-study/cluster/db/db-0
 $ mongod --dbpath /home/id/data/mongodb-study/cluster/db/db-0 --port 30000 --oplogSize 700 --logpath /home/id/data/mongodb-study/cluster/log/db-0.log --rest --replSet firstset

Init DB(1), Port: 30001, Path: /home/id/data/mongodb-study/cluster/db/db-1
 $ mongod --dbpath /home/id/data/mongodb-study/cluster/db/db-1 --port 30001 --oplogSize 700 --logpath /home/id/data/mongodb-study/cluster/log/db-1.log --rest --replSet firstset

Init DB(2), Port: 30002, Path: /home/id/data/mongodb-study/cluster/db/db-2
 $ mongod --dbpath /home/id/data/mongodb-study/cluster/db/db-2 --port 30002 --oplogSize 700 --logpath /home/id/data/mongodb-study/cluster/log/db-2.log --rest --replSet firstset

nohup: ignoring input and appending output to `nohup.out'
nohup: ignoring input and appending output to `nohup.out'
nohup: ignoring input and appending output to `nohup.out'

Waiting...
        localhost:30000: .....OK
        localhost:30001: OK
        localhost:30002: OK

Connect to localhost:30000

Initialize the First Replica Set:

$ monogo localhost:30000/admin
mongo> db.runCommand( {'replSetInitiate': {'_id': 'firstset', 'members': [{'host': 'localhost:30000', '_id': 1}, {'host': 'localhost:30001', '_id': 2}, {'host': 'localhost:30002', '_id': 3}]}} )

Result:
{u'info': u'Config now saved locally.  Should come online in about a minute.', u'ok': 1.0}

All is done.

server info:
 $ mongo localhost:30000/admin --eval 'printjson(rs.status())'

shutdown servers:
 $ mongo localhost:30000/admin --eval 'db.shutdownServer()'
 $ mongo localhost:30001/admin --eval 'db.shutdownServer()'
 $ mongo localhost:30002/admin --eval 'db.shutdownServer()'


使用認證模式:

$ python replset_init.py --reset --auth-key-file keyfile --auth-user account --auth-pass password

Init DB(0), Port: 30000, Path: /home/id/data/mongodb-study/cluster/db/db-0
 $ mongod --dbpath /home/id/data/mongodb-study/cluster/db/db-0 --port 30000 --oplogSize 700 --logpath /home/id/data/mongodb-study/cluster/log/db-0.log --rest
nohup: ignoring input and appending output to `nohup.out'

Waiting...
        localhost:30000: ..OK

Connect to localhost:30000

Initialize the First Replica Set:

$ monogo localhost:30000/admin
mongo> db.addUser( {user: "account", pwd:"password", roles:["userAdminAnyDatabase"] })

Add account done.

Restart the mongod:

Init DB(0), Port: 30000, Path: /home/id/data/mongodb-study/cluster/db/db-0
 $ mongod --dbpath /home/id/data/mongodb-study/cluster/db/db-0 --port 30000 --oplogSize 700 --logpath /home/id/data/mongodb-study/cluster/log/db-0.log --rest --replSet firstset --keyFile keyfile

Init DB(1), Port: 30001, Path: /home/id/data/mongodb-study/cluster/db/db-1
 $ mongod --dbpath /home/id/data/mongodb-study/cluster/db/db-1 --port 30001 --oplogSize 700 --logpath /home/id/data/mongodb-
study/cluster/log/db-1.log --rest --replSet firstset --keyFile keyfile

nohup: ignoring input and appending output to `nohup.out'
Init DB(2), Port: 30002, Path: /home/id/data/mongodb-study/cluster/db/db-2
 $ mongod --dbpath /home/id/data/mongodb-study/cluster/db/db-2 --port 30002 --oplogSize 700 --logpath /home/id/data/mongodb-
study/cluster/log/db-2.log --rest --replSet firstset --keyFile keyfile

nohup: ignoring input and appending output to `nohup.out'
nohup: ignoring input and appending output to `nohup.out'
nohup: ignoring input and appending output to `nohup.out'

Waiting...
        localhost:30000: .......OK
        localhost:30001: ...................................OK
        localhost:30002: .OK

Connect to localhost:30000

Initialize the First Replica Set:

$ monogo localhost:30000/admin
mongo> db.runCommand( {'replSetInitiate': {'_id': 'firstset', 'members': [{'host': 'localhost:30000', '_id': 1}, {'host': 'localhost:30001', '_id': 2}, {'host': 'localhost:30002', '_id': 3}]}} )

Result:
{u'info': u'Config now saved locally.  Should come online in about a minute.', u'ok': 1.0}

All is done.

server info:
 $ mongo localhost:30000/admin -u account -p password --eval 'printjson(rs.status())'

shutdown servers:
 $ mongo localhost:30000/admin -u account -p password --eval 'db.shutdownServer()'
 $ mongo localhost:30001/admin -u account -p password --eval 'db.shutdownServer()'
 $ mongo localhost:30002/admin -u account -p password --eval 'db.shutdownServer()'

2014年2月13日 星期四

翻來翻去的筆記: Proxy、VPN 和 SSH Tunnel、iptables port forwarding 混用

???

有想要翻出去,就會有人想要翻進去 XD 在指定國家內架一檯 server ,提供 proxy 、 VPN 服務,提供另一個國家連線,這是一個滿常見的做法,只是,如果國家網路掌控很威,那需要嘗試一些小手段:

前提:

自家機器 IP: localhost , 可用 local_port
指定國家 SERVER IP: x.x.x.x , target service port: xx
第二國家 SERVER IP: y.y.y.y , port forward: yy

SSH Tunnel

假設在指定國家內的 server 服務都是跑 TCP 時,如果直接連它還是不夠順,那就試試 Service over SSH tunnel 吧!

$ ssh -N -L local_port:x.x.x.x:xx x.x.x.x

如此一來,原先的服務就可以改連 localhost:local_port 代替 x.x.x.x:xx 服務。若 ssh tunnel 需要斷掉還能自動重連,在 Ubuntu 就是用 autossh ,把 ssh 指令換成 autossh 即可。若開機就執行,就寫在 /etc/rc.local,另外也用 key-pair 登入來取代密碼輸入:

$ sudo vim /etc/rc.local
su -s /bin/sh account -c 'autossh -N -f -L x.x.x.x:xx x.x.x.x'


有時候單純是 routing table 的問題,需要假藉另一個國家 y.y.y.y 當跳板,這時 TCP/UDP 都可以用 iptables 處理掉(-p tcp 或 -p udp),在 y.y.y.y 機器設定:

$ iptables -A PREROUTING -t nat -i eth0 -p tcp --dport yy -j DNAT --to x.x.x.x:xx
$ iptables -A FORWARD -p tcp -d x.x.x.x --dport xx -j ACCEPT


如此一來,連 y.y.y.y:yy 等同連到 x.x.x.x:xx 。

此外,純 TCP 在 Ubuntu 中還可以透過 redir 套件來處理,若 redir 已安裝好後,好處相較于 iptables 可以不需要 root 權限:

$ redir --lport yy --laddr=y.y.y.y --cport=xx --caddr=x.x.x.x

以 Proxy、VPN 這類服務而言,大概能搞的招數就這些吧,例如 Proxy over SSH tunnel 或甚至 SSH tunnel 是從第二國家建立,以此強制 routing 的改變。

iOS 開發筆記 - Xcode: Your application is being uploaded: request timeout / Application loader: Authenticating with the iTunes Store

your application is being uploaded

重灌 OSX ,試了一下發現這種現象 Orz 參考以下資料:

搞了一陣子憑證外,也裝了 Java 等等,當然,連 Application loader 也試過一輪,仍還沒改善,最後看到這則:
看完比較釋懷,然後,再試試 Application loader (with https.proxyPort=80) 幾次時,就意外通過了 XD 

大概耗掉兩小時的生命啊...人生啊~

[Linux] 架設 OpenVPN server 與 OpenVPN client 設定 @ Ubuntu 12.04

嘗過過 PPTP、L2TP 後,接著把玩 OpenVPN。此例 OpenVPN server 跟 OpenVPN client 都是 Ubuntu 12.04,其中一個是 server 版,另一個是 desktop 版。

Ubuntu Server: 安裝:

$ sudo apt-get install openvpn

Ubuntu Server: 憑証(順便把 OpenVPN client 做完):

$ cp -r /usr/share/doc/openvpn/examples/easy-rsa/2.0 /tmp/easy-rsa
$ cp /tmp/easy-rsa/openssl-1.0.0.cnf /tmp/easy-rsa/openssl.cnf
$ cd /tmp/easy-rsa/
$ source ./vars
$ ./clean-all
$ ./build-dh
$ ./pkitool --initca
$ ./pkitool --server server
$ ./build-key client
$ sudo su
# chown -R root:root /tmp/easy-rsa/keys/
# cd keys
# cp ca.crt server.crt server.key dh*.pem /etc/openvpn/


Ubuntu Server: 建立 /etc/openvpn/server.conf:

$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn
$ sudo gunzip /etc/openvpn/server.conf.gz


Ubuntu Server: 設定 /etc/openvpn/server.conf (修改要跑的 port, ip, tcp or udp mode):

$ sudo vim /etc/openvpn/server.conf

Ubuntu Server: 讓 OpenVPN client 可以透過 OpenVPN server 連出去外網,其中 eth0 為 OpenVPN server 對外網卡、10.8.0.0/24 是預設的 OpenVPN 區段:

$ sudo vim /etc/openvpn/server.conf
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"

$ iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE


Ubuntu Client: 安裝(從OpenVPN server 取得 ca.crt、client.crt、client.key):

$ sudo apt-get install openvpn
$ sudo cp -r /usr/share/doc/openvpn/examples/sample-config-files/ /etc/openvpn/example
$ sudo cp ca.crt client.crt client.key /etc/openvpn/example


Ubuntu Client: 設定(修改 server ip, port, tcp or udp mode):

$ sudo vim /etc/openvpn/example/client.conf

Ubuntu Client: 執行:

$ sudo openvpn --config /etc/openvpn/example/client.conf

Ubuntu Client: 開機執行:

$ sudo vim /etc/rc.local
openvpn --config /etc/openvpn/example/client.conf > /var/log/openvpn_client.log 2>&1 &


如果 OpenVPN client 還有提供 NAT 服務,那記得 iptables 要加上這段(tun0可以用 ifconfig查看):

VIF="tun0"
iptables -t nat -A POSTROUTING -o $VIF -j MASQUERADE