added logging

This commit is contained in:
Calamitas 2022-07-13 21:50:20 -05:00
parent 6c0ef42e6a
commit 19a63f7715

View File

@ -4,11 +4,13 @@ import (
"fmt"
"log"
"net/http"
"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
ip := GetRealIP(r)
fmt.Fprintf(w, ip)
WritetoFile(ip)
}
func GetRealIP(r *http.Request) string {
@ -24,6 +26,17 @@ func GetRealIP(r *http.Request) string {
}
return IP
}
func WritetoFile(IP string) {
f, err := os.OpenFile("ListofIPs.txt",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
if _, err := f.WriteString(IP + "\n"); err != nil {
log.Println(err)
}
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8081", nil))