2016年4月23日 星期六

debian 7 部署 gfw.press + 3proxy 輕鬆翻牆

前言

twitter上有一用戶名為趙國劣紳石斑魚大爺@chinashiyu開發了新一代翻牆工具:
gfw.press https://github.com/chinashiyu/gfw.press
推上高手@FWT_T_O_O分析了源碼 https://goo.gl/pYWnmY
使得我甚感興趣,經過在推上和2人交流後,決定嘗試部署一下

部署3proxy工作

測試部署的系統為debian 7 x86 ,vps為openvz 128/128 vswap nat ipv4 share vps
1)先執行系統更新 apt-get update && apt-get upgrade -y
2)安裝需要的元件
   apt-get install nano curl build-essential libssl-dev openjdk-7-jre git -y
3)下載gfw.press和3proxy源碼
   git clone https://github.com/z3APA3A/3proxy.git
   git clone https://github.com/chinashiyu/gfw.press.git
4)轉入3proxy目錄
   cd 3proxy/
5)編譯3proxy
   make -f Makefile.Linux  等待完成
6) 轉入下一層src目錄
   cd src
7) mkdir -p /usr/local/etc/3proxy/bin/  製造新目錄
8) 安裝3proxy到系統
    install 3proxy /usr/local/etc/3proxy/bin/3proxy
    install mycrypt /usr/local/etc/3proxy/bin/mycrypt
9)制造3proxy.cfg
    touch /usr/local/etc/3proxy/3proxy.cfg
10) 製造3proxy的log文件目錄
     mkdir -p /usr/local/etc/3proxy/log/
11)設置權限
     chown -R root:root /usr/local/etc/3proxy/
     chown -R 65535 /usr/local/etc/3proxy/log/
     touch /usr/local/etc/3proxy/3proxy.pid
     chown 65535 /usr/local/etc/3proxy/3proxy.pid
12)編輯3proxy.cfg
     nano /usr/local/etc/3proxy/3proxy.cfg  內容如下


-------------begin------this line is not the part of 3proxy.cfg-----------


# configure nameserver and nscache which good to save speed, traffic and bandwidth
# Important -> DNS-Server must work. At the moment the Free Google DNS is configured
nscache 8192
nserver 8.8.8.8
timeouts 1 5 30 60 180 1800 15 60

#specify the startup mode as Deamon
daemon

#write pid of current process to file. It can be used to manipulate 3proxy with signals under Unix.
pidfile 3proxy.pid

#Path to configuration file to use on 3proxy restart or to save configuration.
config 3proxy.cfg

#If file monitored changes in modification time or size,
# proxy reloads configuration within one minute.
monitor 3proxy.cfg

# log allows to specify log file location and rotation, D means logfile is created daily
log log/3proxy.log D
logformat "L%d-%m-%Y %H:%M:%S %z %N.%p %E %U %C:%c %R:%r %O %I %h %T"

# We will keep last 30 log files
rotate 30

# user authentication is not needed, we catch tariffic from gfw.press only
# gfw.press has password to auth
auth none

# and allow HTTP and HTTPS traffic.
allow * * * 80-88,8080-8088 HTTP
allow * * * 443,8443 HTTPS

# Sets the proxy on port 3128 with high anonymous flag -a
# You need root priviliges to bind to a port lower than 1000
proxy -i127.0.0.1 -a -p3128
#socks -i127.0.0.1 -a -p1080
flush

# now we needn't any root rights. We can chroot and setgid/setuid.
chroot /usr/local/etc/3proxy/
setgid 65535
setuid 65535


-------------end of 3proxy.cfg--------------------------------------

13)轉到/etc/init.d/目錄,我們需要寫個開機啟動腳本
     cd /etc/init.d/
     nano 3proxyinit  , 腳本內容如下


-----------begin 3proxyinit------------------------------------------

#!/bin/sh
#
### BEGIN INIT INFO
# Provides: 3Proxy
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Initialize 3proxy server
# Description: starts 3proxy
### END INIT INFO

cd /usr/local/etc/3proxy/
case "$1" in
start)
echo Starting 3Proxy

