Goit

Simple and lightweight Git web server
Mirror of https://github.com/Jamozed/Goit
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2023-12-17 09:28:16
Commite530f2c3c78a07dfb1531cfb8274183ba664a68f
Parent54be405223f1437c7715f9d6d407539e6eaf3ef6

Add option to use forwarded IP in logs

Diffstat

M src/goit/auth.go | 4 ++--
M src/goit/goit.go | 28 ++++++++++++++++------------
M src/goit/http.go | 10 ++++++++++
M src/main.go | 37 ++++++++++++++++++++-----------------
M src/user/login.go | 6 +-----

5 files changed, 49 insertions, 36 deletions

diff --git a/src/goit/auth.go b/src/goit/auth.go
index b0a9c31..41832d4 100644
--- a/src/goit/auth.go
+++ b/src/goit/auth.go
@@ -8,7 +8,6 @@ import (
 	"encoding/base64"
 	"fmt"
 	"log"
-	"net"
 	"net/http"
 	"strconv"
 	"strings"
@@ -183,7 +182,8 @@ func Auth(w http.ResponseWriter, r *http.Request, renew bool) (bool, *User, erro
 
 	/* Renew the session if appropriate */
 	if renew && time.Until(s.Expiry) < 24*time.Hour {
-		ip, _, _ := net.SplitHostPort(r.RemoteAddr)
+		ip := Ip(r)
+
 		s1, err := NewSession(uid, ip, time.Now().Add(2*24*time.Hour))
 		if err != nil {
 			log.Println("[auth/renew]", err.Error())
diff --git a/src/goit/goit.go b/src/goit/goit.go
index 37405f8..9067d28 100644
--- a/src/goit/goit.go
+++ b/src/goit/goit.go
@@ -28,23 +28,25 @@ import (
 )
 
 type Config struct {
-	DataPath   string `json:"data_path"`
-	HttpAddr   string `json:"http_addr"`
-	HttpPort   string `json:"http_port"`
-	GitPath    string `json:"git_path"`
-	IpSessions bool   `json:"ip_sessions"`
-	UsesHttps  bool   `json:"uses_https"`
-	CsrfSecret string `json:"csrf_secret"`
+	DataPath    string `json:"data_path"`
+	HttpAddr    string `json:"http_addr"`
+	HttpPort    string `json:"http_port"`
+	GitPath     string `json:"git_path"`
+	IpSessions  bool   `json:"ip_sessions"`
+	UsesHttps   bool   `json:"uses_https"`
+	IpForwarded bool   `json:"ip_forwarded"`
+	CsrfSecret  string `json:"csrf_secret"`
 }
 
 var Conf = Config{
-	DataPath:   filepath.Join(xdg.DataHome, "goit"),
-	HttpAddr:   "",
-	HttpPort:   "8080",
-	GitPath:    "git",
-	IpSessions: true,
-	UsesHttps:  false,
-	CsrfSecret: "1234567890abcdef1234567890abcdef",
+	DataPath:    filepath.Join(xdg.DataHome, "goit"),
+	HttpAddr:    "",
+	HttpPort:    "8080",
+	GitPath:     "git",
+	IpSessions:  true,
+	UsesHttps:   false,
+	IpForwarded: false,
+	CsrfSecret:  "1234567890abcdef1234567890abcdef",
 }
 
 var db *sql.DB
diff --git a/src/goit/http.go b/src/goit/http.go
index 8bf43d0..80f1b9e 100644
--- a/src/goit/http.go
+++ b/src/goit/http.go
@@ -6,6 +6,7 @@ package goit
 import (
 	"fmt"
 	"html/template"
+	"net"
 	"net/http"
 
 	"github.com/Jamozed/Goit/res"
@@ -50,3 +51,12 @@ func HttpError(w http.ResponseWriter, code int) {
 func HttpNotFound(w http.ResponseWriter, r *http.Request) {
 	HttpError(w, http.StatusNotFound)
 }
+
+func Ip(r *http.Request) string {
+	if fip := r.Header.Get("X-Forwarded-For"); Conf.IpForwarded && fip != "" {
+		return fip
+	}
+
+	ip, _, _ := net.SplitHostPort(r.RemoteAddr)
+	return ip
+}
diff --git a/src/main.go b/src/main.go
index 14784ec..7087994 100644
--- a/src/main.go
+++ b/src/main.go
@@ -84,12 +84,7 @@ func main() {
 	h := chi.NewRouter()
 	h.NotFound(goit.HttpNotFound)
 	h.Use(middleware.RedirectSlashes)
-
-	if util.Debug {
-		h.Use(middleware.Logger)
-	} else {
-		h.Use(logHttp)
-	}
+	h.Use(logHttp)
 
 	protect = csrf.Protect(
 		[]byte(goit.Conf.CsrfSecret), csrf.FieldName("csrf.Token"), csrf.CookieName("csrf"),
@@ -164,22 +159,28 @@ func main() {
 
 	/* Listen for HTTP on the specified port */
 	if err := http.ListenAndServe(goit.Conf.HttpAddr+":"+goit.Conf.HttpPort, h); err != nil {
-		log.Fatalln("[HTTP]", err.Error())
+		log.Fatalln("[http]", err.Error())
 	}
 }
 
-func logHttp(handler http.Handler) http.Handler {
+func logHttp(next http.Handler) http.Handler {
 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		log.Println("[HTTP]", r.RemoteAddr, r.Method, r.URL.String())
-		// log.Println("[HTTP]", r.Header)
-		handler.ServeHTTP(w, r)
+		t1 := time.Now()
+		next.ServeHTTP(w, r)
+
+		ip := r.RemoteAddr
+		if fip := r.Header.Get("X-Forwarded-For"); goit.Conf.IpForwarded && fip != "" {
+			ip = fip
+		}
+
+		log.Println("[http]", r.Method, r.URL.String(), "from", ip, "in", time.Since(t1))
 	})
 }
 
 func handleStyle(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "text/css")
 	if _, err := w.Write([]byte(res.Style)); err != nil {
-		log.Println("[Style]", err.Error())
+		log.Println("[style]", err.Error())
 	}
 }
 
@@ -189,7 +190,7 @@ func handleFavicon(w http.ResponseWriter, r *http.Request) {
 	} else {
 		w.Header().Set("Content-Type", "image/png")
 		if _, err := w.Write(goit.Favicon); err != nil {
-			log.Println("[Favicon]", err.Error())
+			log.Println("[favicon]", err.Error())
 		}
 	}
 }
diff --git a/src/user/login.go b/src/user/login.go
index fc3defa..aa81c61 100644
--- a/src/user/login.go
+++ b/src/user/login.go
@@ -7,7 +7,6 @@ import (
 	"bytes"
 	"html/template"
 	"log"
-	"net"
 	"net/http"
 	"time"
 
@@ -46,10 +45,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
 			goto execute
 		}
 
-		ip, _, _ := net.SplitHostPort(r.RemoteAddr)
-		if fip := r.Header.Get("X-Forwarded-For"); fip != "" {
-			ip = fip
-		}
+		ip := goit.Ip(r)
 
 		user, err := goit.GetUserByName(data.Name)
 		if err != nil {