const API = require('../../lib/API') const { BaseStdResponse } = require('../../BaseStdResponse') function getClientIp(req) { let ip = null if (req.headers['x-forwarded-for']) { ip = String(req.headers['x-forwarded-for']).split(',')[0].trim() } else if (req.headers['x-real-ip']) { ip = String(req.headers['x-real-ip']).trim() } else { ip = req.connection?.remoteAddress || req.socket?.remoteAddress || '' } if (String(ip).startsWith('::ffff:')) ip = String(ip).replace('::ffff:', '') return ip || '0.0.0.0' } class ProxySelfIpEcho extends API { constructor() { super() this.noEncrypt() this.setPath('/Corn/ProxySelfIpEcho') this.setMethod('GET') } async onRequest(req, res) { return res.json({ ...BaseStdResponse.OK, data: { ip: getClientIp(req), x_forwarded_for: req.headers['x-forwarded-for'] || '', x_real_ip: req.headers['x-real-ip'] || '', remote_address: req.connection?.remoteAddress || req.socket?.remoteAddress || '' } }) } } module.exports.ProxySelfIpEcho = ProxySelfIpEcho