forked from herozmy/sing-box-mosdns-fakeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhy2-gohome.sh
More file actions
331 lines (328 loc) · 9.55 KB
/
Copy pathhy2-gohome.sh
File metadata and controls
331 lines (328 loc) · 9.55 KB
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
#!/bin/bash
echo -e "hysteria2 回家 自签证书"
echo -e "开始创建证书存放目录"
mkdir -p /root/hysteria
echo -e "自签bing.com证书100年"
openssl ecparam -genkey -name prime256v1 -out /root/hysteria/private.key && openssl req -new -x509 -days 36500 -key /root/hysteria/private.key -out /root/hysteria/cert.pem -subj "/CN=bing.com"
while true; do
# 提示用户输入域名
read -p "请输入家庭DDNS域名: " domain
# 检查域名格式是否正确
if [[ $domain =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
break
else
echo "域名格式不正确,请重新输入"
fi
done
# 输入端口号
while true; do
read -p "请输入端口号: " hyport
# 检查端口号是否为数字
if [[ $hyport =~ ^[0-9]+$ ]]; then
break
else
echo "端口号格式不正确,请重新输入"
fi
done
read -p "请输入密码: " password
echo "您输入的域名是: $domain"
echo "您输入的端口号是: $hyport"
echo "您输入的密码是: $password"
sleep 2
echo "开始生成配置文件"
# 检查sb配置文件是否存在
config_file="/etc/sing-box/config.json"
if [ ! -f "$config_file" ]; then
echo "错误:配置文件 $config_file 不存在"
echo "请选择生成singbox或者P核singbox config.json脚本"
exit 1
fi
hy_config='{
"type": "hysteria2",
"tag": "hy2-in",
"listen": "::",
"listen_port": '"${hyport}"',
"sniff": true,
"sniff_override_destination": false,
"sniff_timeout": "100ms",
"users": [
{
"password": "'"${password}"'"
}
],
"ignore_client_bandwidth": true,
"tls": {
"enabled": true,
"alpn": [
"h3"
],
"certificate_path": "/root/hysteria/cert.pem",
"key_path": "/root/hysteria/private.key"
}
},
'
line_num=$(grep -n 'inbounds' /etc/sing-box/config.json | cut -d ":" -f 1)
# 如果找到了行号,则在其后面插入 JSON 字符串,否则不进行任何操作
if [ ! -z "$line_num" ]; then
# 将文件分成两部分,然后在中间插入新的 JSON 字符串
head -n "$line_num" /etc/sing-box/config.json > tmpfile
echo "$hy_config" >> tmpfile
tail -n +$(($line_num + 1)) /etc/sing-box/config.json >> tmpfile
mv tmpfile /etc/sing-box/config.json
fi
echo "HY2回家配置写入完成"
echo "开始重启sing-box"
systemctl restart sing-box
echo "开始生成sing-box回家-手机配置"
cat << EOF > "/root/go_home.json"
{
"log": {
"level": "info",
"timestamp": false
},
"dns": {
"servers": [
{
"tag": "dns_proxy",
"address": "tls://1.1.1.1:853",
"strategy": "ipv4_only",
"detour": "proxy"
},
{
"tag": "dns_direct",
"address": "https://223.5.5.5/dns-query",
"strategy": "prefer_ipv6",
"detour": "direct"
},
{
"tag": "dns_resolver",
"address": "223.5.5.5",
"detour": "direct"
},
{
"tag": "dns_success",
"address": "rcode://success"
},
{
"tag": "dns_refused",
"address": "rcode://refused"
},
{
"tag": "dns_fakeip",
"address": "fakeip"
}
],
"rules": [
{
"domain_suffix": [
"${domain}"
],
"server": "dns_direct",
"disable_cache": true
},
{
"rule_set": "geosite-cn",
"query_type": [
"A",
"AAAA"
],
"server": "dns_direct"
},
{
"rule_set": "geosite-cn",
"query_type": [
"CNAME"
],
"server": "dns_direct"
},
{
"rule_set": "geosite-geolocation-!cn",
"query_type": [
"A"
],
"server": "dns_fakeip"
},
{
"rule_set": "geosite-geolocation-!cn",
"query_type": [
"CNAME"
],
"server": "dns_proxy"
},
{
"query_type": [
"A",
"AAAA",
"CNAME"
],
"invert": true,
"server": "dns_refused",
"disable_cache": true
}
],
"final": "dns_proxy",
"independent_cache": true,
"fakeip": {
"enabled": true,
"inet4_range": "198.18.0.0/15",
"inet6_range": "fc00::/18"
}
},
"route": {
"rule_set": [
{
"tag": "geosite-category-ads-all",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-category-ads-all.srs",
"download_detour": "proxy"
},
{
"tag": "geosite-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-cn.srs",
"download_detour": "proxy"
},
{
"tag": "geosite-geolocation-!cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-geolocation-!cn.srs",
"download_detour": "proxy"
},
{
"tag": "geoip-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geoip/rule-set/geoip-cn.srs",
"download_detour": "proxy"
}
],
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"ip_cidr": [
"10.10.10.0/24"
],
"outbound": "telecom_home"
},
{
"network": "udp",
"port": 443,
"outbound": "block"
},
{
"domain_suffix": [
".cn"
],
"outbound": "direct"
},
{
"domain_suffix": [
"office365.com"
],
"outbound": "direct"
},
{
"domain_suffix": [
"push.apple.com",
"iphone-ld.apple.com",
"lcdn-locator.apple.com",
"lcdn-registration.apple.com"
],
"outbound": "direct"
},
{
"rule_set": "geosite-cn",
"outbound": "direct"
},
{
"rule_set": "geosite-geolocation-!cn",
"outbound": "proxy"
},
{
"rule_set": "geoip-cn",
"outbound": "direct"
},
{
"ip_is_private": true,
"outbound": "direct"
}
],
"final": "proxy",
"auto_detect_interface": true
},
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"inet4_address": "172.16.0.1/30",
"inet6_address": "fd00::1/126",
"mtu": 1400,
"auto_route": true,
"strict_route": true,
"stack": "gvisor",
"sniff": true,
"sniff_override_destination": false
}
],
"outbounds": [
{
"tag":"proxy",
"type":"selector",
"outbounds":[
"telecom_home"
]
},
{
"type": "hysteria2",
"server": "${domain}",
"server_port": ${hyport},
"tag": "telecom_home",
"up_mbps": 50,
"down_mbps": 500,
"password": "${password}",
"tls": {
"enabled": true,
"server_name": "bing.com",
"insecure": true,
"alpn": [
"h3"
]
}
},
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
}
],
"experimental": {
"cache_file": {
"enabled": true,
"path": "cache.db",
"store_fakeip": true
}
}
}
EOF
sleep 1
echo "=================================================================="
echo -e "\t\t\tSing-Box 回家配置生成完毕"
echo -e "\t\t\tPowered by www.herozmy.com 2024"
echo -e "\n"
echo -e "sing-box 回家配置生成路径为: /root/go_home.json\t\t请自行复制至 sing-box 客户端"
echo -e "温馨提示:\n本脚本仅在 LXC ubuntu22.04 环境下测试,其他环境未经验证,仅供个人使用"
echo -e "本脚本仅适用于学习与研究等个人用途,请勿用于任何违反国家法律的活动!"
echo "================================================================="