forked from jason5ng32/MyIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad1974e
commit 9d80cb6
Showing
14 changed files
with
184 additions
and
133 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { get } from 'https'; | ||
|
||
function isValidIP(ip) { | ||
const ipv4Pattern = | ||
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; | ||
const ipv6Pattern = | ||
/^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})|(([0-9a-fA-F]{1,4}:){0,6}([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:){0,6}([0-9a-fA-F]{1,4})?))$/; | ||
return ipv4Pattern.test(ip) || ipv6Pattern.test(ip); | ||
}; | ||
|
||
|
||
export default (req, res) => { | ||
|
||
// 限制只能从指定域名访问 | ||
const allowedDomains = ['localhost', ...(process.env.ALLOWED_DOMAINS || '').split(',')]; | ||
const referer = req.headers.referer; | ||
|
||
if (referer) { | ||
const domain = new URL(referer).hostname; | ||
if (!allowedDomains.includes(domain)) { | ||
return res.status(403).json({ error: 'Access denied' }); | ||
} | ||
} else { | ||
return res.status(403).json({ error: 'What are you doing?' }); | ||
} | ||
|
||
// 从请求中获取 IP 地址 | ||
const ipAddress = req.query.ip; | ||
if (!ipAddress) { | ||
return res.status(400).json({ error: 'No IP address provided' }); | ||
} | ||
|
||
// 检查 IP 地址是否合法 | ||
if (!isValidIP(ipAddress)) { | ||
return res.status(400).json({ error: 'Invalid IP address' }); | ||
} | ||
|
||
const lang = req.query.lang || 'en'; | ||
|
||
const key = process.env.IPChecking_API_KEY; | ||
|
||
// 构建请求 IPCheck.ing 的 URL | ||
const url = new URL(`https://api.ipcheck.ing/ipinfo?key=${key}&ip=${ipAddress}&lang=${lang}`); | ||
|
||
get(url, apiRes => { | ||
let data = ''; | ||
apiRes.on('data', chunk => data += chunk); | ||
apiRes.on('end', () => { | ||
try { | ||
const originalJson = JSON.parse(data); | ||
res.json(originalJson); | ||
} catch (e) { | ||
res.status(500).json({ error: 'Error parsing JSON' }); | ||
} | ||
}); | ||
}).on('error', (e) => { | ||
res.status(500).json({ error: e.message }); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.