add sort
This commit is contained in:
parent
be8a8fc2b5
commit
68b87a9956
33
index.ts
33
index.ts
@ -1,8 +1,7 @@
|
|||||||
import { serve } from 'bun';
|
import { serve } from 'bun';
|
||||||
import { RouterOSAPI } from 'node-routeros';
|
import { RouterOSAPI } from 'node-routeros';
|
||||||
|
|
||||||
// const path = '/config/config.json';
|
const path = '/config/config.json';
|
||||||
const path = './config.json';
|
|
||||||
const cfgfile = Bun.file(path);
|
const cfgfile = Bun.file(path);
|
||||||
let cfg = await cfgfile.json();
|
let cfg = await cfgfile.json();
|
||||||
console.log(cfg);
|
console.log(cfg);
|
||||||
@ -15,7 +14,6 @@ let api = new RouterOSAPI({
|
|||||||
|
|
||||||
let maincidr = cfg.maincidr;
|
let maincidr = cfg.maincidr;
|
||||||
let proxycidr = cfg.proxycidr;
|
let proxycidr = cfg.proxycidr;
|
||||||
|
|
||||||
let haslogin = cfg.login;
|
let haslogin = cfg.login;
|
||||||
type Device = {
|
type Device = {
|
||||||
id: string;
|
id: string;
|
||||||
@ -74,7 +72,8 @@ const server = serve({
|
|||||||
if (url.pathname === '/core/get/') {
|
if (url.pathname === '/core/get/') {
|
||||||
if (!haslogin)
|
if (!haslogin)
|
||||||
return new Response('ROS配置未设置', { status: 401 });
|
return new Response('ROS配置未设置', { status: 401 });
|
||||||
return new Response(JSON.stringify(await getDHCPList()));
|
let ipaddr = request.headers.get('x-forwarded-for');
|
||||||
|
return new Response(JSON.stringify(await getDHCPList(ipaddr)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.pathname === '/core/switch/') {
|
if (url.pathname === '/core/switch/') {
|
||||||
@ -102,6 +101,9 @@ const server = serve({
|
|||||||
if (group !== 'main' && group !== 'proxy') {
|
if (group !== 'main' && group !== 'proxy') {
|
||||||
return new Response('group错误', { status: 401 });
|
return new Response('group错误', { status: 401 });
|
||||||
}
|
}
|
||||||
|
if (maincidr === '' || proxycidr === '') {
|
||||||
|
return new Response('配置文件错误', { status: 401 });
|
||||||
|
}
|
||||||
if (await switchNetgate(id, addr, group)) {
|
if (await switchNetgate(id, addr, group)) {
|
||||||
return new Response('切换成功');
|
return new Response('切换成功');
|
||||||
} else {
|
} else {
|
||||||
@ -131,18 +133,27 @@ async function connectAPI() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDHCPList() {
|
async function getDHCPList(addr: string | null) {
|
||||||
const result = await api.write('/ip/dhcp-server/lease/print');
|
const result = await api.write('/ip/dhcp-server/lease/print');
|
||||||
list = [];
|
list = [];
|
||||||
for (let eq in result) {
|
for (let eq in result) {
|
||||||
list.push({
|
let format = {
|
||||||
id: result[eq]['.id'],
|
id: result[eq]['.id'],
|
||||||
address: result[eq]['address'],
|
address: result[eq]['address'],
|
||||||
mac: result[eq]['mac-address'],
|
mac: result[eq]['mac-address'],
|
||||||
status: result[eq]['status'],
|
status: result[eq]['status'],
|
||||||
host: result[eq]['host-name'],
|
host: result[eq]['host-name'],
|
||||||
comment: result[eq]['comment'],
|
comment: result[eq]['comment'],
|
||||||
});
|
};
|
||||||
|
if (addr !== null || addr !== undefined || addr !== '') {
|
||||||
|
if (addr === format.address) {
|
||||||
|
list.unshift(format);
|
||||||
|
} else {
|
||||||
|
list.push(format);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
list.push(format);
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@ -153,11 +164,7 @@ async function switchNetgate(id: string, addr: string, group: string) {
|
|||||||
let targetcidr = (group === 'proxy' ? proxycidr : maincidr)
|
let targetcidr = (group === 'proxy' ? proxycidr : maincidr)
|
||||||
.split('/')[0]
|
.split('/')[0]
|
||||||
.split('.');
|
.split('.');
|
||||||
console.log(proxycidr);
|
|
||||||
console.log(maincidr);
|
|
||||||
console.log(targetcidr);
|
|
||||||
let address = addr.split('.');
|
let address = addr.split('.');
|
||||||
console.log(address);
|
|
||||||
let num = 2;
|
let num = 2;
|
||||||
if (mask == 16) {
|
if (mask == 16) {
|
||||||
num = 1;
|
num = 1;
|
||||||
@ -195,7 +202,7 @@ async function writeConfig(
|
|||||||
cfg.proxycidr = pcidr;
|
cfg.proxycidr = pcidr;
|
||||||
|
|
||||||
haslogin = true;
|
haslogin = true;
|
||||||
await Bun.write('/config/config.json', JSON.stringify(cfg));
|
await Bun.write(path, JSON.stringify(cfg));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cleanConfig() {
|
async function cleanConfig() {
|
||||||
@ -206,5 +213,5 @@ async function cleanConfig() {
|
|||||||
cfg.password = '';
|
cfg.password = '';
|
||||||
cfg.maincidr = '';
|
cfg.maincidr = '';
|
||||||
cfg.proxycidr = '';
|
cfg.proxycidr = '';
|
||||||
await Bun.write('/config/config.json', JSON.stringify(cfg));
|
await Bun.write(path, JSON.stringify(cfg));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user