From df181a30659867653270d7306780da5aa7c03a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=A1=A0=E1=A0=B5=E1=A1=A0=E1=A1=B3=20=E1=A1=A0=E1=A0=B5?= =?UTF-8?q?=E1=A1=A0=20=E1=A0=AE=E1=A0=A0=E1=A0=A8=E1=A1=A9=E1=A0=8B?= =?UTF-8?q?=E1=A0=A0=E1=A0=A8?= <125150101+UjuiUjuMandan@users.noreply.github.com> Date: Sun, 5 Jan 2025 11:20:23 +0800 Subject: [PATCH] migranting libtun2socks from 2dust/AndroidLibV2rayLite (#90) --- .gitmodules | 6 ++ Makefile | 31 +++++++ badvpn | 1 + compile-tun2socks.sh | 41 +++++++++ libancillary | 1 + tun2socks.mk | 137 ++++++++++++++++++++++++++++++ ubuntu-cli-install-android-sdk.sh | 79 +++++++++++++++++ 7 files changed, 296 insertions(+) create mode 100644 .gitmodules create mode 100644 Makefile create mode 160000 badvpn create mode 100755 compile-tun2socks.sh create mode 160000 libancillary create mode 100644 tun2socks.mk create mode 100644 ubuntu-cli-install-android-sdk.sh diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..83b4a2a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "libancillary"] + path = libancillary + url = https://github.com/shadowsocks/libancillary +[submodule "badvpn"] + path = badvpn + url = https://github.com/XTLS/badvpn.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a4e1bce --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +pb: + go get -u github.com/golang/protobuf/protoc-gen-go + @echo "pb Start" +asset: + mkdir -p assets data + bash gen_assets.sh download + cp -v data/*.dat assets/ + #cd assets;curl https://raw.githubusercontent.com/2dust/AndroidLibV2rayLite/master/data/geosite.dat > geosite.dat + #cd assets;curl https://raw.githubusercontent.com/2dust/AndroidLibV2rayLite/master/data/geoip.dat > geoip.dat + +fetchDep: + go get -v golang.org/x/mobile/cmd/... + mkdir -p $(shell go env GOPATH)/src/v2ray.com/core + git clone https://github.com/v2fly/v2ray-core.git $(shell go env GOPATH)/src/v2ray.com/core + go get -d github.com/2dust/AndroidLibV2rayLite + +ANDROID_HOME?=$(HOME)/android-sdk-linux +export ANDROID_HOME +PATH:=$(PATH):$(GOPATH)/bin +export PATH +downloadGoMobile: + cd ~ ;curl -L https://raw.githubusercontent.com/2dust/AndroidLibV2rayLite/master/ubuntu-cli-install-android-sdk.sh | sudo bash - + ls ~ + ls ~/android-sdk-linux/ + +BuildMobile: + gomobile init + gomobile bind -androidapi 19 -v -ldflags='-s -w' github.com/2dust/AndroidLibV2rayLite + +all: asset pb fetchDep + @echo DONE diff --git a/badvpn b/badvpn new file mode 160000 index 0000000..c440609 --- /dev/null +++ b/badvpn @@ -0,0 +1 @@ +Subproject commit c440609ca7fb8b85d83d528526b3f02c1a63376d diff --git a/compile-tun2socks.sh b/compile-tun2socks.sh new file mode 100755 index 0000000..467eb65 --- /dev/null +++ b/compile-tun2socks.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -o errexit +set -o pipefail +set -o nounset + +# Set magic variables for current file & dir +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" +__base="$(basename ${__file} .sh)" + +if [[ ! -d $NDK_HOME ]]; then + echo "Android NDK: NDK_HOME not found. please set env \$NDK_HOME" + exit 1 +fi + +TMPDIR=$(mktemp -d) +clear_tmp () { + rm -rf $TMPDIR +} + +trap 'echo -e "Aborted, error $? in command: $BASH_COMMAND"; trap ERR; clear_tmp; exit 1' ERR INT +install -m644 $__dir/tun2socks.mk $TMPDIR/ + +pushd $TMPDIR +ln -s $__dir/badvpn badvpn +ln -s $__dir/libancillary libancillary +$NDK_HOME/ndk-build \ + NDK_PROJECT_PATH=. \ + APP_BUILD_SCRIPT=./tun2socks.mk \ + APP_ABI=all \ + APP_PLATFORM=android-19 \ + NDK_LIBS_OUT=$TMPDIR/libs \ + NDK_OUT=$TMPDIR/tmp \ + APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -j4 \ + LOCAL_LDFLAGS=-Wl,--build-id=none + +tar cvfz $__dir/libtun2socks.so.tgz libs +popd + +rm -rf $TMPDIR diff --git a/libancillary b/libancillary new file mode 160000 index 0000000..232d69a --- /dev/null +++ b/libancillary @@ -0,0 +1 @@ +Subproject commit 232d69a5ebb4461b572bd3f3b97088091e01c243 diff --git a/tun2socks.mk b/tun2socks.mk new file mode 100644 index 0000000..10cc033 --- /dev/null +++ b/tun2socks.mk @@ -0,0 +1,137 @@ +# Copyright (C) 2009 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +LOCAL_PATH := $(call my-dir) +ROOT_PATH := $(LOCAL_PATH) + +######################################################## +## libancillary +######################################################## + +include $(CLEAR_VARS) + +ANCILLARY_SOURCE := fd_recv.c fd_send.c + +LOCAL_MODULE := libancillary +#LOCAL_CFLAGS += -I$(LOCAL_PATH)/libancillary +LOCAL_C_INCLUDES := $(LOCAL_PATH)/libancillary +LOCAL_SRC_FILES := $(addprefix libancillary/, $(ANCILLARY_SOURCE)) + +include $(BUILD_STATIC_LIBRARY) + +######################################################## +## tun2socks +######################################################## + +include $(CLEAR_VARS) + +LOCAL_CFLAGS := -std=gnu99 +LOCAL_CFLAGS += -DBADVPN_THREADWORK_USE_PTHREAD -DBADVPN_LINUX -DBADVPN_BREACTOR_BADVPN -D_GNU_SOURCE +LOCAL_CFLAGS += -DBADVPN_USE_SIGNALFD -DBADVPN_USE_EPOLL +LOCAL_CFLAGS += -DBADVPN_LITTLE_ENDIAN -DBADVPN_THREAD_SAFE +LOCAL_CFLAGS += -DNDEBUG -DANDROID +LOCAL_CFLAGS += -I + +LOCAL_STATIC_LIBRARIES := libancillary + +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/badvpn/libancillary \ + $(LOCAL_PATH)/badvpn/lwip/src/include/ipv4 \ + $(LOCAL_PATH)/badvpn/lwip/src/include/ipv6 \ + $(LOCAL_PATH)/badvpn/lwip/src/include \ + $(LOCAL_PATH)/badvpn/lwip/custom \ + $(LOCAL_PATH)/badvpn \ + $(LOCAL_PATH)/libancillary + +TUN2SOCKS_SOURCES := \ + base/BLog_syslog.c \ + system/BReactor_badvpn.c \ + system/BSignal.c \ + system/BConnection_common.c \ + system/BConnection_unix.c \ + system/BTime.c \ + system/BUnixSignal.c \ + system/BNetwork.c \ + flow/StreamRecvInterface.c \ + flow/PacketRecvInterface.c \ + flow/PacketPassInterface.c \ + flow/StreamPassInterface.c \ + flow/SinglePacketBuffer.c \ + flow/BufferWriter.c \ + flow/PacketBuffer.c \ + flow/PacketStreamSender.c \ + flow/PacketPassConnector.c \ + flow/PacketProtoFlow.c \ + flow/PacketPassFairQueue.c \ + flow/PacketProtoEncoder.c \ + flow/PacketProtoDecoder.c \ + socksclient/BSocksClient.c \ + tuntap/BTap.c \ + lwip/src/core/udp.c \ + lwip/src/core/memp.c \ + lwip/src/core/init.c \ + lwip/src/core/pbuf.c \ + lwip/src/core/tcp.c \ + lwip/src/core/tcp_out.c \ + lwip/src/core/netif.c \ + lwip/src/core/def.c \ + lwip/src/core/ip.c \ + lwip/src/core/mem.c \ + lwip/src/core/tcp_in.c \ + lwip/src/core/stats.c \ + lwip/src/core/inet_chksum.c \ + lwip/src/core/timeouts.c \ + lwip/src/core/ipv4/icmp.c \ + lwip/src/core/ipv4/igmp.c \ + lwip/src/core/ipv4/ip4_addr.c \ + lwip/src/core/ipv4/ip4_frag.c \ + lwip/src/core/ipv4/ip4.c \ + lwip/src/core/ipv4/autoip.c \ + lwip/src/core/ipv6/ethip6.c \ + lwip/src/core/ipv6/inet6.c \ + lwip/src/core/ipv6/ip6_addr.c \ + lwip/src/core/ipv6/mld6.c \ + lwip/src/core/ipv6/dhcp6.c \ + lwip/src/core/ipv6/icmp6.c \ + lwip/src/core/ipv6/ip6.c \ + lwip/src/core/ipv6/ip6_frag.c \ + lwip/src/core/ipv6/nd6.c \ + lwip/custom/sys.c \ + tun2socks/tun2socks.c \ + base/DebugObject.c \ + base/BLog.c \ + base/BPending.c \ + system/BDatagram_unix.c \ + flowextra/PacketPassInactivityMonitor.c \ + tun2socks/SocksUdpGwClient.c \ + udpgw_client/UdpGwClient.c + +LOCAL_MODULE := tun2socks + +LOCAL_LDLIBS := -ldl -llog + +LOCAL_SRC_FILES := $(addprefix badvpn/, $(TUN2SOCKS_SOURCES)) + +LOCAL_BUILD_SCRIPT := BUILD_EXECUTABLE +LOCAL_MAKEFILE := $(local-makefile) +$(call check-defined-LOCAL_MODULE,$(LOCAL_BUILD_SCRIPT)) +$(call check-LOCAL_MODULE,$(LOCAL_MAKEFILE)) +$(call check-LOCAL_MODULE_FILENAME) +# we are building target objects +my := TARGET_ +$(call handle-module-filename,lib,$(TARGET_SONAME_EXTENSION)) +$(call handle-module-built) +LOCAL_MODULE_CLASS := EXECUTABLE +include $(BUILD_SYSTEM)/build-module.mk diff --git a/ubuntu-cli-install-android-sdk.sh b/ubuntu-cli-install-android-sdk.sh new file mode 100644 index 0000000..6c91338 --- /dev/null +++ b/ubuntu-cli-install-android-sdk.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# Thanks to https://gist.github.com/wenzhixin/43cf3ce909c24948c6e7 +# Execute this script in your home directory. Lines 17 and 21 will prompt you for a y/n + +# Install Oracle JDK 8 +apt-get update +apt-get install -y openjdk-8-jdk +apt-get install -y unzip make expect # NDK stuff + +# Get SDK tools (link from https://developer.android.com/studio/index.html#downloads) +wget -q https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip +mkdir android-sdk-linux +unzip sdk*.zip -d android-sdk-linux + +# Get NDK (https://developer.android.com/ndk/downloads/index.html) +# wget -q https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip +# unzip android-ndk*.zip >> /dev/null + +ACCEPT_LICENSES_URL=https://gist.githubusercontent.com/xiaokangwang/1489fd223d26581bfec92adb3cb0088e/raw/328eb6925099df5aae3e76790f8232f0fc378f8b/accept-licenses + +ACCEPT_LICENSES_ITEM="android-sdk-license-bcbbd656|intel-android-sysimage-license-1ea702d1|android-sdk-license-2742d1c5" + +# Let it update itself and install some stuff +cd android-sdk-linux/tools + +curl -L -o accept-licenses $ACCEPT_LICENSES_URL + +chmod +x accept-licenses + +./accept-licenses "./android update sdk --use-sdk-wrapper --all --no-ui" $ACCEPT_LICENSES_ITEM >/dev/null + +# Download every build-tools version that has ever existed +# This will save you time! Thank me later for this + +#./accept-licenses "./android update sdk --use-sdk-wrapper --all --no-ui --filter 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27" $ACCEPT_LICENSES_ITEM + +PACKAGE_PARSE_URL=https://gist.githubusercontent.com/xiaokangwang/06268fb23034ed94bc301880e862da09/raw/afd95cbbe2f8c1d9e7b0277b7c5ef39af756a6ee/parse.awk + +reduceout=https://gist.githubusercontent.com/xiaokangwang/4684bdb5c3415b943f52aa4803386480/raw/b46dab1cc60f02c0d87f88f01e27157034218faa/out.awk + +cd bin + +curl -L -o parse.awk $PACKAGE_PARSE_URL + +curl -L -o reduce.awk $reduceout + +sudo apt-get install gawk + +./sdkmanager --verbose --list |awk -f parse.awk > ~/package_to_install + +readarray -t filenames < $HOME/package_to_install + +cat $HOME/package_to_install + +yes|./sdkmanager --verbose "${filenames[@]}" |awk -f reduce.awk + +# If you need additional packages for your app, check available packages with: +# ./android list sdk --all + +# install certain packages with: +# ./android update sdk --no-ui --all --filter 1,2,3,<...>,N +# where N is the number of the package in the list (see previous command) + +./sdkmanager "ndk-bundle" + +# Add the directory containing executables in PATH so that they can be found +echo 'export ANDROID_HOME=$HOME/android-sdk-linux' >> ~/.bashrc +echo 'export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools' >> ~/.bashrc +# echo 'export NDK_HOME=$HOME/android-ndk-r15c' >> ~/.bashrc +# echo 'export ANDROID_NDK_HOME=$NDK_HOME' >> ~/.bashrc + + +source ~/.bashrc + +# Make sure you can execute 32 bit executables if this is 64 bit machine, otherwise skip this +dpkg --add-architecture i386 +apt-get update +apt-get install -y libc6:i386 libstdc++6:i386 zlib1g:i386 lib32z1 expect \ No newline at end of file