Skip to content

Commit 5aaca57

Browse files
committed
~tproxy entrypoint
1 parent ce310ee commit 5aaca57

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

.github/workflows/xray-tproxy-docker.yaml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,12 @@ jobs:
110110
linux/s390x
111111
needs:
112112
- check-updates
113-
if: ${{ needs.check-updates.outputs.should_build == 'true' }}
113+
if: ${{ needs.check-updates.outputs.should_build == 'true' }} || || github.event_name == 'workflow_dispatch'
114114
steps:
115-
- name: Docker meta
116-
id: meta
117-
uses: docker/metadata-action@v5
118-
with:
119-
images: ${{ env.REGISTRY_IMAGE }}
115+
- name: Prepare
116+
run: |
117+
echo "TODAY=$(date +'%Y%m%d')" >> $GITHUB_ENV
118+
echo "TODAY: $TODAY"
120119
121120
- name: Checkout
122121
uses: actions/checkout@v4
@@ -150,9 +149,9 @@ jobs:
150149
labels: ${{ steps.meta.outputs.labels }}
151150
tags: |
152151
${{ env.REGISTRY_IMAGE }}:latest
153-
${{ env.REGISTRY_IMAGE }}:${{ needs.check-updates.outputs.remote_version }}
152+
${{ env.REGISTRY_IMAGE }}:${{ needs.check-updates.outputs.remote_version }}-${{ env.TODAY }}
154153
ghcr.io/${{ env.REGISTRY_IMAGE }}:latest
155-
ghcr.io/${{ env.REGISTRY_IMAGE }}:${{ needs.check-updates.outputs.remote_version }}
154+
ghcr.io/${{ env.REGISTRY_IMAGE }}:${{ needs.check-updates.outputs.remote_version }}-${{ env.TODAY }}
156155
push: true
157156
build-args: |
158157
VERSION=${{ needs.check-updates.outputs.remote_version }}

xray-tproxy/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,16 @@ All the environment variables that you can set in `docker-compose.yaml` file:
122122
* Default: 0x1
123123
* `ROUTE_TABLE`: Specify a route table number. All packets with fwmark 0x1 (default) will be routed to this table.
124124
* Default: 100
125-
* `RESERVED_IPS`: Reversed IP ranges. If destination IP is in these ranges, the packets will not be proxyed.
125+
* `RESERVED_IP4`: Reversed IPv4 ranges. If destination IP is in these ranges, the packets will not be proxyed.
126126
* Default: "0.0.0.0/8 10.0.0.0/8 127.0.0.0/8 169.254.0.0/16 172.16.0.0/12 192.168.0.0/16 224.0.0.0/4 240.0.0.0/4"
127127
* Removing `172.16.0.0/12` makes it possible to access remote docker network via proxy.
128+
* `RESERVED_IP6`: Reversed IPv6 ranges. If destination IP is in these ranges, the packets will not be proxyed.
129+
* Default: "::1/128 fc00::/7 fe80::/10"
130+
* `ENFORCE_LAN_SRC_IP4` and `ENFORCE_LAN_SRC_IP6`: Enforce source IP must be in LAN.
131+
* Default: "" (empty)
132+
* Example: `ENFORCE_LAN_SRC_IP4="192.168.2.0/24"`
133+
* This enforce `xray-tproxy` only accept traffic from within the network `192.168.2.0/24`.
134+
* This is useful if your box has a public IP address, and this avoids access from your WAN port neighbors.
128135

129136
> Note: Set these environment variables will override the default ones.
130137
@@ -134,5 +141,4 @@ All the environment variables that you can set in `docker-compose.yaml` file:
134141

135142
* `xray-tproxy` container uses `dnsmasq` to provide DNS service.
136143
* All the DNS requests are re-directed to `xray` via `nftables`.
137-
* Thus, you can safely speicify the IP address of `xray-tproxy` container on your client devices (Phone, PC, etc).
138-
144+
* Thus, you can safely speicify the IP address of `xray-tproxy` container on your client devices (Phone, PC, etc).

xray-tproxy/entrypoint.sh

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
# # Reference: https://github.com/scenery/mihomo-tproxy-docker
44

