2024-08-26 22:01:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerConfig struct {
|
|
|
|
listen string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
config := ServerConfig{
|
|
|
|
listen: getEnvOr("IP_CHECKER_LISTEN", ":8080"),
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := template.Must(template.ParseFiles("assets/index.html"))
|
|
|
|
|
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets/static"))))
|
|
|
|
|
2024-10-19 04:19:03 +00:00
|
|
|
http.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
|
2024-08-26 22:01:24 +00:00
|
|
|
handleRequest(w, r, tmpl)
|
|
|
|
})
|
|
|
|
|
2024-10-19 04:19:03 +00:00
|
|
|
http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, `User-Agent: *
|
|
|
|
Disallow: /harming/humans
|
|
|
|
Disallow: /ignoring/human/orders
|
|
|
|
Disallow: /harm/to/self
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
2024-08-26 22:01:24 +00:00
|
|
|
log.Printf("Starting server on %s", config.listen)
|
|
|
|
log.Fatal(http.ListenAndServe(config.listen, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleRequest(w http.ResponseWriter, r *http.Request, tmpl *template.Template) {
|
2024-10-19 05:28:17 +00:00
|
|
|
sourceIP := func() string {
|
2024-10-19 05:33:40 +00:00
|
|
|
if r.Header.Get("CF-Connecting-IP") != "" {
|
|
|
|
return r.Header.Get("CF-Connecting-IP")
|
|
|
|
}
|
2024-10-19 05:28:17 +00:00
|
|
|
if r.Header.Get("X-Forwarded-For") != "" {
|
|
|
|
return r.Header.Get("X-Forwarded-For")
|
|
|
|
}
|
2024-10-19 05:33:40 +00:00
|
|
|
return strings.Split(r.RemoteAddr, ":")[0]
|
2024-10-19 05:28:17 +00:00
|
|
|
}()
|
|
|
|
|
2024-08-26 22:01:24 +00:00
|
|
|
isHeadless := strings.HasPrefix(r.Header.Get("User-Agent"), "curl/")
|
|
|
|
|
|
|
|
log.Printf("r.URL.Path: %s, sourceIP: %s, isHeadless: %t, User-Agent: %s", r.URL.Path, sourceIP, isHeadless, r.Header.Get("User-Agent"))
|
|
|
|
|
2024-10-19 04:19:03 +00:00
|
|
|
if isHeadless {
|
|
|
|
fmt.Fprintf(w, "%s\n", sourceIP)
|
|
|
|
} else {
|
|
|
|
csp := []string{
|
|
|
|
"default-src 'none'",
|
|
|
|
"img-src 'self'",
|
|
|
|
"script-src-elem 'self'",
|
|
|
|
"style-src-elem 'self' fonts.googleapis.com",
|
|
|
|
"font-src fonts.gstatic.com",
|
|
|
|
"connect-src api.ip.sb api-v3.speedtest.cn api.ipapi.is",
|
2024-08-26 22:01:24 +00:00
|
|
|
}
|
2024-10-19 04:19:03 +00:00
|
|
|
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
|
2024-08-26 22:01:24 +00:00
|
|
|
|
2024-10-19 04:19:03 +00:00
|
|
|
data := map[string]string{
|
|
|
|
"sourceIP": sourceIP,
|
|
|
|
}
|
2024-08-26 22:01:24 +00:00
|
|
|
|
2024-10-19 04:19:03 +00:00
|
|
|
err := tmpl.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to load template", http.StatusInternalServerError)
|
|
|
|
log.Printf("Template execution error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2024-08-26 22:01:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEnvOr(key, defaultValue string) string {
|
|
|
|
if value, exists := os.LookupEnv(key); exists {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|