forked from k3s-io/klipper-lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry
executable file
·90 lines (78 loc) · 2.31 KB
/
entry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
set -ex
trap exit TERM INT
BIN_DIR="/sbin"
info() {
{ set +x; } 2> /dev/null
echo '[INFO] ' "$@"
set -x
}
fatal() {
{ set +x; } 2> /dev/null
echo '[ERROR] ' "$@" >&2
set -x
exit 1
}
check_iptables_mode() {
set +e
lsmod | grep -qF nf_tables 2> /dev/null
if [ $? = 0 ]; then
mode=nft
else
mode=legacy
fi
set -e
case "$mode" in
nft)
info "nft mode detected"
set_nft
;;
legacy)
info "legacy mode detected"
set_legacy
;;
*)
fatal "invalid iptables mode"
;;
esac
}
set_nft() {
for i in iptables iptables-save iptables-restore ip6tables; do
ln -sf /sbin/xtables-nft-multi "$BIN_DIR/$i";
done
}
set_legacy() {
for i in iptables iptables-save iptables-restore ip6tables; do
ln -sf /sbin/xtables-legacy-multi "$BIN_DIR/$i";
done
}
start_proxy() {
for src_range in ${SRC_RANGES//,/ }; do
if echo ${src_range} | grep -Eq ":"; then
ip6tables -t filter -I FORWARD -s ${src_range} -p ${DEST_PROTO} --dport ${DEST_PORT} -j ACCEPT
else
iptables -t filter -I FORWARD -s ${src_range} -p ${DEST_PROTO} --dport ${DEST_PORT} -j ACCEPT
fi
done
for dest_ip in ${DEST_IPS//,/ }; do
if echo ${dest_ip} | grep -Eq ":"; then
if [ $(cat /proc/sys/net/ipv6/conf/all/forwarding) == 1 ]; then
ip6tables -t filter -A FORWARD -d ${dest_ip}/128 -p ${DEST_PROTO} --dport ${DEST_PORT} -j DROP
ip6tables -t nat -I PREROUTING -p ${DEST_PROTO} --dport ${SRC_PORT} -j DNAT --to [${dest_ip}]:${DEST_PORT}
ip6tables -t nat -I POSTROUTING -d ${dest_ip}/128 -p ${DEST_PROTO} -j MASQUERADE
fi
else
if [ $(cat /proc/sys/net/ipv4/ip_forward) == 1 ]; then
iptables -t filter -A FORWARD -d ${dest_ip}/32 -p ${DEST_PROTO} --dport ${DEST_PORT} -j DROP
iptables -t nat -I PREROUTING -p ${DEST_PROTO} --dport ${SRC_PORT} -j DNAT --to ${dest_ip}:${DEST_PORT}
iptables -t nat -I POSTROUTING -d ${dest_ip}/32 -p ${DEST_PROTO} -j MASQUERADE
fi
fi
done
}
check_iptables_mode
start_proxy
if [ ! -e /pause ]; then
mkfifo /pause
fi
</pause