From 3328c46ecfa7772b5a44e421a16b715761e7a3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD?= Date: Sun, 1 Oct 2023 02:11:13 +0600 Subject: [PATCH] Create main.go --- main.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..52e2a86 --- /dev/null +++ b/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "os/exec" + "regexp" +) + +func getIP(w http.ResponseWriter, r *http.Request) { + ip := r.Header.Get("X-Real-IP") + if ip == "" { + ip = r.RemoteAddr + } + + err := addToWhiteList(ip) + if err != nil { + fmt.Println("Error:", err) + // Handle the error response as needed. + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } +} + +func addToWhiteList(newIPAddress string) error { + serviceFilePath := "/etc/systemd/system/caddy.service" + + content, err := ioutil.ReadFile(serviceFilePath) + if err != nil { + fmt.Println("Error reading file:", err) + return err + } + + originalString := string(content) + + re := regexp.MustCompile(`(WHITE_LIST=)([^"\s]+)`) + + match := re.FindString(originalString) + + if match != "" { + existingWhiteList := re.FindStringSubmatch(originalString)[2] + + newWhiteList := existingWhiteList + " " + newIPAddress + + updatedString := re.ReplaceAllString(originalString, "${1}"+newWhiteList) + + err = ioutil.WriteFile(serviceFilePath, []byte(updatedString), 0644) + if err != nil { + fmt.Println("Error writing file:", err) + return err + } + + if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil { + return err + } + + if err := exec.Command("systemctl", "restart", "caddy.service").Run(); err != nil { + return err + } + + fmt.Println("IP successfully bleached - ", newIPAddress) + + return nil + } else { + fmt.Println("WHITE_LIST not found.") + return nil + } +} + +func main() { + http.HandleFunc("/", getIP) + + port := 50009 + fmt.Printf("Server is running on port %d...\n", port) + + err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil) + if err != nil { + fmt.Println("Error starting server:", err) + } +}