55
# configs
6+
ENFORCE_LAN_SRC_IP4=${ENFORCE_LAN_SRC_IP4:-""}
7+
ENFORCE_LAN_SRC_IP6=${ENFORCE_LAN_SRC_IP6:-""}
68
ALLOW_QUIC=${ALLOW_QUIC:-"false"}
79
LOCAL_DNS=${LOCAL_DNS:-"114.114.114.114"}
810
REMOTE_DNS=${REMOTE_DNS:-"1.1.1.1 8.8.8.8"}
911
XRAY_INBOUND_PORT=${XRAY_INBOUND_PORT:-"12345"}
1012
XRAY_INBOUND_MARK=${XRAY_INBOUND_MARK:-"0x1"}
1113
ROUTE_TABLE=${ROUTE_TABLE:-"100"}
12-
RESERVED_IPS=${RESERVED_IPS:-"0.0.0.0/8 10.0.0.0/8 127.0.0.0/8 169.254.0.0/16 172.16.0.0/12 192.168.0.0/16 224.0.0.0/4 240.0.0.0/4"}
13-
NFT_RESERVED_IPS="{ $(echo $RESERVED_IPS | sed 's/ /, /g') }"
14+
15+
# reserved ip ranges
16+
RESERVED_IP4=${RESERVED_IP4:-"0.0.0.0/8 10.0.0.0/8 127.0.0.0/8 169.254.0.0/16 172.16.0.0/12 192.168.0.0/16 224.0.0.0/4 240.0.0.0/4"}
17+
RESERVED_IP6=${RESERVED_IP6:-"::1/128 fc00::/7 fe80::/10"}
18+
NFT_RESERVED_IP4="{ $(echo $RESERVED_IP4 | sed 's/ /, /g') }"
19+
NFT_RESERVED_IP6="{ $(echo $RESERVED_IP6 | sed 's/ /, /g') }"
1420

1521
# paths
1622
MAIN_NFT="/tmp/main.nft"
@@ -22,24 +28,37 @@ setup_nftables() {
2228

2329
# Start of main.nft
2430
cat > "$MAIN_NFT" <<EOF
25-
# 1. 创建表
26-
table ip xray {
31+
table inet xray {
2732
28-
# 2. PRE_XRAY 链 (用于 NOTRACK)
33+
# PRE_XRAY 链 (用于 NOTRACK)
2934
# 确保在连接跟踪之前处理 (priority raw = -300)
3035
chain PRE_XRAY {
3136
type filter hook prerouting priority raw; policy accept;
3237
33-
# 忽略代理的连接跟踪对应 iptables -t raw -A PREROUTING -m mark --mark 1 -j NOTRACK
38+
# 忽略代理的连接跟踪 (对应 iptables -t raw -A PREROUTING -m mark --mark 1 -j NOTRACK)
3439
meta mark == $XRAY_INBOUND_MARK notrack
3540
}
3641
37-
# 3. XRAY 链 (用于 TPROXY 规则)
42+
# XRAY 链 (用于 TPROXY 规则)
3843
# priority mangle = -150
3944
chain XRAY {
4045
type filter hook prerouting priority mangle; policy accept;
4146
EOF
4247

48+
# Enforce src ip (ipv4) must be in LAN
49+
if [ "$ENFORCE_LAN_SRC_IP4" != "" ]; then
50+
cat >> "$MAIN_NFT" <<EOF
51+
ip saddr != $ENFORCE_LAN_SRC_IP4 return
52+
EOF
53+
fi
54+
55+
# Enforce src ip (ipv6) must be in LAN
56+
if [ "$ENFORCE_LAN_SRC_IP6" != "" ]; then
57+
cat >> "$MAIN_NFT" <<EOF
58+
ip6 saddr != $ENFORCE_LAN_SRC_IP6 return
59+
EOF
60+
fi
61+
4362
# Add QUIC rule if needed
4463
if [ "$ALLOW_QUIC" = "false" ]; then
4564
cat >> "$MAIN_NFT" <<EOF
@@ -55,13 +74,17 @@ EOF
5574
tcp dport 22 return
5675
5776
# 不处理目标地址是局域网的流量 (私有网络)
58-
ip daddr $NFT_RESERVED_IPS return
77+
ip daddr $NFT_RESERVED_IP4 return
78+
ip6 daddr $NFT_RESERVED_IP6 return
5979
6080
# --- TPROXY 转发规则 ---
6181
62-
# TCP UDP 流量: 标记 1 并重定向到 XRAY_INBOUND_PORT
63-
ip protocol tcp tproxy to 127.0.0.1:$XRAY_INBOUND_PORT meta mark set $XRAY_INBOUND_MARK
64-
ip protocol udp tproxy to 127.0.0.1:$XRAY_INBOUND_PORT meta mark set $XRAY_INBOUND_MARK
82+
# 对 IPv4 的 TCP/UDP 流量进行 TPROXY (使用 meta nfproto)
83+
meta nfproto ipv4 meta l4proto { tcp, udp } tproxy ip to 127.0.0.1:12345 meta mark set $XRAY_INBOUND_MARK
84+
85+
# 对 IPv6 的 TCP/UDP 流量进行 TPROXY (使用 meta nfproto)
86+
meta nfproto ipv6 meta l4proto { tcp, udp } tproxy ip6 to [::1]:12345 meta mark set $XRAY_INBOUND_MARK
87+
6588
}
6689
6790
# 4. OUTPUT 链

0 commit comments

Comments
 (0)