From 80ccdf058726c2d1248606838345f70015db3465 Mon Sep 17 00:00:00 2001 From: huhu415 Date: Mon, 2 Dec 2024 21:18:13 +0800 Subject: [PATCH] add: ipk install --- .goreleaser.yaml | 32 ++++- README.md | 17 +-- assets/FAQ.md | 40 +++--- shell/Install.sh | 268 --------------------------------------- shell/UnInstall.sh | 57 --------- shell/ipk/postinstall.sh | 42 ++++++ shell/ipk/postremove.sh | 1 + shell/ipk/preinstall.sh | 20 +++ shell/ipk/preremove.sh | 46 +++++++ shell/uaProxy.procd | 4 - 10 files changed, 160 insertions(+), 367 deletions(-) delete mode 100755 shell/Install.sh delete mode 100755 shell/UnInstall.sh create mode 100644 shell/ipk/postinstall.sh create mode 100644 shell/ipk/postremove.sh create mode 100644 shell/ipk/preinstall.sh create mode 100644 shell/ipk/preremove.sh diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 0533ee0..e33c323 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -39,14 +39,35 @@ archives: - format: tar.gz wrap_in_directory: true # wrap the archive in a directory named after the project # this name template makes the OS and Arch compatible with the results of `uname`. - name_template: >- - {{ .ProjectName }}_ - {{- title .Os }}_ - {{- .Arch }} + name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}" files: - LICENSE - README.md - shell + +nfpms: + - id: uaproxy-ipk + package_name: uaproxy + file_name_template: "{{ .ProjectName }}_openwrt_{{ .Arch }}" + vendor: huhu415 + homepage: https://github.com/huhu415/uaProxy + maintainer: huhu415 + description: UA Proxy Service + license: MIT + formats: + - ipk + contents: + - src: ./shell/uaProxy.procd + dst: /etc/init.d/ + type: config + file_info: + mode: 0755 # rwxr-xr-x 权限 + scripts: + preinstall: "shell/ipk/postinstall.sh" + postinstall: "shell/ipk/postinstall.sh" + preremove: "shell/ipk/preremove.sh" + postremove: "shell/ipk/postremove.sh" + checksum: name_template: "checksums.txt" @@ -59,8 +80,7 @@ release: header: | ## 🥳Changes - - 修复了pg数据库连接不上问题 - - 增加启动时版本显示 + - 增加了对OpenWrt的ipk安装方法 ```sh tar -xzvf uaProxy-linux-xxxxxxx.tar.gz # 解压 diff --git a/README.md b/README.md index a25f248..0ef14cf 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,16 @@ uaProxy 是一个基于 Go 的高性能代理程序,能够高效监控和修 ![uaProxy](assets/uaProxy.png) ## 安装与使用 -### 手动安装(推荐) +### ipk安装(推荐) +下载并安装[相应](https://github.com/huhu415/uaProxy/releases)架构的ipk包 + +### 手动安装 1. 网关设备开启 IP 转发。 在 `/etc/sysctl.conf` 文件添加一行 `net.ipv4.ip_forward=1` ,执行下列命令生效:`sysctl -p` 2. 运行uaProxy - 下载[相应](https://github.com/huhu415/uaProxy/releases)的压缩包, `tar -xzvf uaProxy-xxx-xxx`解压后 - - 把可执行程序放到`/usr/sbin`目录里面 + - 把可执行程序放到`/usr/bin`目录里面 - 如果是procd(OpenWrt)**选一个即可** - 把[脚本文件](shell/uaProxy.procd)放到`/etc/init.d`目录里面 - 执行`chmod +x /etc/init.d/uaProxy-openwrt`, 赋予执行权限 @@ -44,16 +47,6 @@ iptables -t nat -A OUTPUT -p tcp -j uaProxy # 对本机进行透明代理. 可 ``` > 设置前, 确保已经清空了`iptables`规则, 以免影响正常使用: `iptables -t nat -F` -### 脚本 -安装: -```sh -curl https://raw.githubusercontent.com/huhu415/uaProxy/refs/heads/main/shell/Install.sh | sh -``` - -卸载: -```sh -curl https://raw.githubusercontent.com/huhu415/uaProxy/refs/heads/main/shell/UnInstall.sh | sh -``` ### 参数说明: - `--stats` 开启统计信息 diff --git a/assets/FAQ.md b/assets/FAQ.md index da47d2d..1bbc0f0 100644 --- a/assets/FAQ.md +++ b/assets/FAQ.md @@ -1,30 +1,30 @@ -## ❓Questions +## ❓常见问题 -### 如何选择Assets版本? +### 如何选择 Assets 版本? -- **386**: 32位的x86架构处理器 -- **amd64**: 64位的x86架构处理器(也称为x86_64) -- **arm**: 32位的ARM架构处理器 -- **arm64**: 64位的ARM架构处理器(也称为AArch64) -- **loong64**: 龙芯处理器的64位架构 -- **mips**: 32位的大端序MIPS处理器 -- **mipsle**: 32位的小端序MIPS处理器 -- **mips64**: 64位的大端序MIPS处理器 -- **mips64le**: 64位的小端序MIPS处理器 -- **ppc64**: 64位的大端序PowerPC处理器 -- **ppc64le**: 64位的小端序PowerPC处理器 +1. **确认 CPU 架构**:使用命令 `uname -m` 查看是arm64, arm, mips, mips64等 +2. **确认 CPU 大端小段**:一般都是大端, 很少很少有小端, 除非是单片机这种, 可咨询 [ChatGPT](https://chatgpt.com) 或 [kimi](https://kimi.moonshot.cn/chat/)。 -> 注意, 所有Assets都是Linux版本的, darwin,windows,freebsd等等没有iptables, 无法使用 +处理器架构对应版本: -### 我是路由器, 选择哪个版本? +| 版本 | 处理器架构 | +| ------------ | -------------------- | +| **386** | 32 位 x86 | +| **amd64** | 64 位 x86(x86_64) | +| **arm** | 32 位 ARM | +| **arm64** | 64 位 ARM(AArch64) | +| **loong64** | 64 位龙芯 | +| **mips** | 32 位大端 MIPS | +| **mipsle** | 32 位小端 MIPS | +| **mips64** | 64 位大端 MIPS | +| **mips64le** | 64 位小端 MIPS | +| **ppc64** | 64 位大端 PowerPC | +| **ppc64le** | 64 位小端 PowerPC | -先查一下自己的cpu的bits, 是32位还是64位的. _这就可以去掉一半了_ - -再看看是什么架构, 如果是硬路由, 比如ac2100这种, 一般都是`MIPS`类型的 - - 如果是`MIPS`类型的, _那么就看看是大端序还是小端序的, 可以去问[ChatGpt](https://chatgpt.com)或者[kimi](https://kimi.moonshot.cn/chat/)_ - - 如果不是, 那就是`ARM`或者`x86`的 +> 注意:所有 Assets 均为 Linux 版本,因 darwin、windows、freebsd 等系统没有 iptables,无法使用。 ### 校园网常见检测方式 + - [x] TTL - ```sh iptables -t mangle -A POSTROUTING -j TTL --ttl-set 64 # 修改出口 TTL 为 64 diff --git a/shell/Install.sh b/shell/Install.sh deleted file mode 100755 index 6d47926..0000000 --- a/shell/Install.sh +++ /dev/null @@ -1,268 +0,0 @@ -#!/bin/sh - -ARCH_TYPE="" -get_arch_type() { - local ARCH=$(uname -m) - - # 检测大小端 - local mipstype=$(echo -n I | hexdump -o 2>/dev/null | awk '{ print substr($2,6,1); exit}') - - case $ARCH in - i386|i686) - ARCH_TYPE="386" - ;; - x86_64) - ARCH_TYPE="amd64" - ;; - armv7*|armv6*|armv8*) - ARCH_TYPE="arm" - ;; - aarch64) - ARCH_TYPE="arm64" - ;; - loongarch64) - ARCH_TYPE="loong64" - ;; - mips) - # 如果mipstype=0是大端,如果是1则是小端 - if [ "$mipstype" = "0" ]; then - ARCH_TYPE="mips" # 大端 - else - ARCH_TYPE="mipsle" # 小端 - fi - ;; - mips64) - # 如果mipstype=0是大端,如果是1则是小端 - if [ "$mipstype" = "0" ]; then - ARCH_TYPE="mips64" # 大端 - else - ARCH_TYPE="mips64le" # 小端 - fi - ;; - ppc64) - # 如果mipstype=0是大端,如果是1则是小端 - if [ "$mipstype" = "0" ]; then - ARCH_TYPE="ppc64" # 大端 - else - ARCH_TYPE="ppc64le" # 小端 - fi - ;; - riscv64) - ARCH_TYPE="riscv64" - ;; - *) - ARCH_TYPE=$ARCH - return 1 - ;; - esac - - return 0 -} - -ckcmd() { - command -v sh >/dev/null 2>&1 && command -v $1 >/dev/null 2>&1 || type $1 >/dev/null 2>&1 -} - -# 检查是否为root用户 -if [ "$(id -u)" != "0" ]; then - echo "This script must be run as root" - exit 1 -fi - -# 检查是不是linux -if [ "$(uname)" != "Linux" ]; then - echo "This script must be run on Linux" - exit 1 -fi - -# 检查是否为支持的架构 -if get_arch_type; then - echo "Detected architecture type: $ARCH_TYPE" -else - echo "$ARCH_TYPE arch is not supported" - exit 1 -fi - -# 检测并配置IP转发 -echo "正在检查 IP 转发状态..." -if [ "$(cat /proc/sys/net/ipv4/ip_forward)" = "0" ]; then - echo "-----------------------------------------------" - echo -e "\033[33m检测到系统尚未开启IP转发,局域网设备将无法正常连接网络。\033[0m" - read -p "是否立即开启IP转发?(Y/n) " res - case "$res" in - [nN]) - echo "未开启IP转发,部分功能可能无法正常使用" - ;; - *) - echo "正在开启IP转发..." - if ! grep -q "net.ipv4.ip_forward=1" /etc/sysctl.conf; then - echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf || { - echo "写入 sysctl.conf 失败" - exit 1 - } - fi - sysctl -w net.ipv4.ip_forward=1 || { - echo "应用 sysctl 配置失败" - exit 1 - } - echo "IP转发已成功开启" - ;; - esac -fi - -# 检测系统类型: OpenWrt procd 或 systemd -INIT_SYSTEM="" -if [ -f /etc/rc.common ] && [ "$(cat /proc/1/comm)" = "procd" ]; then - INIT_SYSTEM="procd" - echo "检测到 OpenWrt procd 系统" -elif ckcmd systemctl && [ "$(cat /proc/1/comm)" = "systemd" ]; then - INIT_SYSTEM="systemd" - echo "检测到 systemd 系统" -else - echo "未能识别系统启动方式,将采用默认配置" - INIT_SYSTEM="unknown" -fi - -# 下载并安装 uaProxy -UaProxy_Name="uaProxy_Linux_${ARCH_TYPE}" -DOWNLOAD_URL="https://github.com/huhu415/uaProxy/releases/latest/download/${UaProxy_Name}.tar.gz" - -# 检查压缩包是否存在 -if [ ! -f "${UaProxy_Name}.tar.gz" ]; then - echo "正在下载 uaProxy.tar.gz..." - # 下载 - wget -q "$DOWNLOAD_URL" || { - echo "下载失败,请检查网络连接或下载链接是否正确" - exit 1 - } -else - echo "压缩包已存在,跳过下载" -fi - -# 删除已经存在的解压后的文件夹 -rm -rf $UaProxy_Name - -# 解压 -tar -xzf $UaProxy_Name.tar.gz || { - echo "解压失败" - exit 1 -} - -# 安装主程序 -mv $UaProxy_Name/uaProxy /usr/sbin/ || { - echo "移动 uaProxy 到 /usr/sbin/ 失败" - exit 1 -} -chmod +x /usr/sbin/uaProxy || { - echo "修改 uaProxy 权限失败" - exit 1 -} - -# 根据系统类型安装不同的服务文件 -case $INIT_SYSTEM in - procd) - mv $UaProxy_Name/shell/uaProxy.procd /etc/init.d/ || { - echo "移动 uaProxy.procd 到 /etc/init.d/ 失败" - exit 1 - } - chmod +x /etc/init.d/uaProxy.procd || { - echo "修改 uaProxy.procd 权限失败" - exit 1 - } - ;; - systemd) - mv $UaProxy_Name/shell/uaProxy.service /etc/systemd/system/ || { - echo "移动 uaProxy.service 到 /etc/systemd/system/ 失败" - exit 1 - } - systemctl daemon-reload || { - echo "重载 systemd 配置失败" - exit 1 - } - ;; - *) - echo "警告: 未能识别系统类型,跳过服务安装, 自行配置开机自启" - ;; -esac - -rm -rf $UaProxy_Name -rm $UaProxy_Name.tar.gz - - -# 3. 配置 iptables 规则 -echo "正在配置 iptables 规则..." -# iptables -t nat -F // 清空 nat 表 -# iptables -t nat -X uaProxy -iptables -t nat -L uaProxy >/dev/null 2>&1 || iptables -t nat -N uaProxy - -iptables -t nat -A uaProxy -d 192.168.0.0/16 -j RETURN || { - echo "添加 iptables 规则失败 (1/5)" - exit 1 -} -iptables -t nat -A uaProxy -p tcp -j RETURN -m mark --mark 0xff || { - echo "添加 iptables 规则失败 (2/5)" - exit 1 -} -iptables -t nat -A uaProxy -p tcp -j REDIRECT --to-ports 12345 || { - echo "添加 iptables 规则失败 (3/5)" - exit 1 -} -iptables -t nat -A PREROUTING -p tcp -j uaProxy || { - echo "添加 iptables 规则失败 (4/5)" - exit 1 -} -iptables -t nat -A OUTPUT -p tcp -j uaProxy || { - echo "添加 iptables 规则失败 (5/5)" - exit 1 -} - -# 配置开机启动 -echo "正在配置开机启动..." -case $INIT_SYSTEM in - procd) - /etc/init.d/uaProxy.procd enable - echo "已配置 OpenWrt procd 启动项" - ;; - systemd) - systemctl enable uaProxy.service - echo "已配置 systemd 启动项" - ;; - *) - echo "未能配置开机启动,请手动配置" - ;; -esac - -# 启动服务 -echo "正在启动服务..." -case $INIT_SYSTEM in - procd) - /etc/init.d/uaProxy.procd start - ;; - systemd) - systemctl start uaProxy.service - ;; - *) - echo "请手动启动服务" - ;; -esac - -echo "安装完成!" -echo "----------------------------------------" -echo "使用说明:" -case $INIT_SYSTEM in - procd) - echo "1. 使用 '/etc/init.d/uaProxy.procd {start|stop|restart|status}' 控制服务" - echo "2. 使用 'logread | grep uaProxy' 查看运行日志" - ;; - systemd) - echo "1. 使用 'systemctl {start|stop|restart} uaProxy' 控制服务" - echo "2. 使用 'systemctl status uaProxy.service' 查看运行状态" - ;; - *) - echo "1. 请根据您的系统手动控制服务" - echo "2. 请根据您的系统手动查看运行日志" - ;; -esac -echo "3. iptables中nat表配置完成" -echo "4. IP 转发已配置完成" -echo "----------------------------------------" diff --git a/shell/UnInstall.sh b/shell/UnInstall.sh deleted file mode 100755 index c436aef..0000000 --- a/shell/UnInstall.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/sh - -ckcmd() { - command -v sh >/dev/null 2>&1 && command -v $1 >/dev/null 2>&1 || type $1 >/dev/null 2>&1 -} - -# 检查是否为root用户 -if [ "$(id -u)" != "0" ]; then - echo "This script must be run as root" - exit 1 -fi - -# 检测系统类型: OpenWrt procd 或 systemd -INIT_SYSTEM="" -if [ -f /etc/rc.common ] && [ "$(cat /proc/1/comm)" = "procd" ]; then - INIT_SYSTEM="procd" - echo "检测到 OpenWrt procd 系统" -elif ckcmd systemctl && [ "$(cat /proc/1/comm)" = "systemd" ]; then - INIT_SYSTEM="systemd" - echo "检测到 systemd 系统" -else - echo "未能识别系统启动方式,将采用默认配置" - INIT_SYSTEM="unknown" -fi - -# 根据系统类型停止服务并删除启动脚本 -case $INIT_SYSTEM in - "procd") - # OpenWrt系统 - /etc/init.d/uaProxy stop - rm -f /etc/init.d/uaProxy.procd - ;; - "systemd") - # systemd系统 - systemctl stop uaProxy - systemctl disable uaProxy - rm -f /etc/systemd/system/uaProxy.service - systemctl daemon-reload - ;; - *) - echo "未知系统类型,跳过服务停止和启动脚本删除" - ;; -esac - -# 清理 iptables 规则 -echo "正在清理 iptables 规则..." -iptables -t nat -F # 清空 nat 表 -iptables -t nat -X uaProxy # 删除自定义链 - -# 删除可执行文件 -rm -f /usr/sbin/uaProxy || { - echo "删除 uaProxy 失败" - exit 1 -} - - -echo "卸载完成" diff --git a/shell/ipk/postinstall.sh b/shell/ipk/postinstall.sh new file mode 100644 index 0000000..9e6e4ea --- /dev/null +++ b/shell/ipk/postinstall.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +SERVICE_NAME="uaProxy.procd" +INIT_SCRIPT="/etc/init.d/$SERVICE_NAME" + +echo "正在配置 iptables 规则..." +iptables -t nat -F +iptables -t nat -L uaProxy >/dev/null 2>&1 || iptables -t nat -N uaProxy + +iptables -t nat -A uaProxy -d 192.168.0.0/16 -j RETURN || { + echo "添加 iptables 规则失败 (1/5)" + exit 1 +} +iptables -t nat -A uaProxy -p tcp -j RETURN -m mark --mark 0xff || { + echo "添加 iptables 规则失败 (2/5)" + exit 1 +} +iptables -t nat -A uaProxy -p tcp -j REDIRECT --to-ports 12345 || { + echo "添加 iptables 规则失败 (3/5)" + exit 1 +} +iptables -t nat -A PREROUTING -p tcp -j uaProxy || { + echo "添加 iptables 规则失败 (4/5)" + exit 1 +} +iptables -t nat -A OUTPUT -p tcp -j uaProxy || { + echo "添加 iptables 规则失败 (5/5)" + exit 1 +} + +# 确保服务脚本可执行 +if [ -f "$INIT_SCRIPT" ]; then + chmod +x "$INIT_SCRIPT" +fi + +# 启用服务 +if [ -x "$INIT_SCRIPT" ]; then + "$INIT_SCRIPT" enable + "$INIT_SCRIPT" start +fi + +exit 0 diff --git a/shell/ipk/postremove.sh b/shell/ipk/postremove.sh new file mode 100644 index 0000000..1a24852 --- /dev/null +++ b/shell/ipk/postremove.sh @@ -0,0 +1 @@ +#!/bin/sh diff --git a/shell/ipk/preinstall.sh b/shell/ipk/preinstall.sh new file mode 100644 index 0000000..d29a820 --- /dev/null +++ b/shell/ipk/preinstall.sh @@ -0,0 +1,20 @@ +# 检查是否为Linux系统 +if [ "$(uname)" != "Linux" ]; then + echo "This script must be run on Linux" + exit 1 +fi + + +# 检查系统启动方式 +if [ ! -f /etc/rc.common ] || [ "$(cat /proc/1/comm)" != "procd" ]; then + echo "This package is designed for OpenWrt systems only" + exit 1 +fi + +# 检查必要的系统命令 +for cmd in iptables grep; do + if ! command -v $cmd >/dev/null 2>&1; then + echo "Required command '$cmd' not found" + exit 1 + fi +done diff --git a/shell/ipk/preremove.sh b/shell/ipk/preremove.sh new file mode 100644 index 0000000..1e7fc40 --- /dev/null +++ b/shell/ipk/preremove.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +remove() { + printf "\033[32m 执行 uaProxy 包的卸载操作...\033[0m\n" + + # 停止服务 + /etc/init.d/uaProxy.procd stop + + # 清理 uaProxy 相关的 iptables 规则 + echo "正在清理 uaProxy 相关的 iptables 规则..." + iptables -t nat -F uaProxy # 清空 uaProxy 链中的规则 + iptables -t nat -X uaProxy # 删除 uaProxy 链 + + echo "卸载完成" > /tmp/postremove-proof +} + +# purge() { +# printf "\033[32m Post Remove purge, deb only\033[0m\n" +# echo "Purge" > /tmp/postremove-proof +# } + +upgrade() { + printf "\033[32m Post Remove of an upgrade\033[0m\n" + echo "Upgrade" > /tmp/postremove-proof +} + +echo "$@" + +action="$1" + +case "$action" in + "0" | "remove") + remove + ;; + "1" | "upgrade") + upgrade + ;; + "purge") + remove + # purge + ;; + *) + printf "\033[32m Alpine\033[0m" + remove + ;; +esac diff --git a/shell/uaProxy.procd b/shell/uaProxy.procd index afac02d..020069f 100644 --- a/shell/uaProxy.procd +++ b/shell/uaProxy.procd @@ -14,7 +14,3 @@ start_service() { procd_set_param stderr 1 # same for stderr procd_close_instance } - -stop_service() { - killall -9 $PROG # 强制结束进程 -}