40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
const { execSync } = require('child_process');
|
||
const ip = require('./utils/ip');
|
||
const { port, command } = require('./config');
|
||
const express = require('express')
|
||
const app = express()
|
||
const { join } = require('path');
|
||
|
||
app.use(express.static(join(__dirname, 'public')));
|
||
|
||
app.use((req, res, next) => {
|
||
res.header('Access-Control-Allow-Origin', '*');
|
||
res.header('Access-Control-Allow-Methods', 'GET, POST');
|
||
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
|
||
next();
|
||
});
|
||
|
||
/**
|
||
* 提供接口,查询ubuntu server上的ipv6地址,处理字符串后,序列化为json返回.
|
||
* 案例: https://ip.bmycode.help/ip?interface=enp3s0
|
||
* 参数: [可选] interface 网卡名称
|
||
*/
|
||
app.get('/ip', async (req, res) => {
|
||
let interface = req.query.interface || "enp3s0"
|
||
let output = execSync(command(interface), { encoding: "utf8" });
|
||
res.json(await ip.parseNetworkInfo(output))
|
||
})
|
||
|
||
// 静态页面,调用 /ip 接口,做页面展示
|
||
app.get('/', async (req, res) => {
|
||
res.sendFile(join(__dirname, 'public', 'index.html'));
|
||
})
|
||
|
||
app.listen(port, () => {
|
||
console.log(`app listening on port: http://127.0.0.1:${port}`)
|
||
})
|
||
|
||
|
||
|
||
|