gr794_asr il y a 2 ans
commit
6909b7f21d
4 fichiers modifiés avec 205 ajouts et 0 suppressions
  1. 6 0
      dockerfile
  2. 3 0
      go.mod
  3. 119 0
      laba4.go
  4. 77 0
      laba4.html

+ 6 - 0
dockerfile

@@ -0,0 +1,6 @@
+FROM golang:onbuild
+WORKDIR ./laba4
+COPY ./ ./
+RUN go build -o ip .
+EXPOSE 8000
+ENTRYPOINT ["./ip"]

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module 1
+
+go 1.19

+ 119 - 0
laba4.go

@@ -0,0 +1,119 @@
+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
+
+}

+ 77 - 0
laba4.html

@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+    
+</head>
+<body>
+<div class="container-fluid">
+    <h3>Свойства: IP версии 4 (TCP/IPv4)</h3>
+
+    <table class="dialog_form">
+        <form action="/info" method="post">
+        <tr>
+            <th>IP address</th>
+        </tr>
+        <tr>
+            <td><input type="text" name="ip"></td>
+        </tr>
+       
+        <tr>
+            <th>
+                NET Mask
+            </th>
+        </tr>
+        <tr>
+            <td>
+                <input type="text" name="mask"/>
+            </td>
+        </tr>
+        
+        <tr>
+            <th>
+                Default gateway
+            </th>
+        </tr>
+        <tr>
+            <td><input type="text" name="Dget"/></td>
+        </tr>
+        
+        <tr>
+            <th>
+                DNS-server
+            </th>
+        </tr>
+        <tr>
+            <td><input type="text" name="DNS"/></td>
+        </tr>
+      
+        <tr>
+            <th>
+                Alternative DNS-server
+            </th>
+        </tr>
+        <tr>
+            <td><input type="text" name="adns"/></td>
+        </tr>
+        
+        <tr>
+            <td id="buttonlist">
+                <input type="checkbox" id="alertbutton"/><label for="alertbutton"> Подтвердить параметры</label>
+            </td>
+        </tr>
+       
+        <tr>
+            <td>
+        <button style="float: right" class="btn btn-light">OK</button>
+    </form>
+
+            </td>
+        </tr>
+
+    </table>
+
+</div>
+</body>
+</html>