diff --git a/rpm/SOURCES/etc/init.d/shadowsocks-rust.init b/rpm/SOURCES/etc/init.d/shadowsocks-rust.init new file mode 100644 index 000000000000..446811357bf2 --- /dev/null +++ b/rpm/SOURCES/etc/init.d/shadowsocks-rust.init @@ -0,0 +1,136 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: shadowsocks-rust +# Required-Start: $network $local_fs $remote_fs +# Required-Stop: $remote_fs +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: lightweight secured socks5 proxy +# Description: Rust port of shadowsocks. +# shadowsocks is a fast tunnel proxy that helps you bypass firewalls. +### END INIT INFO + +# Author: Simon Shi + +# PATH should only include /usr/ if it runs after the mountnfs.sh script +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC=shadowsocks-rust # Introduce a short description here +NAME=shadowsocks-rust # Introduce the short server's name here +DAEMON=/usr/bin/ssserver # Introduce the server's location here +DAEMON_ARGS="-u --log-without-time" # Arguments to run the daemon with +PIDFILE=/var/run/$NAME/$NAME.pid +SCRIPTNAME=/etc/init.d/$NAME + +# Exit if the package is not installed +[ -x $DAEMON ] || exit 0 + +# Read configuration variable file if it is present +[ -r /etc/default/$NAME ] && . /etc/default/$NAME + +[ "$START" = "yes" ] || exit 0 + +: ${USER:="nobody"} +: ${GROUP:="nogroup"} + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.0-6) to ensure that this file is present. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + # Modify the file descriptor limit + ulimit -n ${MAXFD} + + # Take care of pidfile permissions + mkdir /var/run/$NAME 2>/dev/null || true + chown "$USER:$GROUP" /var/run/$NAME + + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER:$GROUP --exec $DAEMON --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER:$GROUP --exec $DAEMON -- \ + -c "$CONFFILE" -u -f $PIDFILE $DAEMON_ARGS \ + || return 2 +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + start-stop-daemon --stop --quiet --retry=TERM/5 --pidfile $PIDFILE --exec $DAEMON + RETVAL="$?" + [ "$RETVAL" = 2 ] && return 2 + # Wait for children to finish too if this is a daemon that forks + # and if the daemon is only ever run from this initscript. + # If the above conditions are not satisfied then add some other code + # that waits for the process to drop all resources that could be + # needed by services started subsequently. A last resort is to + # sleep for some time. + start-stop-daemon --stop --quiet --oknodo --retry=KILL/5 --exec $DAEMON + [ "$?" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; +stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; +esac +;; + status) + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + restart|force-reload) + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; +*) + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + +: diff --git a/rpm/SOURCES/systemd/shadowsocks-rust-local@.service b/rpm/SOURCES/systemd/shadowsocks-rust-local@.service new file mode 100644 index 000000000000..44438c017b18 --- /dev/null +++ b/rpm/SOURCES/systemd/shadowsocks-rust-local@.service @@ -0,0 +1,19 @@ +# This file is part of shadowsocks-rust. +# +# This is a template unit file. Users may copy and rename the file into +# config directories to make new service instances. See systemd.unit(5) +# for details. + +[Unit] +Description=Shadowsocks-rust Custom Client Service for %I +Documentation=https://github.com/shadowsocks/shadowsocks-rust +After=network.target + +[Service] +Type=simple +CapabilityBoundingSet=CAP_NET_BIND_SERVICE +ExecStart=/usr/bin/sslocal --log-without-time -c /etc/shadowsocks-rust/%i.json + +[Install] +WantedBy=multi-user.target + diff --git a/rpm/SOURCES/systemd/shadowsocks-rust-server@.service b/rpm/SOURCES/systemd/shadowsocks-rust-server@.service new file mode 100644 index 000000000000..2295f7fe3946 --- /dev/null +++ b/rpm/SOURCES/systemd/shadowsocks-rust-server@.service @@ -0,0 +1,19 @@ +# This file is part of shadowsocks-rust. +# +# This is a template unit file. Users may copy and rename the file into +# config directories to make new service instances. See systemd.unit(5) +# for details. + +[Unit] +Description=Shadowsocks-rust Custom Server Service for %I +Documentation=https://github.com/shadowsocks/shadowsocks-rust +After=network.target + +[Service] +Type=simple +CapabilityBoundingSet=CAP_NET_BIND_SERVICE +ExecStart=/usr/bin/ssserver --log-without-time -c /etc/shadowsocks-rust/%i.json + +[Install] +WantedBy=multi-user.target + diff --git a/rpm/SOURCES/systemd/shadowsocks-rust.default b/rpm/SOURCES/systemd/shadowsocks-rust.default new file mode 100644 index 000000000000..a62325b48ac8 --- /dev/null +++ b/rpm/SOURCES/systemd/shadowsocks-rust.default @@ -0,0 +1,25 @@ +# Defaults for shadowsocks initscript +# sourced by /etc/init.d/shadowsocks-rust +# installed at /etc/default/shadowsocks-rust by the maintainer scripts + +# +# This is a POSIX shell fragment +# +# Note: `START', `GROUP' and `MAXFD' options are not recognized by systemd. +# Please change those settings in the corresponding systemd unit file. + +# Enable during startup? +START=yes + +# Configuration file +CONFFILE="/etc/shadowsocks-rust/config.json" + +# Extra command line arguments +DAEMON_ARGS="-u" + +# User and group to run the server as +USER=nobody +GROUP=nogroup + +# Number of maximum file descriptors +MAXFD=32768 diff --git a/rpm/SOURCES/systemd/shadowsocks-rust.service b/rpm/SOURCES/systemd/shadowsocks-rust.service new file mode 100644 index 000000000000..d80ee271d564 --- /dev/null +++ b/rpm/SOURCES/systemd/shadowsocks-rust.service @@ -0,0 +1,21 @@ +# This file is part of shadowsocks-rust. +# +# This file is default for Debian packaging. See also +# /etc/default/shadowsocks-rust for environment variables. + +[Unit] +Description=Shadowsocks-rust Default Server Service +Documentation=https://github.com/shadowsocks/shadowsocks-rust +After=network.target + +[Service] +Type=simple +EnvironmentFile=/etc/default/shadowsocks-rust +User=nobody +Group=nogroup +LimitNOFILE=32768 +ExecStart=/usr/bin/ssserver --log-without-time -c ${CONFFILE} ${DAEMON_ARGS} + +[Install] +WantedBy=multi-user.target + diff --git a/rpm/SPECS/shadowsocks-rust.spec.in b/rpm/SPECS/shadowsocks-rust.spec.in new file mode 100644 index 000000000000..0d4db133365f --- /dev/null +++ b/rpm/SPECS/shadowsocks-rust.spec.in @@ -0,0 +1,128 @@ +Name: @NAME@ +Version: @VERSION@ +Release: @RELEASE@%{?dist} +Summary: A Rust port of shadowsocks + +Group: Applications/Internet +License: GPLv3+ +URL: https://github.com/shadowsocks/%{name} +Source0: @SOURCE@ + +AutoReq: no +Conflicts: python-shadowsocks python3-shadowsocks +#BuildRequires: make gcc pcre-devel libtool mbedtls-devel libsodium-devel >= 1.0.7 libev-devel udns-devel rust cargo llvm7.0-libs +Requires: pcre libcap + + +%if 0%{?suse_version} +Requires: libopenssl1_0_0 +BuildRequires: libopenssl-devel +%else +Requires: openssl +BuildRequires: openssl-devel +%endif + +%if 0%{?fedora} >= 15 || 0%{?rhel} >=7 || 0%{?suse_version} >= 1210 +%global use_systemd 1 +%else +%global use_systemd 0 +%endif + +%if 0%{?use_systemd} +%{?systemd_requires} +%if 0%{?suse_version} +BuildRequires: systemd-rpm-macros +%else +BuildRequires: systemd +%endif +%endif + + +%description +shadowsocks-rust is a rust port of lightweight secured scoks5 proxy. + + +%prep +#%setup -q +%setup -q -n @NAME_VERSION@ + +%build +SODIUM_BUILD_STATIC=yes make TARGET=release + + +%install +mkdir -p %{buildroot}%{_initddir} +make install TARGET=release PREFIX=/usr/bin DESTDIR=%{buildroot} +mkdir -p %{buildroot}/etc/shadowsocks-rust +%if ! 0%{?use_systemd} +#mkdir -p %{buildroot}%{_initddir} +install -m 755 %{_builddir}/%{buildsubdir}/rpm/SOURCES/etc/init.d/shadowsocks-rust.init %{buildroot}%{_initddir}/shadowsocks-rust +%else +mkdir -p %{buildroot}%{_sysconfdir}/sysconfig +mkdir -p %{buildroot}%{_unitdir} +install -m 644 %{_builddir}/%{buildsubdir}/rpm/SOURCES/systemd/shadowsocks-rust.default %{buildroot}%{_sysconfdir}/sysconfig/shadowsocks-rust +install -m 644 %{_builddir}/%{buildsubdir}/rpm/SOURCES/systemd/shadowsocks-rust.service %{buildroot}%{_unitdir}/shadowsocks-rust.service +install -m 644 %{_builddir}/%{buildsubdir}/rpm/SOURCES/systemd/shadowsocks-rust-*.service %{buildroot}%{_unitdir}/ +%endif +install -m 644 %{_builddir}/%{buildsubdir}/debian/config.json %{buildroot}%{_sysconfdir}/shadowsocks-rust/config.json +%if 0%{?use_systemd} && 0%{?suse_version} +%service_add_pre shadowsocks-rust.service +%endif + +%post +%if ! 0%{?use_systemd} +/sbin/chkconfig --add shadowsocks-rust > /dev/null 2>&1 || : +%else +%if 0%{?suse_version} +%service_add_post shadowsocks-rust.service +%else +%systemd_post shadowsocks-rust.service +%endif +%endif +setcap cap_net_bind_service+ep %{_bindir}/sslocal \ + cap_net_bind_service+ep %{_bindir}/ssserver \ + +%preun +%if ! 0%{?use_systemd} +if [ $1 -eq 0 ]; then + /sbin/service shadowsocks-rust stop > /dev/null 2>&1 || : + /sbin/chkconfig --del shadowsocks-rust > /dev/null 2>&1 || : +fi +%else +%if 0%{?suse_version} +%service_del_preun shadowsocks-rust.service +%else +%systemd_preun shadowsocks-rust.service +%preif [ $1 -eq 0 ] ; then + # Package removal, not upgrade + systemctl stop shadowsocks-rust-server@'*'.service > /dev/null 2>&1 || : + systemctl stop shadowsocks-rust-local@'*'.service > /dev/null 2>&1 || : + systemctl --no-reload disable shadowsocks-rust-server@.service > /dev/null 2>&1 || : + systemctl --no-reload disable shadowsocks-rust-local@.service > /dev/null 2>&1 || : +fi +%endif +%endif + +%postun +%if 0%{?use_systemd} +%if 0%{?suse_version} +%service_del_postun shadowsocks-rust.service +%else +%systemd_postun_with_restart shadowsocks-rust.service +%systemd_postun_with_restart shadowsocks-rust-server@'*'.service +%systemd_postun_with_restart shadowsocks-rust-local@'*'.service +%endif +%endif + +%files +%{_bindir}/* +%config(noreplace) %{_sysconfdir}/shadowsocks-rust/config.json +%if ! 0%{?use_systemd} +%{_initddir}/shadowsocks-rust +%else +%{_unitdir}/shadowsocks-rust.service +%{_unitdir}/shadowsocks-rust-*.service +%config(noreplace) %{_sysconfdir}/sysconfig/shadowsocks-rust +%endif + +%changelog diff --git a/rpm/genrpm.sh b/rpm/genrpm.sh new file mode 100755 index 000000000000..5f8e3f9fec9d --- /dev/null +++ b/rpm/genrpm.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +set -e + +NAME=shadowsocks-rust + +SELF=$(readlink -f -- "$0") +HERE=$(dirname -- "$SELF") + +SOURCES="${HERE}"/SOURCES +SPEC_TEMPLATE="${HERE}"/SPECS/${NAME}.spec.in +SPEC_FILE="${SPEC_TEMPLATE%%.in}" + +GIT_VERSION=$("${HERE}"/../scripts/git_version.sh) + +OPT_OUTDIR="${HERE}/SRPMS" +OPT_USE_SYSTEM_LIB=0 +OUT_BUILD_RPM=0 + +version=$(echo ${GIT_VERSION} | cut -d' ' -f1) +release=$(echo ${GIT_VERSION} | cut -d' ' -f2) + +name_version=${NAME}-${version}-${release} +source_name=${name_version}.tar.gz + +archive() +{ + "${HERE}"/../scripts/git_archive.sh -o "${SOURCES}" -n ${name_version} +} + +build_src_rpm() +{ + rpmbuild -bs "${SPEC_FILE}" \ + --undefine "dist" \ + --define "%_topdir ${HERE}" \ + --define "%_srcrpmdir ${OPT_OUTDIR}" +} + +build_rpm() +{ + rpmbuild --rebuild "${OPT_OUTDIR}"/${name_version}.src.rpm \ + --define "%_topdir ${HERE}" \ + --define "%use_system_lib ${OPT_USE_SYSTEM_LIB}" \ + -D 'debug_package %{nil}' +} + +create_spec() +{ + sed -e "s/@NAME@/${NAME}/g" \ + -e "s/@VERSION@/${version}/g" \ + -e "s/@RELEASE@/${release}/g" \ + -e "s/@SOURCE@/${source_name}/g" \ + -e "s/@NAME_VERSION@/${name_version}/g" \ + "${SPEC_TEMPLATE}" > "${SPEC_FILE}" +} + +show_help() +{ + echo -e "$(basename $0) [OPTION...]" + echo -e "Create and build shadowsocks-libev SRPM" + echo + echo -e "Options:" + echo -e " -h show this help." + echo -e " -b use rpmbuld to build resulting SRPM" + echo -e " -s use system shared libraries (RPM only)" + echo -e " -o output directory" +} + +while getopts "hbso:" opt +do + case ${opt} in + h) + show_help + exit 0 + ;; + b) + OPT_BUILD_RPM=1 + ;; + s) + OPT_USE_SYSTEM_LIB=1 + ;; + o) + OPT_OUTDIR=$(readlink -f -- $OPTARG) + ;; + *) + show_help + exit 1 + ;; + esac +done + +create_spec +archive +build_src_rpm +if [ "${OPT_BUILD_RPM}" = "1" ] ; then + build_rpm +fi diff --git a/scripts/build_deb.sh b/scripts/build_deb.sh new file mode 100755 index 000000000000..514e710da0bd --- /dev/null +++ b/scripts/build_deb.sh @@ -0,0 +1,493 @@ +#!/bin/sh +# Copyright 2017-2018 Roger Shimizu +# +# This is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. + +help_usage() { +cat << EOT + +Build shadowsocks-libev and its dependencies +Usage: + $(basename $0) [--help|-h] [lib|bin|all] + + --help|-h Show this usage. + kcp Build kcptun package (with its dependencies) only. + lib Build library packages only. + bin Build binary packages only. + However, you need the libraries built previously, in current working directory. + For advanced user only. + all Build both binary and library packages (default). + The safe choice for everyone. + +Please run this script in a clean place. +e.g. + mkdir -p ~/build-area + cd ~/build-area + ln -s $(readlink -f $0) . + ./$(basename $0) + +EOT +exit +} + +help_lib() { +cat << EOT + +Failed to install required library: + $1 +You can try to fix it by: + + $0 lib + +EOT +exit +} + +apt_init() { + DEPS="$1" + DEPS_BPO="$2" + DEPS_EXTRA="$3" + if [ -n "$DEPS_BPO" ]; then + BPO=${OSVER}-backports + case "$OSID" in + debian) + REPO=http://deb.debian.org/debian + ;; + ubuntu) + REPO=http://archive.ubuntu.com/ubuntu + ;; + esac + sudo sh -c "printf \"deb $REPO ${OSVER}-backports main\" > /etc/apt/sources.list.d/${OSVER}-backports.list" + [ -n "$DEPS_EXTRA" -a "$DEPS_EXTRA" = "sloppy" ] && + sudo sh -c "printf \"\\ndeb $REPO ${OSVER}-backports-sloppy main\" >> /etc/apt/sources.list.d/${OSVER}-backports.list" + sudo apt-get update + sudo apt-get install --no-install-recommends -y -t $BPO $DEPS_BPO + [ -n "$DEPS_EXTRA" -a "$DEPS_EXTRA" = "sloppy" ] && + sudo apt-get install --no-install-recommends -y -t ${BPO}-sloppy $DEPS_BPO + else + sudo apt-get update + fi + sudo apt-get install -y $DEPS +} + +# Cleanup +apt_clean() { + sudo apt-get purge -y $DEPS $DEPS_BPO debhelper \ + libbloom-dev libcork-dev libcorkipset-dev libmbedtls-dev libsodium-dev + sudo apt-get purge -y libcork-build-deps libcorkipset-build-deps \ + libsodium-build-deps mbedtls-build-deps + sudo apt-get purge -y libbloom-build-deps + sudo apt-get purge -y simple-obfs-build-deps + sudo apt-get purge -y shadowsocks-libev-build-deps + +if [ $BUILD_KCP -eq 1 ]; then + sudo apt-get purge -y golang-github-klauspost-reedsolomon-build-deps \ + golang-github-xtaci-kcp-build-deps golang-github-xtaci-smux-build-deps \ + kcptun-build-deps + sudo apt-get purge -y golang-github-urfave-cli-build-deps + sudo apt-get purge -y golang-github-golang-snappy-build-deps \ + dh-golang-build-deps golang-github-pkg-errors-build-deps + + sudo apt-get purge -y golang-github-klauspost-reedsolomon-dev \ + golang-github-xtaci-kcp-dev golang-github-xtaci-smux-dev + sudo apt-get purge -y golang-github-urfave-cli-dev + sudo apt-get purge -y golang-github-pkg-errors-dev \ + golang-github-golang-snappy-dev dh-golang +fi + + sudo apt-get autoremove -y +} + +gbp_build() { + REPO=$1 + BRANCH=$2 + PROJECT_NAME=$(basename $1|sed s/\.git$//) + gbp clone --pristine-tar $REPO + cd $PROJECT_NAME + [ -n "$BRANCH" ] && git checkout $BRANCH + [ -n "$BRANCH" -a "$BRANCH" = "trusty" ] && # try to rebase the trusty patch + if ! git rebase master; then git rebase --abort; git rebase debian; fi + [ -n "$DEPS_BPO" ] && BPO_REPO="-t ${OSVER}-backports" + mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y $BPO_REPO" + rm -f ${PROJECT_NAME}-build-deps_*.deb + gbp buildpackage -us -uc --git-ignore-branch --git-pristine-tar + git clean -fdx + git reset --hard HEAD + cd - +} + +git_build() { + REPO=$1 + BRANCH=$2 + PROJECT_NAME=$(basename $1|sed s/\.git$//) + git clone $REPO + cd $PROJECT_NAME + [ -n "$BRANCH" ] && git checkout $BRANCH + mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y" + rm ${PROJECT_NAME}-build-deps_*.deb + gbp buildpackage -us -uc --git-ignore-branch + git clean -fdx + git reset --hard HEAD + cd - +} + +dsc_build() { + DSC=$1 + DSC_FILE=$(basename $1) + dget -ux $DSC + PROJECT_NAME=$(grep ^Source: $DSC_FILE|cut -d" " -f2) + echo cd ${PROJECT_NAME}-* + cd ${PROJECT_NAME}-* + mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y" + rm ${PROJECT_NAME}-build-deps_*.deb + dpkg-buildpackage -us -uc + cd - +} + +# Build and install libcork deb +build_install_libcork() { +if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then + BRANCH=$1 + if [ $BUILD_LIB -eq 1 ]; then + gbp_build https://github.com/rogers0/libcork $BRANCH + else + ls libcork-dev_*.deb libcork16_*.deb 2>&1 > /dev/null || + help_lib "libcork-dev libcork16" + fi + sudo dpkg -i libcork-dev_*.deb libcork16_*.deb +fi +} + +# Build and install libcorkipset deb +build_install_libcorkipset() { +if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then + BRANCH=$1 + if [ $BUILD_LIB -eq 1 ]; then + gbp_build https://github.com/rogers0/libcorkipset $BRANCH + else + ls libcorkipset-dev_*.deb libcorkipset1_*.deb 2>&1 > /dev/null || + help_lib "libcorkipset-dev libcorkipset1" + fi + sudo dpkg -i libcorkipset-dev_*.deb libcorkipset1_*.deb +fi +} + +# Build libmbedtls deb +build_install_libmbedtls() { +if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then + BRANCH=$1 + if [ $BUILD_LIB -eq 1 ]; then + gbp_build https://salsa.debian.org/debian/mbedtls.git $BRANCH + else + ls libmbed*.deb 2>&1 > /dev/null || + help_lib libmbedtls + fi + sudo dpkg -i libmbed*.deb +fi +} + +# Build libsodium deb +build_install_libsodium() { +if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then + if [ $BUILD_LIB -eq 1 ]; then + git clone https://github.com/gcsideal/debian-libsodium.git libsodium + cd libsodium; LIBSODIUM=$(dpkg-parsechangelog --show-field Version); cd - + dget -ud https://deb.debian.org/debian/pool/main/libs/libsodium/libsodium_${LIBSODIUM}.dsc + DHVER=$(dpkg -l debhelper|grep debhelper|awk '{print $3}'|head -n1) + cd libsodium + if dpkg --compare-versions $DHVER lt 10; then + sed -i 's/debhelper ( >= 11)/debhelper (>= 9), dh-autoreconf/' debian/control; + sed -i 's/debhelper ( >= 10)/debhelper (>= 9), dh-autoreconf/' debian/control; + echo 9 > debian/compat; + dch -D unstable -l~bpo~ "Rebuild as backports" + git add -u; + git commit -m "Patch to work with ubuntu" + elif dpkg --compare-versions $DHVER lt 11; then + sed -i 's/debhelper ( >= 11)/debhelper (>= 10)/' debian/control; + echo 10 > debian/compat; + dch -D unstable -l~bpo~ "Rebuild as backports" + git add -u; + git commit -m "Patch to work with ubuntu" + fi + mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y" + rm libsodium-build-deps_*.deb + gbp buildpackage -us -uc --git-ignore-branch --git-tarball-dir=.. --git-export-dir=.. --git-overlay + cd - + else + ls libsodium*.deb 2>&1 > /dev/null || + help_lib libsodium + fi + sudo dpkg -i libsodium*.deb +fi +} + +# Build libbloom deb +build_install_libbloom() { +if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then + BRANCH=$1 + if [ $BUILD_LIB -eq 1 ]; then + gbp_build https://salsa.debian.org/bridges-team/libbloom.git $BRANCH + else + ls libbloom-dev_*.deb libbloom1_*.deb 2>&1 > /dev/null || + help_lib "libbloom-dev libbloom1" + fi + sudo dpkg -i libbloom-dev_*.deb libbloom1_*.deb +fi +} + +# Add patch to work on system with debhelper 9 only +patch_sslibev_dh9() { +if [ $BUILD_BIN -eq 1 ]; then + BRANCH=$1 + gbp clone --pristine-tar https://salsa.debian.org/bridges-team/shadowsocks-libev.git + cd shadowsocks-libev + [ -n "$BRANCH" ] && git checkout $BRANCH + sed -i 's/dh $@/dh $@ --with systemd,autoreconf/' debian/rules + sed -i 's/debhelper (>= 10)/debhelper (>= 9), dh-systemd, dh-autoreconf/' debian/control + echo 9 > debian/compat + dch -D unstable -l~bpo~ "Rebuild as backports" + git add -u + git commit -m "Patch to work with ubuntu trusty (14.04)" + cd - +fi +} + +# Build and install shadowsocks-libev deb +build_install_sslibev() { +if [ $BUILD_BIN -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/bridges-team/shadowsocks-libev.git $BRANCH + sudo dpkg -i shadowsocks-libev_*.deb + sudo apt-get install -fy +fi +} + +# Build and install simple-obfs +build_install_simpleobfs() { +if [ $BUILD_BIN -eq 1 ]; then + BRANCH=$1 + git_build https://salsa.debian.org/bridges-team/simple-obfs.git $BRANCH + sudo dpkg -i simple-obfs_*.deb + sudo apt-get install -fy +fi +} + +# Build and install dh-golang deb +build_install_dhgolang() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/dh-golang.git $BRANCH + sudo dpkg -i dh-golang_*.deb + sudo apt-get install -fy +fi +} + +# Build and install golang-github-klauspost-reedsolomon deb +build_install_reedsolomondev() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/golang-github-klauspost-reedsolomon.git $BRANCH + sudo dpkg -i golang-github-klauspost-reedsolomon-dev_*.deb + sudo apt-get install -fy +fi +} + +# Build and install golang-github-pkg-errors deb +build_install_errorsdev() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/golang-github-pkg-errors.git $BRANCH + sudo dpkg -i golang-github-pkg-errors-dev_*.deb + sudo apt-get install -fy +fi +} + +# Add patch to work on system with xenial +patch_urfaveclidev_xenial() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp clone --pristine-tar https://salsa.debian.org/go-team/packages/golang-github-urfave-cli.git + cd golang-github-urfave-cli + [ -n "$BRANCH" ] && git checkout $BRANCH + sed -i 's/golang-github-burntsushi-toml-dev/golang-toml-dev/; s/golang-gopkg-yaml.v2-dev/golang-yaml.v2-dev/' debian/control + dch -D unstable -l~bpo~ "Rebuild as backports" + git add -u + git commit -m "Patch to work with ubuntu xenial (16.04)" + cd - +fi +} + +# Build and install golang-github-urfave-cli-dev deb +build_install_urfaveclidev() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/golang-github-urfave-cli.git $BRANCH + sudo dpkg -i build-area/golang-github-urfave-cli-dev_*.deb + sudo apt-get install -fy +fi +} + +# Build and install golang-github-golang-snappy deb +build_install_snappydev() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/golang-github-golang-snappy.git $BRANCH + sudo dpkg -i golang-github-golang-snappy-dev_*.deb + sudo apt-get install -fy +fi +} + +# Build and install golang-github-xtaci-kcp deb +build_install_kcpdev() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/golang-github-xtaci-kcp.git $BRANCH + sudo dpkg -i golang-github-xtaci-kcp-dev_*.deb + sudo apt-get install -fy +fi +} + +# Build and install golang-github-xtaci-smux deb +build_install_smuxdev() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/golang-github-xtaci-smux.git $BRANCH + sudo dpkg -i golang-github-xtaci-smux-dev_*.deb + sudo apt-get install -fy +fi +} + +# Build and install kcptun deb +build_install_kcptun() { +if [ $BUILD_KCP -eq 1 ]; then + BRANCH=$1 + gbp_build https://salsa.debian.org/go-team/packages/kcptun.git $BRANCH + sudo dpkg -i kcptun_*.deb + sudo apt-get install -fy +fi +} + +export XZ_DEFAULTS=--memlimit=128MiB + +OSID=$(grep ^ID= /etc/os-release|cut -d= -f2) +OSVER=$(lsb_release -cs) +BUILD_KCP=0 +BUILD_LIB=0 +BUILD_BIN=0 + +case "$1" in +--help|-h) + help_usage + ;; +kcp) + BUILD_KCP=1 + ;; +lib) + BUILD_LIB=1 + ;; +bin) + BUILD_BIN=1 + ;; +all|"") + BUILD_LIB=1 + BUILD_BIN=1 + ;; +*) + echo Parameter error, exiting ... + exit +esac + +# Exit if in a git repo +[ -d .git ] && help_usage + +case "$OSVER" in +jessie) + BPO="debhelper libbloom-dev libsodium-dev" + BPOEXTRA=sloppy + ;; +stretch) + BPO=libbloom-dev + ;; +xenial) + BPO=debhelper + ;; +esac +apt_init "git-buildpackage pristine-tar equivs" "$BPO" $BPOEXTRA + +[ $BUILD_KCP -eq 1 ] && case "$OSVER" in +wheezy|precise|trusty) + echo Sorry, your system $OSID/$OSVER is not supported. + ;; +jessie) + build_install_urfaveclidev + build_install_reedsolomondev + build_install_kcpdev + build_install_smuxdev + build_install_kcptun + ;; +stretch|unstable|sid|yakkety|zesty) + build_install_reedsolomondev + build_install_kcpdev + build_install_smuxdev + build_install_kcptun + ;; +xenial) + build_install_dhgolang debian/jessie-backports + build_install_reedsolomondev + build_install_errorsdev + patch_urfaveclidev_xenial + build_install_urfaveclidev + build_install_snappydev debian/jessie-backports + build_install_kcpdev + build_install_smuxdev + build_install_kcptun + ;; +esac + +[ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ] && case "$OSVER" in +wheezy|precise) + echo Sorry, your system $OSID/$OSVER is not supported. + ;; +jessie|stretch|buster|testing|unstable|sid) + build_install_sslibev + ;; +artful|bionic) + build_install_libbloom + build_install_sslibev + build_install_simpleobfs + ;; +zesty) + build_install_libsodium + build_install_libbloom + build_install_sslibev + build_install_simpleobfs + ;; +xenial|yakkety) + build_install_libcork debian + build_install_libcorkipset debian + build_install_libsodium + build_install_libbloom + build_install_sslibev + build_install_simpleobfs + ;; +trusty) + build_install_libcork trusty + build_install_libcorkipset trusty + build_install_libmbedtls debian/jessie-backports + build_install_libsodium + build_install_libbloom trusty + patch_sslibev_dh9 + build_install_sslibev + build_install_simpleobfs trusty + ;; +*) + echo Your system $OSID/$OSVER is not supported yet. + echo Please report issue: + echo " https://github.com/shadowsocks/shadowsocks-libev/issues/new" + ;; +esac + +apt_clean diff --git a/scripts/chroot_build.sh b/scripts/chroot_build.sh new file mode 100755 index 000000000000..2026e7f6a7e5 --- /dev/null +++ b/scripts/chroot_build.sh @@ -0,0 +1,125 @@ +#!/bin/sh +# Copyright 2018 Roger Shimizu +# +# This is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. + +set -e + +help_usage() { +cat << EOT + +Call build_deb.sh script in a chrooted environment +Usage: + sudo $(basename $0) [--help|-h] [codename] + + --help|-h Show this usage. + [code name] Debian/Ubuntu release codename + e.g. jessie/stretch/trusty/xenial + +EOT +exit +} + +# POSIX-compliant maint function recommend by devref +# to check for the existence of a command +# https://www.debian.org/doc/manuals/developers-reference/ch06.html#bpp-debian-maint-scripts +pathfind() { + OLDIFS="$IFS" + IFS=: + for p in $PATH; do + if [ -x "$p/$*" ]; then + IFS="$OLDIFS" + return 0 + fi + done + IFS="$OLDIFS" + return 1 +} + +case "$1" in +wheezy|precise) + echo Sorry, the system $1 is not supported. + ;; +jessie|stretch|buster|testing|unstable|sid) + OSID=debian + REPO=http://deb.debian.org/debian + ;; +trusty|yakkety|zesty|xenial|artful|bionic) + OSID=ubuntu + REPO=http://archive.ubuntu.com/ubuntu + ;; +--help|-h|*) + help_usage +esac + +if ! pathfind debootstrap; then + echo Please install debootstrap package. + exit 1 +fi + +OSVER=$1 +CHROOT=/tmp/${OSVER}-build-$(date +%Y%m%d%H%M) +TIMESTAMP0=$(date) + +mkdir -p ${CHROOT}/etc +echo en_US.UTF-8 UTF-8 > ${CHROOT}/etc/locale.gen +if ! debootstrap --variant=minbase --include=ca-certificates,git,sudo,wget,whiptail --exclude=upstart,systemd $OSVER $CHROOT $REPO; then + echo debootstrap failed. Please kindly check whether proper sudo or not. + exit 1 +fi +case "$OSID" in +debian) + echo deb $REPO ${OSVER} main > ${CHROOT}/etc/apt/sources.list + echo deb $REPO ${OSVER}-updates main >> ${CHROOT}/etc/apt/sources.list + echo deb $REPO-security ${OSVER}/updates main >> ${CHROOT}/etc/apt/sources.list + ;; +ubuntu) + echo deb $REPO $OSVER main universe > ${CHROOT}/etc/apt/sources.list + echo deb $REPO ${OSVER}-updates main universe >> ${CHROOT}/etc/apt/sources.list + echo deb $REPO ${OSVER}-security main universe >> ${CHROOT}/etc/apt/sources.list + ;; +esac + +cat << EOL | chroot $CHROOT +apt-get purge -y udev +apt-get update +apt-get -fy install +apt-get -y upgrade +apt-get -y install --no-install-recommends lsb-release +# dh_auto_test of mbedtls (faketime) depends on /dev/shm. https://bugs.debian.org/778462 +mkdir -p ~ /dev/shm +mount tmpfs /dev/shm -t tmpfs + +date > /TIMESTAMP1 +git config --global user.email "script@example.com" +git config --global user.name "build script" +if [ -n "$http_proxy" ]; then + git config --global proxy.http $http_proxy + echo Acquire::http::Proxy \"$http_proxy\"\; > /etc/apt/apt.conf + export http_proxy=$http_proxy + export https_proxy=$https_proxy + export no_proxy=$no_proxy +fi +cd /tmp +wget https://raw.githubusercontent.com/shadowsocks/shadowsocks-libev/master/scripts/build_deb.sh +chmod 755 build_deb.sh +./build_deb.sh +date > /TIMESTAMP2 +./build_deb.sh kcp +umount /dev/shm +EOL + +TIMESTAMP1=$(cat ${CHROOT}/TIMESTAMP1) +TIMESTAMP2=$(cat ${CHROOT}/TIMESTAMP2) +TIMESTAMP3=$(date) + +printf \\n"All built deb packages:"\\n +ls -l ${CHROOT}/tmp/*.deb +echo +echo Start-Time: $TIMESTAMP0 +echo ChrootDone: $TIMESTAMP1 +echo SsDeb-Done: $TIMESTAMP2 +echo \ Kcp-Done : $TIMESTAMP3 diff --git a/scripts/code-format.bat b/scripts/code-format.bat new file mode 100644 index 000000000000..f51e20bc7045 --- /dev/null +++ b/scripts/code-format.bat @@ -0,0 +1,35 @@ +@echo off + +set root=%~dp0 +set source=%root%src + +goto start + +:format +set filelist=%1 +for /r "%filelist%" %%f in (*) do ( + if "%%~xf" equ ".h" ( + call :format_file %%f + ) else if "%%~xf" equ ".c" ( + call :format_file %%f + ) +) +goto end + +:format_file +set f=%1 +if "%~n1" neq "base64" ( + if "%~n1" neq "json" ( + if "%~n1" neq "uthash" ( + echo 'format file "%f%"' + uncrustify -c %root%\.uncrustify.cfg -l C --replace --no-backup %f% + DEL %~dp1*.uncrustify >nul 2>nul + ) + ) +) +goto end + +:start +call :format %source% + +:end diff --git a/scripts/code-format.sh b/scripts/code-format.sh new file mode 100755 index 000000000000..3f4ed536a23c --- /dev/null +++ b/scripts/code-format.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +root=$(pwd) +source="$root"/src + +function format() { + filelist=$(ls "$1") + pushd "$1" + for file in $filelist; do + if test -d "$file"; then + echo "format directory $file" + format "$file" + else + if ([ "${file%%.*}" != "base64" ] && + [ "${file%%.*}" != "json" ] && + [ "${file%%.*}" != "uthash" ]) && + ([ "${file##*.}" = "h" ] || [ "${file##*.}" = "c" ]); then + echo "format file $file" + uncrustify -c "$root"/.uncrustify.cfg -l C --replace --no-backup "$file" + rm ./*.uncrustify >/dev/null 2>&1 + fi + fi + done + popd +} + +format "$source" diff --git a/scripts/git_archive.sh b/scripts/git_archive.sh new file mode 100755 index 000000000000..aa932df063d2 --- /dev/null +++ b/scripts/git_archive.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +set -e + +archive() { + export TARBALL_NAME=$1 + export TARBALL_OUTDIR=$2 + + # archive this repo + cd "$(git rev-parse --show-toplevel)" + git archive HEAD --format=tar --prefix="${TARBALL_NAME}/" \ + -o "${TARBALL_OUTDIR}/${TARBALL_NAME}.tar" + # archive submodules + git submodule update --init + git submodule foreach --quiet 'git archive HEAD --format=tar \ + --prefix="${TARBALL_NAME}/${path}/" \ + -o "${TARBALL_OUTDIR}/${TARBALL_NAME}-submodule-${path}-${sha1}.tar" + tar -n --concatenate --file="${TARBALL_OUTDIR}/${TARBALL_NAME}.tar" \ + "${TARBALL_OUTDIR}/${TARBALL_NAME}-submodule-${path}-${sha1}.tar"' + gzip -c "${TARBALL_OUTDIR}/${TARBALL_NAME}.tar" > "${TARBALL_OUTDIR}/${TARBALL_NAME}.tar.gz" + + # clean-up + git submodule foreach --quiet 'rm ${TARBALL_OUTDIR}/${TARBALL_NAME}-submodule-${path}-${sha1}.tar' + rm "${TARBALL_OUTDIR}/${TARBALL_NAME}.tar" +} + +TARGET_TARBALL_NAME=shadowsocks-libev +TARGET_TARBALL_DIR=$(git rev-parse --show-toplevel) + +while getopts "n:o:" opt +do + case ${opt} in + o) + TARGET_TARBALL_DIR=$(readlink -f -- $OPTARG) + ;; + n) + TARGET_TARBALL_NAME=$OPTARG + ;; + \?) + exit 1 + ;; + esac +done + +archive "${TARGET_TARBALL_NAME}" "${TARGET_TARBALL_DIR}" diff --git a/scripts/git_version.sh b/scripts/git_version.sh new file mode 100755 index 000000000000..272ae145e5bc --- /dev/null +++ b/scripts/git_version.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -e + +# determine version and release number +GIT_DESCRIBE=$(git describe --tags --match 'v*' --long) +# GIT_DESCRIBE is like v3.0.3-11-g1e3f35c-dirty +if [[ ! "$GIT_DESCRIBE" =~ ^v([^-]+)-([0-9]+)-g([0-9a-f]+)$ ]]; then + >&2 echo 'ERROR - unrecognized `git describe` output: '"$GIT_DESCRIBE" + exit 1 +fi + +version=${BASH_REMATCH[1]} +commits=${BASH_REMATCH[2]} +short_hash=${BASH_REMATCH[3]} + +release=1 +if [ "${commits}" -gt 0 ] ; then + release+=.${commits}.git${short_hash} +fi + +echo "${version} ${release}" diff --git a/scripts/iperf.sh b/scripts/iperf.sh new file mode 100755 index 000000000000..ac235fceaa1e --- /dev/null +++ b/scripts/iperf.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +number=$1 +method=$2 + +ss-tunnel -k test -m $method -l 8387 -L 127.0.0.1:8388 -s 127.0.0.1 -p 8389 & +ss_tunnel_pid=$! +ss-server -k test -m $method -s 127.0.0.1 -p 8389 & +ss_server_pid=$! + +iperf -s -p 8388 & +iperf_pid=$! + +sleep 1 + +iperf -c 127.0.0.1 -p 8387 -n $number + +# Wait for iperf server to receive all data. +# One second should be enough in most cases. +sleep 1 + +kill $ss_tunnel_pid +kill $ss_server_pid +kill $iperf_pid + +sleep 1 +echo "Test Finished"