package main
import (
"fmt"
"html/template"
"net"
"net/http"
"strings"
)
var tpl = template.Must(template.ParseFiles("pus.html"))
type IPv4 struct {
IpAddress string
NetMask string
DefGateway string
MainDNS string
AltDNS string
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/info", info)
mux.HandleFunc("/error", err)
mux.HandleFunc("/", indexHandler)
http.ListenAndServe(":8080", mux)
}
func err(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Ты по-моему перепутал")
}
func info(w http.ResponseWriter, r *http.Request) {
IP_Address := r.FormValue("IP_Address")
NET_Mask := r.FormValue("NET_Mask")
Def_gateway := r.FormValue("Def_gateway")
Main_DNS := r.FormValue("Main_DNS")
Alt_DNS := r.FormValue("Alt_DNS")
if IP_Address == "" || NET_Mask == "" {
http.Redirect(w, r, "/error", http.StatusSeeOther)
}
if !valid(IP_Address) || !valid(NET_Mask) || !valid(Def_gateway) || !valid(Main_DNS) || !valid(Alt_DNS) {
http.Redirect(w, r, "/error", http.StatusSeeOther)
}
if !validMask(NET_Mask) {
http.Redirect(w, r, "/error", http.StatusSeeOther)
}
info := IPv4{IpAddress: IP_Address, NetMask: NET_Mask, DefGateway: Def_gateway, MainDNS: Main_DNS, AltDNS: Alt_DNS}
fmt.Fprintf(w, info.GetAllInfo())
}
func (ip IPv4) GetAllInfo() string {
return fmt.Sprintf("IP Address: %s \nNet Mask: %s \nDefault Gateway: %s \nDNS-Server: %s \nAlternative DNS-Server: %s", ip.IpAddress, ip.NetMask, ip.DefGateway, ip.MainDNS, ip.AltDNS)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
tpl.Execute(w, nil)
}
func valid(ip string) bool {
Ipv4Addr := net.ParseIP(ip)
if Ipv4Addr == nil {
return false
}
return true
}
func validMask(mask string) bool {
count := 0
s := strings.Split(mask, ".")
fmt.Println("mask: ", s)
for i := 0; i < len(s)-1; i++ {
if s[i] == "255" {
count++
}
}
if count == 3 && (s[len(s)-1] == "0" || s[len(s)-1] == "128" || s[len(s)-1] == "192" || s[len(s)-1] == "224" || s[len(s)-1] == "240" || s[len(s)-1] == "248" || s[len(s)-1] == "254" || s[len(s)-1] == "255") {
return true
}
return false
}