// A small Go web‑server that converts an IPv4 address into an IPv6 address // inside a user‑defined /96 ULA prefix and outputs Windows 11‑ready fields. // // Environment variables (all optional): // // ULA_PREFIX – /96 prefix to embed into, default "fdcb:7de3:a12a:0::" // FORM_DEFAULT_IP - legt das Form Placeholder IP Feld fest (default: 172.16.0.0) // DNS1 – preferred DNS IPv6 address (default: ) // DNS2 – alternate DNS IPv6 address (default: ) // // Build & run: // // export ULA_PREFIX="fde5:1234:abcd:0::" // export DNS1="fde5:1234:abcd:1::53" # optional // export DNS2="fde5:1234:abcd:1::54" # optional // go run main.go // // Open http://localhost:8080 in a browser. package main import ( "fmt" "html/template" "log" "net" "net/http" "os" "strings" ) const ( listenAddr = ":8080" defaultPrefix = "fd09:cafe:affe:4010::" // fallback /96 prefixLen = 96 // fixed /96 mapping defaultIP = "172.16.0.0" ) var ( ulaPrefix string // effective /96 prefix (always ends with ::) dns1 string // preferred DNS dns2 string // alternate DNS pageIP string pageTemplate *template.Template // compiled HTML template ) // viewData is passed into the template. type viewData struct { IPv4 string IPv6 string Gateway string DNS1 string DNS2 string Error string HaveResult bool } func main() { initConfigAndTemplate() http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { renderPage(w, viewData{}) }) 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.HaveResult = true data.IPv6 = ipv6 data.Gateway = ulaPrefix + "1" // router IP = ::1 data.DNS1 = dns1 data.DNS2 = dns2 } renderPage(w, data) }) log.Printf("Server läuft auf http://localhost%s (Präfix %s, DNS1 %s, DNS2 %s)", listenAddr, ulaPrefix, dns1, dns2) log.Fatal(http.ListenAndServe(listenAddr, nil)) } // initConfigAndTemplate reads env vars and prepares HTML template. func initConfigAndTemplate() { // ---- ULA prefix ------------------------------------------------------- ulaPrefix = os.Getenv("ULA_PREFIX") if ulaPrefix == "" { ulaPrefix = defaultPrefix } pageIP = os.Getenv("FORM_DEFAULT_IP") if pageIP == "" { pageIP = defaultIP } // ensure trailing :: so we can simply append hex words if !strings.HasSuffix(ulaPrefix, "::") { if strings.HasSuffix(ulaPrefix, ":") { ulaPrefix += ":" } else { ulaPrefix += "::" } } // ---- DNS addresses ---------------------------------------------------- dns1 = os.Getenv("DNS1") dns2 = os.Getenv("DNS2") // default DNS: change subnet 0 → 1 and append ::53 / ::54 if dns1 == "" { dns1 = strings.Replace(ulaPrefix, "::", "::53", 1) } if dns2 == "" { dns2 = strings.Replace(ulaPrefix, "::", "::54", 1) } // ---- HTML template ---------------------------------------------------- html := fmt.Sprintf(` IPv4 → IPv6‑Mapper

IPv4 → IPv6‑Mapper

{{if .HaveResult}}

Windows 11‑Eingaben

{{end}} {{if .Error}}

Fehler: {{.Error}}

{{end}}

Aktives Präfix: %s (/96)

`, pageIP, ulaPrefix) pageTemplate = template.Must(template.New("page").Parse(html)) } // embedIPv4 converts a dotted IPv4 string into an IPv6 address within ulaPrefix/96. func embedIPv4(ipv4 string) (string, error) { ip := net.ParseIP(ipv4).To4() if ip == nil { return "", fmt.Errorf("'%s' ist keine gültige IPv4-Adresse", ipv4) } hi := uint16(ip[0])<<8 | uint16(ip[1]) lo := uint16(ip[2])<<8 | uint16(ip[3]) return fmt.Sprintf("%s%x:%x", ulaPrefix, hi, lo), nil } func renderPage(w http.ResponseWriter, d viewData) { w.Header().Set("Content-Type", "text/html; charset=utf-8") if err := pageTemplate.Execute(w, d); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }