chore: move to jq

This commit is contained in:
Ivan K
2024-12-12 16:34:56 +03:00
parent e66ee9dda6
commit a4fcbfd70a

View File

@@ -970,20 +970,24 @@ sing_box_config_vless() {
server=$(echo "$STRING" | cut -d'@' -f2 | cut -d':' -f1 | tr -d '\n' | tr -d '\r' | sed 's/False//g') server=$(echo "$STRING" | cut -d'@' -f2 | cut -d':' -f1 | tr -d '\n' | tr -d '\r' | sed 's/False//g')
port=$(echo "$STRING" | cut -d'@' -f2 | cut -d':' -f2 | cut -d'?' -f1 | cut -d'/' -f1 | cut -d'#' -f1 | tr -d '\n' | tr -d '\r' | sed 's/False//g') port=$(echo "$STRING" | cut -d'@' -f2 | cut -d':' -f2 | cut -d'?' -f1 | cut -d'/' -f1 | cut -d'#' -f1 | tr -d '\n' | tr -d '\r' | sed 's/False//g')
type=$(get_param "type") jq -n \
security=$(get_param "security") --arg listen_port "$listen_port" \
sni=$(get_param "sni") --arg server "$server" \
fp=$(get_param "fp") --argjson port "$port" \
flow=$(get_param "flow") --arg uuid "$uuid" \
pbk=$(get_param "pbk") --arg type "$(get_param "type")" \
sid=$(get_param "sid") --arg flow "$(get_param "flow")" \
path=$(get_param "path") --arg sni "$(get_param "sni")" \
host=$(get_param "host") --arg fp "$(get_param "fp")" \
spx=$(get_param "spx") --arg security "$(get_param "security")" \
alpn=$(get_param "alpn") --arg pbk "$(get_param "pbk")" \
--arg sid "$(get_param "sid")" \
cat > /tmp/vless_temp.json << EOF --arg alpn "$(get_param "alpn")" \
{ --arg path "$(get_param "path")" \
--arg host "$(get_param "host")" \
--arg spx "$(get_param "spx")" \
--arg insecure "$(get_param "allowInsecure")" \
'{
"log": { "log": {
"level": "warn" "level": "warn"
}, },
@@ -991,107 +995,71 @@ sing_box_config_vless() {
{ {
"type": "tproxy", "type": "tproxy",
"listen": "::", "listen": "::",
"listen_port": $listen_port, "listen_port": ($listen_port|tonumber),
"sniff": false "sniff": false
} }
], ],
"outbounds": [ "outbounds": [
{ {
"type": "vless", "type": "vless",
"server": "$server", "server": $server,
"server_port": $port, "server_port": ($port|tonumber),
"uuid": "$uuid", "uuid": $uuid,
"packet_encoding": "", "packet_encoding": "",
"domain_strategy": "" "domain_strategy": ""
EOF
if [ -n "$flow" ]; then
echo " ,\"flow\": \"$flow\"" >> /tmp/vless_temp.json
fi
if [ "$type" = "ws" ]; then
cat >> /tmp/vless_temp.json << EOF
,
"transport": {
"type": "ws",
"path": "$path"
EOF
if [ -n "$host" ]; then
cat >> /tmp/vless_temp.json << EOF
,
"headers": {
"Host": "$host"
}
EOF
fi
echo " }" >> /tmp/vless_temp.json
elif [ "$type" = "grpc" ]; then
cat >> /tmp/vless_temp.json << EOF
,
"transport": {
"type": "grpc"
}
EOF
elif [ "$type" = "tcp" ]; then
: # tcp doesn't need additional transport configuration
fi
if [ "$security" = "reality" ]; then
cat >> /tmp/vless_temp.json << EOF
,
"tls": {
"enabled": true,
"server_name": "$sni",
"utls": {
"enabled": true,
"fingerprint": "$fp"
},
"reality": {
"enabled": true,
"public_key": "$pbk",
"short_id": "$sid"
}
}
EOF
elif [ "$security" = "tls" ]; then
cat >> /tmp/vless_temp.json << EOF
,
"tls": {
"enabled": true,
"server_name": "$sni",
"insecure": $([ "$(get_param 'allowInsecure')" = "1" ] && echo "true" || echo "false"),
"utls": {
"enabled": true,
"fingerprint": "$fp"
}
EOF
if [ -n "$alpn" ]; then
local alpn_json=$(echo "$alpn" | tr ',' '\n' | jq -R . | jq -s .)
echo " ,\"alpn\": $alpn_json" >> /tmp/vless_temp.json
fi
echo " }" >> /tmp/vless_temp.json
fi
cat >> /tmp/vless_temp.json << EOF
} }
], ],
"route": { "route": {
"auto_detect_interface": true "auto_detect_interface": true
} }
} } |
EOF
if jq . /tmp/vless_temp.json >/tmp/vless_config.json 2>/dev/null; then if $flow != "" then .outbounds[0].flow = $flow else . end |
mv /tmp/vless_config.json /etc/sing-box/config.json
if $type == "ws" then
.outbounds[0].transport = {
"type": "ws",
"path": $path
} |
if $host != "" then
.outbounds[0].transport.headers = {
"Host": $host
}
else . end
elif $type == "grpc" then
.outbounds[0].transport = {
"type": "grpc"
}
else . end |
if $security == "reality" or $security == "tls" then
.outbounds[0].tls = {
"enabled": true,
"server_name": $sni,
"utls": {
"enabled": true,
"fingerprint": $fp
},
"insecure": ($insecure == "1")
} |
if $alpn != "" then
.outbounds[0].tls.alpn = ($alpn | split(","))
else . end |
if $security == "reality" then
.outbounds[0].tls.reality = {
"enabled": true,
"public_key": $pbk,
"short_id": $sid
}
else . end
else . end' > /etc/sing-box/config.json
if [ $? -eq 0 ]; then
echo "Config created successfully" echo "Config created successfully"
else else
echo "Error: Invalid JSON config generated" echo "Error: Invalid JSON config generated"
cat /tmp/vless_temp.json
rm -f /tmp/vless_temp.json
return 1 return 1
fi fi
rm -f /tmp/vless_temp.json
} }
sing_box_config_check() { sing_box_config_check() {