go5.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "net"
  6. "net/http"
  7. "strings"
  8. )
  9. var tpl = template.Must(template.ParseFiles("biba.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_Address")
  29. NET_Mask := r.FormValue("NET_Mask")
  30. Def_gateway := r.FormValue("Def_gateway")
  31. Main_DNS := r.FormValue("Main_DNS")
  32. Alt_DNS := r.FormValue("Alt_DNS")
  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 !validMask(NET_Mask) {
  40. http.Redirect(w, r, "/error", http.StatusSeeOther)
  41. }
  42. info := IPv4{IpAddress: IP_Address, NetMask: NET_Mask, DefGateway: Def_gateway, MainDNS: Main_DNS, AltDNS: Alt_DNS}
  43. fmt.Fprintf(w, info.GetAllInfo())
  44. }
  45. func (ip IPv4) GetAllInfo() string {
  46. 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)
  47. }
  48. func indexHandler(w http.ResponseWriter, r *http.Request) {
  49. tpl.Execute(w, nil)
  50. }
  51. func valid(ip string) bool {
  52. Ipv4Addr := net.ParseIP(ip)
  53. if Ipv4Addr == nil {
  54. return false
  55. }
  56. return true
  57. }
  58. func validMask(mask string) bool {
  59. count := 0
  60. s := strings.Split(mask, ".")
  61. fmt.Println("mask: ", s)
  62. for i := 0; i < len(s)-1; i++ {
  63. if s[i] == "255" {
  64. count++
  65. }
  66. }
  67. 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") {
  68. return true
  69. }
  70. return false
  71. }