/usr/local/etc/3proxy/bin/3proxy /usr/local/etc/3proxy/3proxy.cfg
;;
stop)
echo Stopping 3Proxy
kill `pidof 3proxy`
;;
*)
echo Usage: \$0 "{start|stop}"
exit 1
esac
exit 0


------------end of 3proxyinit---------------------------

14)使其成為執行檔
     chmod +x 3proxyinit
     update-rc.d 3proxyinit defaults
15)運行3proxy
     cd
    /etc/init.d/3proxyinit start

如需要停止便使用  /etc/init.d/3proxyinit stop

以上參考 http://www.tor.eu/?p=685
3proxy的安裝完成,接下來設置gfw.press

1) 轉到gfw.press目錄
    cd gfw.press
2) 編輯user.txt
    nano user.txt ,  其內容是

   10006 ChangeMe1
   10007 ChangeMe2
   10008 ChangeMe3

前面的是端口,後面的是密碼,更改為你喜歡的,格式是端口 空格一個 密碼,每條一行

3)把server.sh改成可執行文件
    chmod +x server.sh
4) 執行server.sh
    ./server.sh &
5) 檢查是否在協同工作
    netstat -nlp | grep -i listen

--------scriptshot--------------------------------------------------

root@inethome:~/gfw.press# netstat -nlp | grep -i listen
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3048/sshd      
tcp        0      0 127.0.0.1:3128          0.0.0.0:*               LISTEN      9572/3proxy    
tcp6       0      0 :::19501                :::*                    LISTEN      9585/java      
tcp6       0      0 :::19502                :::*                    LISTEN      9585/java      
tcp6       0      0 :::19503                :::*                    LISTEN      9585/java      
tcp6       0      0 :::19504                :::*                    LISTEN      9585/java      
tcp6       0      0 :::19505                :::*                    LISTEN      9585/java      
tcp6       0      0 :::22                   :::*                    LISTEN      3048/sshd    

---------end---------------------------------------------------------------------------

正常會是這樣,java的進程看開了多少個端口,我只開了5個,因為vps太渣
服務器部署工作已完成

客戶端安裝(window 10)

http://gfw.press/GFW.Press.msi 下載客戶端,雙擊安裝
chrome 使用SwitchyOmega新建一個情景模式,選http協議
代理伺服器 127.0.0.1 代理埠 3128

運行gfw.press客戶端,填入相關資訊,按確定,chrome轉為代理模式上網
訪問http://whatismyproxy.com  看看是否IP轉變了

後記

感謝推友@FWT_T_O_O解答疑問,感謝趙國劣紳石斑魚大爺@chinashiyu無私奉獻
據說可設為socks代理,只需在gfw.press目錄下增加一個server.json

內容如是

{
    "ProxyHost": 127.0.0.1,
    "ProxyPort": 1080,
 }

在3proxy.cfg中把

#socks -i127.0.0.1 -a -p1080 改為
socks -i127.0.0.1 -a -p1080
我已預先寫了,但時間關係沒有測試,有興趣的推油自行測試

:)


2016年4月9日 星期六

stunnel+tinyproxy一鍵腳本

在第一個網誌寫了安裝stunnel + tinyproxy的流程,竟然有數百網友圍觀,出乎意料,
於是有了動力,決定編寫一鍵安裝腳本,方便需要的網友,也正好自己更換/新購vps
不用每次重複勞動,一舉两得 :) 代碼如下

[code]
#! /bin/bash
# author: twfcc@twitter
# $PROG: install_sslproxy.sh
# $Usage: $0 -s|--standard -n|--nat
# description: install stunnel+tinyproxy on NAT IPv4 Share VPS(OpenVZ) or Standard VPS(OpenVZ)
# Public Domain use as your own risk!

Usage="$0 -s|--standard -n|--nat"
msg1="-s or --standard install https proxy on standard openvz vps."
msg2="-n or --nat install https proxy on nat ipv4 share openvz vps"
argu=$1

# Are you root?

if [ $UID -ne 0 ] ; then
    echo "You must be root to execute this script." >&2
    exit 5
