This commit is contained in:
紫云徽 2024-07-21 20:02:13 +08:00
parent c77b93c950
commit 29cc87399e
9 changed files with 585 additions and 0 deletions

175
.gitignore vendored Normal file
View File

@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

View File

@ -1,2 +1,15 @@
# netgate-switch
To install dependencies:
```bash
bun install
```
To run:
```bash
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.

BIN
bun.lockb Normal file

Binary file not shown.

1
config.json Normal file
View File

@ -0,0 +1 @@
{"login":true,"host":"10.225.0.1","user":"lan","password":"xp258147","maincidr":"10.225.1.0/24","proxycidr":"10.225.2.0/24"}

110
index.html Normal file
View File

@ -0,0 +1,110 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<title>NetgateSwitch</title>
<style>
html,
body {
height: 100%;
}
body {
display: flex;
align-items: center;
background-color: #f5f5f5;
}
.title {
margin: 1rem 0 0 0;
font-size: 1.8rem;
}
main {
width: 100%;
max-width: 30rem;
margin: auto;
text-align: center;
background-color: #ffffff;
border-radius: 1rem;
box-shadow: 0.5rem 0.5rem 1rem rgba(0, 0, 0, 0.2);
}
.btn {
width: 3rem;
height: 2rem;
margin-bottom: 1rem;
border: none;
border-radius: 0.5rem;
background-color: #dfdfdf;
}
.btn:hover {
background-color: #b9b9b9;
}
.btn:active {
background-color: #b9b9b9;
color: #ffffff;
box-shadow: 0 3px #666;
transform: translateY(2px);
}
</style>
</head>
<body>
<main>
<h1 class="title">NetgateSwitch</h1>
<form>
<div class="text-area">
</div>
<button class="btn" id="getlist">获取</button>
<button class="btn" id="switchaddr">更改</button>
</form>
</main>
</body>
<script>
document.getElementById('getlist').addEventListener('click', async (event) => {
try {
let res = await fetch('./core/get/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}, body: {}
});
if (!res.ok) {
alert(await res.text());
}
} catch (err) {
console.log(err);
}
});
document.getElementById('switchaddr').addEventListener('click', async (event) => {
try {
let res = await fetch('./core/switch/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}, body: JSON.stringify({
id: 'a',
group: 'main',
})
});
if (!res.ok) {
alert(await res.text());
}
} catch (err) {
console.log(err);
}
});
</script>
</html>

123
index.ts Normal file
View File

@ -0,0 +1,123 @@
import { serve } from "bun";
import { RouterOSAPI } from 'node-routeros';
import cfg from "./config.json";
let api = new RouterOSAPI({
host: cfg.host,
user: cfg.user,
password: cfg.password
});
let haslogin = cfg.login;
const maincidr = cfg.maincidr;
const proxycidr = cfg.proxycidr;
if (haslogin) {
if (!await connectAPI()) {
haslogin = false;
}
}
const server = serve({
port: 3000, async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/") {
let file = haslogin ? Bun.file("./index.html") : Bun.file("./login.html");
return new Response(file);
}
if (url.pathname === "/core/login/") {
let data = await request.json();
if (data.user === "" || data.password === "" || data.host === "") {
return new Response("参数错误", { status: 400 });
}
api = new RouterOSAPI({
host: data.host,
user: data.user,
password: data.password
});
if (await connectAPI()) {
writeConfig(data.host, data.user, data.password, data.main, data.proxy);
console.log("配置文件已更新");
return new Response("登入成功");
} else {
return new Response("ROS登入失败", { status: 400 });
}
}
if (url.pathname === "/core/get/") {
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 });
let data = await request.json();
let id = data.id;
let mac = data.mac;
let group = data.group;
if ((id === "" && mac === "") || group === "") {
return new Response("400 Bad Request", { status: 400 });
}
switchNetgate('*458',group);
}
if (url.pathname === "/core/logout/") {
cleanConfig();
return new Response("已登出");
}
return new Response("Page not found", { status: 404 });
}
});
console.log(`Listening on http://localhost:${server.port} ...`);
async function connectAPI() {
try {
await api.connect();
console.log("API登入成功");
return true;
} catch (err) {
console.log(err);
console.log("API登入失败");
return false;
}
}
async function getDHCPList() {
const result = await api.write('/ip/dhcp-server/lease/print');
let list = [];
for (let eq in result) {
list.push({
'id': result[eq]['.id'],
'address': result[eq]['address'],
'mac-address': result[eq]['mac-address'],
'status': result[eq]['status'],
'host-name': result[eq]['host-name'],
'comment': result[eq]['comment'],
});
}
console.log(list);
return list;
}
async function switchNetgate(id:string,group:string) {
const result = await api.write('/ip/dhcp-server/lease/set',['=.id='+id,'=address='+(group==='proxy'?'10.225.1.100':'10.225.0.100')]);
}
async function writeConfig(host: string, user: string, password: string, mcidr: string, pcidr: string) {
cfg.login = true;
cfg.host = host;
cfg.user = user;
cfg.password = password;
cfg.maincidr = mcidr;
cfg.proxycidr = pcidr;
haslogin = true;
await Bun.write("./config.json", JSON.stringify(cfg));
}
async function cleanConfig() {
cfg.login = false;
haslogin = false;
await Bun.write("./config.json", JSON.stringify(cfg));
}

