Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
c910cc6cd6
4 changed files with 90 additions and 7 deletions
  1. 58 5
      mod/geodb/netutils.go
  2. 3 1
      mod/statistic/statistic.go
  3. 17 0
      mod/statistic/utils.go
  4. 12 1
      mod/tcpprox/handler.go

+ 58 - 5
mod/geodb/netutils.go

@@ -1,6 +1,7 @@
 package geodb
 
 import (
+	"log"
 	"net"
 	"net/http"
 	"strings"
@@ -8,14 +9,56 @@ import (
 
 // Utilities function
 func GetRequesterIP(r *http.Request) string {
-	ip := r.Header.Get("X-Forwarded-For")
+	ip := r.Header.Get("X-Real-Ip")
 	if ip == "" {
-		ip = r.Header.Get("X-Real-IP")
-		if ip == "" {
-			ip = strings.Split(r.RemoteAddr, ":")[0]
+		ip = r.Header.Get("X-Forwarded-For")
+	}
+	if ip == "" {
+		ip = r.RemoteAddr
+	}
+
+	/*
+		Possible shits that might be extracted by this code
+		127.0.0.1:61001
+		[15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7]:61002
+		127.0.0.1
+		158.250.160.114,109.21.249.211
+		[15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7],109.21.249.211
+
+		We need to extract just the first ip address
+	*/
+	requesterRawIp := ip
+	if strings.Contains(requesterRawIp, ",") {
+		//Trim off all the forwarder IPs
+		requesterRawIp = strings.Split(requesterRawIp, ",")[0]
+	}
+
+	//Trim away the port number
+	if strings.Contains(requesterRawIp, "]:") {
+		//e.g. [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7]:61002
+		requesterRawIp = strings.Split(requesterRawIp, "]:")[0]
+		requesterRawIp = strings.TrimSpace(requesterRawIp[1:])
+		//The remaining part should be a ipv6 string
+		//e.g. 15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7
+		if !IsIPv6(requesterRawIp) {
+			//WTF is this?
+			log.Println("Unknown request IPv6 extracted: " + requesterRawIp)
+			return ""
 		}
+	} else if strings.Contains(requesterRawIp, ":") && strings.Contains(requesterRawIp, ".") {
+		//e.g. 127.0.0.1:61001
+		requesterRawIp = strings.Split(requesterRawIp, ":")[0]
+		requesterRawIp = strings.TrimSpace(requesterRawIp)
+		if !IsIPv4(requesterRawIp) {
+			log.Println("Unknown request IPv4 extracted: " + requesterRawIp)
+			return ""
+		}
+	} else if strings.HasPrefix(requesterRawIp, "[") && strings.HasSuffix(requesterRawIp, "]") {
+		//e.g. [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7]
+		requesterRawIp = requesterRawIp[1 : len(requesterRawIp)-1]
 	}
-	return ip
+
+	return requesterRawIp
 }
 
 // Match the IP address with a wildcard string
@@ -75,3 +118,13 @@ func IsIPv6(ipStr string) bool {
 
 	return ip.To4() == nil && ip.To16() != nil
 }
+
+// Check if an Ip string is ipv6
+func IsIPv4(ipStr string) bool {
+	ip := net.ParseIP(ipStr)
+	if ip == nil {
+		return false
+	}
+
+	return ip.To4() != nil
+}

+ 3 - 1
mod/statistic/statistic.go

@@ -145,7 +145,9 @@ func (c *Collector) RecordRequest(ri RequestInfo) {
 		//Filter out CF forwarded requests
 		if strings.Contains(ri.IpAddr, ",") {
 			ips := strings.Split(strings.TrimSpace(ri.IpAddr), ",")
-			if len(ips) >= 1 {
+			if len(ips) >= 1 && IsValidIPAddress(strings.TrimPrefix(ips[0], "[")) {
+				//Example when forwarded from CF: 158.250.160.114,109.21.249.211
+				//Or IPv6 [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7],109.21.249.211
 				ri.IpAddr = ips[0]
 			}
 		}

+ 17 - 0
mod/statistic/utils.go

@@ -2,6 +2,7 @@ package statistic
 
 import (
 	"fmt"
+	"net"
 	"time"
 )
 
@@ -26,3 +27,19 @@ func IsBeforeToday(dateString string) bool {
 	today := time.Now().UTC().Truncate(24 * time.Hour)
 	return date.Before(today) || dateString == time.Now().Format(layout)
 }
+
+// Check if the IP string is a valid ip address
+func IsValidIPAddress(ip string) bool {
+	// Check if the string is a valid IPv4 address
+	if parsedIP := net.ParseIP(ip); parsedIP != nil && parsedIP.To4() != nil {
+		return true
+	}
+
+	// Check if the string is a valid IPv6 address
+	if parsedIP := net.ParseIP(ip); parsedIP != nil && parsedIP.To16() != nil {
+		return true
+	}
+
+	// If the string is neither a valid IPv4 nor IPv6 address, return false
+	return false
+}

+ 12 - 1
mod/tcpprox/handler.go

@@ -179,7 +179,18 @@ func (m *Manager) HandleRemoveProxy(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	m.RemoveConfig(targetProxyConfig.UUID)
+	if targetProxyConfig.IsRunning() {
+		utils.SendErrorResponse(w, "Service is running")
+		return
+	}
+
+	err = m.RemoveConfig(targetProxyConfig.UUID)
+	if err != nil {
+		utils.SendErrorResponse(w, err.Error())
+		return
+	}
+
+	utils.SendOK(w)
 }
 
 func (m *Manager) HandleGetProxyStatus(w http.ResponseWriter, r *http.Request) {