Browse Source

Use IP from form if set

Volodymyr Tkach 2 years ago
parent
commit
ee8834d723
1 changed files with 6 additions and 0 deletions
  1. 6 0
      internal/server/handler/page/page_index/page_index.go

+ 6 - 0
internal/server/handler/page/page_index/page_index.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"html/template"
 	"net/http"
+	"strings"
 
 	"github.com/vladimirok5959/golang-ip2location/internal/consts"
 	"github.com/vladimirok5959/golang-ip2location/internal/server/handler/base"
@@ -29,6 +30,11 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
 	additional.ClientIP = helpers.ClientIP(r)
 
+	ip := strings.Trim(r.FormValue("ip"), " ")
+	if ip != "" && len([]rune(ip)) <= 15 {
+		additional.ClientIP = ip
+	}
+
 	if h.Client != nil {
 		if res, err := h.Client.IP2Location(r.Context(), additional.ClientIP); err == nil {
 			if j, err := json.Marshal(res); err == nil {