reply-ip/cmd/serverFunc.go

50 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-05-09 09:31:14 +02:00
package cmd
import (
"fmt"
"github.com/gin-gonic/gin"
2024-05-17 13:45:29 +02:00
"log"
2024-05-09 09:31:14 +02:00
"net/http"
2024-05-17 13:45:29 +02:00
"os"
"time"
2024-05-09 09:31:14 +02:00
)
var (
listenPort string = "1302"
)
func server() {
fmt.Printf("Listening on port %v...\n", listenPort)
2024-05-17 13:45:29 +02:00
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
2024-05-09 09:31:14 +02:00
router := gin.New()
router.GET("/", serveIP)
router.GET("/:id", serveIP)
2024-05-09 09:31:14 +02:00
router.Run(":" + listenPort)
}
func serveIP(c *gin.Context) {
2024-05-17 13:45:29 +02:00
c.Writer.Header().Set("Federal-Agents", "Outside my home")
2024-05-09 09:31:14 +02:00
c.String(http.StatusOK, c.ClientIP()+"\n")
2024-05-17 13:45:29 +02:00
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
2024-05-09 09:31:14 +02:00
}