Esempio n. 1
0
// Perform the test of an individual server
func testFallbackServer(fb *client.ChainedServerInfo, workerID int) (output fullOutput) {
	// Test connectivity
	fb.Pipelined = true
	dialer, err := fb.Dialer(DeviceID)
	if err != nil {
		output.err = fmt.Errorf("%v: error building dialer: %v", fb.Addr, err)
		return
	}
	c := &http.Client{
		Transport: &http.Transport{
			Dial: dialer.Dial,
		},
	}
	req, err := http.NewRequest("GET", "http://www.google.com/humans.txt", nil)
	if err != nil {
		output.err = fmt.Errorf("%v: NewRequest to humans.txt failed: %v", fb.Addr, err)
		return
	}
	if *verbose {
		reqStr, _ := httputil.DumpRequestOut(req, true)
		output.info = []string{"\n" + string(reqStr)}
	}

	req.Header.Set("X-LANTERN-AUTH-TOKEN", fb.AuthToken)
	resp, err := c.Do(req)
	if err != nil {
		output.err = fmt.Errorf("%v: requesting humans.txt failed: %v", fb.Addr, err)
		return
	}
	if *verbose {
		respStr, _ := httputil.DumpResponse(resp, true)
		output.info = append(output.info, "\n"+string(respStr))
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {
			log.Debugf("Unable to close response body: %v", err)
		}
	}()
	if resp.StatusCode != 200 {
		output.err = fmt.Errorf("%v: bad status code: %v", fb.Addr, resp.StatusCode)
		return
	}
	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		output.err = fmt.Errorf("%v: error reading response body: %v", fb.Addr, err)
		return
	}
	body := string(bytes)
	if body != expectedBody {
		output.err = fmt.Errorf("%v: wrong body: %s", fb.Addr, body)
		return
	}

	log.Debugf("Worker %d: Fallback %v OK.\n", workerID, fb.Addr)

	if *verify {
		verifyFallback(fb, c)
	}
	return
}
Esempio n. 2
0
// Perform the test of an individual server
func testFallbackServer(fb *client.ChainedServerInfo, workerId int) (err error) {
	// Test connectivity
	fb.Pipelined = true
	dialer, err := fb.Dialer()
	if err != nil {
		return fmt.Errorf("%v: error building dialer: %v", fb.Addr, err)
	}
	c := &http.Client{
		Transport: &http.Transport{
			Dial: dialer.Dial,
		},
	}
	req, err := http.NewRequest("GET", "http://www.google.com/humans.txt", nil)
	resp, err := c.Do(req)
	if err != nil {
		return fmt.Errorf("%v: requesting humans.txt failed: %v", fb.Addr, err)
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {
			log.Debugf("Unable to close response body: %v", err)
		}
	}()
	if resp.StatusCode != 200 {
		return fmt.Errorf("%v: bad status code: %v", fb.Addr, resp.StatusCode)
	}
	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return fmt.Errorf("%v: error reading response body: %v", fb.Addr, err)
	}
	body := string(bytes)
	if body != expectedBody {
		return fmt.Errorf("%v: wrong body: %s", fb.Addr, body)
	}

	log.Debugf("Worker %d: Fallback %v OK.\n", workerId, fb.Addr)
	return nil
}