|
| 1 | +import { config } from '@/utils/config' |
| 2 | +import karin, { common, contactFriend, logger, segment } from 'node-karin' |
| 3 | +import axios from 'node-karin/axios' |
| 4 | +import os from 'node:os' |
| 5 | + |
| 6 | +const url = { IPv4: ['https://4.ipw.cn'], IPv6: ['https://6.ipw.cn'] } |
| 7 | + |
| 8 | +export const login = karin.command(/#?(面板|web)登录$/i, async (e) => { |
| 9 | + const net = os.networkInterfaces() |
| 10 | + const cfg = config() |
| 11 | + const IP: { lan: { ipv4: null | string, ipv6: null | string }, net: { ipv4: null | string, ipv6: null | string } } = |
| 12 | + { lan: { ipv4: null, ipv6: null }, net: { ipv4: null, ipv6: null } } |
| 13 | + for (const i in net) { |
| 14 | + for (const iface of net[i]!) { |
| 15 | + if (iface.internal) continue |
| 16 | + if (iface.family === 'IPv4') { |
| 17 | + const ip = iface.address |
| 18 | + if ( |
| 19 | + ip.startsWith('192.168.') || |
| 20 | + ip.startsWith('10.') || |
| 21 | + (ip.startsWith('172.') && parseInt(ip.split('.')[1]) >= 16 && parseInt(ip.split('.')[1]) <= 31) |
| 22 | + ) { |
| 23 | + IP.lan.ipv4 = ip |
| 24 | + } |
| 25 | + } else if (iface.family === 'IPv6') { |
| 26 | + const ip = iface.address |
| 27 | + if (ip.startsWith('fd') || ip.startsWith('fe80:') || ip.startsWith('fc')) { |
| 28 | + IP.lan.ipv6 = ip |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + IP.net.ipv4 = await getnetIP('IPv4') |
| 34 | + IP.net.ipv6 = await getnetIP('IPv6') |
| 35 | + const port = process.env.HTTP_PORT |
| 36 | + const token = process.env.HTTP_AUTH_KEY |
| 37 | + const msg = [segment.text('面板登录地址:')] |
| 38 | + if (cfg.domain) msg.push(segment.text(`- 自定义域名: ${cfg.domain}/web/login?token=${token}`)) |
| 39 | + msg.push(segment.text(`- 内网地址: ${IP.lan.ipv4 ? `http://${IP.lan.ipv4}:${port}/web/login?token=${token}` : `http://${IP.lan.ipv4}:${port}/web/login?token=${token}`}`)) |
| 40 | + if (IP.net.ipv4) msg.push(segment.text(`- 外网IPv4地址: http://${IP.net.ipv4}:${port}/web/login?token=${token}`)) |
| 41 | + if (IP.net.ipv6) msg.push(segment.text(`- 外网IPv6地址: http://${IP.net.ipv6}:${port}/web/login?token=${token}`)) |
| 42 | + try { |
| 43 | + const content = common.makeForward(msg, e.selfId, e.bot.account.name) |
| 44 | + await e.bot.sendForwardMsg(contactFriend(e.userId), content) |
| 45 | + if (e.isGroup) await e.reply('登录地址已经私信给主人了哦~') |
| 46 | + } catch (err) { |
| 47 | + msg.forEach(item => { |
| 48 | + item.text = item.text + '\n' |
| 49 | + }) |
| 50 | + await e.bot.sendMsg(contactFriend(e.userId), msg) |
| 51 | + if (e.isGroup) await e.reply('登录地址已经私信给主人了哦~') |
| 52 | + return true |
| 53 | + } |
| 54 | + return true |
| 55 | +}, { name: '面板登录', perm: 'admin' }) |
| 56 | + |
| 57 | +async function getnetIP (type: 'IPv4' | 'IPv6') { |
| 58 | + for (const i of url[type] || []) { |
| 59 | + try { |
| 60 | + const res = await axios.get(i) |
| 61 | + if (res.data) { |
| 62 | + return res.data |
| 63 | + } |
| 64 | + } catch (e) { |
| 65 | + logger.error(`访问${i}获取外网${type}地址失败: ${e}`) |
| 66 | + continue |
| 67 | + } |
| 68 | + } |
| 69 | + return null |
| 70 | +} |
0 commit comments