fi

# Check argument

case "$argu" in
     -s|--standard) flag=0 ;;
          -n|--nat) flag=1 ;;
                 *) printf "\n$Usage\n$msg1\n$msg2\n\n" >& 2
                    exit 6
                    ;;
esac

# First, we need to install some component and update OS
apt-get update && apt-get upgrade -y
apt-get install build-essential -y
if ! which openssl &> /dev/null ; then
     apt-get install openssl -y
fi
if ! which curl &> /dev/null ; then
    apt-get install curl -y
fi

# get vps public IPv4
myip=$(curl -s v4.ifconfig.co)

# what is the type of this vps?

if [ $flag -ne 0 ] ; then
     internal_ip=$(ifconfig venet0:0 \
                  | awk -F: '$2 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/{print $2}' \
                  | cut -d" " -f1)
     port=$(echo -e ${internal_ip##*.})20 #nat-vps we use the last fixed port  
else
     pick=($(for i in {19901..19999} ;do echo $i ;done)) #ports from 19901-19999
     count=${#pick[@]} #how many ports
     port=${pick[$((RANDOM%count-1))]} #pick a random port
fi

# install stunnel and tinyproxy

apt-get install stunnel tinyproxy -y

# edit /etc/tinyproxy.conf ; just listen local port
tinyconf="/etc/tinyproxy.conf"
mv -f "$tinyconf" "$tinyconf".bak # backup orginal conf
sed 's/#Listen 192.168.0.1/Listen 127.0.0.1/' $tinyconf.bak > $tinyconf

service tinyproxy restart # restart tinyproxy

# setup stunnel ; generate self-signed certificate first
openssl genrsa -out privatekey.pem 2048
openssl req -new -x509 -key privatekey.pem -subj \
"/C=CN/ST=MyTunnel/L=Mytunnel/O=$myip/CN=$myip" \
-out publickey.pem -days 1095

#copy both pem together and make a crt for browser

if [ -e privatekey.pem ] && [ -e publickey.pem ] ; then
   cat privatekey.pem publickey.pem > /etc/stunnel/stunnel.pem
   cat publickey.pem > publickey.crt
fi

# we create stunnel.conf

cat >stunnel.conf<<EOF
client = no
debug = 7
output = /var/log/stunnel4/stunnel.log
[tinyproxy]
accept = $port
connect = 127.0.0.1:8888
cert = /etc/stunnel/stunnel.pem

EOF

mv -f stunnel.conf /etc/stunnel/

# set stunnel as a deamon

cp -f /etc/default/stunnel4 /etc/default/stunnel4.bak
sed -i 's/^ENABLED=0$/ENABLED=1/' /etc/default/stunnel4
service stunnel4 restart
if netstat -nlp | grep -i 'tinyproxy' &> /dev/null \
   && netstat -nlp | grep -i 'stunnel4' &> /dev/null
then
   printf "HTTPS/SSL Proxy is running.\nCopy publickey.crt to browser.\n"
   printf "\nYour IPv4:${myip}\n Port:${port}\nEnjoy!\n"
else
   printf "Failed to install HTTPS/SSL proxy.\n" >&2
   exit 1
fi
exit 0
[/code]

已在nat ipv4 share vps的debian 7/8 x86_64,ubuntu 14.04 x86,普通的vps
ubunbu 1404 x86,debian 8 x86_64成功通過測試,失敗的只有ubuntu 15.05,
這腳本會在nat-vps會選用供應商分配給你的20個端口最後一個:XXX20,前面的三
個數字是你的內網ip最後一組數字,腳本會自動找到,標準vps它會在19901-19999
之間隨機選一個端口,這避免太多人使用腳本安裝變成固定端口(其實沒幾人用,多餘
的做法),安裝前兼查一下該端口/該一段端口有否被佔用中,不然會錯誤


最後付上腳本下載連結
礙於編程功力,代碼非常難看,歡迎散播,改進,詢問和建議
我的twitter id: @twfcc

2016年4月7日 星期四

搭建stunnel配合tinyproxy,輕鬆翻過防火長城

搭建stunnel配合tinyproxy,輕鬆翻過防火長城

stunnel是一個老牌加密ssl wrapper,它能把客戶與服務器端的通訊加密,tinyproxy是一個輕量級的http代理,它也支援CONNECT的方式,能夠處理https的連線,而且使用的資源很少,服務器啟動只使用2M左右的記憶體,而且速度飛快


以下為部署過程,安裝在最為低階的NAT IPv4 share VPS,128M/128M vSwap OpenVZ虛擬方案,系統是Ubuntu 14.04 LTS 32bit

1) 登錄vps,以root執行apt-get update -y && apt-get upgrade -y , 確保系統軟件為最新版本
2) 執行apt-get install tinyproxy -y
3) 編輯/etc/tinyproxy.conf
    a) 找到Port 8888 ,可以改成你喜歡的端口或者不改動,我沒有改動的
    b) 往下找到#Listen 192.168.0.1 ,在其下面新增一行Listen 127.0.0.1,
        因為我們只需監聽本地通訊
    c) 再往下找看到Timeout 600,可以改成300,這個隨你喜歡
    d) 往下找到LogLevel Info,改成LogLevel Connect,避免看到頭痛
    e) 往下找到MaxClients 100,因應需要可改或不改動,我設為150
    f) 繼續往下,看到MinSpareServers 5和MaxSpareServers 20,可因應需要改動,
        我用預設的值
    g) StartServers 10和MaxRequestsPerChild 0建議使用預設的值可以了
    h) 檢查Allow 127.0.0.1前面是否有#,如有把它去掉,預設只能是本地通訊
    i) 儲存文件,並重啟服務,執行service tinyproxy restart
