package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func getRealIP(c *gin.Context) string {
	// 获取用户请求的 IP 地址
	ip := c.Request.Header.Get("X-Real-IP")
	if ip == "" {
		ip = c.Request.Header.Get("X-Forwarded-For")
	}
	if ip == "" {
		ip = c.Request.RemoteAddr
	}
	return ip
}

func main() {
	router := gin.Default()

	router.GET("/get_real_ip", func(c *gin.Context) {
		// 调用 getRealIP 函数获取真实 IP 地址
		realIP := getRealIP(c)

		// 返回客户端的真实 IP 地址
		c.String(http.StatusOK, "Your real IP is %s", realIP)
	})

	//启动web引擎
	router.Run(":8080")
}
