ansible-core-net / deploy (push) Failing after 1m27s
API отдаёт массив пар, а не одиночные privateKey/publicKey. Co-authored-by: Cursor <cursoragent@cursor.com>
287 lines
10 KiB
Django/Jinja
287 lines
10 KiB
Django/Jinja
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
API_BASE="https://${RW_PANEL_DOMAIN}/api"
|
|
# Admin JWT без X-Remnawave-Client-Type: browser → 403 на всех эндпоинтах (Remnawave ≥3).
|
|
CURL=(curl -sSk --resolve "${RW_PANEL_DOMAIN}:443:127.0.0.1" -H "X-Remnawave-Client-Type: browser")
|
|
AUTH_HDR=()
|
|
|
|
admin_created=0
|
|
token_updated=0
|
|
profile_created=0
|
|
node_created=0
|
|
host_created=0
|
|
node_secret=""
|
|
active_token="${RW_API_TOKEN:-}"
|
|
|
|
api() {
|
|
local method="$1"; shift
|
|
local path="$1"; shift
|
|
local tmp code
|
|
tmp=$(mktemp)
|
|
if [ -n "${AUTH_HDR[*]:-}" ]; then
|
|
code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" "${AUTH_HDR[@]}" \
|
|
-H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true
|
|
else
|
|
code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" \
|
|
-H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true
|
|
fi
|
|
if [[ ! "${code}" =~ ^2 ]]; then
|
|
echo "API ${method} ${path} → HTTP ${code}" >&2
|
|
cat "$tmp" >&2 || true
|
|
echo >&2
|
|
rm -f "$tmp"
|
|
return 1
|
|
fi
|
|
cat "$tmp"
|
|
rm -f "$tmp"
|
|
}
|
|
|
|
status_json=$("${CURL[@]}" "${API_BASE}/auth/status")
|
|
is_register=$(printf '%s' "${status_json}" | jq -r '.response.isRegisterAllowed // .isRegisterAllowed // false')
|
|
|
|
# Весь bootstrap держим на admin JWT + browser. API-токен создаём отдельно
|
|
# и НЕ переключаем на него: иначе пустые/неверные scopes → 403 на /keygen/.
|
|
if [ "${is_register}" = "true" ]; then
|
|
reg_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}')
|
|
reg_resp=$(api POST /auth/register -d "${reg_payload}")
|
|
access=$(printf '%s' "${reg_resp}" | jq -r '.response.accessToken // .accessToken // empty')
|
|
if [ -z "${access}" ]; then
|
|
echo "Не удалось зарегистрировать админа Remnawave" >&2
|
|
printf '%s\n' "${reg_resp}" >&2
|
|
exit 1
|
|
fi
|
|
AUTH_HDR=(-H "Authorization: Bearer ${access}")
|
|
admin_created=1
|
|
else
|
|
login_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}')
|
|
login_resp=$(api POST /auth/login -d "${login_payload}")
|
|
access=$(printf '%s' "${login_resp}" | jq -r '.response.accessToken // .accessToken // empty')
|
|
if [ -z "${access}" ]; then
|
|
echo "Не удалось войти в панель Remnawave (REMNAWAVE_ADMIN_* из /services/remnawave)" >&2
|
|
printf '%s\n' "${login_resp}" >&2
|
|
exit 1
|
|
fi
|
|
AUTH_HDR=(-H "Authorization: Bearer ${access}")
|
|
fi
|
|
|
|
# Постоянный API-токен: scopes ["*"] (wildcard из GET /tokens/scopes).
|
|
# Если создать нельзя — не валим bootstrap; пользователь может положить токен вручную.
|
|
if [ -z "${active_token}" ] || [ "${admin_created}" -eq 1 ]; then
|
|
tokens_json=$(api GET /tokens/ || echo '{"response":[]}')
|
|
old_uuid=$(printf '%s' "${tokens_json}" | jq -r --arg n "$RW_TOKEN_NAME" '
|
|
(.response // .)
|
|
| if type=="array" then . elif .tokens then .tokens else [] end
|
|
| map(select(.name == $n or .tokenName == $n))
|
|
| .[0].uuid // empty')
|
|
if [ -n "${old_uuid}" ]; then
|
|
api DELETE "/tokens/${old_uuid}" >/dev/null || true
|
|
fi
|
|
|
|
wildcard=$(api GET /tokens/scopes 2>/dev/null | jq -r '.response.wildcard // "*"')
|
|
token_payload=$(jq -n --arg n "$RW_TOKEN_NAME" --arg s "${wildcard:-*}" \
|
|
'{name: $n, expiresInDays: 3650, scopes: [$s]}')
|
|
if token_resp=$(api POST /tokens/ -d "${token_payload}"); then
|
|
created=$(printf '%s' "${token_resp}" | jq -r '
|
|
.response.token // .response.apiToken // .token // .apiToken // empty')
|
|
if [ -n "${created}" ]; then
|
|
active_token="${created}"
|
|
token_updated=1
|
|
else
|
|
echo "WARN: POST /tokens/ без поля token — положите REMNAWAVE_API_TOKEN вручную" >&2
|
|
printf '%s\n' "${token_resp}" >&2
|
|
fi
|
|
else
|
|
echo "WARN: не удалось создать API-токен автоматически — создайте в UI и положите в Infisical /services/remnawave как REMNAWAVE_API_TOKEN" >&2
|
|
fi
|
|
fi
|
|
|
|
# SECRET_KEY ноды — отдельный endpoint /api/keygen/ (не ответ create node)
|
|
if [ -n "${RW_NODE_SECRET:-}" ]; then
|
|
node_secret="${RW_NODE_SECRET}"
|
|
else
|
|
keygen_resp=$(api GET /keygen/)
|
|
node_secret=$(printf '%s' "${keygen_resp}" | jq -r '.response.secretKey // .secretKey // empty')
|
|
if [ -z "${node_secret}" ]; then
|
|
echo "Не удалось получить SECRET_KEY ноды из /api/keygen/" >&2
|
|
printf '%s\n' "${keygen_resp}" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
profiles_json=$(api GET /config-profiles/)
|
|
profile_uuid=$(printf '%s' "${profiles_json}" | jq -r --arg n "$RW_PROFILE_NAME" '
|
|
(.response.configProfiles // .response // [])
|
|
| map(select(.name == $n)) | .[0].uuid // empty')
|
|
pub=""
|
|
|
|
if [ -z "${profile_uuid}" ]; then
|
|
key_resp=$(api GET /system/tools/x25519/generate 2>/dev/null || echo '{}')
|
|
# v3: {response:{keypairs:[{publicKey,privateKey},...]}}
|
|
priv=$(printf '%s' "${key_resp}" | jq -r '
|
|
.response.keypairs[0].privateKey
|
|
// .response.privateKey // .response.privKey // .privateKey // .privKey // empty')
|
|
pub=$(printf '%s' "${key_resp}" | jq -r '
|
|
.response.keypairs[0].publicKey
|
|
// .response.publicKey // .response.pubKey // .publicKey // .pubKey // empty')
|
|
if [ -z "${priv}" ] || [ -z "${pub}" ]; then
|
|
echo "Не удалось сгенерировать X25519 ключи Reality" >&2
|
|
printf '%s\n' "${key_resp}" >&2
|
|
exit 1
|
|
fi
|
|
short_id=$(openssl rand -hex 8)
|
|
profile_config=$(sed \
|
|
-e "s|__REALITY_PRIVATE_KEY__|${priv}|g" \
|
|
-e "s|__REALITY_SHORT_ID__|${short_id}|g" \
|
|
"$RW_PROFILE_FILE")
|
|
create_profile=$(jq -n --arg n "$RW_PROFILE_NAME" --argjson c "${profile_config}" '{name: $n, config: $c}')
|
|
profile_resp=$(api POST /config-profiles/ -d "${create_profile}")
|
|
profile_uuid=$(printf '%s' "${profile_resp}" | jq -r '.response.uuid // .uuid // empty')
|
|
if [ -z "${profile_uuid}" ]; then
|
|
echo "Не удалось создать config profile" >&2
|
|
printf '%s\n' "${profile_resp}" >&2
|
|
exit 1
|
|
fi
|
|
profile_created=1
|
|
fi
|
|
|
|
profile_full=$(api GET "/config-profiles/${profile_uuid}")
|
|
inbound_uuid=$(printf '%s' "${profile_full}" | jq -r --arg t "$RW_INBOUND_TAG" '
|
|
(.response.inbounds // .inbounds // [])
|
|
| map(select(.tag == $t)) | .[0].uuid // empty')
|
|
if [ -z "${inbound_uuid}" ]; then
|
|
inbounds_json=$(api GET "/config-profiles/${profile_uuid}/inbounds" 2>/dev/null || echo '{}')
|
|
inbound_uuid=$(printf '%s' "${inbounds_json}" | jq -r --arg t "$RW_INBOUND_TAG" '
|
|
(.response // .)
|
|
| if type=="array" then . else (.inbounds // []) end
|
|
| map(select(.tag == $t)) | .[0].uuid // empty')
|
|
fi
|
|
if [ -z "${inbound_uuid}" ]; then
|
|
echo "Не найден inbound ${RW_INBOUND_TAG} в профиле ${profile_uuid}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
nodes_json=$(api GET /nodes/)
|
|
node_uuid=$(printf '%s' "${nodes_json}" | jq -r --arg n "$RW_NODE_NAME" '
|
|
(.response // []) | map(select(.name == $n)) | .[0].uuid // empty')
|
|
|
|
if [ -z "${node_uuid}" ]; then
|
|
node_payload=$(jq -n \
|
|
--arg name "$RW_NODE_NAME" \
|
|
--arg addr "$RW_NODE_ADDR" \
|
|
--argjson port "$RW_NODE_PORT" \
|
|
--arg cc "$RW_NODE_CC" \
|
|
--arg pu "$profile_uuid" \
|
|
--arg iu "$inbound_uuid" \
|
|
'{
|
|
name: $name,
|
|
address: $addr,
|
|
port: $port,
|
|
countryCode: $cc,
|
|
configProfile: {
|
|
activeConfigProfileUuid: $pu,
|
|
activeInbounds: [$iu]
|
|
}
|
|
}')
|
|
node_resp=$(api POST /nodes/ -d "${node_payload}")
|
|
node_uuid=$(printf '%s' "${node_resp}" | jq -r '.response.uuid // .uuid // empty')
|
|
if [ -z "${node_uuid}" ]; then
|
|
echo "Не удалось создать ноду" >&2
|
|
printf '%s\n' "${node_resp}" >&2
|
|
exit 1
|
|
fi
|
|
node_created=1
|
|
fi
|
|
|
|
node_update=$(jq -n \
|
|
--arg u "$node_uuid" \
|
|
--arg pu "$profile_uuid" \
|
|
--arg iu "$inbound_uuid" \
|
|
'{
|
|
uuid: $u,
|
|
configProfile: {
|
|
activeConfigProfileUuid: $pu,
|
|
activeInbounds: [$iu]
|
|
}
|
|
}')
|
|
api PATCH /nodes/ -d "${node_update}" >/dev/null 2>&1 \
|
|
|| api PUT /nodes/ -d "${node_update}" >/dev/null 2>&1 \
|
|
|| true
|
|
|
|
hosts_json=$(api GET /hosts/)
|
|
host_uuid=$(printf '%s' "${hosts_json}" | jq -r --arg a "$RW_SELFSTEAL" '
|
|
(.response // []) | map(select(.address == $a)) | .[0].uuid // empty')
|
|
|
|
if [ -z "${host_uuid}" ]; then
|
|
host_payload=$(jq -n \
|
|
--arg remark "$RW_HOST_REMARK" \
|
|
--arg addr "$RW_SELFSTEAL" \
|
|
--arg path "$RW_XHTTP_PATH" \
|
|
--arg sni "$RW_SELFSTEAL" \
|
|
--arg host "$RW_SELFSTEAL" \
|
|
--arg pu "$profile_uuid" \
|
|
--arg iu "$inbound_uuid" \
|
|
--arg nu "$node_uuid" \
|
|
'{
|
|
remark: $remark,
|
|
address: $addr,
|
|
port: 443,
|
|
path: $path,
|
|
sni: $sni,
|
|
host: $host,
|
|
fingerprint: "chrome",
|
|
isDisabled: false,
|
|
securityLayer: "DEFAULT",
|
|
alpn: "h2",
|
|
xHttpExtraParams: {
|
|
xmux: {
|
|
maxConcurrency: "16-32",
|
|
hMaxRequestTimes: "600-900",
|
|
hMaxReusableSecs: "1800-3000"
|
|
},
|
|
noSSEHeader: true,
|
|
xPaddingBytes: "100-1000"
|
|
},
|
|
inbound: {
|
|
configProfileUuid: $pu,
|
|
configProfileInboundUuid: $iu
|
|
},
|
|
nodes: [$nu]
|
|
}')
|
|
host_resp=$(api POST /hosts/ -d "${host_payload}")
|
|
host_uuid=$(printf '%s' "${host_resp}" | jq -r '.response.uuid // .uuid // empty')
|
|
if [ -z "${host_uuid}" ]; then
|
|
echo "Не удалось создать host" >&2
|
|
printf '%s\n' "${host_resp}" >&2
|
|
exit 1
|
|
fi
|
|
host_created=1
|
|
fi
|
|
|
|
jq -n \
|
|
--arg api_token "${active_token}" \
|
|
--arg node_secret "${node_secret}" \
|
|
--arg profile_uuid "${profile_uuid}" \
|
|
--arg node_uuid "${node_uuid}" \
|
|
--arg host_uuid "${host_uuid}" \
|
|
--arg reality_pub "${pub}" \
|
|
--argjson admin_created "${admin_created}" \
|
|
--argjson token_updated "${token_updated}" \
|
|
--argjson profile_created "${profile_created}" \
|
|
--argjson node_created "${node_created}" \
|
|
--argjson host_created "${host_created}" \
|
|
'{
|
|
api_token: $api_token,
|
|
node_secret: $node_secret,
|
|
profile_uuid: $profile_uuid,
|
|
node_uuid: $node_uuid,
|
|
host_uuid: $host_uuid,
|
|
reality_public_key: $reality_pub,
|
|
admin_created: ($admin_created == 1),
|
|
token_updated: ($token_updated == 1),
|
|
profile_created: ($profile_created == 1),
|
|
node_created: ($node_created == 1),
|
|
host_created: ($host_created == 1),
|
|
changed: (($admin_created + $token_updated + $profile_created + $node_created + $host_created) > 0)
|
|
}'
|