通過AI寫了個訂閲批量換host的脚本

機場運營 · 17 天前 · 23 人浏览

這是免流一般用到的,可以在機場後臺整個API來自定義客戶需求,本站API地址為:https://blog.yariaa.lol/Api/Change_Host.php?url=你的訂閲地址&host=修改的地址

PHP版本如下:

<?php
function fetch_data($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); // 只允許 HTTP/HTTPS
    curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); // 只允許 HTTP/HTTPS 重定向
    $response = curl_exec($ch);
    if ($response === false) {
        throw new Exception("Failed to fetch data from URL: " . curl_error($ch));
    }
    curl_close($ch);
    return $response;
}

function decode_base64($data) {
    $decoded_data = base64_decode($data, true);
    if ($decoded_data === false) {
        throw new Exception("Failed to decode Base64 data.");
    }
    return $decoded_data;
}

function process_line($line, $new_host) {
    if (!preg_match('/^(.*:\/\/)(.*)$/', $line, $matches)) {
        return null;
    }

    $node_type = $matches[1];  // 協議類型(如 vmess://)
    $node_base64 = $matches[2];  // Base64 部分

    // 解碼 Base64 部分
    $data_json = decode_base64($node_base64);

    // 解析 JSON 數據
    $data = json_decode($data_json, true);
    if ($data === null) {
        throw new Exception("Invalid JSON data.");
    }

    // 驗證 JSON 結構
    if (!isset($data['host']) || !isset($data['port'])) {
        throw new Exception("Invalid JSON structure.");
    }

    // 修改 host 字段
    $data['host'] = $new_host;

    // 將修改後的 JSON 重新編碼為 Base64
    $modified_json = json_encode($data, JSON_UNESCAPED_UNICODE);  // 確保中文字符正常顯示
    $modified_base64 = base64_encode($modified_json);

    // 生成新的節點鏈接
    return $node_type . $modified_base64 . "\n";
}

function change_host($url, $new_host) {
    try {
        // 獲取數據
        $response = fetch_data($url);

        // Base64 解碼
        $decoded_data = decode_base64($response);

        $new_nodes = "";
        // 按行分割數據
        $lines = explode("\n", $decoded_data);
        foreach ($lines as $line) {
            try {
                $new_node = process_line($line, $new_host);
                if ($new_node !== null) {
                    $new_nodes .= $new_node;
                }
            } catch (Exception $e) {
                error_log("Failed to process line: $line. Error: " . $e->getMessage());
            }
        }

        // 將所有新節點鏈接重新編碼為 Base64
        return base64_encode($new_nodes);
    } catch (Exception $e) {
        error_log("Error: " . $e->getMessage());
        return null;
    }
}

// 獲取輸入參數並驗證
$url = filter_input(INPUT_GET, 'url', FILTER_SANITIZE_URL);
$new_host = filter_input(INPUT_GET, 'host', FILTER_SANITIZE_STRING);

if (empty($url) || empty($new_host)) {
    echo "URL and host parameters are required.\n";
    exit;
}

if (!preg_match('/^https?:\/\/[^\s]+$/', $url)) {
    echo "Invalid URL format.\n";
    exit;
}

if (!preg_match('/^[a-zA-Z0-9.-]+$/', $new_host)) {
    echo "Invalid host format.\n";
    exit;
}

// 執行並輸出結果
$result = change_host($url, $new_host);
if ($result !== null) {
    echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
}
本站立足于美利堅合衆國,請讀者自覺遵守當地法律!如有違規,本站不承擔任何法律責任! This site is based in the United States of America, readers are requested to abide by local laws! If there are any violations, this site does not bear any legal responsibility! Theme Jasmine by Kent Liao