alpine 使用redsocks 实现 http 全局代理

本文共有2429个字,关键词:

继这一篇文章的centos实现 https://pch18.cn/archives/66.html

编译

可以在其他同环境下编译好拷贝过去使用

# centos安装依赖
yum install libevent-devel git gcc make openssl-devel

# alpine安装依赖
apk add make gcc libc-dev libevent-dev iptables-dev openssl-dev

# 开始编译
git clone https://github.com/pch18-fork/redsocks
cd redsocks/
make

运行

如果本机编译就不需要安装依赖了,如果直接用编译好的文件,需要安装依赖libevent
这里用alpine做例子

apk update
apk add libevent
cp ./redsocks /usr/bin/redsocks
cp ./redsocks.conf /etc/redsocks.conf
cp ./redsocks.service /etc/init.d/redsocks
chmod +x /usr/bin/redsocks
chmod +x /etc/init.d/redsocks
adduser -u 12345 -G root -D -g redsocks -h /usr/bin/redsocks -s /sbin/nologin redsocks
chown redsocks /usr/bin/redsocks
rc-update add redsocks boot

apk add iptables # 安装 iptables
rm -f /etc/profile.d/proxy.sh

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

iptables -t nat -N REDSOCKS
iptables -t nat -A REDSOCKS -o lo -j RETURN
iptables -t nat -A REDSOCKS -o docker0 -j RETURN # 访问docker内地址不走代理
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN # 出口内网不走代理

iptables -t nat -A REDSOCKS -d proxy.itc.kansai-u.ac.jp -j RETURN
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-port 12345
iptables -t nat -A REDSOCKS -p udp -j REDIRECT --to-port 12346
iptables -t nat -I PREROUTING -p tcp -j REDSOCKS
iptables -t nat -I OUTPUT -p tcp -j REDSOCKS

service iptables save
cp /etc/iptables/rules-save /etc/iptables/rules-save_origin
rc-update add iptables boot

service 文件是这样的

#!/sbin/openrc-run

description="Transparent socks redirector"

CFGFILE="/etc/redsocks.conf"
PIDFILE="/var/run/redsocks.pid"

depend() {
        need localmount net
        use dns logger
        after bootmisc firewall
}

checkconfig() {
    /usr/bin/redsocks -t -c ${CFGFILE}
        if [ "$?" -gt 0 ]; then
                eerror "Problem on configuration file ${CFGFILE}"
                return 1
        fi
}

start() {
        checkconfig || return 1
        ebegin "Starting ${SVCNAME}"
        start-stop-daemon --start --exec /usr/bin/redsocks --pidfile "${PIDFILE}" \
                -- -c "${CFGFILE}" -p "${PIDFILE}"
        eend $?
}

stop() {
        checkconfig || return 1
        ebegin "Stopping ${SVCNAME}"
        start-stop-daemon --stop --exec /usr/bin/redsocks --pidfile "${PIDFILE}"
        eend $?
}

「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」

pch18

(๑>ڡ<)☆谢谢老板~

使用微信扫描二维码完成支付

版权声明:如无特别说明,本文为作者原创,转载请在首行注明来源:https://pch18.cn/archives/259.html
添加新评论
暂无评论