Example #1
0
// GetServers will get the full server list
func GetServers() []Server {
	var servers []Server

	client := &http.Client{
		Timeout: HTTPConfigTimeout,
	}
	req, _ := http.NewRequest("GET", SpeedtestServersURL, nil)
	req.Header.Set("Cache-Control", "no-cache")
	resp, err := client.Do(req)

	if err != nil {
		log.Fatalf("Cannot get servers list from speedtest.net: 'Cannot contact server'\n")
	}
	defer resp.Body.Close()

	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		log.Fatalf("Cannot get servers list from speedtest.net: 'Cannot read body'\n")
	}

	s := new(stxml.ServerSettings)

	err3 := xml.Unmarshal(body, &s)
	if err3 != nil {
		log.Fatalf("Cannot get servers list from speedtest.net: 'Cannot unmarshal XML'\n")
	}

	for xmlServer := range s.ServersContainer.XMLServers {
		server := new(Server)
		server.URL = s.ServersContainer.XMLServers[xmlServer].URL
		server.Lat = misc.ToFloat(s.ServersContainer.XMLServers[xmlServer].Lat)
		server.Lon = misc.ToFloat(s.ServersContainer.XMLServers[xmlServer].Lon)
		server.Name = s.ServersContainer.XMLServers[xmlServer].Name
		server.Country = s.ServersContainer.XMLServers[xmlServer].Country
		server.CC = s.ServersContainer.XMLServers[xmlServer].CC
		server.Sponsor = s.ServersContainer.XMLServers[xmlServer].Sponsor
		server.ID = s.ServersContainer.XMLServers[xmlServer].ID
		servers = append(servers, *server)
	}
	return servers
}
Example #2
0
// GetConfig downloads the master config from speedtest.net
func GetConfig() Config {
	client := &http.Client{
		Timeout: HTTPConfigTimeout,
	}
	req, _ := http.NewRequest("GET", SpeedtestConfigURL, nil)
	req.Header.Set("Cache-Control", "no-cache")
	resp, err := client.Do(req)

	if err != nil {
		log.Fatalf("Couldn't retrieve our config from speedtest.net: 'Could not create connection'\n")
	}
	defer resp.Body.Close()
	if checkHTTP(resp) != true {
		log.Fatalf("Couldn't retrieve our config from speedtest.net: '%s'\n", resp.Status)
	}

	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		log.Fatalf("Couldn't retrieve our config from speedtest.net: 'Cannot read body'\n")
	}

	cx := new(stxml.XMLConfigSettings)

	err3 := xml.Unmarshal(body, &cx)
	if err3 != nil {
		log.Fatalf("Couldn't retrieve our config from speedtest.net: 'Cannot unmarshal XML'\n")
	}

	c := new(Config)
	c.IP = cx.Client.IP
	c.Lat = misc.ToFloat(cx.Client.Lat)
	c.Lon = misc.ToFloat(cx.Client.Lon)
	c.Isp = cx.Client.Isp

	return *c
}