Skip to content

Commit 2ce6e3b

Browse files
committed
Updates
1 parent 65d10d7 commit 2ce6e3b

File tree

1 file changed

+78
-20
lines changed

1 file changed

+78
-20
lines changed

oldhost.go

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import (
55
"flag"
66
"fmt"
77
"net/http"
8+
"net/url"
89
"os"
10+
"strings"
911
"sync"
1012
"time"
1113
)
1214

1315
func main() {
1416
ipsArg := flag.String("ips", "", "IPs (string or filename)")
1517
hostsArg := flag.String("hosts", "", "Hosts (string or filename)")
18+
curlArg := flag.Bool("curl", false, "Output as Curl command")
1619
silent := flag.Bool("silent", false, "Silent mode")
1720
flag.Parse()
1821

@@ -28,10 +31,6 @@ func main() {
2831
return
2932
}
3033

31-
client := http.Client{
32-
Timeout: 1 * time.Second,
33-
}
34-
3534
var wg sync.WaitGroup
3635
semaphore := make(chan struct{}, 5) // Limit concurrent calls to 5
3736

@@ -46,31 +45,34 @@ func main() {
4645
<-semaphore // Release the slot back to the semaphore
4746
}()
4847

49-
url := fmt.Sprintf("http://%s", ip)
50-
req, err := http.NewRequest("GET", url, nil)
51-
if err != nil {
52-
if !*silent {
53-
fmt.Printf("Error creating request for IP %s and host %s: %s\n", ip, host, err)
54-
}
48+
// Do HTTP request
49+
respStatusCode, contentLength, error := doHttpRequest(ip, host, *silent)
50+
51+
if *silent && (respStatusCode != http.StatusOK || error != nil) {
5552
return
5653
}
57-
req.Host = host
5854

59-
resp, err := client.Do(req)
60-
if err != nil {
61-
if !*silent {
62-
fmt.Printf("Error making request for IP %s and host %s: %s\n", ip, host, err)
55+
if respStatusCode == http.StatusOK {
56+
if *curlArg {
57+
fmt.Printf("curl -ik http://%s -H \"Host: %s\"\t(Content-Length: %d)\n", ip, host, contentLength)
58+
} else {
59+
fmt.Printf("%d\t%s\t%s\n", contentLength, ip, host)
6360
}
64-
return
6561
}
66-
defer resp.Body.Close()
6762

68-
if *silent && resp.StatusCode != http.StatusOK {
63+
// Do HTTPS request
64+
respStatusCode, contentLength, error = doHttpsRequest(ip, host, *silent)
65+
66+
if *silent && (respStatusCode != http.StatusOK || error != nil) {
6967
return
7068
}
7169

72-
if resp.StatusCode == http.StatusOK {
73-
fmt.Printf("%s\t%s\n", ip, host)
70+
if respStatusCode == http.StatusOK {
71+
if *curlArg {
72+
fmt.Printf("curl -ik http://%s -H \"Host: %s\"\t(Content-Length: %d)\n", ip, host, contentLength)
73+
} else {
74+
fmt.Printf("%d\t%s\t%s\thttps\n", contentLength, ip, host)
75+
}
7476
}
7577
}(ip, host)
7678
}
@@ -79,6 +81,62 @@ func main() {
7981
wg.Wait()
8082
}
8183

84+
func doHttpRequest(ip string, host string, silent bool) (int, int64, error) {
85+
return doRequest(ip, host, silent, false)
86+
}
87+
88+
func doHttpsRequest(ip string, host string, silent bool) (int, int64, error) {
89+
return doRequest(ip, host, silent, true)
90+
}
91+
92+
func doRequest(ip string, host string, silent bool, https bool) (int, int64, error) {
93+
var scheme string
94+
if https {
95+
scheme = "https"
96+
} else {
97+
scheme = "http"
98+
}
99+
100+
client := http.Client{
101+
Timeout: 1 * time.Second,
102+
}
103+
104+
url := fmt.Sprintf("%s://%s", scheme, ip)
105+
req, err := http.NewRequest("GET", url, nil)
106+
if err != nil {
107+
if !silent {
108+
fmt.Printf("Error creating request for IP %s and host %s: %s\n", ip, host, err)
109+
}
110+
return 0, 0, err
111+
}
112+
req.Host = cleanupHost(host)
113+
114+
resp, err := client.Do(req)
115+
if err != nil {
116+
if !silent {
117+
fmt.Printf("Error making request for IP %s and host %s: %s\n", ip, host, err)
118+
}
119+
return 0, 0, err
120+
}
121+
defer resp.Body.Close()
122+
123+
contentLength := resp.ContentLength
124+
125+
return resp.StatusCode, contentLength, nil
126+
}
127+
128+
func cleanupHost(host string) string {
129+
host = strings.TrimSpace(host)
130+
host = strings.TrimSuffix(host, "/")
131+
132+
u, err := url.Parse(host)
133+
if err == nil {
134+
host = u.Host
135+
}
136+
137+
return host
138+
}
139+
82140
func readLines(arg string) ([]string, error) {
83141
lines := []string{}
84142

0 commit comments

Comments
 (0)