This commit is contained in:
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module git.send.nrw/StadtHilden/ipv6calculator
|
module git.send.nrw/sendnrw/ipv6calculator
|
||||||
|
|
||||||
go 1.23.1
|
go 1.23.1
|
||||||
|
115
main.go
115
main.go
@@ -6,45 +6,23 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
listenAddr = ":8080" // server port
|
listenAddr = ":8080" // TCP port for the web UI
|
||||||
ulaPrefix = "fdcb:7de3:a12a:0::" // fixed ULA /96 prefix
|
defaultPrefix = "fdcb:7de3:a12a:0::" // fallback when ULA_PREFIX is unset
|
||||||
|
defaultIP = "172.16.0.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
// pageTemplate is served for both GET (empty result) and POST (with result).
|
var (
|
||||||
var pageTemplate = template.Must(template.New("page").Parse(`<!DOCTYPE html>
|
ulaPrefix string // effective /96 prefix (always ending in "::")
|
||||||
<html lang="de">
|
pageTemplate *template.Template // populated in init()
|
||||||
<head>
|
pageIP string
|
||||||
<meta charset="utf-8"/>
|
)
|
||||||
<title>IPv4 → IPv6‑Mapper</title>
|
|
||||||
<style>
|
|
||||||
body { font-family: system-ui, sans-serif; margin: 2rem; }
|
|
||||||
form { display:flex; gap:.5rem; }
|
|
||||||
input[type=text] { flex:1; padding:.4rem; font-size:1rem; }
|
|
||||||
button { padding:.5rem 1rem; font-size:1rem; cursor:pointer; }
|
|
||||||
#result { margin-top:1.5rem; font-weight:bold; }
|
|
||||||
.error { color:#b00; }
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>IPv4 → IPv6‑Mapper</h1>
|
|
||||||
<form action="/convert" method="post">
|
|
||||||
<input type="text" name="ipv4" placeholder="172.29.0.0" value="{{.IPv4}}" required />
|
|
||||||
<button type="submit">Umrechnen</button>
|
|
||||||
</form>
|
|
||||||
{{if .IPv6}}
|
|
||||||
<div id="result">IPv6‑Adresse: <code>{{.IPv6}}</code></div>
|
|
||||||
{{end}}
|
|
||||||
{{if .Error}}
|
|
||||||
<div class="error">Fehler: {{.Error}}</div>
|
|
||||||
{{end}}
|
|
||||||
<p>Präfix: <code>` + ulaPrefix + `</code> (feste /96‑Zuordnung)</p>
|
|
||||||
</body>
|
|
||||||
</html>`))
|
|
||||||
|
|
||||||
// viewData transports data into the page template.
|
// viewData feeds data into the HTML template.
|
||||||
type viewData struct {
|
type viewData struct {
|
||||||
IPv4 string
|
IPv4 string
|
||||||
IPv6 string
|
IPv6 string
|
||||||
@@ -52,9 +30,10 @@ type viewData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
initPrefixAndTemplate()
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Empty page on root
|
renderPage(w, viewData{}) // empty form
|
||||||
renderPage(w, viewData{})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/convert", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/convert", func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -73,19 +52,73 @@ func main() {
|
|||||||
renderPage(w, data)
|
renderPage(w, data)
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("Server läuft auf http://localhost%s …", listenAddr)
|
log.Printf("Server läuft auf http://localhost%s (Präfix: %s)", listenAddr, ulaPrefix)
|
||||||
log.Fatal(http.ListenAndServe(listenAddr, nil))
|
log.Fatal(http.ListenAndServe(listenAddr, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
// embedIPv4 converts an IPv4 dotted string into the ulaPrefix‑based IPv6 address.
|
// 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(`<!DOCTYPE html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>IPv4 → IPv6‑Mapper</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: system-ui, sans-serif; margin: 2rem; }
|
||||||
|
form { display: flex; gap: .5rem; }
|
||||||
|
input[type=text] { flex: 1; padding: .4rem; font-size: 1rem; }
|
||||||
|
button { padding: .5rem 1rem; font-size: 1rem; cursor: pointer; }
|
||||||
|
#result { margin-top: 1.5rem; font-weight: bold; }
|
||||||
|
.error { color: #b00; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>IPv4 → IPv6-Mapper</h1>
|
||||||
|
<form action="/convert" method="post">
|
||||||
|
<input type="text" name="ipv4" placeholder="%s" value="{{.IPv4}}" required />
|
||||||
|
<button type="submit">Umrechnen</button>
|
||||||
|
</form>
|
||||||
|
{{if .IPv6}}
|
||||||
|
<div id="result">IPv6-Adresse: <code>{{.IPv6}}</code></div>
|
||||||
|
{{end}}
|
||||||
|
{{if .Error}}
|
||||||
|
<div class="error">Fehler: {{.Error}}</div>
|
||||||
|
{{end}}
|
||||||
|
<p>Präfix: <code>%s</code> (/96‑Zuordnung)</p>
|
||||||
|
</body>
|
||||||
|
</html>`, pageIP, ulaPrefix)
|
||||||
|
|
||||||
|
pageTemplate = template.Must(template.New("page").Parse(html))
|
||||||
|
}
|
||||||
|
|
||||||
|
// embedIPv4 converts a dotted IPv4 string into the chosen ULA /96 IPv6 address.
|
||||||
func embedIPv4(ipv4 string) (string, error) {
|
func embedIPv4(ipv4 string) (string, error) {
|
||||||
ip := net.ParseIP(ipv4).To4()
|
ip := net.ParseIP(ipv4).To4()
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return "", fmt.Errorf("'%s' ist keine gültige IPv4‑Adresse", ipv4)
|
return "", fmt.Errorf("'%s' ist keine gültige IPv4-Adresse", ipv4)
|
||||||
}
|
}
|
||||||
first := uint16(ip[0])<<8 | uint16(ip[1]) // High 16 bits
|
hi := uint16(ip[0])<<8 | uint16(ip[1]) // high 16 bits (octets 0–1)
|
||||||
second := uint16(ip[2])<<8 | uint16(ip[3]) // Low 16 bits
|
lo := uint16(ip[2])<<8 | uint16(ip[3]) // low 16 bits (octets 2–3)
|
||||||
return fmt.Sprintf("%s%x:%x", ulaPrefix, first, second), nil
|
return fmt.Sprintf("%s%x:%x", ulaPrefix, hi, lo), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderPage(w http.ResponseWriter, d viewData) {
|
func renderPage(w http.ResponseWriter, d viewData) {
|
||||||
|
Reference in New Issue
Block a user