add /api/outbound-ip api

This commit is contained in:
c9s 2022-04-14 10:14:19 +08:00
parent 46ce0476be
commit cd957460c9

View File

@ -88,6 +88,19 @@ func (s *Server) newEngine() *gin.Engine {
}) })
}) })
r.GET("/api/outbound-ip", func(c *gin.Context) {
outboundIP, err := GetOutboundIP()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
c.JSON(http.StatusOK, gin.H{
"outboundIP": outboundIP.String(),
})
})
r.GET("/api/trades", func(c *gin.Context) { r.GET("/api/trades", func(c *gin.Context) {
if s.Environ.TradeService == nil { if s.Environ.TradeService == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "database is not configured"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "database is not configured"})
@ -621,3 +634,14 @@ func listenAndServe(srv *http.Server) error {
return nil return nil
} }
func GetOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return nil, err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}