package main import ( "fmt" "html/template" "log" "net" "net/http" "os" "strings" ) const ( listenAddr = ":8080" // TCP port for the web UI defaultPrefix = "fdcb:7de3:a12a:0::" // fallback when ULA_PREFIX is unset defaultIP = "172.16.0.0" ) var ( ulaPrefix string // effective /96 prefix (always ending in "::") pageTemplate *template.Template // populated in init() pageIP string ) // viewData feeds data into the HTML template. type viewData struct { IPv4 string IPv6 string Error string } func main() { initPrefixAndTemplate() http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { renderPage(w, viewData{}) // empty form }) http.HandleFunc("/convert", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { renderPage(w, viewData{Error: "Ungültiges Formular"}) return } ipv4Str := r.FormValue("ipv4") ipv6, err := embedIPv4(ipv4Str) data := viewData{IPv4: ipv4Str} if err != nil { data.Error = err.Error() } else { data.IPv6 = ipv6 } renderPage(w, data) }) log.Printf("Server läuft auf http://localhost%s (Präfix: %s)", listenAddr, ulaPrefix) log.Fatal(http.ListenAndServe(listenAddr, nil)) } // initPrefixAndTemplate reads the environment variable and prepares the HTML template. func initPrefixAndTemplate() { ulaPrefix = os.Getenv("ULA_PREFIX") if ulaPrefix == "" { ulaPrefix = defaultPrefix } pageIP = os.Getenv("IPv4") if pageIP == "" { pageIP = defaultIP } // Ensure the prefix ends with exactly two colons (::) so that we can append hex words. if !strings.HasSuffix(ulaPrefix, "::") { if strings.HasSuffix(ulaPrefix, ":") { ulaPrefix += ":" } else { ulaPrefix += "::" } } html := fmt.Sprintf(`
{{.IPv6}}
Präfix: %s
(/96‑Zuordnung)