wo_scripts/prod_or_dev.js
2026-01-21 12:15:11 +08:00

25 lines
899 B
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')
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)
})