summaryrefslogtreecommitdiff
path: root/edgemesh/tools/initContainer/script/edgemesh-iptables.sh
blob: 1447ef7fb73a3112b67e64b79df8731ea7b0a5f0 (about) (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env bash

function usage() {
	echo 'this is the edgemesh-iptables usage'
	echo "${0} -p PROXY_PORT [-i HIJACK_IP] [-t HIJACK_PORT] [-b EXCLUDE_IP] [-c EXCLUDE_PORT] [-h]"
	echo ''
	echo '  -p: Specify the edgemesh port to which all TCP traffic from the Pod will be redirected to. (default 10001)'
	echo '  -i: Comma separated list of outbound IP for which traffic is to be redirected to edgemesh. The'
	echo '      wildcard character "*" can be used to configure redirection for all IPs. (default "*")'
	echo '  -t: Comma separated list of outbound Port for which traffic is to be redirected to edgemesh. The'
	echo '      wildcard character "*" can be used to configure redirection for all Ports. (default "*")'
	echo '  -b: Comma separated list of outbound IP range in CIDR to be excluded from redirection to edgemesh.'
	echo '      The Empty character "" can be used to configure redirection for all IPs. (default "")'
	echo '  -c: Comma separated list of outbound Port to be excluded from redirection to edgemesh. The'
	echo '      Empty character "" can be used to configure redirection for all Ports. (default "")'
	echo '  -h: for some help'
}

# network namespace 
NETMODE=

# get the container network mode 
function getContainerNetMode() {
	if ip link |grep docker0 > /dev/null; then
		echo 'this is the host mode,share with net namespace with host'
		NETMODE='HOST'
	else
		echo 'this is the ohter container net mode(none,bridge),independent of the host net namespace'
		NETMODE='OTHER'
	fi
}

# judge if argument is a valid ip address
function isValidIP() {
	if isIPv4 "${1}"; then
		true
	elif isIPv6 "${1}"; then
		true
	else
		false
	fi		
}

function isIPv4() {
	local ipv4matchString="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
	if [[ ${1} =~ ${ipv4matchString} ]]; then
		true
	else
		false
	fi
}

function isIPv6() {
	local ipv6matchString="^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$"
	if [[ ${1} =~ ${ipv6matchString} ]]; then
		true
	else
		false
	fi
}

function hostNetMode() {
	echo 'this func used for host net mode'
	echo 'TODO'
}

function bridgeNetMode() {
	echo 'this func used for bridge net mode'
	# get default route
	default_route=$(ip route show |grep default |awk '{print $3}')
	
	#clear EDGEMESH chain and rule,if exist
	iptables -t nat -D OUTPUT -p tcp -j EDGEMESH_OUTBOUND 2>/dev/null
	iptables -t nat -D OUTPUT -p udp --dport "53" -j EDGEMESH_OUTBOUND_DNS 2>/dev/null
	iptables -t nat -F EDGEMESH_OUTBOUND 2>/dev/null
	iptables -t nat -X EDGEMESH_OUTBOUND 2>/dev/null
	
	iptables -t nat -F EDGEMESH_OUTBOUND_REDIRECT 2>/dev/null
	iptables -t nat -X EDGEMESH_OUTBOUND_REDIRECT 2>/dev/null
	
	iptables -t nat -F EDGEMESH_OUTBOUND_DNS 2>/dev/null
	iptables -t nat -X EDGEMESH_OUTBOUND_DNS 2>/dev/null
	
	# make chain for edgemesh hijacking
	iptables -t nat -N EDGEMESH_OUTBOUND_REDIRECT
	iptables -t nat -A EDGEMESH_OUTBOUND_REDIRECT -p tcp -j DNAT --to-destination "${default_route}:${EDGEMESH_PROXY_PORT}"
	iptables -t nat -N EDGEMESH_OUTBOUND
	iptables -t nat -A OUTPUT -p tcp -j EDGEMESH_OUTBOUND
	
	# support dns use udp for dest port 53
	iptables -t nat -N EDGEMESH_OUTBOUND_DNS
	iptables -t nat -A EDGEMESH_OUTBOUND_DNS -j DNAT --to-destination "${default_route}"
	iptables -t nat -A OUTPUT -p udp --dport "53" -j EDGEMESH_OUTBOUND_DNS
	
	# excluded traffic for some port incloude some special port,such as 22
	iptables -t nat -A EDGEMESH_OUTBOUND -p tcp --dport "22" -j RETURN
	if [ -n "${EDGEMESH_EXCLUDE_PORT}" ]; then 
		for port in "${port_exclude_list[@]}"; do 
			iptables -t nat -A EDGEMESH_OUTBOUND -p tcp --dport "${port}" -j RETURN
		done
	fi
	# excluded traffic for some ips
	if [ ${#ipv4_exclude_list[@]} -gt 0 ]; then
		for ip in "${ipv4_exclude_list[@]}"; do
			iptables -t nat -A EDGEMESH_OUTBOUND -d "${ip}" -j RETURN
		done
	fi
	
	# Redirect app callback to itself via Service IP (default not redirected)
	get_local_IP=$(ip addr |grep inet|grep -v inet6|awk '{print $2}'|tr -d "addr:")
	
	for LOCAL_IP in $get_local_IP; do
		ele=${LOCAL_IP%$splt}
		echo "LOCAL_IP: $LOCAL_IP , $ele"
		if isIPv4 $ele; then
			iptables -t nat -A EDGEMESH_OUTBOUND -o lo ! -d "${LOCAL_IP}" -j EDGEMESH_OUTBOUND_REDIRECT
		fi
	done
	# loopback traffic
	iptables -t nat -A EDGEMESH_OUTBOUND -d 127.0.0.1/32 -j RETURN
	
	# hijacking
	if [ ${#ipv4_include_list[@]} -gt 0 ]; then
		# include Ips and ports are *
		if [[ "${ipv4_include_list}" == "*" && "${EDGEMESH_HIJACK_PORT}" == "*" ]]; then
			iptables -t nat -A EDGEMESH_OUTBOUND -p tcp -j EDGEMESH_OUTBOUND_REDIRECT
		else
			if [ "${ipv4_include_list}" != "*" ]; then
				for ip in "${ipv4_include_list[@]}"; do
					iptables -t nat -A EDGEMESH_OUTBOUND -p tcp -d "${ip}" -j EDGEMESH_OUTBOUND_REDIRECT
				done
			fi
			if [ "${EDGEMESH_HIJACK_PORT}" != "*" ]; then
				for port in "${port_include_list[@]}"; do 
					iptables -t nat -A EDGEMESH_OUTBOUND -p tcp --dport "${port}" -j EDGEMESH_OUTBOUND_REDIRECT
				done
			fi
			
			iptables -t nat -A EDGEMESH_OUTBOUND -j RETURN
		fi
	fi
}

# variable
ipv4_exclude_list=()
ipv4_include_list=()
ipv6_exclude_list=()
ipv6_exclude_list=()
port_exclude_list=()
port_include_list=()

splt='/*'
EDGEMESH_PROXY_PORT=${PROXY_PORT-10001}  # default PROXY_PORT 10001
EDGEMESH_HIJACK_IP=${HIJACK_IP-"*"}
EDGEMESH_HIJACK_PORT=${HIJACK_PORT-"*"}
EDGEMESH_EXCLUDE_IP=${EXCLUDE_IP-}
EDGEMESH_EXCLUDE_PORT=${EXCLUDE_PORT-}

function main() {
	getContainerNetMode
	
	while getopts ":p:i:t:b:c:h" opt; do
		case ${opt} in
			p)
				EDGEMESH_PROXY_PORT=${OPTARG}
				;;
			i)
				EDGEMESH_HIJACK_IP=${OPTARG}
				;;
			t) 
				EDGEMESH_HIJACK_PORT=${OPTARG}
				;;
			b)
				EDGEMESH_EXCLUDE_IP=${OPTARG}
				;;
			c) 
				EDGEMESH_EXCLUDE_PORT=${OPTARG}
				;;
			h)
				usage
				exit 0
				;;
			?)
				echo "Invalid option: -$OPTARG" >&2
				usage
				exit 1
				;;
		esac
	done
	
	echo "EdgeMesh iptables configration:"
	echo "====================================="
	echo "Container Network mode is: ${NETMODE}"
	echo "Variables:"
	echo "EDGEMESH_PROXY_PORT=${EDGEMESH_PROXY_PORT-10001}"
	echo "EDGEMESH_HIJACK_IP=${EDGEMESH_HIJACK_IP-"*"}"
	echo "EDGEMESH_HIJACK_PORT=${EDGEMESH_HIJACK_PORT-"*"}"
	echo "EDGEMESH_EXCLUDE_IP=${EDGEMESH_EXCLUDE_IP-}"
	echo "EDGEMESH_EXCLUDE_PORT=${EDGEMESH_EXCLUDE_PORT-}"
	
	# parse parameter
	IFS=',' read -ra EXCLUDE_IP <<< "${EDGEMESH_EXCLUDE_IP}"
	IFS=',' read -ra INCLUDE_IP <<< "${EDGEMESH_HIJACK_IP}"
	# echo "EXCLUDE_IP: ${EXCLUDE_IP}"
	for range in "${EXCLUDE_IP[@]}"; do
		r=${range%$splt}
		if isValidIP "$r"; then
			if isIPv4 "$r"; then
				ipv4_exclude_list+=("$range")
			elif isIPv6 "$r"; then
				ipv6_exclude_list+=("$range")
			fi
		fi
	done
	
	if [ "${EDGEMESH_HIJACK_IP}" == "*" ]; then
		ipv4_include_list=("*")
		ipv6_include_list=("*")
	else
		for range in "${INCLUDE_IP[@]}"; do
			r=${range%$splt}
			if isValidIP "$r";then
				if isIPv4 "$r"; then
					ipv4_include_list+=("$range")
				elif isIPv6 "$r"; then
					ipv6_include_list+=("$range")
				fi
			fi		
		done
	fi
	
	IFS=',' read -ra INCLUDE_PORT <<< "${EDGEMESH_HIJACK_PORT}"
	IFS=',' read -ra EXCLUDE_PORT <<< "${EDGEMESH_EXCLUDE_PORT}"
	if [ "${EDGEMESH_HIJACK_PORT}" != "*" ]; then
		for port in "${INCLUDE_PORT[@]}"; do
			port_include_list+=("$port")
		done
	fi
	
	if [ -n "${EDGEMESH_EXCLUDE_PORT}" ]; then 
		for port in "${EXCLUDE_PORT[@]}"; do 
			port_exclude_list+=("$port")
		done
	fi
	
	echo "ipv4_include_list : ${ipv4_include_list[@]}"
	echo "ipv4_exclude_list : ${ipv4_exclude_list[@]}"
	echo "port_include_list : ${port_include_list[@]}"
	echo "port_exclude_list : ${port_exclude_list[@]}"
	
	# bridge mode(port map) container network
	if [ "${NETMODE}" = "OTHER" ]; then	
		echo " ${NETMODE} iptables configration"
		bridgeNetMode
		# if set ipv6 option
		if false; then
			echo 'TODO'
		fi
	# host mode container network
	elif [ "${NETMODE}" = "HOST" ]; then
		#hostNetMode
		echo ${NETMODE}
		# if set ipv6 option
		if false; then
			echo 'TODO'
		fi
	else
		echo 'Dont support this container network '
	fi
}

# start to configure
main "${@}"