diff --git a/.dockerignore b/.dockerignore index f965aed..a3e40ea 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,6 +4,7 @@ docker-compose* .dockerignore .git .gitignore +.prettierrc README.md LICENSE .vscode diff --git a/.gitignore b/.gitignore index 9b1ee42..c02a6af 100644 --- a/.gitignore +++ b/.gitignore @@ -173,3 +173,7 @@ dist # Finder (MacOS) folder config .DS_Store + +.prettierrc + +package-lock.json \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 20e4cb7..5215fef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ -FROM oven/bun:1 +FROM oven/bun:latest WORKDIR /usr/src/app COPY . . +COPY config.json /etc/netgateswitch/config.json RUN bun install --frozen-lockfile --production ENV NODE_ENV=production EXPOSE 7000/tcp diff --git a/compose.yaml b/compose.yaml index b6410d1..ceaecf6 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,8 @@ services: - netgate-switch: - build: . - ports: - - "7000:7000" - restart: unless-stopped + netgate-switch: + build: . + ports: + - '7000:7000' + restart: unless-stopped + volumes: + - /home/ns:/etc/netgateswitch:ro diff --git a/index.ts b/index.ts index a442b8f..4f7dfe8 100644 --- a/index.ts +++ b/index.ts @@ -1,11 +1,11 @@ import { serve } from 'bun'; import { RouterOSAPI } from 'node-routeros'; -import cfg from './config.json'; +import cfg from '/etc/netgateswitch/config.json'; let api = new RouterOSAPI({ host: cfg.host, user: cfg.user, - password: cfg.password + password: cfg.password, }); const maincidr = cfg.maincidr; @@ -13,26 +13,29 @@ const proxycidr = cfg.proxycidr; let haslogin = cfg.login; type Device = { - 'id': string, - 'address': string, - 'mac': string, - 'status': string, - 'host': string, - 'comment': string -} + id: string; + address: string; + mac: string; + status: string; + host: string; + comment: string; +}; let list: Device[] = []; if (haslogin) { - if (!await connectAPI()) { + if (!(await connectAPI())) { haslogin = false; } } const server = serve({ - port: cfg.port, async fetch(request) { + port: cfg.port, + async fetch(request) { const url = new URL(request.url); if (url.pathname === '/') { - let file = haslogin ? Bun.file('./index.html') : Bun.file('./login.html'); + let file = haslogin + ? Bun.file('./index.html') + : Bun.file('./login.html'); return new Response(file); } @@ -44,10 +47,16 @@ const server = serve({ api = new RouterOSAPI({ host: data.host, user: data.user, - password: data.password + password: data.password, }); if (await connectAPI()) { - writeConfig(data.host, data.user, data.password, data.main, data.proxy); + writeConfig( + data.host, + data.user, + data.password, + data.main, + data.proxy + ); console.log('配置文件已更新'); return new Response('登入成功'); } else { @@ -56,17 +65,19 @@ const server = serve({ } if (url.pathname === '/core/get/') { - if (!haslogin) return new Response('ROS配置未设置', { status: 401 }); + if (!haslogin) + return new Response('ROS配置未设置', { status: 401 }); return new Response(JSON.stringify(await getDHCPList())); } if (url.pathname === '/core/switch/') { - if (!haslogin) return new Response('ROS配置未设置', { status: 401 }); + if (!haslogin) + return new Response('ROS配置未设置', { status: 401 }); let data = await request.json(); let id = data.id; let mac = data.mac; let group = data.group; - if ((id === '' && mac === '')) { + if (id === '' && mac === '') { return new Response('参数缺失', { status: 401 }); } let isExists: boolean = false; @@ -78,14 +89,14 @@ const server = serve({ isExists = true; } } - if ((!isExists)) { + if (!isExists) { return new Response('设备不存在', { status: 401 }); } if (group !== 'main' && group !== 'proxy') { return new Response('group错误', { status: 401 }); } if (await switchNetgate(id, addr, group)) { - return new Response('登入成功'); + return new Response('切换成功'); } else { return new Response('切换失败', { status: 401 }); } @@ -97,7 +108,7 @@ const server = serve({ } return new Response('Page not found', { status: 404 }); - } + }, }); console.log(`前端已开启: http://localhost:${server.port} ...`); @@ -118,12 +129,12 @@ async function getDHCPList() { list = []; for (let eq in result) { list.push({ - 'id': result[eq]['.id'], - 'address': result[eq]['address'], - 'mac': result[eq]['mac-address'], - 'status': result[eq]['status'], - 'host': result[eq]['host-name'], - 'comment': result[eq]['comment'], + id: result[eq]['.id'], + address: result[eq]['address'], + mac: result[eq]['mac-address'], + status: result[eq]['status'], + host: result[eq]['host-name'], + comment: result[eq]['comment'], }); } return list; @@ -132,7 +143,9 @@ async function getDHCPList() { async function switchNetgate(id: string, addr: string, group: string) { try { let mask = Number.parseInt(maincidr.split('/')[1]); - let targetcidr = (group === 'proxy' ? proxycidr : maincidr).split('/')[0].split('.'); + let targetcidr = (group === 'proxy' ? proxycidr : maincidr) + .split('/')[0] + .split('.'); let address = addr.split('.'); let num = 2; if (mask == 16) { @@ -145,7 +158,10 @@ async function switchNetgate(id: string, addr: string, group: string) { } console.log('原IP' + addr + '切换至' + address.join('.')); - const result = await api.write('/ip/dhcp-server/lease/set', ['=.id=' + id, '=address=' + address.join('.')]); + const result = await api.write('/ip/dhcp-server/lease/set', [ + '=.id=' + id, + '=address=' + address.join('.'), + ]); return true; } catch (err) { console.log(err); @@ -153,7 +169,13 @@ async function switchNetgate(id: string, addr: string, group: string) { } } -async function writeConfig(host: string, user: string, password: string, mcidr: string, pcidr: string) { +async function writeConfig( + host: string, + user: string, + password: string, + mcidr: string, + pcidr: string +) { cfg.login = true; cfg.host = host; cfg.user = user; @@ -162,16 +184,16 @@ async function writeConfig(host: string, user: string, password: string, mcidr: cfg.proxycidr = pcidr; haslogin = true; - await Bun.write('./config.json', JSON.stringify(cfg)); + await Bun.write('/etc/netgateswitch/config.json', JSON.stringify(cfg)); } async function cleanConfig() { haslogin = false; cfg.login = false; - cfg.host = ""; - cfg.user = ""; - cfg.password = ""; - cfg.maincidr = ""; - cfg.proxycidr = ""; - await Bun.write('./config.json', JSON.stringify(cfg)); -} \ No newline at end of file + cfg.host = ''; + cfg.user = ''; + cfg.password = ''; + cfg.maincidr = ''; + cfg.proxycidr = ''; + await Bun.write('/etc/netgateswitch/config.json', JSON.stringify(cfg)); +} diff --git a/package.json b/package.json index 845ed7a..ff1c1fe 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "type": "module", "dependencies": { + "netgate-switch": "file:", "node-routeros": "^1.6.9" } -} \ No newline at end of file +}