Files
IPV6/index.js
编码猿 72f5679140
All checks were successful
continuous-integration/drone/push Build is passing
3333
2025-10-09 04:42:59 +08:00

40 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.bbtlwlkj.top/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}`)
})