123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package main
- import (
- "fmt"
- "html/template"
- "net/http"
- "strconv"
- "strings"
- )
- var tpl = template.Must(template.ParseFiles("laba4.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")
- NET_Mask := r.FormValue("mask")
- Def_gateway := r.FormValue("Dget")
- Main_DNS := r.FormValue("DNS")
- Alt_DNS := r.FormValue("adns")
- 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 !validOctet(IP_Address) || !validOctet(NET_Mask) || !validOctet(Def_gateway) || !validOctet(Main_DNS) || !validOctet(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)
- }
- var s = "0123456789."
- func valid(ip string) bool {
- lenIp := len(ip)
- count := 0
- for i := 0; i < lenIp; i++ {
- for x := 0; x < len(s); x++ {
- if ip[i] == s[x] {
- count++
- }
- }
- }
- fmt.Println(count, lenIp)
- if count == lenIp {
- return true
- }
- return false
- }
- func validOctet(ip string) bool {
- s := strings.Split(ip, ".")
- for i := 0; i < len(s); i++ {
- a, _ := strconv.Atoi(s[i])
- if a > 255 || s[0] == "0" || s[0] == "00" || s[0] == "01" || s[0] == "02" || s[0] == "03" || s[0] == "04" || s[0] == "05" || s[0] == "06" || s[0] == "07" || s[0] == "08" || s[0] == "09" {
- return false
- }
- }
- return true
- }
- func validMask(mask string) bool {
- count := 0
- s := strings.Split(mask, ".")
- 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
- }
|