Mac电脑交叉编译iphone设备可以运行的redsocks, openssl, libsevent
准备:intel x86_64芯片的mac电脑,系统为mac os15.3.1,iphone为6s的ios14.4(rootful越狱)
第一步:准备工具链(推荐使用 theos
+ clang
)
如果你已经安装过 Theos(或 NewTheos),直接用它内置的 clang
就能编译出 iOS Mach-O 可执行文件。
如果没有,可以这样安装:
# 安装 theos(推荐)
git clone --recursive https://github.com/theos/theos.git ~/theos
第二步:准备依赖库 libevent
你需要为 iOS 编译静态版本的 libevent:
git clone https://github.com/libevent/libevent.git
cd libevent
brew install autoconf automake libtool
./autogen.sh# 设置编译环境(假设你用 Xcode CLI 工具)
export SDK=$(xcrun --sdk iphoneos --show-sdk-path)
export CC="$(xcrun --sdk iphoneos -f clang)"
export CFLAGS="-isysroot $SDK -arch arm64 -miphoneos-version-min=10.0"./configure --host=arm-apple-darwin --disable-shared --enable-static
make
第三步:获取并修改 redsocks 源码
git clone https://github.com/darkk/redsocks.git
cd redsocks
更新一下Makefile更是和mac系统
# Compiler settings
CC := gcc
CFLAGS := -g -O2 -std=c99 -D_XOPEN_SOURCE=600 -D_DEFAULT_SOURCE -D_GNU_SOURCE -Wall
LDFLAGS := # Libraries
LIBS := -levent_core
ifeq ($(DBG_BUILD),1)LIBS += -levent_extra
endif# Sources and objects
SRCS := parser.c main.c redsocks.c log.c http-connect.c socks4.c socks5.c http-relay.c base.c base64.c md5.c http-auth.c utils.c redudp.c dnstc.c dnsu2t.c gen/version.c
OBJS := $(SRCS:.c=.o)
DEPS := .depend
OUT := redsocks
VERSION := 0.5
CONF := config.h# Directories
GEN_DIR := gen# Targets
.PHONY: all clean distclean test tags# Default target
all: $(OUT)# Generate version.c
gen/version.c: *.c *.h $(GEN_DIR)/.buildrm -f $@.tmpecho '/* this file is auto-generated during build */' > $@.tmpecho '#include "../version.h"' >> $@.tmpecho 'const char* redsocks_version = ' >> $@.tmpif [ -d .git ]; then \echo '"redsocks.git/'`git describe --tags`'"'; \if [ `git status --porcelain | grep -v -c '^??'` != 0 ]; then \echo '"'"-unclean-$(shell date --rfc-3339=seconds | tr ' ' 'T')-$(USER)@$(shell uname -n)"'"'; \fi \else \echo '"redsocks/$(VERSION)"'; \fi >> $@.tmpecho ';' >> $@.tmpmv -f $@.tmp $@# Create necessary directories
$(GEN_DIR)/.build:mkdir -p $(GEN_DIR)touch $@# Dependency generation
$(DEPS): $(SRCS)$(CC) -MM $(SRCS) 2>/dev/null > $(DEPS) || \( \# Handle missing dependencies properly with correct formatting \for SRC in $(SRCS); do \echo -n "$${SRC%.c}.o: " >> $(DEPS); \# Collect includes for each source file \export SRC_DEPS="`sed '/^\#[ \t]*include \?"\(.*\)".*/!d;s//\1/' $$SRC | sort`"; \while true; do \export SRC_DEPS_OLD="$$SRC_DEPS"; \# Recursively collect dependencies \export SRC_DEPS="`echo $$SRC_DEPS | sed 's/ */\n/g' | sort`"; \test "$$SRC_DEPS" = "$$SRC_DEPS_OLD" && break; \done; \echo $$SRC $$SRC_DEPS >> $(DEPS); \done; \)# Include dependencies
-include $(DEPS)# Final executable
$(OUT): $(OBJS)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)# Clean the build
clean:$(RM) $(OUT) $(OBJS) $(CONF) $(DEPS)# Clean everything, including generated files
distclean: clean$(RM) tags $(DEPS)$(RM) -r $(GEN_DIR)# Create tags for code navigation
tags: *.c *.hctags -R# Run tests
test: tests/__build-tstamp__ tests/prlimit-nofilecd tests && env $(TEST_ENV) ./run# Build tests
tests/__build-tstamp__: $(OUT) tests/[a-z]* tests/[a-z]*/*cd tests && ./buildtouch $@# Build test for prlimit
tests/prlimit-nofile: tests/prlimit-nofile.c$(CC) $(CFLAGS) -o $@ $^
第四步:交叉编译 redsocks
export SDK=$(xcrun --sdk iphoneos --show-sdk-path)
export CC="$(xcrun --sdk iphoneos -f clang)"
export CFLAGS="-isysroot $SDK -arch arm64 -miphoneos-version-min=10.0"
export LDFLAGS="-L/path/to/libevent/.libs"make
第五步:签名(越狱设备免验证也建议做)
brew install ldid
ldid -S redsocks
第六步:推送到 iPhone
scp redsocks root@<iphone-ip>:/usr/local/bin/
scp redsocks.conf root@<iphone-ip>:/etc/
第七步:启动测试
chmod +x /usr/local/bin/redsocks
/usr/local/bin/redsocks -c /etc/redsocks.conf
第八步:编译Openssl3.5.0(可选)
git clone https://github.com/x2on/OpenSSL-for-iPhone.git
cd OpenSSL-for-iPhone#您可以尝试在构建脚本中添加以下选项来禁用 Bitcode:export CFLAGS="-fno-bitcode"
export LDFLAGS="-fno-bitcode"./build-libssl.sh clean
./build-libssl.sh