2011年8月31日 星期三

[阿宅專區] The Architecture of Open Source Applications


剛剛逛到這本書,裡面有許多有名的專案,其中唐鳳所做的SocialCalc看起來相當有趣阿,話說我應該會買個PDF的版本支持一下


2011年8月29日 星期一

[Network] Configuring multiple default routes and name resolving with 1 network interface card

上篇的多重routing與name resolving,再稍做改良就可以用一張網卡同時使用PPPoE撥號連線並支援多重路由,直接看script吧
#!/bin/bash

# pppoe dial up
RETRY=5
PPP=`ifconfig|grep -in ppp`
until [ $RETRY -le 0 ] || [ -n "$PPP" ]
do pon dsl-provider
  sleep 30
  PPP=`ifconfig|grep -in ppp`
  ((RETRY=RETRY-1))
done

# eth0 setting
IF2=eth0
IP2=10.157.132.104
NM2=255.255.240.0
NT2=10.157.128.0/20
GW2=10.157.143.254

IP3=172.17.125.5
NM3=255.255.255.0
NT3=172.17.125.0/24

# some network for my own purpose
NM4=224.0.0.0

# default gateway
DGW=${GW2}

# set ip/netmask
ifconfig ${IF2} ${IP2} netmask ${NM2}

# create a new policy routing table entry
T2=`cat /etc/iproute2/rt_tables|awk '{print $2}'|grep -in T2|cut -d : -f 2`
if [ -z "$T2" ]; then
  echo "1 T2" >> /etc/iproute2/rt_tables
fi

# add new entry within this policy table
ip route add ${NT2} dev ${IF2} src ${IP2} table T2
ip route add default via ${GW2} dev ${IF2} table T2

ip rule add from ${NT2} table T2
ip rule add to ${NT2} table T2

ip rule add from ${NT3} table T2
ip rule add to ${NT3} table T2

# add some routing for my own purpose
route add -net ${NM4} netmask ${NM4} dev ${IF2}

# options timeout:1 rotate attempts:1
echo "options rotate"          >  /etc/resolv.conf
echo "nameserver 168.95.192.1" >> /etc/resolv.conf
echo "nameserver 172.17.125.5" >> /etc/resolv.conf

# disable reverse packet filter for my own purpose
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/lo/rp_filter

[Tips] Ubuntu下PPPoE安裝設定

速記一下Ubuntu的PPPoE設定...

查詢有關PPPoE的package
$ sudo apt-cache search pppoe
pppoeconf - configures PPPoE/ADSL connections
pppoe - PPP over Ethernet driver
pppstatus - console-based PPP status monitor

安裝
$ sudo apt-get install pppoeconf pppoe pppstatus

設定
$ sudo pppoeconf

根據問題回答yes/no,並且設定帳號密碼,設定完成後將自動連線

中斷
$ sudo poff
若超過一個連線
$ sudo poff -a

重新建立連線
$ sudo pon dsl-provider

查詢log
$ sudo plog

預設撥接設定儲存位置,會override /etc/ppp/options
$ sudo vim /etc/ppp/peers/dsl-provider
lcp-echo-interval 30      # interval of sending request to adsl server
lcp-echo-failure 10       # times of retry if sending request fail
# Override any connect script that may have been set in /etc/ppp/options.
persist                   # auto dial-up if lost connection 
user "user@isp.net"       # dial-up user account

若是使用PAP驗證,則帳號密碼會儲存於
$ sudo vim /etc/ppp/pap-secrets
"user@isp.net" * "passowrd"

PPPoE斷線問題檢測先從查詢plog開始,若出現以下訊息表示超出嘗試次數還未能連接上
pppd[1624]: Timeout waiting for PADO packets

解決方法就是定時檢查連線狀態,若沒有連線則持續撥號,常用的檢查方法有ifconfig或是running process,簡單寫一下重新撥號的script
#!/bin/bash
# auto dial-up 

# check ifconfig or check running process
RETRY=5
PPP=`ifconfig|grep -in ppp`
until [ $RETRY -le 0 ] || [ -n "$PPP" ] || [ -e /var/run/ppp0.pid ]
do pon dsl-provider
  sleep 30
  PPP=`ifconfig|grep -in ppp`
  ((RETRY=RETRY-1))
