Compare commits

..

2 Commits
v1.0.0 ... main

Author SHA1 Message Date
raul 79c41b12ac Return IP on any GET request sent to the server 2024-05-21 08:25:57 +02:00
raul 94e84cce41 Add logging capabilities 2024-05-17 13:45:29 +02:00
1 changed files with 27 additions and 0 deletions

View File

@ -3,7 +3,10 @@ package cmd
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"time"
)
var (
@ -12,11 +15,35 @@ var (
func server() {
fmt.Printf("Listening on port %v...\n", listenPort)
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
router := gin.New()
router.GET("/", serveIP)
router.GET("/:id", serveIP)
router.Run(":" + listenPort)
}
func serveIP(c *gin.Context) {
c.Writer.Header().Set("Federal-Agents", "Outside my home")
c.String(http.StatusOK, c.ClientIP()+"\n")
writeToLog(c.ClientIP(), c.Request.UserAgent(), c.Request.URL.Path, c.Request.Method)
}
func writeToLog(ip string, useragent string, request string, requestmethod string) {
f, err := os.OpenFile("./reply-ip.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0660)
if err != nil {
log.Printf("Error happened opening file: %v\n", err)
}
defer f.Close()
log := fmt.Sprintf("[%v] | [%v] | [%v] | [%v \"%v\"]\n", getTime(), useragent, ip, requestmethod, request)
f.WriteString(log)
}
func getTime() string {
t := time.Now()
currentTime := fmt.Sprintf("%d-%02d-%02d | %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
return currentTime
}