laba4.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. )
  9. var tpl = template.Must(template.ParseFiles("laba4.html"))
  10. type IPv4 struct {
  11. IpAddress string
  12. NetMask string
  13. DefGateway string
  14. MainDNS string
  15. AltDNS string
  16. }
  17. func main() {
  18. mux := http.NewServeMux()
  19. mux.HandleFunc("/info", info)
  20. mux.HandleFunc("/error", err)
  21. mux.HandleFunc("/", indexHandler)
  22. http.ListenAndServe(":8080", mux)
  23. }
  24. func err(w http.ResponseWriter, r *http.Request) {
  25. fmt.Fprintf(w, "Введите повторно")
  26. }
  27. func info(w http.ResponseWriter, r *http.Request) {
  28. IP_Address := r.FormValue("ip")
  29. NET_Mask := r.FormValue("mask")
  30. Def_gateway := r.FormValue("Dget")
  31. Main_DNS := r.FormValue("DNS")
  32. Alt_DNS := r.FormValue("adns")
  33. if IP_Address == "" || NET_Mask == "" {
  34. http.Redirect(w, r, "/error", http.StatusSeeOther)
  35. }
  36. if !valid(IP_Address) || !valid(NET_Mask) || !valid(Def_gateway) || !valid(Main_DNS) || !valid(Alt_DNS) {
  37. http.Redirect(w, r, "/error", http.StatusSeeOther)
  38. }
  39. if !validOctet(IP_Address) || !validOctet(NET_Mask) || !validOctet(Def_gateway) || !validOctet(Main_DNS) || !validOctet(Alt_DNS) {
  40. http.Redirect(w, r, "/error", http.StatusSeeOther)
  41. }
  42. if !validMask(NET_Mask) {
  43. http.Redirect(w, r, "/error", http.StatusSeeOther)
  44. }
  45. info := IPv4{IpAddress: IP_Address, NetMask: NET_Mask, DefGateway: Def_gateway, MainDNS: Main_DNS, AltDNS: Alt_DNS}
  46. fmt.Fprintf(w, info.GetAllInfo())
  47. }
  48. func (ip IPv4) GetAllInfo() string {
  49. 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)
  50. }
  51. func indexHandler(w http.ResponseWriter, r *http.Request) {
  52. tpl.Execute(w, nil)
  53. }
  54. var s = "0123456789."
  55. func valid(ip string) bool {
  56. lenIp := len(ip)
  57. count := 0
  58. for i := 0; i < lenIp; i++ {
  59. for x := 0; x < len(s); x++ {
  60. if ip[i] == s[x] {
  61. count++
  62. }
  63. }
  64. }
  65. fmt.Println(count, lenIp)
  66. if count == lenIp {
  67. return true
  68. }
  69. return false
  70. }
  71. func validOctet(ip string) bool {
  72. s := strings.Split(ip, ".")
  73. for i := 0; i < len(s); i++ {
  74. a, _ := strconv.Atoi(s[i])
  75. 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" {
  76. return false
  77. }
  78. }
  79. return true
  80. }
  81. func validMask(mask string) bool {
  82. count := 0
  83. s := strings.Split(mask, ".")
  84. for i := 0; i < len(s)-1; i++ {
  85. if s[i] == "255" {
  86. count++
  87. }
  88. }
  89. 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") {
  90. return true
  91. }
  92. return false
  93. }