Fix und Anpassungen für DNS und Gateway
All checks were successful
release-tag / release-image (push) Successful in 1m52s
All checks were successful
release-tag / release-image (push) Successful in 1m52s
This commit is contained in:
145
main.go
145
main.go
@@ -1,3 +1,21 @@
|
|||||||
|
// 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: <prefix with 1::53>)
|
||||||
|
// DNS2 – alternate DNS IPv6 address (default: <prefix with 1::54>)
|
||||||
|
//
|
||||||
|
// 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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -11,29 +29,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
listenAddr = ":8080" // TCP port for the web UI
|
listenAddr = ":8080"
|
||||||
defaultPrefix = "fdcb:7de3:a12a:0::" // fallback when ULA_PREFIX is unset
|
defaultPrefix = "fd09:cafe:affe:4010::" // fallback /96
|
||||||
|
prefixLen = 96 // fixed /96 mapping
|
||||||
defaultIP = "172.16.0.0"
|
defaultIP = "172.16.0.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ulaPrefix string // effective /96 prefix (always ending in "::")
|
ulaPrefix string // effective /96 prefix (always ends with ::)
|
||||||
pageTemplate *template.Template // populated in init()
|
dns1 string // preferred DNS
|
||||||
|
dns2 string // alternate DNS
|
||||||
pageIP string
|
pageIP string
|
||||||
|
pageTemplate *template.Template // compiled HTML template
|
||||||
)
|
)
|
||||||
|
|
||||||
// viewData feeds data into the HTML template.
|
// viewData is passed into the template.
|
||||||
type viewData struct {
|
type viewData struct {
|
||||||
IPv4 string
|
IPv4 string
|
||||||
IPv6 string
|
IPv6 string
|
||||||
|
Gateway string
|
||||||
|
DNS1 string
|
||||||
|
DNS2 string
|
||||||
Error string
|
Error string
|
||||||
|
HaveResult bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
initPrefixAndTemplate()
|
initConfigAndTemplate()
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
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) {
|
||||||
@@ -47,28 +72,33 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
data.Error = err.Error()
|
data.Error = err.Error()
|
||||||
} else {
|
} else {
|
||||||
|
data.HaveResult = true
|
||||||
data.IPv6 = ipv6
|
data.IPv6 = ipv6
|
||||||
|
data.Gateway = ulaPrefix + "1" // router IP = <prefix>::1
|
||||||
|
data.DNS1 = dns1
|
||||||
|
data.DNS2 = dns2
|
||||||
}
|
}
|
||||||
renderPage(w, data)
|
renderPage(w, data)
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("Server läuft auf http://localhost%s (Präfix: %s)", listenAddr, ulaPrefix)
|
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))
|
log.Fatal(http.ListenAndServe(listenAddr, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
// initPrefixAndTemplate reads the environment variable and prepares the HTML template.
|
// initConfigAndTemplate reads env vars and prepares HTML template.
|
||||||
func initPrefixAndTemplate() {
|
func initConfigAndTemplate() {
|
||||||
|
// ---- ULA prefix -------------------------------------------------------
|
||||||
ulaPrefix = os.Getenv("ULA_PREFIX")
|
ulaPrefix = os.Getenv("ULA_PREFIX")
|
||||||
if ulaPrefix == "" {
|
if ulaPrefix == "" {
|
||||||
ulaPrefix = defaultPrefix
|
ulaPrefix = defaultPrefix
|
||||||
}
|
}
|
||||||
|
|
||||||
pageIP = os.Getenv("IPv4")
|
pageIP = os.Getenv("FORM_DEFAULT_IP")
|
||||||
if pageIP == "" {
|
if pageIP == "" {
|
||||||
pageIP = defaultIP
|
pageIP = defaultIP
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the prefix ends with exactly two colons (::) so that we can append hex words.
|
// ensure trailing :: so we can simply append hex words
|
||||||
if !strings.HasSuffix(ulaPrefix, "::") {
|
if !strings.HasSuffix(ulaPrefix, "::") {
|
||||||
if strings.HasSuffix(ulaPrefix, ":") {
|
if strings.HasSuffix(ulaPrefix, ":") {
|
||||||
ulaPrefix += ":"
|
ulaPrefix += ":"
|
||||||
@@ -77,47 +107,102 @@ func initPrefixAndTemplate() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- 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(`<!DOCTYPE html>
|
html := fmt.Sprintf(`<!DOCTYPE html>
|
||||||
<html lang="de">
|
<html lang="de">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>IPv4 → IPv6‑Mapper</title>
|
<title>IPv4 → IPv6‑Mapper</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: system-ui, sans-serif; margin: 2rem; }
|
body{font-family:system-ui,sans-serif;margin:2rem;max-width:46rem}
|
||||||
form { display: flex; gap: .5rem; }
|
form{display:flex;gap:.5rem;flex-wrap:wrap}
|
||||||
input[type=text] { flex: 1; padding: .4rem; font-size: 1rem; }
|
input[type=text],input[readonly]{padding:.4rem;font-size:1rem;border:1px solid #ccc;border-radius:4px;flex:1}
|
||||||
button { padding: .5rem 1rem; font-size: 1rem; cursor: pointer; }
|
button{padding:.5rem 1rem;font-size:1rem;cursor:pointer;border-radius:4px;border:1px solid #666;background:#eee}
|
||||||
#result { margin-top: 1.5rem; font-weight: bold; }
|
button.copy{padding:.3rem .6rem;font-size:.9rem;margin-left:.3rem;background:#def}
|
||||||
.error { color: #b00; }
|
#result{margin-top:1.5rem}
|
||||||
</style>
|
.row{display:flex;align-items:center;margin-bottom:.4rem}
|
||||||
|
.row label{width:14rem}
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
async function copy(id){
|
||||||
|
const val=document.getElementById(id).value;
|
||||||
|
await navigator.clipboard.writeText(val);
|
||||||
|
const btn=document.getElementById(id+"Btn");
|
||||||
|
const old=btn.textContent;
|
||||||
|
btn.textContent="✔ kopiert";
|
||||||
|
setTimeout(()=>btn.textContent=old,1200);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>IPv4 → IPv6-Mapper</h1>
|
<h1>IPv4 → IPv6‑Mapper</h1>
|
||||||
<form action="/convert" method="post">
|
<form action="/convert" method="post">
|
||||||
<input type="text" name="ipv4" placeholder="%s" value="{{.IPv4}}" required />
|
<input type="text" name="ipv4" placeholder="%s" value="{{.IPv4}}" required />
|
||||||
<button type="submit">Umrechnen</button>
|
<button type="submit">Umrechnen</button>
|
||||||
</form>
|
</form>
|
||||||
{{if .IPv6}}
|
|
||||||
<div id="result">IPv6-Adresse: <code>{{.IPv6}}</code></div>
|
{{if .HaveResult}}
|
||||||
|
<div id="result">
|
||||||
|
<h2>Windows 11‑Eingaben</h2>
|
||||||
|
<div class="row">
|
||||||
|
<label for="ip">IP‑Adresse</label>
|
||||||
|
<input readonly id="ip" value="{{.IPv6}}" />
|
||||||
|
<button type="button" class="copy" id="ipBtn" onclick="copy('ip')">Kopieren</button>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<label for="pl">Subnetzpräfixlänge</label>
|
||||||
|
<input readonly id="pl" value="96" />
|
||||||
|
<button type="button" class="copy" id="plBtn" onclick="copy('pl')">Kopieren</button>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<label for="gw">Gateway (Bleibt vorerst leer!)</label>
|
||||||
|
<input readonly id="gw" value="{{.Gateway}}" />
|
||||||
|
<button type="button" class="copy" id="gwBtn" onclick="copy('gw')">Kopieren</button>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<label for="dns1">Bevorzugter DNS</label>
|
||||||
|
<input readonly id="dns1" value="{{.DNS1}}" />
|
||||||
|
<button type="button" class="copy" id="dns1Btn" onclick="copy('dns1')">Kopieren</button>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<label for="dns2">Alternativer DNS</label>
|
||||||
|
<input readonly id="dns2" value="{{.DNS2}}" />
|
||||||
|
<button type="button" class="copy" id="dns2Btn" onclick="copy('dns2')">Kopieren</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{if .Error}}
|
{{if .Error}}
|
||||||
<div class="error">Fehler: {{.Error}}</div>
|
<p style="color:#b00">Fehler: {{.Error}}</p>
|
||||||
{{end}}
|
{{end}}
|
||||||
<p>Präfix: <code>%s</code> (/96‑Zuordnung)</p>
|
|
||||||
|
<p style="margin-top:1.5rem">Aktives Präfix: <code>%s</code> (/96)</p>
|
||||||
</body>
|
</body>
|
||||||
</html>`, pageIP, ulaPrefix)
|
</html>`, pageIP, ulaPrefix)
|
||||||
|
|
||||||
pageTemplate = template.Must(template.New("page").Parse(html))
|
pageTemplate = template.Must(template.New("page").Parse(html))
|
||||||
}
|
}
|
||||||
|
|
||||||
// embedIPv4 converts a dotted IPv4 string into the chosen ULA /96 IPv6 address.
|
// embedIPv4 converts a dotted IPv4 string into an IPv6 address within ulaPrefix/96.
|
||||||
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)
|
||||||
}
|
}
|
||||||
hi := uint16(ip[0])<<8 | uint16(ip[1]) // high 16 bits (octets 0–1)
|
hi := uint16(ip[0])<<8 | uint16(ip[1])
|
||||||
lo := uint16(ip[2])<<8 | uint16(ip[3]) // low 16 bits (octets 2–3)
|
lo := uint16(ip[2])<<8 | uint16(ip[3])
|
||||||
return fmt.Sprintf("%s%x:%x", ulaPrefix, hi, lo), nil
|
return fmt.Sprintf("%s%x:%x", ulaPrefix, hi, lo), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user