-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers-survey.go
140 lines (113 loc) · 2.81 KB
/
servers-survey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
"os"
"sort"
"strings"
"time"
)
type surveyResult struct {
site string
serverSoftware string
}
const workerPoolSize = 30
var surveyNumber = 0
func surveyWorker(id int, siteJobs <-chan string, results chan<- surveyResult) {
serverSW := "<unknown>"
timeout := time.Duration(3 * time.Second)
httpClient := http.Client{Timeout: timeout}
for hostname := range siteJobs {
surveyNumber += 1
fmt.Printf("%3d (worker %2d): %s\n", surveyNumber, id, hostname)
resp, err := httpClient.Get(fmt.Sprintf("http://%s", hostname))
if err != nil {
log.Print(err)
results <- surveyResult{site: hostname, serverSoftware: serverSW}
continue
}
defer resp.Body.Close()
if len(resp.Header.Get("Server")) > 0 {
serverSW = resp.Header.Get("Server")
}
results <- surveyResult{site: hostname, serverSoftware: serverSW}
}
}
func readTabDelimList(tabListFile string) []string {
file, err := os.Open(tabListFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
sites := make([]string, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if !strings.HasPrefix(scanner.Text(), "#") {
siteDomain := strings.Split(scanner.Text(), "\t")[6]
sites = append(sites, siteDomain)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return sites
}
func readCSVlist(csvFile string) []string {
siteListFile, err := os.Open(csvFile)
if err != nil {
log.Fatal(err)
}
defer siteListFile.Close()
sites := make([]string, 0)
r := csv.NewReader(siteListFile)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
siteDomain := record[2]
sites = append(sites, siteDomain)
}
return sites
}
func main() {
// sites := readCSVlist("fortune500-2014.csv")
sites := readTabDelimList("fortune1000_companies.tab")
siteServer := make(map[string]string)
jobs := make(chan string, len(sites))
results := make(chan surveyResult, len(sites))
for _, site := range sites {
jobs <- site
}
// Start up the pool of workers. They will block until they have
// something to do, which arrives to them via the jobs channel.
for w := 1; w <= workerPoolSize; w++ {
go surveyWorker(w, jobs, results)
}
for n := 1; n <= len(sites); n++ {
res := <-results
siteServer[res.site] = res.serverSoftware
}
serverCount := make(map[string]int)
softwareVersions := make([]string, 0)
for _, software := range siteServer {
if count, exists := serverCount[software]; exists {
serverCount[software] = count + 1
} else {
serverCount[software] = 1
softwareVersions = append(softwareVersions, software)
}
}
sort.Strings(softwareVersions)
fmt.Printf("\n\n")
for _, software := range softwareVersions {
fmt.Printf("%3d %-50s\n", serverCount[software], software)
}
}