Compare commits

...

8 Commits

Author SHA1 Message Date
6d2366d929 更新readme 2024-09-19 12:27:57 +08:00
f10fe2843e 断线重连 2024-09-12 22:39:20 +08:00
bc6936def1 2 2024-08-12 16:03:35 +08:00
107dc62e40 add color 2024-08-12 16:00:55 +08:00
a8857e40d4 test 2024-08-12 15:38:58 +08:00
b27eceb18c test3 2024-08-10 20:43:16 +08:00
54c19ffe9c test 2024-08-10 20:41:34 +08:00
1f9557df61 test 2024-08-10 19:22:26 +08:00
4 changed files with 58 additions and 24 deletions

View File

@@ -1,15 +1,23 @@
# netgate-switch # ROS 网段切换器
To install dependencies: 基于 bun 运行的 RouterOS 网段切换器前/后端。局域网环境下经常使用网段划分不同用途、类型的设备,以便利用 DHCP 下发不同的网关、DNS、NTP 等信息。直接使用 ros 前端或者 winbox 进行修改是个好方法,然而不是所有设备都能方便地登入以及操作(如移动设备),同时对 ros 的完全访问可能带来额外的安全风险。本项目旨在使局域网中各种设备都能快速便捷切换所在网段。
# 使用方法
1.在 ROS 中创建一个可读写账户
2.准备两个网段,如 10.0.0.0/16、10.1.0.0/16。CRID 数字形式表达,仅支持/8 /16 /24。各设备主机号将被保留。
3.安装依赖:
```bash ```bash
bun install bun install
``` ```
To run: 4.运行:
```bash ```bash
bun run index.ts bun run index.ts
``` ```
This project was created using `bun init` in bun v1.1.18. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. 5.填写网关、账密信息登入后即可运行

View File

@@ -2,6 +2,7 @@ services:
netgate-switch: netgate-switch:
build: . build: .
pull_policy: build pull_policy: build
container_name: NetgateSwitch
ports: ports:
- '7000:7000' - '7000:7000'
restart: unless-stopped restart: unless-stopped

View File

@@ -54,6 +54,14 @@
background-color: #dfdfdf; background-color: #dfdfdf;
} }
.device {
background-color: #ffff00;
}
.online {
color: #00ff00;
}
.btn:hover { .btn:hover {
background-color: #b9b9b9; background-color: #b9b9b9;
} }
@@ -144,7 +152,9 @@
tb.append(trn); tb.append(trn);
for (let i in list) { for (let i in list) {
trn = document.createElement('tr'); trn = document.createElement('tr');
trn.setAttribute('class', list[i].device === true ? 'device' : '');
td1 = document.createElement('td'); td1 = document.createElement('td');
td1.setAttribute('class', list[i].status === 'bound' ? 'online' : '');
td2 = document.createElement('td'); td2 = document.createElement('td');
td3 = document.createElement('td'); td3 = document.createElement('td');
td4 = document.createElement('td'); td4 = document.createElement('td');
@@ -158,7 +168,7 @@
btn2.setAttribute('cidrgroup', 'proxy'); btn2.setAttribute('cidrgroup', 'proxy');
btn2.setAttribute('deviceid', list[i].id); btn2.setAttribute('deviceid', list[i].id);
btn2.append('切换至Proxy'); btn2.append('切换至Proxy');
td1.append(list[i].host); td1.append(list[i].comment === undefined ? list[i].host : list[i].comment);
td2.append(list[i].address); td2.append(list[i].address);
td3.append(list[i].mac); td3.append(list[i].mac);
td4.append(btn1); td4.append(btn1);

View File

@@ -72,8 +72,12 @@ const server = serve({
if (!haslogin) if (!haslogin)
return new Response('ROS配置未设置', { status: 401 }); return new Response('ROS配置未设置', { status: 401 });
const sockaddr = server.requestIP(request); const sockaddr = server.requestIP(request);
let ipaddr = sockaddr === null ? '' : sockaddr.address; let ipinfo = sockaddr === null ? '' : sockaddr.address;
return new Response(JSON.stringify(await getDHCPList(ipaddr))); let ipaddr = ipinfo.split(':');
console.log(sockaddr);
return new Response(
JSON.stringify(await getDHCPList(ipaddr[ipaddr.length - 1]))
);
} }
if (url.pathname === '/core/switch/') { if (url.pathname === '/core/switch/') {
@@ -134,26 +138,37 @@ async function connectAPI() {
} }
async function getDHCPList(addr: string | null) { async function getDHCPList(addr: string | null) {
const result = await api.write('/ip/dhcp-server/lease/print');
list = []; list = [];
for (let eq in result) { try {
let format = { const result = await api.write('/ip/dhcp-server/lease/print');
id: result[eq]['.id'], for (let eq in result) {
address: result[eq]['address'], let format = {
mac: result[eq]['mac-address'], id: result[eq]['.id'],
status: result[eq]['status'], address: result[eq]['address'],
host: result[eq]['host-name'], mac: result[eq]['mac-address'],
comment: result[eq]['comment'], status: result[eq]['status'],
}; host: result[eq]['host-name'],
if (addr !== null || addr !== undefined || addr !== '') { comment: result[eq]['comment'],
if (addr === format.address) { device: false,
list.unshift(format); };
} else { if (addr !== null || addr !== undefined || addr !== '') {
list.push(format); if (addr === format.address) {
format.device = true;
list.unshift(format);
} else {
list.push(format);
}
continue;
} }
continue; list.push(format);
} }
list.push(format); } catch (e) {
api = new RouterOSAPI({
host: cfg.host,
user: cfg.user,
password: cfg.password,
});
api.connect();
} }
return list; return list;
} }