u
This commit is contained in:
parent
a415b53549
commit
c22b8d6b73
23
get_runmode.js
Normal file
23
get_runmode.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// 使用Node.js实现Shell脚本逻辑:获取公网IP并检查是否以.172结尾
|
||||||
|
const https = require('https')
|
||||||
|
const { execFileSync } = require('child_process')
|
||||||
|
|
||||||
|
// https.get 没有同步版本,使用 child_process.execFileSync 同步获取公网 IP
|
||||||
|
let ip
|
||||||
|
try {
|
||||||
|
// 使用 curl 同步请求获取 IP
|
||||||
|
ip = execFileSync('curl', ['-s', 'https://api.ipify.org'], { encoding: 'utf8' }).trim()
|
||||||
|
} catch (e) {
|
||||||
|
// 如果 curl 失败,降级为固定开发环境
|
||||||
|
ip = '0.0.0.0'
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = ip.endsWith('.172') ? 'production' : ip.endsWith('.60') ? 'test' : 'development'
|
||||||
|
|
||||||
|
// 直接导出结果字符串,require 时即可拿到 IP 对应的 env
|
||||||
|
module.exports = result
|
||||||
|
|
||||||
|
// 如果直接运行此文件,则打印结果
|
||||||
|
if (require.main === module) {
|
||||||
|
console.log(result)
|
||||||
|
}
|
||||||
@ -1,36 +0,0 @@
|
|||||||
// 使用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
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user