-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (141 loc) · 3.64 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
153
/*c
Copyright (c) 2021 Naxii.
https://github.com/Naxii-e/HTCD-JSON
*/
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"
)
type Host struct {
Disp string
Url string
}
type ReHost struct {
Disp string `json:"disp"`
Url string `json:"url"`
Code int `json:"code"`
Time time.Time `json:"time"`
}
func info(msg string) {
fmt.Println("[INFO]", msg)
}
func ReadCsv(filename string) ([][]string, error) {
// Open CSV file
f, err := os.Open(filename)
if err != nil {
return [][]string{}, err
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
}
}(f)
// Read File into a Variable
lines, err := csv.NewReader(f).ReadAll()
if err != nil {
return [][]string{}, err
}
return lines, nil
}
func main() {
WelcomeMsg := `
__ __________________ _______ ____ _ __
/ / / /_ __/ ____/ __ \ / / ___// __ \/ | / /
/ /_/ / / / / / / / / /_______ / /\__ \/ / / / |/ /
/ __ / / / / /___/ /_/ /_____/ /_/ /___/ / /_/ / /| /
/_/ /_/ /_/ \____/_____/ \____//____/\____/_/ |_/
Ver 1.1 - BETA
Author: Naxii
GitHub: https://github.com/Naxii-e
Keybase: https://keybase.io/naxii_e
SourceCode: https://github.com/Naxii-e/get_http_code
License: MIT
Copyright (c) 2021 Naxii.
`
var intOpt = flag.String("debug", "false", "output debug console")
var intOpt2 = flag.String("f", "hosts.csv", "file path of csv")
flag.Parse()
DebugOptionMode := false
if *intOpt == "true" {
DebugOptionMode = true
}
fmt.Println(WelcomeMsg)
if DebugOptionMode == true {
info("!デバッグモードが有効です!")
}
in, err := ReadCsv(*intOpt2)
if err != nil {
println("csv ファイルが見つかりません。")
time.Sleep(5 * time.Second)
log.Fatal(err)
}
info("csv ファイルを読み込みました。")
info("")
info("---=取得を開始します=---")
// 非同期処理へ対応・排他制御が必要
var wg sync.WaitGroup
var mu sync.Mutex
var rehost []ReHost
for _, host := range in {
wg.Add(1)
h := host
go func(wg *sync.WaitGroup) {
defer wg.Done()
defer mu.Unlock()
mu.Lock()
resu := new(ReHost)
data := Host{
Disp: h[0],
Url: h[1],
}
req, err := http.NewRequest("GET", data.Url, nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Maenmo; Linux armv71; rv:10.0.1) Gecko/20100101 Firefox/10.0.1 Fennec/10.0.1")
res, err := http.DefaultClient.Do(req)
var rescode int
if err != nil {
println("[FAIL] ", data.Disp, "のURLを処理できませんでした。")
rescode = -1
} else {
rescode = res.StatusCode
fmt.Println("[_OK_] ", data.Disp, "->", rescode)
}
resu.Disp = data.Disp
resu.Url = data.Url
rehost = append(rehost, ReHost{Disp: data.Disp, Url: data.Url, Code: rescode, Time: time.Now()})
json.Marshal(rehost)
}(&wg)
}
wg.Wait()
result, _ := json.Marshal(rehost)
var buf bytes.Buffer
json.Indent(&buf, result, "", " ")
indentResult := buf.String()
//fmt.Println(indentResult)
info("---=取得を終了しました=---")
info("")
info("jsonファイルに書き出しています...")
if DebugOptionMode == true {
fmt.Println("==========BEGIN DEBUG==========")
fmt.Printf("[JSON CONTENTS]\n%s\n", indentResult)
fmt.Println("==========END DEBUG==========")
}
err = ioutil.WriteFile("http_response_results.json", []byte(indentResult), 0644)
if err != nil {
println("ファイル生成に失敗しました。")
time.Sleep(5 * time.Second)
log.Fatal(err)
}
info("jsonファイルの書き出しが完了しました。")
info("10秒後に自動終了します...")
time.Sleep(10 * time.Second)
}