<commit_before>package bsw

import (
	"crypto/tls"
	"net/http"
	"net/url"
	"regexp"
)

// Using http(s), attempt to connect to an IP. If connection is successfull return any hostnames from the possible
// 'Location' headers.
func Headers(ip string) (Results, error) {
	results := []Result{}
	for _, proto := range []string{"http", "https"} {
		if host := hostnameFromHttpLocationHeader(ip, proto); host != "" {
			results = append(results, Result{Source: "Headers", IP: ip, Hostname: host})
		}
	}
	return results, nil
}

// Performs http(s) request and parses possible 'Location' headers.
func hostnameFromHttpLocationHeader(ip, protocol string) string {
	req, err := http.NewRequest("GET", protocol+"://"+ip, nil)
	if err != nil {
		return ""
	}
	tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
	res, err := tr.RoundTrip(req)
	if err != nil {
		return ""
	}
	location := res.Header["Location"]
	if location != nil {
		u, err := url.Parse(location[0])
		if err != nil {
			return ""
		}
		host := u.Host
		if m, _ := regexp.Match("[a-zA-Z]+", []byte(host)); m == true {<commit_msg>Adds timeout for header functions<commit_after>package bsw

import (
	"crypto/tls"
	"net"
	"net/http"
	"net/url"
	"regexp"
	"time"
)

// Using http(s), attempt to connect to an IP. If connection is successfull return any hostnames from the possible
// 'Location' headers.
func Headers(ip string) (Results, error) {
	results := []Result{}
	for _, proto := range []string{"http", "https"} {
		if host := hostnameFromHttpLocationHeader(ip, proto); host != "" {
			results = append(results, Result{Source: "Headers", IP: ip, Hostname: host})
		}
	}
	return results, nil
}

// Performs http(s) request and parses possible 'Location' headers.
func hostnameFromHttpLocationHeader(ip, protocol string) string {
	req, err := http.NewRequest("GET", protocol+"://"+ip, nil)
	if err != nil {
		return ""
	}
	tr := &http.Transport{
		Dial: func(network, addr string) (net.Conn, error) {
			return net.DialTimeout(network, addr, time.Duration(1*time.Second))
		},
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	res, err := tr.RoundTrip(req)
	if err != nil {
		return ""
	}
	location := res.Header["Location"]
	if location != nil {
		u, err := url.Parse(location[0])
		if err != nil {
			return ""
		}
		host := u.Host
		if m, _ := regexp.Match("[a-zA-Z]+", []byte(host)); m == true {
{'fraction_non_alphanumeric': 0.1669449081803005, 'fraction_numerical': 0.0012520868113522537, 'mean_word_length': 4.067653276955602, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3544876', 'n_tokens_mistral': 854, 'n_tokens_neox': 822, 'n_words': 233}