ROS 定时更新 china ip 列表

Scheduler 配置

System -> Scheduler 添加定时任务,配置参考截图

:log info "start update china address"
foreach i in={ "all_china"; "mobile"} do={
    :log info "download $i list"
    /tool fetch url="http://192.168.3.77/fetch_ros_list.php?listname=$i" dst-path=$i
    :log info "remove $i list"

    /system logging disable numbers=0
    /ip firewall address-list remove [/ip firewall address-list find list=$i]
    /system logging enable numbers=0

    :log info "import $i file"
    /system logging disable numbers=0
    /import file-name=$i
    /system logging enable numbers=0

    :log info "remove $i file"
    /file remove $i
}
:log info "end update china address"

需要自己部署 fetch_ros_list.php

<?php

function printCommand($content, $listName)
{
    echo "/ip firewall address-list\n";
    $pattern = '/address=(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+) /';
    preg_match_all($pattern, $content, $rs);
    foreach ($rs[0] as $item) {
        $item = trim($item);
        $item = str_replace("address=", "", $item);
        echo "add address=$item disabled=no list=$listName\n";
    }
    http_response_code(200);
}


http_response_code(404);
$requestTime = time();
if(isset($_GET['time'])) {
    if((int)$_GET['time'] > 1) $requestTime = (int)$_GET['time'];
}
$listName = "all_china";
$fileUrl = "http://www.tcp5.com/list/" . date("Y.m/", $requestTime);
if(isset($_GET['listname'])) {
    $listName = trim($_GET['listname']);
    if(!in_array($listName, ['cernet', 'mobile', 'other', 'telecom', 'unicom', 'all_china'])) {
        die();
    }
}
$outListName = $listName;
if (isset($_GET['list'])) {
    $outListName = (string)$_GET['list'];
}

$cacheFile = "list/" .  $listName . date('Y-m-d', $requestTime) . ".rsc";
if(file_exists($cacheFile)) {
    http_response_code(200);
    header('x-cache: true');
    printCommand(file_get_contents($cacheFile), $outListName);
    exit();
}

$html = file_get_contents($fileUrl);
if(!$html) {
    echo "html 404";
    die();
}
$fileName = $listName . date('Y-m-d-', $requestTime);
$pattern = '/' . $fileName . '(\d+)\.rsc/';
preg_match_all($pattern, $html, $rs);
if (count($rs[1]) == 0) {
    echo "not found file";
    die();
}

http_response_code(200);
$fileName = $fileName . $rs[1][0] . ".rsc";

$content = file_get_contents($fileUrl . $fileName);
if($content) {
    file_put_contents($cacheFile, $content);
    printCommand($content, $outListName);
} else {
    echo "file 404";
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据