Files
Ai-Single-Page/z.ai/ip-replace/vmess-replace.php
T

254 lines
8.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// 设置默认时区(可选,防止警告)
date_default_timezone_set('Asia/Shanghai');
// 处理表单提交
$result_content = '';
$original_content = '';
$target_ip = '';
$replace_from = '';
$replace_to = '';
// 辅助函数:解析一行 vmess 链接,返回 [bool 成功, array 配置]
function parse_vmess_line(string $line): array {
$line = trim($line);
if ($line === '' || stripos($line, 'vmess://') !== 0) {
return [false, null];
}
$b64 = substr($line, strlen('vmess://'));
$b64 = trim($b64);
if ($b64 === '') {
return [false, null];
}
// 补齐 Base64 的 '=' 填充,兼容部分客户端
$mod = strlen($b64) % 4;
if ($mod > 0) {
$b64 .= str_repeat('=', 4 - $mod);
}
$json_str = base64_decode($b64, true);
if ($json_str === false) {
return [false, null];
}
$data = json_decode($json_str, true);
if (!is_array($data)) {
return [false, null];
}
return [true, $data];
}
// 辅助函数:将配置重新编码为 vmess 行
function build_vmess_line(array $data): string {
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$b64 = base64_encode($json);
return 'vmess://' . $b64;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$target_ip = trim($_POST['target_ip'] ?? '');
$original_content = trim($_POST['content'] ?? '');
$replace_from = trim($_POST['replace_from'] ?? '');
$replace_to = trim($_POST['replace_to'] ?? '');
if ($original_content !== '') {
$lines = preg_split("/\r\n|\n|\r/", $original_content);
$result_lines = [];
foreach ($lines as $line) {
$raw_line = rtrim($line, "\r\n");
if (trim($raw_line) === '') {
$result_lines[] = $raw_line;
continue;
}
[$ok, $data] = parse_vmess_line($raw_line);
if (!$ok) {
// 不是合法的 vmess 链接,原样返回
$result_lines[] = $raw_line;
continue;
}
// 1. 替换 IP:修改 JSON 中的 add 字段
if ($target_ip !== '') {
$data['add'] = $target_ip;
}
// 2. 批量字符串替换:在所有字符串字段中做 str_replace
if ($replace_from !== '' && $replace_to !== '') {
array_walk_recursive($data, function (&$value) use ($replace_from, $replace_to) {
if (is_string($value) && $value !== '') {
$value = str_replace($replace_from, $replace_to, $value);
}
});
}
$result_lines[] = build_vmess_line($data);
}
$result_content = implode(PHP_EOL, $result_lines);
}
} else {
// 默认演示文本:一条 vmess 链接示例
$original_content = "vmess://ew0KICAidiI6ICIyIiwNCiAgInBzIjogIkFsaVBDLUdHWS1TaGFyZS1CZXN0dm0tU0ciLA0KICAiYWRkIjogIjEuMS4xLjEiLA0KICAicG9ydCI6ICIzMzg4OCIsDQogICJpZCI6ICI3N2MwYWFmZC03MTNhLTQwZDUtYmI2MS1mZWMyN2JiMTI0MTEiLA0KICAiYWlkIjogIjAiLA0KICAic2N5IjogImF1dG8iLA0KICAibmV0IjogInRjcCIsDQogICJ0eXBlIjogIm5vbmUiLA0KICAiaG9zdCI6ICIiLA0KICAicGF0aCI6ICIiLA0KICAidGxzIjogIiIsDQogICJzbmkiOiAiIiwNCiAgImFscG4iOiAiIiwNCiAgImZwIjogIiIsDQogICJpbnNlY3VyZSI6ICIwIg0KfQ==";
$result_content = $original_content; // 默认结果显示原样
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>vmess 批量替换工具</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f6f9;
color: #333;
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
}
.container {
background-color: #fff;
width: 100%;
max-width: 900px;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #2c3e50;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
color: #555;
}
input[type="text"] {
width: 100%;
padding: 10px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
}
.btn-submit {
background-color: #007bff;
color: white;
border: none;
padding: 12px 24px;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
width: 100%;
transition: background-color 0.3s;
}
.btn-submit:hover {
background-color: #0056b3;
}
.grid-layout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-top: 20px;
}
@media (max-width: 768px) {
.grid-layout {
grid-template-columns: 1fr;
}
.form-row {
grid-template-columns: 1fr;
}
}
textarea {
width: 100%;
height: 300px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: monospace;
resize: vertical;
box-sizing: border-box;
background-color: #fafafa;
}
.box-title {
font-size: 14px;
color: #777;
margin-bottom: 5px;
display: flex;
justify-content: space-between;
}
.tag {
background: #e9ecef;
padding: 2px 6px;
border-radius: 3px;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<h1>vmess 批量替换工具</h1>
<form method="post" action="">
<div class="form-group">
<label for="target_ip">替换为的新 IP(会写入 vmess JSON 的 add 字段)</label>
<input type="text" id="target_ip" name="target_ip" placeholder="例如: 192.168.1.1" value="<?php echo htmlspecialchars($target_ip); ?>">
</div>
<div class="form-row">
<div class="form-group">
<label for="replace_from">替换原始字符(例如:AliPC-GGY</label>
<input type="text" id="replace_from" name="replace_from" placeholder="原始字符" value="<?php echo htmlspecialchars($replace_from); ?>">
</div>
<div class="form-group">
<label for="replace_to">目标字符(例如:TX-Haha</label>
<input type="text" id="replace_to" name="replace_to" placeholder="目标字符" value="<?php echo htmlspecialchars($replace_to); ?>">
</div>
</div>
<div class="grid-layout">
<div>
<div class="box-title">
<span>原始 vmess 地址(每行一条)</span>
<span class="tag">Input</span>
</div>
<textarea name="content" placeholder="在此粘贴 vmess:// 开头的分享地址,每行一条"><?php echo htmlspecialchars($original_content); ?></textarea>
</div>
<div>
<div class="box-title">
<span>替换后的 vmess 地址</span>
<span class="tag">Output</span>
</div>
<textarea readonly onclick="this.select()" placeholder="结果会显示在这里"><?php echo htmlspecialchars($result_content); ?></textarea>
</div>
</div>
<div style="margin-top: 20px;">
<button type="submit" class="btn-submit">执行替换</button>
</div>
</form>
</div>
</body>
</html>