至此已配置好了tinyproxy,接下來安裝stunnel4和配置協同工作

安裝stunnel4的流程如下

1) 執行 apt-get install stunnel4
2) 生成私鑰,執行openssl genrsa -out privatekey.pem 2048
3) 生成公鑰,執行openssl req -new -x509 -key privatekey.pem -out publickey.pem -days 3650
    它會詢問一些資料如地區,那些亂填就好,到第四項組織和CN(Common Name)填入
     vps公網IPv4 ,電郵地址直接enter鍵,自簽名證書已製作好
4) 合併證書,cat privatekey.pem publickey.pem >> /etc/stunnel/stunnel.pem
5) 新建一個配置文件,/etc/stunnel/stunnel.conf ,內容如下

client = no
debug = 7
output = /var/log/stunnel4/stunnel.log
[tinyproxy]
accept = 16920
connect = 127.0.0.1:8888
cert = /etc/stunnel/stunnel.pem

其中accept = 16920 是stunnel4對外監聽連接端口,改成你喜歡的,而
connect = 127.0.0.1:8888 必須和tinyproxy監聽的本地端口一致

其它的設定如降低權限,請參考 http://www.arda.homeunix.net/stunnel-setup/

6) 編輯/etc/default/stunnel4 ;把 ENABLED=0 改為 1,儲存離開
7) 執行service stunnel4 restart

stunnel4 和 tinyproxy 已經協同工作,再下來配置客戶端,以Chromium為例

1) 執行cat publickey.pem > publickey.crt
2) 把publickey.crt複製到本地電腦,以linux可以 scp -P vps_ssh_port root@vps_ip:~/publickey.crt .
3) 打開設定->顯示進階設定->管理憑證->選"授權單位"->按"滙入"->把publickey.crt導入即可
4) 下載SwitchyOmega,新建一個情景模式,選代理協議為HTTPS,代理伺服器填入vps的IPv4,
     代理埠填入stunnel4的監聽端口,儲存完成
5) 切換到代理模式,打開 http://whatismyproxy.com 看看是否IP變了

雜項

如要分流,請參考 https://github.com/FelisCatus/SwitchyOmega/wiki/GFWList ,
iOS的Surge的設置自行google

如果出現問題,可以在/var/log/找到相關訊息,或者執行netstat -nlp看看是否服務器都在監聽中

:)