25 lines
899 B
JavaScript
25 lines
899 B
JavaScript
// 使用Node.js实现Shell脚本逻辑:获取公网IP并检查是否以.172结尾
|
||
const https = require('https')
|
||
|
||
https
|
||
.get('https://api.ipify.org', (res) => {
|
||
// `curl -s https://ifconfig.me` returns the IP address only, but open the url in browser or with nodejs http module 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.
|
||
let data = ''
|
||
res.on('data', (chunk) => (data += chunk))
|
||
res.on('end', () => {
|
||
const ip = data.trim().split(/\s+/)[0]
|
||
// console.log('public ip:', ip)
|
||
if (ip.endsWith('.172')) {
|
||
console.log('production')
|
||
} else if (ip.endsWith('.60')) {
|
||
console.log('development')
|
||
} else {
|
||
console.log('development')
|
||
}
|
||
})
|
||
})
|
||
.on('error', (err) => {
|
||
console.error('Request failed:', err)
|
||
})
|