ProxySelfIpEcho.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const API = require('../../lib/API')
  2. const { BaseStdResponse } = require('../../BaseStdResponse')
  3. function getClientIp(req) {
  4. let ip = null
  5. if (req.headers['x-forwarded-for']) {
  6. ip = String(req.headers['x-forwarded-for']).split(',')[0].trim()
  7. } else if (req.headers['x-real-ip']) {
  8. ip = String(req.headers['x-real-ip']).trim()
  9. } else {
  10. ip = req.connection?.remoteAddress || req.socket?.remoteAddress || ''
  11. }
  12. if (String(ip).startsWith('::ffff:')) ip = String(ip).replace('::ffff:', '')
  13. return ip || '0.0.0.0'
  14. }
  15. class ProxySelfIpEcho extends API {
  16. constructor() {
  17. super()
  18. this.noEncrypt()
  19. this.setPath('/Corn/ProxySelfIpEcho')
  20. this.setMethod('GET')
  21. }
  22. async onRequest(req, res) {
  23. return res.json({
  24. ...BaseStdResponse.OK,
  25. data: {
  26. ip: getClientIp(req),
  27. x_forwarded_for: req.headers['x-forwarded-for'] || '',
  28. x_real_ip: req.headers['x-real-ip'] || '',
  29. remote_address: req.connection?.remoteAddress || req.socket?.remoteAddress || ''
  30. }
  31. })
  32. }
  33. }
  34. module.exports.ProxySelfIpEcho = ProxySelfIpEcho