Update generate-config.sh

This commit is contained in:
jahlib
2026-05-07 17:42:40 +03:00
committed by GitHub
parent 2dd39bcc54
commit a5a3f1ea92
+177 -51
View File
@@ -7,6 +7,27 @@ urldecode() {
echo "$1" | sed 's/%3[dD]/=/g; s/%2[bB]/+/g; s/%2[fF]/\//g; s/%2[cC]/,/g'
}
trim() {
echo "$1" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//'
}
normalize_cidr() {
local addr
local suffix
addr=$(trim "$1")
suffix="$2"
if [ -z "$addr" ]; then
echo ""
return
fi
case "$addr" in
*/*) echo "$addr" ;;
*) echo "${addr}/${suffix}" ;;
esac
}
# Extract parameter from URL
get_param() {
local url="$1"
@@ -14,28 +35,22 @@ get_param() {
echo "$url" | sed -n "s/.*[?&]${param}=\([^&#]*\).*/\1/p"
}
# Parse warp.conf
parse_warp_conf() {
# Read the wg:// URL from file
parse_from_wg_url() {
WG_URL=$(grep "^wg://" "$WARP_CONF" | head -1)
if [ -z "$WG_URL" ]; then
echo "Error: No wg:// URL found in $WARP_CONF"
exit 1
fi
# Extract server and port from wg://SERVER:PORT?...
SERVER=$(echo "$WG_URL" | sed 's|wg://\([^:]*\):.*|\1|')
PORT=$(echo "$WG_URL" | sed 's|wg://[^:]*:\([0-9]*\)?.*|\1|')
# Extract parameters
PRIVATE_KEY=$(urldecode "$(get_param "$WG_URL" "private_key")")
PUBLIC_KEY=$(urldecode "$(get_param "$WG_URL" "peer_public_key")")
RESERVED=$(urldecode "$(get_param "$WG_URL" "reserved")")
MTU=$(get_param "$WG_URL" "mtu")
LOCAL_ADDRESS=$(urldecode "$(get_param "$WG_URL" "local_address")")
# Amnezia parameters
Jc=$(get_param "$WG_URL" "junk_packet_count")
Jmin=$(get_param "$WG_URL" "junk_packet_min_size")
Jmax=$(get_param "$WG_URL" "junk_packet_max_size")
@@ -43,30 +58,20 @@ parse_warp_conf() {
H2=$(get_param "$WG_URL" "response_packet_magic_header")
H3=$(get_param "$WG_URL" "underload_packet_magic_header")
H4=$(get_param "$WG_URL" "transport_packet_magic_header")
INIT_JUNK=$(get_param "$WG_URL" "init_packet_junk_size")
RESP_JUNK=$(get_param "$WG_URL" "response_packet_junk_size")
# Parse reserved (formats: 131-184-249 OR 131,184,249)
S1=$(echo "$RESERVED" | tr ',-' '\n' | sed -n '1p')
S2=$(echo "$RESERVED" | tr ',-' '\n' | sed -n '2p')
S3=$(echo "$RESERVED" | tr ',-' '\n' | sed -n '3p')
# Parse local addresses (formats: ipv4-ipv6 OR ipv4,ipv6)
IPV4=$(echo "$LOCAL_ADDRESS" | tr ',-' '\n' | sed -n '1p')
IPV6=$(echo "$LOCAL_ADDRESS" | tr ',-' '\n' | sed -n '2p')
IPV4=$(normalize_cidr "$IPV4" 32)
IPV6=$(normalize_cidr "$IPV6" 128)
if [ -z "$IPV4" ]; then
echo "Error: local_address (IPv4) is empty"
exit 1
fi
if [ -n "$IPV6" ]; then
ADDRESS_JSON=$(printf '["%s", "%s"]' "$IPV4" "$IPV6")
else
ADDRESS_JSON=$(printf '["%s"]' "$IPV4")
fi
# Set defaults if empty
ADDRESS_JSON=$(printf '"%s"' "$IPV4")
MTU=${MTU:-1280}
Jc=${Jc:-4}
Jmin=${Jmin:-40}
@@ -75,40 +80,144 @@ parse_warp_conf() {
H2=${H2:-2}
H3=${H3:-3}
H4=${H4:-4}
INIT_JUNK=${INIT_JUNK:-0}
RESP_JUNK=${RESP_JUNK:-0}
ALLOWED_IPS="0.0.0.0/0"
TAG="wireguard-out"
LOG_LEVEL="debug"
}
parse_from_ini() {
local section=""
local key=""
local value=""
while IFS= read -r line || [ -n "$line" ]; do
line=$(trim "$line")
[ -z "$line" ] && continue
case "$line" in
\#*|\;*) continue ;;
esac
case "$line" in
"[Interface]") section="Interface"; continue ;;
"[Peer]") section="Peer"; continue ;;
esac
key=$(trim "$(echo "$line" | cut -d'=' -f1)")
value=$(trim "$(echo "$line" | cut -d'=' -f2-)")
[ -z "$key" ] && continue
if [ "$section" = "Interface" ]; then
case "$key" in
PrivateKey) PRIVATE_KEY="$value" ;;
Address)
IPV4=$(trim "$(echo "$value" | cut -d',' -f1)")
IPV6=$(trim "$(echo "$value" | cut -d',' -f2)")
;;
MTU) MTU="$value" ;;
S1) S1="$value" ;;
S2) S2="$value" ;;
S3) S3="$value" ;;
Jc) Jc="$value" ;;
Jmin) Jmin="$value" ;;
Jmax) Jmax="$value" ;;
H1) H1="$value" ;;
H2) H2="$value" ;;
H3) H3="$value" ;;
H4) H4="$value" ;;
I1) I1="$value" ;;
esac
elif [ "$section" = "Peer" ]; then
case "$key" in
PublicKey) PUBLIC_KEY="$value" ;;
AllowedIPs)
if echo "$value" | grep -q "0.0.0.0/0"; then
ALLOWED_IPS="0.0.0.0/0"
else
ALLOWED_IPS=$(trim "$(echo "$value" | cut -d',' -f1)")
fi
;;
Endpoint)
SERVER=$(echo "$value" | cut -d':' -f1)
PORT=$(echo "$value" | cut -d':' -f2)
;;
esac
fi
done < "$WARP_CONF"
IPV4=$(normalize_cidr "$IPV4" 32)
IPV6=$(normalize_cidr "$IPV6" 128)
if [ -z "$IPV4" ]; then
echo "Error: Address (IPv4) is empty"
exit 1
fi
ADDRESS_JSON=$(printf '"%s"' "$IPV4")
MTU=${MTU:-1280}
S1=${S1:-0}
S2=${S2:-0}
S3=${S3:-0}
# Debug output
echo "Parsed values:"
echo "SERVER: $SERVER:$PORT"
echo "PRIVATE_KEY: ${PRIVATE_KEY:0:20}..."
echo "PUBLIC_KEY: ${PUBLIC_KEY:0:20}..."
echo "RESERVED: [$S1, $S2, $S3]"
echo "LOCAL_ADDRESS: $IPV4, $IPV6"
echo "MTU: $MTU"
echo "Amnezia: jc=$Jc, jmin=$Jmin, jmax=$Jmax, h1=$H1, h2=$H2, h3=$H3, h4=$H4"
# Generate config.json
Jc=${Jc:-4}
Jmin=${Jmin:-40}
Jmax=${Jmax:-70}
H1=${H1:-1}
H2=${H2:-2}
H3=${H3:-3}
H4=${H4:-4}
ALLOWED_IPS=${ALLOWED_IPS:-0.0.0.0/0}
if [ -z "$SERVER" ] || [ -z "$PORT" ]; then
echo "Error: Endpoint is empty"
exit 1
fi
TAG="wireguard-out"
LOG_LEVEL="debug"
}
write_config() {
if [ -n "$I1" ]; then
H4_COMMA=","
I1_LINE=$(printf ' "i1": "%s"\n' "$I1")
else
H4_COMMA=""
I1_LINE=""
fi
cat > "$OUTPUT_CONFIG" <<EOF
{
"log": {
"level": "info"
"level": "$LOG_LEVEL"
},
"dns": {
"servers": [
{
"type": "local",
"tag": "default"
"tag": "default",
"type": "udp",
"server": "76.76.2.0",
"detour": "direct"
},
{
"tag": "dns-proxy",
"type": "tls",
"server": "176.32.33.6",
"detour": "wireguard-out"
},
{
"tag": "local",
"type": "udp",
"server": "127.0.0.1",
"detour": "direct"
}
]
},
"endpoints": [
{
"type": "wireguard",
"tag": "warp-out",
"tag": "$TAG",
"mtu": $MTU,
"address": $ADDRESS_JSON,
"private_key": "$PRIVATE_KEY",
@@ -118,8 +227,7 @@ parse_warp_conf() {
"address": "$SERVER",
"port": $PORT,
"public_key": "$PUBLIC_KEY",
"allowed_ips": ["0.0.0.0/0", "::/0"],
"reserved": [$S1, $S2, $S3]
"allowed_ips": "$ALLOWED_IPS"
}
],
"udp_timeout": "5m0s",
@@ -127,18 +235,19 @@ parse_warp_conf() {
"jc": $Jc,
"jmin": $Jmin,
"jmax": $Jmax,
"s1": ${S1:-0},
"s2": ${S2:-0},
"h1": $H1,
"h2": $H2,
"h3": $H3,
"h4": $H4
}
"h4": $H4$H4_COMMA
$I1_LINE }
}
],
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "0.0.0.0",
"listen_port": 2080
}
],
@@ -149,7 +258,7 @@ parse_warp_conf() {
}
],
"route": {
"final": "warp-out",
"final": "$TAG",
"default_domain_resolver": "default",
"auto_detect_interface": true
}
@@ -157,6 +266,23 @@ parse_warp_conf() {
EOF
}
parse_warp_conf() {
local first_line
first_line=$(head -n 1 "$WARP_CONF" | tr -d '\r')
first_line=$(trim "$first_line")
if echo "$first_line" | grep -q "^wg://"; then
parse_from_wg_url
elif echo "$first_line" | grep -q "^\[Interface\]"; then
parse_from_ini
else
echo "Error: Unsupported warp.conf format (expected wg:// or [Interface])"
exit 1
fi
write_config
}
# Main
if [ ! -f "$WARP_CONF" ]; then
echo "Error: $WARP_CONF not found!"