将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);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.