-- Lua 5.1 / executor向けの最小サンプル。利用環境での動作保証はありません。
-- executorのhttp.post/http.request、JSON、loadstringの対応は環境ごとに確認してください。
-- 許可されたスクリプトにのみ使用し、キーやHWIDをログへ出力しないでください。
local BASE_URL = "https://fizex.net"
local KEY = "[YOUR_KEY]"
local HWID = "[YOUR_DEVICE_ID]" -- 利用環境の安定した識別子に置き換える
local json = require("json") -- executor提供のJSON encode/decodeに合わせて置き換える

local messages = {
    expired = "キーの有効期限が切れています。",
    revoked = "キーは失効しています。",
    hwid_mismatch = "端末が一致しません。作成者へ確認してください。",
    killed = "作成者が配信を停止しています。",
    rate_limited = "リクエストが多すぎます。Retry-Afterの時間を待ってください。",
    invalid_key = "キーが見つかりません。"
}

-- この例ではhttp.post(url, body, headers)が{StatusCode, Body, Headers}を返す想定。
-- executorのAPI仕様が異なる場合は、この呼び出しと応答フィールドを調整してください。
local ok, response = pcall(function()
    return http.post(BASE_URL .. "/api/v1/validate",
        json.encode({key = KEY, hwid = HWID}),
        {["Content-Type"] = "application/json"})
end)
if not ok or type(response) ~= "table" then
    print("検証APIに接続できませんでした。")
    return
end
local decoded, result = pcall(json.decode, response.Body or "")
if not decoded or type(result) ~= "table" then
    print("検証APIの応答を読み取れませんでした。")
    return
end
if response.StatusCode ~= 200 then
    local code = type(result.error) == "table" and result.error.code or "unknown_error"
    print(messages[code] or ("検証失敗: " .. tostring(code)))
    return
end
-- 成功契約: {url, expires_at}。expires_atはRFC3339 UTC、URLは5分で失効。
if type(result.url) ~= "string" or type(result.expires_at) ~= "string" then
    print("配信URLがありません。")
    return
end
-- 現行APIの相対URLだけを許可し、意図しないホストからの実行を防ぐ。
if not string.match(result.url, "^/api/v1/script/[%w_-]+$") then
    print("想定外の配信URLです。")
    return
end
local fetched, script_response = pcall(function()
    return http.request({Url = BASE_URL .. result.url, Method = "GET"})
end)
if not fetched or type(script_response) ~= "table" or script_response.StatusCode ~= 200 then
    print("スクリプトを取得できません。期限・失効・配信停止を確認してください。")
    return
end
local source = script_response.Body
if type(source) ~= "string" then
    print("スクリプト本文がありません。")
    return
end
local chunk, compile_error = loadstring(source)
if not chunk then
    print("Luaを読み込めません: " .. tostring(compile_error))
    return
end
local executed, runtime_error = pcall(chunk)
if not executed then print("実行エラー: " .. tostring(runtime_error)) end
