Last active
May 9, 2025 13:08
-
-
Save krisstibex/13a4a942b9adb045c4bc770cecca0e25 to your computer and use it in GitHub Desktop.
bash <(curl -fsSL https://gist.github.com/krisstibex/13a4a942b9adb045c4bc770cecca0e25/raw/mac_portforward.sh)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 检查 root 权限 | |
if [ "$EUID" -ne 0 ]; then | |
echo "请使用 sudo 运行本脚本" | |
exit 1 | |
fi | |
# 默认参数 | |
PROTO="tcp" | |
IFACE="lo0" | |
LISTEN_ADDR="127.0.0.1" | |
# 参数解析 | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--inport) | |
INPORT="$2" | |
shift 2 | |
;; | |
--outport) | |
OUTPORT="$2" | |
shift 2 | |
;; | |
--protocol) | |
PROTO="$2" | |
shift 2 | |
;; | |
--interface) | |
IFACE="$2" | |
shift 2 | |
;; | |
--listen) | |
LISTEN_ADDR="$2" | |
shift 2 | |
;; | |
*) | |
echo "未知参数: $1" | |
exit 1 | |
;; | |
esac | |
done | |
# 参数校验 | |
if [[ -z "$INPORT" || -z "$OUTPORT" ]]; then | |
echo "错误:必须指定 --inport 和 --outport" | |
exit 1 | |
fi | |
if [[ "$PROTO" != "tcp" && "$PROTO" != "udp" ]]; then | |
echo "协议错误:仅支持 tcp 或 udp" | |
exit 1 | |
fi | |
# 临时 pf 规则文件 | |
TMP_RULES="/tmp/port_forward_rules.conf" | |
cat > "$TMP_RULES" <<EOF | |
rdr pass on $IFACE inet proto $PROTO from any to any port $INPORT -> $LISTEN_ADDR port $OUTPORT | |
EOF | |
# 启用 pf 并加载规则 | |
pfctl -e 2>/dev/null | |
pfctl -f "$TMP_RULES" | |
# 输出状态 | |
echo "-------------------------------------------" | |
echo "临时端口转发已启用" | |
echo "监听接口 : $IFACE" | |
echo "监听端口 : $INPORT" | |
echo "协议 : $PROTO" | |
echo "目标地址 : $LISTEN_ADDR:$OUTPORT" | |
echo "按 Ctrl+C 停止并清除规则" | |
echo "-------------------------------------------" | |
# 清理规则 | |
trap 'echo; echo 正在清理规则...; pfctl -F all -f /etc/pf.conf; echo 已清除。' INT | |
while true; do sleep 1; done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 默认参数 | |
PROTO="TCP" | |
INPORT="" | |
OUTPORT="" | |
IFACE="lo0" | |
LISTEN_ADDR="127.0.0.1" | |
# 参数解析 | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--inport) | |
INPORT="$2" | |
shift 2 | |
;; | |
--outport) | |
OUTPORT="$2" | |
shift 2 | |
;; | |
--protocol) | |
PROTO="$2" | |
shift 2 | |
;; | |
--interface) | |
IFACE="$2" | |
shift 2 | |
;; | |
--listen) | |
LISTEN_ADDR="$2" | |
shift 2 | |
;; | |
*) | |
echo "未知参数: $1" | |
exit 1 | |
;; | |
esac | |
done | |
# 参数校验 | |
if [[ -z "$INPORT" || -z "$OUTPORT" ]]; then | |
echo "错误:必须指定 --inport 和 --outport" | |
exit 1 | |
fi | |
# socat 命令 | |
echo "启动端口转发: $PROTO $INPORT -> $LISTEN_ADDR:$OUTPORT" | |
socat $PROTO-LISTEN:$INPORT,fork,reuseaddr $PROTO:$LISTEN_ADDR:$OUTPORT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment