将socks5列表转换成sing-box格式的json

将socks5列表转换成sing-box格式的json

function getRowMap($tag, $server, $port, $user, $pass) {
    $mRow = [];
    $mRow['tag'] = (string)$tag;
    $mRow['type'] = 'socks';
    $mRow['version'] = '5';
    $mRow['server'] = $server;
    $mRow['server_port'] = intval($port);
    $mRow['username'] = $user;
    $mRow['password'] = $pass;
    return $mRow;
}
function getInet4Address($i) {
    $start = ip2long('172.19.0.1');
    return long2ip($start + $i * 4) . '/' . 30;
}
function getInbounds($count) {
    $aRet = [];
    for($i = 0; $i < $count; $i++) {
        $aRet[] = [
            'tag' => (string)$i,
            'type' => 'tun',
            'interface_name' => 'tun' . $i,
            'inet4_address' => getInet4Address($i),
        ];
    }
    return $aRet;
}
function getOutbounds($sList) {
    $sList = str_replace("\r", "\n", $sList);
    $sList = str_replace("\n\n", "\n", $sList);
    $aList = explode("\n", $sList);
    $aRet = [];
    foreach ($aList as $sRow) {
        if (!$sRow) {
            continue;
        }
        $aRow = explode(':', $sRow);
        if ($mRow = getRowMap(count($aRet), $aRow[0], $aRow[1], $aRow[2], $aRow[3])) {
            $aRet[] = $mRow;
        }
    }
    return $aRet;
}
function getRules($count) {
    $aRet = [];
    for($i = 0; $i < $count; $i++) {
        $aRet[] = [
            'inbound' => (string)$i,
            'outbound' => (string)$i,
        ];
    }
    return $aRet;
}
$sList = '';
$aOutbounds = getOutbounds($sList);
$mJson = [];
$mJson['log'] = ['level' => 'error'];
$mJson['inbounds'] = getInbounds(count($aOutbounds));
$mJson['outbounds'] = $aOutbounds;
$mJson['route'] = ['rules' => getRules(count($aOutbounds))];
echo json_encode($mJson, JSON_PRETTY_PRINT);
posted on 2024-06-22 22:47  项希盛  阅读(41)  评论(0编辑  收藏  举报