func Ping(pi pingInfo) pingResponse { pr := pingResponse{} pr.err = nil conn, err := icmp.ListenPacket("udp4", configuration.GetDeviceIP()) if err != nil { msg := "Could not create a packet endpoint on ip: " + configuration.GetDeviceIP() fmt.Println(err) logger.WriteString(msg) pr.err = errors.New(msg) return pr } defer conn.Close() logger.WriteString("Starting test number " + strconv.Itoa(sequence)) testNetwork(&pr, pi, conn) testInternet(&pr, pi, conn) // testDNS(&pr, pi, conn) logger.WriteString("Finished test number " + strconv.Itoa(sequence)) sequence++ return pr }
// This checks if we can convert an URL to an IP func testDNS(pr *pingResponse, pi pingInfo, conn *icmp.PacketConn) { start := time.Now() msg := createMessage() msg_bytes, err := msg.Marshal(nil) if err != nil { emsg := "Could not marshal the message to []byte." logger.WriteString(emsg) pr.err = errors.New(emsg) return } ip, _ := net.LookupHost(pi.externalurl) if _, err := conn.WriteTo(msg_bytes, &net.UDPAddr{IP: net.ParseIP(ip[0]), Zone: "en0"}); err != nil { emsg := "Could not write to the internal ip address: " + ip[0] logger.WriteString(emsg) pr.external_url = false pr.err = errors.New(emsg) return } pr.external_url = true response := make([]byte, 1500) count, peer, err := conn.ReadFrom(response) if err != nil { emsg := "Could not read the response." logger.WriteString(emsg) pr.external_url = false pr.err = errors.New(emsg) return } _, err = icmp.ParseMessage(protocolICMP, response[:count]) if err != nil { emsg := "Could not parse the message received." logger.WriteString(emsg) pr.external_url = false pr.err = errors.New(emsg) return } logger.WriteString("Response " + strconv.Itoa(sequence) + " received from " + peer.String() + " after " + time.Now().Sub(start).String()) }
// CheckLogin - Validates a login attempt or redirects the user to // an error page which redirects them to "Root" func CheckLogin(w http.ResponseWriter, r *http.Request) { uname := r.FormValue("username") pword := r.FormValue("password") // Authenticate user credentials // Hash password hash := sha256.Sum256([]byte(pword)) authenticated := auth.CheckCredentials(uname, hash) if authenticated == true { logger.WriteString("User \"" + uname + "\" authenticated successfully.") auth.SetSessionID(w) http.Redirect(w, r, "/dashboard", http.StatusFound) } else { logger.WriteString("User \"" + uname + "\" did not authenticate successfully.") servePageStatic(w, r, loc+"/html/error.html") } }
func RunTest(pi pingInfo, runlen int) { tl := map[string]int{ "internal": 0, "extip": 0, "exturl": 0, "total": 0, } lp := time.Now() end := time.Now().Add(time.Duration(runlen) * time.Hour) logger.WriteString("Starting the test...") for time.Now().Before(end) { delay, _ := strconv.Atoi(pi.pingDelay) if time.Now().After(lp.Add(time.Duration(delay) * time.Second)) { pr := Ping(pi) lp = time.Now() if pr.internal { tl["internal"]++ } if pr.external_ip { tl["extip"]++ } // if pr.external_url { // tl["exturl"]++ // } tl["total"]++ timeline := "Internal Ping Success Count: " + strconv.Itoa(tl["internal"]) + "/" + strconv.Itoa(tl["total"]) + "\nExternal IP Ping Success Count: " + strconv.Itoa(tl["extip"]) + "/" + strconv.Itoa(tl["total"]) //+ // "\nExternal URL Ping Success Count: " + strconv.Itoa(tl["exturl"]) + "/" + strconv.Itoa(tl["total"]) reporter.SetTimeline(timeline) } // speedtest -- later } logger.WriteString("The test has been completed.") }
func init() { cl := loc + "/.cookies" logger.WriteString("Removing the existing .cookies file") os.Remove(cl) // this is here because apparently auth's init() is called before this one is if _, err := os.Stat(cl); os.IsNotExist(err) { logger.WriteString("Creating a new .cookies file.") file, e := os.Create(cl) if e != nil { logger.WriteString("There was an error creating the .cookies file.") } file.Close() } if _, err := os.Stat(loc + "/.password"); os.IsNotExist(err) { logger.WriteString("The .password file does not exist. Creating a new .password file.") file, e := os.Create(loc + "/.password") if e != nil { logger.WriteString("There was an error creating the .password file.") } file.Close() } logger.WriteString("Setting the port number to " + configuration.GetPortNumber()) portNumber = ":" + configuration.GetPortNumber() }
// SaveTest - The test has had it's "test specific settings " func SaveTest(w http.ResponseWriter, r *http.Request) { valid := auth.CheckSessionID(r) if valid { if r.FormValue("runlength") == "" || r.FormValue("location") == "" { errMsg = "You need to enter a run length and a location!" http.Redirect(w, r, "/dashboard/start_test", http.StatusFound) } else { td := tools.TestData{} td.Runlen = r.FormValue("runlength") td.Ext_ip = r.FormValue("externalIP") td.Ext_url = r.FormValue("externalURL") td.Location = r.FormValue("location") td.Ping_delay = r.FormValue("pingdelay") td.Speedtest_delay = r.FormValue("speedtestedelay") td.Speedtestfile = r.FormValue("stestfileloc") pi, err := tools.SetupTest(td) if err != nil { errMsg = "No ip associated with the key could be found." http.Redirect(w, r, "/dashboard/start_test", http.StatusFound) } reporter.SetStartTime(time.Now().Format("Jan 02, 2006 - 15:04")) reporter.SetLocation(td.Location) runlen, _ := strconv.Atoi(td.Runlen) logger.WriteString("Running test for: " + td.Runlen) // go tools.RunTest(pi, runlen) tools.RunTest(pi, runlen) http.Redirect(w, r, "/dashboard/reports", http.StatusFound) } } else { http.Redirect(w, r, "/", http.StatusFound) } }