Browse Source

Added not tested banned by request handler

tobychui 3 years ago
parent
commit
b0eb08c9be
3 changed files with 62 additions and 2 deletions
  1. 1 2
      mod/auth/auth.go
  2. 34 0
      mod/auth/blacklist/handler.go
  3. 27 0
      mod/network/network.go

+ 1 - 2
mod/auth/auth.go

@@ -10,7 +10,7 @@ See https://gowebexamples.com/sessions/ for detail.
 Auth database are stored as the following key
 
 auth/login/{username}/passhash => hashed password
-auth/login/{username}/permission => permission level (wip)
+auth/login/{username}/permission => permission level
 
 Other system variables related to auth
 
@@ -26,7 +26,6 @@ import (
 	"strings"
 	"sync"
 
-	//"encoding/json"
 	"encoding/hex"
 	"log"
 	"time"

+ 34 - 0
mod/auth/blacklist/handler.go

@@ -0,0 +1,34 @@
+package blacklist
+
+import (
+	"net/http"
+
+	"imuslab.com/arozos/mod/network"
+)
+
+/*
+	Handler for blacklist module
+
+*/
+
+func (bl *BlackList) HandleAddBannedIP(w http.ResponseWriter, r *http.Request) {
+
+}
+
+func (bl *BlackList) HandleRemoveBannedIP() {
+
+}
+
+func (bl *BlackList) HandleListBannedIPs() {
+
+}
+
+func (bl *BlackList) CheckIsBannedByRequest(r *http.Request) bool {
+	//Get the IP address from the request header
+	requestIP, err := network.GetIpFromRequest(r)
+	if err != nil {
+		return false
+	}
+
+	return bl.IsBanned(requestIP)
+}

+ 27 - 0
mod/network/network.go

@@ -194,3 +194,30 @@ func IsIPv6Addr(ip string) (bool, error) {
 func GetPing(w http.ResponseWriter, r *http.Request) {
 	sendJSONResponse(w, "pong")
 }
+
+func GetIpFromRequest(r *http.Request) (string, error) {
+	ip := r.Header.Get("X-REAL-IP")
+	netIP := net.ParseIP(ip)
+	if netIP != nil {
+		return ip, nil
+	}
+
+	ips := r.Header.Get("X-FORWARDED-FOR")
+	splitIps := strings.Split(ips, ",")
+	for _, ip := range splitIps {
+		netIP := net.ParseIP(ip)
+		if netIP != nil {
+			return ip, nil
+		}
+	}
+
+	ip, _, err := net.SplitHostPort(r.RemoteAddr)
+	if err != nil {
+		return "", err
+	}
+	netIP = net.ParseIP(ip)
+	if netIP != nil {
+		return ip, nil
+	}
+	return "", errors.New("No IP information found")
+}