Esempio n. 1
0
func createPingFactory(
	config *Config,
	hostname string,
	port uint16,
	tls *transport.TLSConfig,
	request *http.Request,
	body []byte,
	validator RespCheck,
) func(*net.IPAddr) monitors.TaskRunner {
	fields := common.MapStr{
		"scheme": request.URL.Scheme,
		"port":   port,
		"url":    request.URL.String(),
	}

	timeout := config.Timeout
	isTLS := request.URL.Scheme == "https"
	checkRedirect := makeCheckRedirect(config.MaxRedirects)

	return monitors.MakePingIPFactory(fields, func(ip *net.IPAddr) (common.MapStr, error) {
		addr := net.JoinHostPort(ip.String(), strconv.Itoa(int(port)))
		d := &dialchain.DialerChain{
			Net: dialchain.ConstAddrDialer("tcp_connect_rtt", addr, timeout),
		}
		if isTLS {
			d.AddLayer(dialchain.TLSLayer("tls_handshake_rtt", tls, timeout))
		}

		measures := common.MapStr{}
		dialer, err := d.BuildWithMeasures(measures)
		if err != nil {
			return nil, err
		}

		var httpStart, httpEnd time.Time

		client := &http.Client{
			CheckRedirect: checkRedirect,
			Timeout:       timeout,
			Transport: &SimpleTransport{
				Dialer:       dialer,
				OnStartWrite: func() { httpStart = time.Now() },
				OnStartRead:  func() { httpEnd = time.Now() },
			},
		}

		event, err := execPing(client, request, body, timeout, validator)
		if event == nil {
			event = measures
		} else {
			event.Update(measures)
		}

		if !httpEnd.IsZero() {
			event["http_rtt"] = look.RTT(httpEnd.Sub(httpStart))
		}
		return event, err
	})
}
Esempio n. 2
0
func buildHostDialerChainFactory(
	scheme string,
	tls *transport.TLSConfig,
	config *Config,
) (func(string) *dialchain.DialerChain, error) {
	template, err := buildDialerChain(scheme, tls, config)
	if err != nil {
		return nil, err
	}

	withProxy := config.Socks5.URL != ""
	return func(addr string) *dialchain.DialerChain {
		if withProxy {
			d := template.Clone()
			d.Layers[0] = dialchain.ConstAddrLayer(addr, d.Layers[0])
			return d
		}

		return &dialchain.DialerChain{
			Net:    dialchain.ConstAddrDialer("tcp_connect_rtt", addr, config.Timeout),
			Layers: template.Layers,
		}
	}, nil
}