done

reference:
* 使用 PPPoEConf 連接 Internet
* Linux PPP HOWTO

2011年8月26日 星期五

[資訊安全] Reversing for Newbies

今天逛到一個不錯的教學網站,希望練習練習,下次wargame能拿第一

2011年8月25日 星期四

[Network] Configuring multiple default routes and name resolving in Linux

problem description:
There are 2 NICs on my Ubuntu 10.10, I need to configure multiple default route and multiple name resolve with my host.

assume that my nic setting
$ ifconfig
wlan0: 192.168.2.115  netmask 255.255.255.0
       default gw 192.168.2.1
eth0:  10.157.132.104 netmask 255.255.240.0
       default gw 10.157.143.254

first you have to make sure your Linux kernel has support "policy routing"
$ cd /usr/src/linux
$ sudo make menuconfig
[*] Networking support  --->
      Networking options  --->    
        [*] IP: advanced router
        [*] IP: policy routing 
        [*] IP: use netfilter MARK value as routing key

create a new policy routing table entry(T2)
$ sudo echo "1 T2" >> /etc/iproute2/rt_tables

add new entry within this policy table
$ sudo ip route add 10.157.128.0/20 dev eth0 src 10.157.132.104 table T2
$ sudo ip route add default via 10.157.143.254 dev eth0 table T2
$ sudo ip rule add from 10.157.128.0/20 table T2
$ sudo ip rule add to 10.157.128.0/20 table T2

add nameserver
$ sudo echo "options rotate" > /etc/resolv.conf
$ sudo echo "nameserver 192.168.2.1" >> /etc/resolv.conf
$ sudo echo "nameserver 172.17.125.5" >> /etc/resolv.conf

I write a shell script for quickly configuring multiple default routes as below
#!/bin/bash

# first nic setting
IF1=wlan0
IP1=192.168.2.115
NM1=255.255.255.0
NT1=192.168.2.0/24
GW1=192.168.2.1

# second nic setting
IF2=eth0
IP2=10.157.132.104
NM2=255.255.240.0
NT2=10.157.128.0/20
GW2=10.157.143.254

# additional setting for second nic
IP3=172.17.125.5
NM3=255.255.255.0
NT3=172.17.125.0/24

# default gateway
DGW=${GW2}

# set ip/netmask
ifconfig ${IF2} ${IP2} netmask ${NM2}
ifconfig ${IF1} ${IP1} netmask ${NM1}

# create a new policy routing table entry
T2=`cat /etc/iproute2/rt_tables|awk '{print $2}'|grep -in T2|cut -d : -f 2`
if [ -z "$T2" ]; then
  echo "1 T2" >> /etc/iproute2/rt_tables
fi

# add new entry within this policy table
ip route add ${NT2} dev ${IF2} src ${IP2} table T2
ip route add default via ${GW2} dev ${IF2} table T2
ip rule add from ${NT2} table T2
ip rule add to ${NT2} table T2
ip rule add from ${NT3} table T2
ip rule add to ${NT3} table T2

# options timeout:1 rotate attempts:1
echo "options rotate"          >  /etc/resolv.conf
echo "nameserver 192.168.2.1"  >> /etc/resolv.conf
echo "nameserver 172.17.125.5" >> /etc/resolv.conf

reference:
* Configuring Multiple Default Routes in Linux --> works for me
* 雙網卡、兩個對外ip、共同存在並能上網
* Linux 用兩張網卡作不同子網路設定

2011年8月23日 星期二

[Tips] SQLite: unable to open database file

最近寫一些小的web service需要存取SQLite,發現在CLI模式下測試正常,可是透過web存取時出現
Warning: sqlite_query() [function.sqlite-query]: unable to open database file in ...
改了code裡面存取的權限後依然不行,
sqlite_open(db, 0666, error)

最後才發現web server要有整個directory的write permission

reference: SQLite3: Unable to Open Database File

2011年8月8日 星期一

[Android] htcDev

我lag了嗎?

今年六月HTCUplinq 2011的keynote宣佈要開放SenseUI的SDK,不過今天我才發現可以下載了... 目前還有unlock的bootloader和kernel source,未來應該會提供更多的資源吧,例如他們的app?