reply-ip/cmd/serverFunc.go

49 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"time"
)
var (
listenPort string = "1302"
)
func server() {
fmt.Printf("Listening on port %v...\n", listenPort)
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
router := gin.New()
router.GET("/", 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
}