63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func main() {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
r := gin.Default()
|
|
r.Use(gin.Logger())
|
|
r.Use(gin.Recovery())
|
|
r.GET("/", func(c *gin.Context) {
|
|
log.Printf("user-agent=%s", c.Request.UserAgent())
|
|
hostname, _ := os.Hostname()
|
|
resultText := fmt.Sprintf("Hostname: %s\nIP: %s\nRemoteAddr: %s\nUser-Agent: %s\nX-Forwarded-For: %s\nX-Forwarded-Port: %s\nX-Forwarded-Server: %s\nX-Real-IP: %s\n",
|
|
hostname, strings.Join(GetIps(), ", "), c.Request.RemoteAddr, c.Request.UserAgent(), c.Request.Header.Get("X-Forwarded-For"),
|
|
c.Request.Header.Get("X-Forwarded-Port"), c.Request.Header.Get("X-Forwarded-Server"), c.Request.Header.Get("X-Real-IP"))
|
|
c.String(200, resultText)
|
|
})
|
|
port := ":80"
|
|
log.Printf("服务器启动在 http://localhost%s", port)
|
|
if err := r.Run(port); err != nil {
|
|
log.Fatal("服务器启动失败:", err)
|
|
}
|
|
}
|
|
|
|
func GetIps() []string {
|
|
ips := []string{}
|
|
interfaces, _ := net.Interfaces()
|
|
for _, iface := range interfaces {
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
continue
|
|
}
|
|
addrs, err := iface.Addrs()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, addr := range addrs {
|
|
var ip net.IP
|
|
switch v := addr.(type) {
|
|
case *net.IPNet:
|
|
ip = v.IP
|
|
case *net.IPAddr:
|
|
ip = v.IP
|
|
}
|
|
if ip == nil || ip.IsLoopback() {
|
|
continue
|
|
}
|
|
ipStr := ip.String()
|
|
if ip.To4() != nil {
|
|
ips = append(ips, ipStr)
|
|
}
|
|
}
|
|
}
|
|
return ips
|
|
}
|