forked from hwdsl2/docker-ipsec-vpn-server
-
Notifications
You must be signed in to change notification settings - Fork 0
342 lines (316 loc) · 14.1 KB
/
cron-debian.yml
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#
# Copyright (C) 2020-2022 Lin Song <linsongui@gmail.com>
#
# This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
# Unported License: http://creativecommons.org/licenses/by-sa/3.0/
#
# Attribution required: please include my name in any derivative and let me
# know how you have improved it!
name: buildx debian cron
on:
schedule:
- cron: '25 2,14 * * *'
jobs:
check_update:
runs-on: ubuntu-20.04
if: github.repository_owner == 'hwdsl2'
env:
DOCKER_USER: ${{ github.repository_owner }}
outputs:
should_test: ${{ steps.check.outputs.should_test }}
should_update: ${{ steps.check.outputs.should_update }}
steps:
- name: Cache
uses: actions/cache@v2
with:
path: |
${{ runner.temp }}/.buildx-bin
${{ runner.temp }}/.buildx-cache
${{ runner.temp }}/.docker-images
key: ${{ secrets.CACHE_NAME }}-${{ github.sha }}-${{ github.run_id }}-check
restore-keys: |
${{ secrets.CACHE_NAME }}-
- name: Prepare
run: |
docker pull debian:buster-slim
docker pull "$DOCKER_USER/ipsec-vpn-server:debian"
- name: Check
id: check
run: |
DEBIAN_UPDATED=false
debian_ts=$(docker inspect --format='{{.Created}}' debian:buster-slim)
image_ts=$(docker inspect --format='{{.Created}}' "$DOCKER_USER/ipsec-vpn-server:debian")
if [ -n "$debian_ts" ] && [ -n "$image_ts" ]; then
debian_ts_s=$(date -d "$debian_ts" +%s)
image_ts_s=$(date -d "$image_ts" +%s)
ts_now=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
ts_now_s=$(date -d "$ts_now" +%s)
diff_s=$((ts_now_s - debian_ts_s))
diff=$(printf '%dd %dh:%dm:%ds\n' $(($diff_s/86400)) $(($diff_s%86400/3600)) $(($diff_s%3600/60)) $(($diff_s%60)))
echo "Debian update time: $debian_ts"
echo "Image update time: $image_ts"
echo "Current time: $ts_now"
echo "Time diff (cur-debian): $diff (${diff_s}s)"
if [ -n "$debian_ts_s" ] && [ -n "$image_ts_s" ] \
&& [ "$debian_ts_s" -ge "$image_ts_s" ] \
&& [ "$diff_s" -ge 14400 ]; then
echo "Starting build..."
DEBIAN_UPDATED=true
else
echo "Not starting build."
fi
fi
echo ::set-output name=should_test::${DEBIAN_UPDATED}
echo ::set-output name=should_update::${DEBIAN_UPDATED}
vpn_test:
needs: check_update
runs-on: ubuntu-20.04
if: needs.check_update.outputs.should_test == 'true'
strategy:
matrix:
test_id: [no-env, with-env]
fail-fast: false
env:
TEST_ID: ${{ matrix.test_id }}
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- name: Test
run: |
[ ! -x /usr/bin/docker ] && exit 1
cd "$GITHUB_WORKSPACE"
pwd
ls -ld vpn.env.example
sed -i '/ swan_ver_latest=/s/^/#/' run.sh
sed -i \
-e '/VPN_IPSEC_PSK/s/# //' \
-e '/VPN_USER/s/# //' \
-e '/VPN_PASSWORD/s/# //' \
-e '/VPN_DNS_NAME/s/# //' \
-e '/VPN_CLIENT_NAME/s/# //' \
-e '/VPN_DNS_SRV1/s/# //' \
-e '/VPN_DNS_SRV2/s/# //' vpn.env.example
set -x
docker build -f Dockerfile.debian -t vpn-test .
if [ "$TEST_ID" = "with-env" ]; then
docker run \
--name "$TEST_ID" \
--env-file ./vpn.env.example \
--restart=always \
-v ikev2-vpn-data:/etc/ipsec.d \
-p 500:500/udp \
-p 4500:4500/udp \
-d --cap-add=NET_ADMIN \
--device=/dev/ppp \
--sysctl net.ipv4.ip_forward=1 \
--sysctl net.ipv4.conf.all.accept_redirects=0 \
--sysctl net.ipv4.conf.all.send_redirects=0 \
--sysctl net.ipv4.conf.all.rp_filter=0 \
--sysctl net.ipv4.conf.default.accept_redirects=0 \
--sysctl net.ipv4.conf.default.send_redirects=0 \
--sysctl net.ipv4.conf.default.rp_filter=0 \
--sysctl net.ipv4.conf.eth0.send_redirects=0 \
--sysctl net.ipv4.conf.eth0.rp_filter=0 \
vpn-test
elif [ "$TEST_ID" = "no-env" ]; then
docker run \
--name "$TEST_ID" \
--restart=always \
-p 500:500/udp \
-p 4500:4500/udp \
-d --privileged \
vpn-test
fi
sleep 30
docker ps | grep "$TEST_ID"
docker logs "$TEST_ID"
docker exec "$TEST_ID" netstat -anpu | grep pluto
docker exec "$TEST_ID" netstat -anpu | grep xl2tpd
docker exec "$TEST_ID" iptables -nvL
docker exec "$TEST_ID" iptables -nvL | grep -q 'ppp+'
docker exec "$TEST_ID" iptables -nvL | grep -q '192\.168\.43\.0/24'
docker exec "$TEST_ID" iptables -nvL -t nat
docker exec "$TEST_ID" iptables -nvL -t nat | grep -q '192\.168\.42\.0/24'
docker exec "$TEST_ID" iptables -nvL -t nat | grep -q '192\.168\.43\.0/24'
docker exec "$TEST_ID" ipsec status
docker exec "$TEST_ID" ipsec status | grep -q l2tp-psk
docker exec "$TEST_ID" ipsec status | grep -q xauth-psk
if [ "$TEST_ID" = "with-env" ]; then
source ./vpn.env.example
docker exec "$TEST_ID" grep "$VPN_IPSEC_PSK" /etc/ipsec.secrets
docker exec "$TEST_ID" grep "$VPN_USER" /etc/ppp/chap-secrets
docker exec "$TEST_ID" grep "$VPN_PASSWORD" /etc/ppp/chap-secrets
docker exec "$TEST_ID" grep "$VPN_USER" /etc/ipsec.d/passwd
docker exec "$TEST_ID" ipsec status | grep -q ikev2-cp
docker exec "$TEST_ID" grep "leftid=vpn.example.com" /etc/ipsec.conf
docker exec "$TEST_ID" grep 'modecfgdns="1.1.1.1 1.0.0.1"' /etc/ipsec.conf
docker exec "$TEST_ID" grep 'ms-dns 1.1.1.1' /etc/ppp/options.xl2tpd
docker exec "$TEST_ID" grep 'ms-dns 1.0.0.1' /etc/ppp/options.xl2tpd
docker exec "$TEST_ID" grep 'leftid=@vpn.example.com' /etc/ipsec.d/ikev2.conf
docker exec "$TEST_ID" grep 'modecfgdns="1.1.1.1 1.0.0.1"' /etc/ipsec.d/ikev2.conf
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/your_client_name.mobileconfig
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/your_client_name.sswan
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/your_client_name.p12
docker exec "$TEST_ID" grep 'vpn.example.com' /etc/ipsec.d/your_client_name.mobileconfig
docker exec "$TEST_ID" grep 'vpn.example.com' /etc/ipsec.d/your_client_name.sswan
docker exec "$TEST_ID" ikev2.sh --addclient vpnclient2
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/vpnclient2.mobileconfig
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/vpnclient2.sswan
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/vpnclient2.p12
docker exec "$TEST_ID" rm -f /etc/ipsec.d/vpnclient2*
docker exec "$TEST_ID" ikev2.sh --exportclient vpnclient2
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/vpnclient2.mobileconfig
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/vpnclient2.sswan
docker exec "$TEST_ID" ls -ld /etc/ipsec.d/vpnclient2.p12
docker exec "$TEST_ID" ikev2.sh --listclients
echo "y" | docker exec -i "$TEST_ID" ikev2.sh --revokeclient vpnclient2
else
docker exec "$TEST_ID" ipsec status | grep -q ikev2-cp && exit 1
fi
docker restart "$TEST_ID"
sleep 10
docker ps | grep "$TEST_ID"
docker logs "$TEST_ID"
docker exec "$TEST_ID" netstat -anpu | grep pluto
docker exec "$TEST_ID" netstat -anpu | grep xl2tpd
docker exec "$TEST_ID" iptables -nvL
docker exec "$TEST_ID" iptables -nvL | grep -q 'ppp+'
docker exec "$TEST_ID" iptables -nvL | grep -q '192\.168\.43\.0/24'
docker exec "$TEST_ID" iptables -nvL -t nat
docker exec "$TEST_ID" iptables -nvL -t nat | grep -q '192\.168\.42\.0/24'
docker exec "$TEST_ID" iptables -nvL -t nat | grep -q '192\.168\.43\.0/24'
docker exec "$TEST_ID" ipsec status
docker exec "$TEST_ID" ipsec status | grep -q l2tp-psk
docker exec "$TEST_ID" ipsec status | grep -q xauth-psk
if [ "$TEST_ID" = "with-env" ]; then
docker exec "$TEST_ID" ipsec status | grep -q ikev2-cp
echo "y" | docker exec -i "$TEST_ID" ikev2.sh --removeikev2
sleep 3
fi
docker exec "$TEST_ID" ipsec status | grep -q ikev2-cp && exit 1
exit 0
- name: Clear
if: always()
run: |
docker rm -f "$TEST_ID" || true
docker rmi vpn-test || true
buildx:
needs: [check_update, vpn_test]
runs-on: ubuntu-20.04
if: needs.check_update.outputs.should_update == 'true'
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- name: Cache
uses: actions/cache@v2
with:
path: |
${{ runner.temp }}/.buildx-bin
${{ runner.temp }}/.buildx-cache
${{ runner.temp }}/.docker-images
key: ${{ secrets.CACHE_NAME }}-${{ github.sha }}-${{ github.run_id }}
restore-keys: |
${{ secrets.CACHE_NAME }}-
- name: Set up Buildx
env:
RUNNER_TEMP: ${{ runner.temp }}
run: |
[ ! -x /usr/bin/docker ] && exit 1
if [ ! -x /usr/bin/wget ] || [ ! -x /usr/bin/jq ]; then
export DEBIAN_FRONTEND=noninteractive
sudo apt-get -yq update
sudo apt-get -yq install wget jq
fi
BUILDX_VER=$(wget -t 3 -T 30 -qO- "https://api.github.com/repos/docker/buildx/releases/latest" | jq -r ".tag_name")
[ -z "${BUILDX_VER}" ] && exit 1
echo "Buildx version: ${BUILDX_VER}"
DOCKER_DIR="${HOME}/.docker/cli-plugins"
CACHE_DIR="${RUNNER_TEMP}/.buildx-bin"
mkdir -p "${DOCKER_DIR}"
if [ -s "${CACHE_DIR}/docker-buildx-${BUILDX_VER}" ]; then
echo "Using buildx binary from cache..."
cp -f "${CACHE_DIR}/docker-buildx-${BUILDX_VER}" "${DOCKER_DIR}/docker-buildx"
else
echo "Downloading buildx..."
BUILDX_URL="https://github.com/docker/buildx/releases/download/${BUILDX_VER}/buildx-${BUILDX_VER}.linux-amd64"
wget -t 3 -T 30 -nv -O "${DOCKER_DIR}/docker-buildx" "${BUILDX_URL}"
[ ! -s "${DOCKER_DIR}/docker-buildx" ] && exit 1
/bin/rm -rf "${CACHE_DIR}"
mkdir -p "${CACHE_DIR}"
/bin/cp -f "${DOCKER_DIR}/docker-buildx" "${CACHE_DIR}/docker-buildx-${BUILDX_VER}"
fi
chmod a+x "${DOCKER_DIR}/docker-buildx"
docker info
docker buildx version
image_cache_dir="${RUNNER_TEMP}/.docker-images"
for image in "quay.io/hwdsl2/qemu-user-static:latest" "moby/buildkit:buildx-stable-1"; do
image_file=$(printf '%s' "${image}.tar.gz" | tr '/' '-' | tr ':' '-')
if [ -s "${image_cache_dir}/${image_file}" ]; then
echo "Using ${image} from cache..."
docker load -i "${image_cache_dir}/${image_file}"
else
echo "Downloading ${image}..."
docker pull -q "${image}"
mkdir -p "${image_cache_dir}"
docker save "${image}" | gzip > "${image_cache_dir}/${image_file}"
fi
done
docker run --rm --privileged quay.io/hwdsl2/qemu-user-static:latest --reset -p yes --credential yes
docker buildx create --name "builder-${GITHUB_SHA::8}" --driver docker-container --use
docker buildx inspect --bootstrap
- name: Docker Hub Login
env:
DOCKER_USER: ${{ github.repository_owner }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
run: echo "$DOCKER_TOKEN" | docker login --username "$DOCKER_USER" --password-stdin 2>&1
- name: Quay.io Login
env:
QUAY_USER: ${{ secrets.QUAY_USER }}
QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
run: echo "$QUAY_TOKEN" | docker login quay.io --username "$QUAY_USER" --password-stdin 2>&1
- name: Docker Buildx
env:
DOCKER_USER: ${{ github.repository_owner }}
BUILD_ONLY: ${{ secrets.BUILD_ONLY }}
RUNNER_TEMP: ${{ runner.temp }}
run: |
cd "$GITHUB_WORKSPACE"
pwd
ls -ld vpn.env.example
[ "$BUILD_ONLY" = "true" ] && BUILD_ONLY=""
ts_now=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
docker buildx build \
--progress plain \
--platform=linux/amd64,linux/arm64,linux/arm/v7 \
--build-arg BUILD_DATE=$ts_now \
--build-arg VCS_REF=${GITHUB_SHA::8} \
--build-arg VERSION=debian-latest \
--cache-from type=local,src="${RUNNER_TEMP}/.buildx-cache" \
--cache-to type=local,dest="${RUNNER_TEMP}/.buildx-cache-new" \
-f Dockerfile.debian \
-t "$DOCKER_USER/ipsec-vpn-server:debian" \
--pull \
${BUILD_ONLY:+--push} \
.
docker buildx build \
--progress plain \
--platform=linux/amd64,linux/arm64,linux/arm/v7 \
--build-arg BUILD_DATE=$ts_now \
--build-arg VCS_REF=${GITHUB_SHA::8} \
--build-arg VERSION=debian-latest \
--cache-from type=local,src="${RUNNER_TEMP}/.buildx-cache-new" \
-f Dockerfile.debian \
-t "quay.io/$DOCKER_USER/ipsec-vpn-server:debian" \
--pull \
${BUILD_ONLY:+--push} \
.
/bin/rm -rf "${RUNNER_TEMP}/.buildx-cache"
/bin/mv -f "${RUNNER_TEMP}/.buildx-cache-new" "${RUNNER_TEMP}/.buildx-cache"
- name: Clear
if: always()
run: |
shred -u "${HOME}/.docker/config.json"
rm -f "${HOME}/.docker/config.json"
docker buildx rm "builder-${GITHUB_SHA::8}" || true