wo_scripts/prod_or_dev.js
2026-01-21 13:36:42 +08:00

37 lines
1.0 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.

// 使用Node.js实现Shell脚本逻辑获取公网IP并检查是否以.172结尾
const https = require('https')
function getEnvByPublicIP () {
return new Promise((resolve, reject) => {
https
.get('https://api.ipify.org', (res) => {
let data = ''
res.on('data', (chunk) => (data += chunk))
res.on('end', () => {
const ip = data.trim().split(/\s+/)[0]
let result
if (ip.endsWith('.172')) {
result = 'production'
} else if (ip.endsWith('.60')) {
result = 'development'
} else {
result = 'development'
}
resolve(result)
})
})
.on('error', (err) => {
reject(err)
})
})
}
// 如果直接运行此文件,则执行并打印结果;否则导出函数供其他模块使用
if (require.main === module) {
getEnvByPublicIP()
.then((env) => console.log(env))
.catch((err) => console.error('Request failed:', err))
} else {
module.exports = getEnvByPublicIP
}