119
login.html Normal file
View File

@ -0,0 +1,119 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<title>NetgateSwitch-Login</title>
<style>
html,
body {
height: 100%;
}
body {
display: flex;
align-items: center;
background-color: #f5f5f5;
}
.title {
margin: 1rem 0 0 0;
font-size: 1.8rem;
}
.form-signin {
width: 100%;
max-width: 16rem;
margin: auto;
text-align: center;
background-color: #ffffff;
border-radius: 1rem;
box-shadow: 0.5rem 0.5rem 1rem rgba(0, 0, 0, 0.2);
}
.text-area {
padding: 1rem;
}
.text {
height: 1.9rem;
width: 12rem;
margin: 0.5rem auto;
padding: 0.2rem;
border: 0.1rem solid #686868;
border-radius: 0.3rem;
}
#loginbtn {
width: 3rem;
height: 2rem;
margin-bottom: 1rem;
border: none;
border-radius: 0.5rem;
background-color: #dfdfdf;
}
#loginbtn:hover {
background-color: #b9b9b9;
}
#loginbtn:active {
background-color: #b9b9b9;
color: #ffffff;
box-shadow: 0 3px #666;
transform: translateY(2px);
}
</style>
</head>
<body>
<main class="form-signin">
<h1 class="title">NetgateSwitch</h1>
<form>
<div class="text-area">
<input class="text" id="address" placeholder="ROS-Address" />
<input class="text" id="user" placeholder="ROS-User" />
<input class="text" id="pwd" type="password" placeholder="ROS-Password" />
<input class="text" id="main" placeholder="MainCIDR(e.g.-10.0.1.0/24)" />
<input class="text" id="proxy" placeholder="ProxyCIDR(e.g.-10.0.2.0/24)" />
</div>
<button id="loginbtn">保存</button>
</form>
</main>
</body>
<script>
document.querySelector('form').addEventListener('submit', (event) => {
event.preventDefault();
});
document.getElementById('loginbtn').addEventListener('click',async (event) => {
try{
let res = await fetch('./core/login/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
host: document.getElementById('address').value,
user: document.getElementById('user').value,
password: document.getElementById('pwd').value,
main: document.getElementById('main').value,
proxy: document.getElementById('proxy').value
})
});
if(!res.ok){
alert(await res.text());
}else{
window.location.href("./");
}
}catch(err){
console.log(err);
}
});
</script>
</html>

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "netgate-switch",
"module": "index.ts",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"scripts": {
"dev": "bun run index.ts"
},
"type": "module",
"dependencies": {
"node-routeros": "^1.6.9"
}
}

27
tsconfig.json Normal file
View File

@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}