My Blog, written in golang!

交叉编译shadowsocks-libev

发表于 2017/02/07 21:40  |  分类于 dd-wrt

在版本 2.6.1 中, shadowsocks-libev 加入了插件系统,可以使用 simple-obfs 插件来进行流量混淆。

而 entware-ng 中 shadowsocks-libev 的版本号是 2.4.5,已经是发布于一年前的“老版本”了。

为了使用这一新的特性,需要交叉编译适用于 dd-wrt 的 shadowsocks-libev


shadowsocks-libev 的最新版本 3.0.2 废弃了对 OpenSSL 和 PolarSSL 的支持,只支持 mbedTLS。

需要编译的程序有 mbedTLSpcrelibsodiumlibevlibudnsshadowsocks-libev 以及 simple-obfs

用到的交叉编译工具是梅林的 hndtools-arm-linux-2.6.36-uclibc-4.5.3 ,放在 /projects/hnd/tools/linux/ 目录下。

假设 1. 已经将 /projects/hnd/tools/linux/hndtools-arm-linux-2.6.36-uclibc-4.5.3/bin 加入到了 path

假设 2. 交叉编译后的文件会安装到 /projects/dists/[project] 中(configure--prefix 选项)

下面,直接给出用到的编译命令(所有的编译都是静态编译):

  1. 编译 mbedTLS

    ver=2.4.0
    wget --no-check-certificate https://tls.mbed.org/download/mbedtls-$ver-gpl.tgz
    tar zxf mbedtls-$ver-gpl.tgz
    cd mbedtls-$ver
    sed -i "s/DESTDIR=\/usr\/local/DESTDIR=\/projects\/dists\/mbedtls/g" Makefile
    CC=arm-linux-gcc AR=arm-linux-ar LD=arm-linux-ld LDFLAGS=-static make install
    
  2. 编译 pcre

    ver=8.40
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$ver.tar.gz
    tar zxf pcre-$ver.tar.gz
    cd pcre-$ver
    ./configure --host=arm-linux --prefix=/projects/dists/pcre --disable-shared --enable-utf8 --enable-unicode-properties
    make && make install
    
  3. 编译 libsodium

    ver=1.0.11
    wget --no-check-certificate https://download.libsodium.org/libsodium/releases/libsodium-$ver.tar.gz
    tar zxf libsodium-$ver.tar.gz
    cd libsodium-$ver
    ./configure --host=arm-linux --prefix=/projects/dists/libsodium --disable-ssp --disable-shared
    make && make install
    
  4. 编译 libev

    ver=4.24
    wget http://dist.schmorp.de/libev/libev-$ver.tar.gz
    tar zxf libev-$ver.tar.gz
    cd libev-$ver
    ./configure --host=arm-linux --prefix=/projects/dists/libev --disable-shared
    make && make install
    
  5. 编译 libudns

    git clone https://github.com/shadowsocks/libudns
    cd libudns
    ./autogen.sh
    ./configure --host=arm-linux --prefix=/projects/dists/libudns
    make && make install
    
  6. 编译 shadowsocks-libev

    ver=3.0.2
    git clone https://github.com/shadowsocks/shadowsocks-libev
    cd shadowsocks-libev
    git checkout v$ver -b v$ver
    git submodule init && git submodule update
    ./autogen.sh
    LIBS="-lpthread -lm" LDFLAGS="-Wl,-static -static -static-libgcc -L/projects/dists/libudns/lib -L/projects/dists/libev/lib" CFLAGS="-I/projects/dists/libudns/include -I/projects/dists/libev/include" ./configure --host=arm-linux --prefix=/projects/dists/shadowsocks-libev --disable-ssp --disable-documentation --with-mbedtls=/projects/dists/mbedtls --with-pcre=/projects/dists/pcre --with-sodium=/projects/dists/libsodium
    make && make install
    
  7. 编译 simple-obfs

    ver=0.0.2
    git clone https://github.com/shadowsocks/simple-obfs
    cd simple-obfs
    git checkout v$ver -b v$ver
    git submodule init && git submodule update
    ./autogen.sh
    LIBS="-lpthread -lm" LDFLAGS="-Wl,-static -static -static-libgcc -L/projects/dists/libsodium/lib -L/projects/dists/libudns/lib -L/projects/dists/libev/lib" CFLAGS="-I/projects/dists/libsodium/include -I/projects/dists/libudns/include -I/projects/dists/libev/include" ./configure --host=arm-linux --prefix=/projects/dists/shadowsocks-libev --disable-ssp --disable-documentation
    make && make install
    

现在,可以在 /projects/dists/shadowsocks-libev/bin 下找到编译好后的文件了:

# ls -l /projects/dists/shadowsocks-libev/bin
total 8836
-rwxr-xr-x 1 user group  605031 Feb  7 23:30 obfs-local
-rwxr-xr-x 1 user group  711113 Feb  7 23:30 obfs-server
-rwxr-xr-x 1 user group 1893284 Feb  7 23:30 ss-local
-rwxr-xr-x 1 user group  579501 Feb  7 23:30 ss-manager
-rwxr-xr-x 1 user group    5388 Feb  7 23:30 ss-nat
-rwxr-xr-x 1 user group 1842120 Feb  7 23:30 ss-redir
-rwxr-xr-x 1 user group 2000710 Feb  7 23:30 ss-server
-rwxr-xr-x 1 user group 1394290 Feb  7 23:30 ss-tunnel

发现静态编译后的文件还是比较大的,使用 stripupx 压缩下(ss-nat 是 shell 文件,排除掉):

find /projects/dists/shadowsocks-libev/bin ! -name 'ss-nat' -type f | xargs arm-uclibc-strip
find /projects/dists/shadowsocks-libev/bin ! -name 'ss-nat' -type f | xargs upx

再看一下文件大小:

# ls -l /projects/dists/shadowsocks-libev/bin
total 1876
-rwxr-xr-x 1 user group 124196 Feb  7 23:39 obfs-local
-rwxr-xr-x 1 user group 137476 Feb  7 23:39 obfs-server
-rwxr-xr-x 1 user group 394784 Feb  7 23:39 ss-local
-rwxr-xr-x 1 user group 127080 Feb  7 23:39 ss-manager
-rwxr-xr-x 1 user group   5388 Feb  7 23:30 ss-nat
-rwxr-xr-x 1 user group 389684 Feb  7 23:39 ss-redir
-rwxr-xr-x 1 user group 408524 Feb  7 23:39 ss-server
-rwxr-xr-x 1 user group 311656 Feb  7 23:39 ss-tunnel

最后看一下文件类型:

# file /projects/dists/shadowsocks-libev/bin/ss-local
/projects/dists/shadowsocks-libev/bin/ss-local: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), statically linked, stripped

确实是静态编译的 arm 版本文件。


20170412 更新:

一键编译脚本 https://gist.github.com/harv/dd0bcd5eba533cbc267f6a0aaf6bee62

Useage: sh ./build_shadowsocks-libev.sh [--host=<host>] [--prefix=<path>]

Options:
     --host=<host>    the machine that you are building for
     --prefix=<path>  install architecture-independent files in prefix[./dists]