Files
wo_scripts/get_runmode.js
T
2026-02-07 11:36:31 +08:00

27 lines
1.0 KiB
JavaScript

// 使用Node.js实现Shell脚本逻辑:获取公网IP并检查是否以.172结尾
const { execFileSync } = require('child_process')
//https.get('https://api.ipify.org', (res) => {
// `curl -s https://ifconfig.me` returns the IP address only, but open the url in browser or https.get() returns the HTML content
// `https://api.ipify.org` returns the IP address only with curl, browser or nodejs. But it seems it doesn't work in China.
// https.get 没有同步版本,使用 child_process.execFileSync 同步获取公网 IP
let ip
try {
// 使用 curl 同步请求获取 IP
ip = execFileSync('curl', ['-s', 'https://ifconfig.me'], { encoding: 'utf8' }).trim()
} catch (e) {
// 如果 curl 失败,降级为固定开发环境
ip = '0.0.0.0'
}
const result = ip.endsWith('.172') ? 'production' : ip.endsWith('.60') ? 'development' : 'development'
// 直接导出结果字符串,require 时即可拿到 IP 对应的 env
module.exports = result
// 如果直接运行此文件,则打印结果
if (require.main === module) {
console.log(result)
}