-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
152 lines (120 loc) · 3.23 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/benderpan/go-wappalyzer/wappalyzer"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strings"
)
var (
runType string
update bool
outputFile string
workers int
apps string
host string
hosts string
crawlCount int
listen string
verbose bool
)
func init() {
flag.StringVar(&runType,"type","standalone","run type, standalone - single tool model, web - web server model")
flag.StringVar(&outputFile, "output", "data.json", "output file")
flag.BoolVar(&update, "update", false, "update apps file")
flag.IntVar(&workers, "worker", 4, "number of concurrent worker")
flag.StringVar(&apps, "apps", "apps.json", "app definition file.")
flag.StringVar(&host, "host", "", "single host to test")
flag.StringVar(&hosts, "hosts", "", "filename with hosts, one host per line.")
flag.IntVar(&crawlCount, "crawl", 0, "links to follow from the root page (default 0)")
flag.StringVar(&listen,"listen","0.0.0.0:8080","Web server listen address")
flag.BoolVar(&verbose,"verbose",true,"show verbose message")
if cpu := runtime.NumCPU(); cpu == 1 {
runtime.GOMAXPROCS(2)
} else {
runtime.GOMAXPROCS(cpu)
}
}
func main() {
flag.Parse()
if runType =="web" {
//run as web server, which will provide restfull api.
was, err := wappalyzer.NewWebAnalyzeServer(apps)
if err != nil {
fmt.Printf("Error:%v", err)
return
}
http.Handle("/", http.FileServer(FS(false)))
//http.Handle("/",http.StripPrefix("/", http.FileServer(http.Dir("./www/"))))
http.HandleFunc("/analyze",was.HttpHandlerAnalyze)
err=http.ListenAndServe(listen,nil)
if err!=nil {
log.Fatal("open web server failed, err: ", err)
}
}else{
var file io.ReadCloser
var err error
if !update && host == "" && hosts == "" {
flag.Usage()
return
}
if update {
err = wappalyzer.DownloadFile(wappalyzer.WappalyzerURL, "apps.json")
if err != nil {
log.Fatalf("error: can not update apps file: %v", err)
}
log.Println("app definition file updated from ", wappalyzer.WappalyzerURL)
if host == "" && hosts == "" {
return
}
}
// check single host or hosts file
if host != "" {
file = ioutil.NopCloser(strings.NewReader(host))
} else {
file, err = os.Open(hosts)
if err != nil {
log.Fatalf("error: can not open host file %s: %s", hosts, err)
}
}
defer file.Close()
results, err := wappalyzer.Init(workers, file, apps, crawlCount)
if err != nil {
log.Fatal("error initializing: ", err)
}
log.Printf("Scanning with %v workers.", workers)
outFile, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("output file create failed. err: ", err)
}
defer outFile.Close()
for result := range results {
if result.Error != "" {
if verbose{
log.Printf("Error: Host=%s Msg=%s", result.Host, result.Error)
}
}
jsonValue, err := json.Marshal(result)
if err != nil {
if verbose{
log.Print("{}\n")
}
} else {
jsonValue = append(jsonValue, '\n')
_,err= outFile.Write(jsonValue)
if err!=nil{
log.Fatal("write message to output file failed, err: ",err)
}
if verbose{
log.Print(string(jsonValue))
}
}
}
}
}