...

Source file src/github.com/letsencrypt/boulder/observer/probers/http/http.go

Documentation: github.com/letsencrypt/boulder/observer/probers/http

     1  package probers
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"net/http"
     7  	"time"
     8  )
     9  
    10  // HTTPProbe is the exported 'Prober' object for monitors configured to
    11  // perform HTTP requests.
    12  type HTTPProbe struct {
    13  	url       string
    14  	rcodes    []int
    15  	useragent string
    16  	insecure  bool
    17  }
    18  
    19  // Name returns a string that uniquely identifies the monitor.
    20  
    21  func (p HTTPProbe) Name() string {
    22  	insecure := ""
    23  	if p.insecure {
    24  		insecure = "-insecure"
    25  	}
    26  	return fmt.Sprintf("%s-%d-%s%s", p.url, p.rcodes, p.useragent, insecure)
    27  }
    28  
    29  // Kind returns a name that uniquely identifies the `Kind` of `Prober`.
    30  func (p HTTPProbe) Kind() string {
    31  	return "HTTP"
    32  }
    33  
    34  // isExpected ensures that the received HTTP response code matches one
    35  // that's expected.
    36  func (p HTTPProbe) isExpected(received int) bool {
    37  	for _, c := range p.rcodes {
    38  		if received == c {
    39  			return true
    40  		}
    41  	}
    42  	return false
    43  }
    44  
    45  // Probe performs the configured HTTP request.
    46  func (p HTTPProbe) Probe(timeout time.Duration) (bool, time.Duration) {
    47  	client := http.Client{
    48  		Timeout: timeout,
    49  		Transport: &http.Transport{
    50  			TLSClientConfig: &tls.Config{InsecureSkipVerify: p.insecure},
    51  		}}
    52  	req, err := http.NewRequest("GET", p.url, nil)
    53  	if err != nil {
    54  		return false, 0
    55  	}
    56  	req.Header.Set("User-Agent", p.useragent)
    57  	start := time.Now()
    58  	// TODO(@beautifulentropy): add support for more than HTTP GET
    59  	resp, err := client.Do(req)
    60  	if err != nil {
    61  		return false, time.Since(start)
    62  	}
    63  	return p.isExpected(resp.StatusCode), time.Since(start)
    64  }
    65  

View as plain text