-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload.sh
executable file
·118 lines (100 loc) · 3.44 KB
/
reload.sh
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
#!/bin/bash
# Function to handle SIGTERM
terminate() {
echo "Received SIGTERM, terminating..."
exit 0
}
# Function to modify config.yaml
modify_config() {
config_file="/config/config.yaml"
# Modify specific lines in config.yaml
sed -i '/^mode:/d' "$config_file"
sed -i '/^secret:/d' "$config_file"
sed -i '/^port: 789.*/d' "$config_file"
sed -i '/^allow-lan:/d' "$config_file"
sed -i '/^socks-port:/d' "$config_file"
sed -i '/^mixed-port:/d' "$config_file"
sed -i '/^log-level:/d' "$config_file"
sed -i '/^external-ui:/d' "$config_file"
sed -i '/^external-controller:/d' "$config_file"
sed -i '/^ sniff-tls-sni:/d' "$config_file"
sed -i '/^experimental:/d' "$config_file"
sed -i '/^tun:$/,+9d' "$config_file"
sed -i '/^ - DOMAIN-SUFFIX,httpbin.org,DIRECT$/d' "$config_file"
sed -i 's/^rules:$/rules:\n - DOMAIN-SUFFIX,httpbin.org,DIRECT/' "$config_file"
cat <<EOL >> "$config_file"
mode: Rule
mixed-port: 7890
allow-lan: true
log-level: warning
external-ui: /var/app/webui
external-controller: 0.0.0.0:9090
experimental:
sniff-tls-sni: true
EOL
if [ -n "$PROXY_SECRET" ]; then
echo "secret: \"$PROXY_SECRET\"" >> "$config_file"
else
echo 'secret: ""' >> "$config_file"
fi
# Add content to the end of config.yaml
if [ "$TUN_ENABLE" = "true" ]; then
cat <<EOL >> "$config_file"
tun:
enable: true
stack: system
dns-hijack:
- 8.8.8.8:53
- tcp://8.8.8.8:53
- any:53
- tcp://any:53
auto-route: true
auto-detect-interface: true
EOL
fi
}
# Trap SIGTERM signal
trap terminate SIGTERM
# Default sleep time in minutes
default_sleep_minutes=15
# Check if the sleep time is specified in the environment variable, otherwise use the default
sleep_minutes=${REFRESH_CONFIG_INTERVAL:-$default_sleep_minutes}
# Convert minutes to seconds
sleep_seconds=$((sleep_minutes * 60))
# Loop indefinitely
while true; do
# Read the first line of /config/rss_url and trim whitespace
touch /config/rss_url
url=$(head -n 1 /config/rss_url | xargs)
if [ -n "$url" ]; then
# Download the content from the URL and save it as config.yaml
curl_output=$(curl -sS -m 5 -w "\n%{http_code}\n" -o /config/config.yaml "$url")
# Extract the HTTP status code from the response
http_code=$(echo "$curl_output" | tail -n 1)
# Check if the HTTP status code is not 200
if [ "$http_code" -ne 200 ]; then
echo "Failed to download config file: HTTP $http_code"
continue
fi
# Modify the downloaded config.yaml
modify_config
# Perform a PUT request to the specified address and capture response and HTTP code
response=$(curl -sS -m 5 -w "\n%{http_code}\n" -X PUT -H "Content-Type: application/json" \
-H "Authorization: Bearer $PROXY_SECRET" \
-d '{"path": "/config/config.yaml"}' \
http://127.0.0.1:9090/configs)
# Extract the HTTP status code from the response
http_code=$(echo "$response" | tail -n 1)
if [ "$http_code" -eq 204 ]; then
echo "Config reloaded."
else
# Print the response and the HTTP code for debugging
echo "Failed to reload config: HTTP $http_code"
echo "$response"
fi
else
echo "URL is empty or not found"
fi
# Sleep for a specified interval before the next iteration
sleep "$sleep_seconds"
done