将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);
标签:box,count,sList,mRow,mJson,function,aRet,socks5,json
From: https://www.cnblogs.com/xiangxisheng/p/18262838