Esempio n. 1
0
// Lookup - lookup host and initialize host IP's
func Lookup(hostport string) (*TCPMap, error) {
	host, port, err := net.SplitHostPort(hostport)
	if err != nil {
		return nil, err
	}
	portInt, err := strconv.Atoi(port)
	if err != nil {
		return nil, err
	}
	resolver, err := dns_resolver.NewFromResolvConf(RESOLVCONFIG)
	if err != nil {
		resolver = dns_resolver.New([]string{"8.8.8.8", "8.8.4.4"})
	} else {
		logrus.Debugf("Found %s DNS config, attempting to use it", RESOLVCONFIG)
	}
	ips, err := resolver.LookupHost(host)
	if err != nil {
		return nil, err
	}
	if len(ips) == 0 {
		return nil, ErrEmptyIPS
	}
	tcp := &TCPMap{}
	for _, ip := range ips {
		tcp.Add(ip, portInt)
	}
	return tcp, nil
}
Esempio n. 2
0
// test tests a site and looks at Via support
func (s *site) test(l *os.File) {
	resolver := dns_resolver.New([]string{resolverName})

	// Check that the origin server resolves

	s.resolves.ran = true
	name := s.origin
	if net.ParseIP(name) == nil {
		_, err := resolver.LookupHost(name)
		if err != nil {
			s.logf(l, "Error resolving name: %s", err)
			s.resolves.yesno = false
			return
		}
	}
	s.resolves.yesno = true

	protocol := "http://"

	// Note: we disable compression in the http.Transport so that the
	// Go library does not add the Accept-Encoding and does not do
	// transparent decompression.
	//
	// The Accept-Encoding header is added to the request which means
	// that we'll potentially get gzipped content in return.

	transport := &http.Transport{}
	transport.DisableCompression = true

	// Custom dialer is needed to use special DNS resolver so that the
	// default resolver can be overriden

	transport.Dial = func(network, address string) (net.Conn, error) {
		host, port, err := net.SplitHostPort(address)
		if err != nil {
			return nil, err
		}

		if net.ParseIP(host) != nil {
			return net.Dial(network, address)
		}

		ips, err := resolver.LookupHost(host)
		if err != nil {
			return nil, err
		}

		if len(ips) == 0 {
			return nil, fmt.Errorf("Failed to get any IPs for %s", address)
		}

		return net.Dial(network, net.JoinHostPort(ips[0].String(), port))
	}

	client := &http.Client{Transport: transport}
	req, err := http.NewRequest("GET", protocol+name, nil)

	req.Header.Set("Accept-Encoding", "gzip,deflate")
	req.Header.Set("Host", s.host)

	s.noVia.ran = true
	if *dump {
		fmt.Printf("%#v\n", req)
	}
	respNoVia, err := client.Do(req)
	if *dump {
		fmt.Printf("%#v\n", respNoVia)
	}
	if err != nil {
		s.logf(l, "HTTP request %#v failed: %s", req, err)
		return
	}
	s.noVia.yesno = true
	sizeNoVia := 0
	if respNoVia != nil && respNoVia.Body != nil {
		b, _ := ioutil.ReadAll(respNoVia.Body)
		sizeNoVia = len(b)
		respNoVia.Body.Close()
	}
	s.noViaSize = sizeNoVia
	s.noViaEncoding = respNoVia.Header.Get("Content-Encoding")
	s.noViaServer = respNoVia.Header.Get("Server")
	transport.CloseIdleConnections()

	// Now add the Via header to the same request and repeate

	req.Header.Set("Via", "viascan 1.0")

	s.via.ran = true
	if *dump {
		fmt.Printf("%#v\n", req)
	}
	respVia, err := client.Do(req)
	if *dump {
		fmt.Printf("%#v\n", respVia)
	}
	if err != nil {
		s.logf(l, "HTTP request %#v failed: %s", req, err)
		return
	}
	s.via.yesno = true
	sizeVia := 0
	if respVia != nil && respVia.Body != nil {
		b, _ := ioutil.ReadAll(respVia.Body)
		sizeVia = len(b)
		respVia.Body.Close()
	}
	s.viaSize = sizeVia
	s.viaEncoding = respVia.Header.Get("Content-Encoding")
	s.viaServer = respVia.Header.Get("Server")
	transport.CloseIdleConnections()
}