-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
270 lines (249 loc) · 7.53 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/gocolly/colly"
_ "github.com/lib/pq"
"github.com/likexian/whois-go"
whoisparser "github.com/likexian/whois-parser-go"
)
// Incoming message from json
type Incoming struct {
Host string `json:"host"`
Status string `json:"status"`
StartTime int64 `json:"startTime"`
Endpoints *json.RawMessage `json:"endpoints"`
}
// Body final response
type Body struct {
Host string `json:"host"`
Servers []*Server `json:"endpoints"`
ServersChanged bool
SslGrade string
previousSslGrade string
Logo string
Title string
IsDown bool
}
// Server structure from json endpoints
type Server struct {
Address string `json:"ipAddress"`
SslGrade string `json:"grade"`
Country string
Owner string
}
func main() {
// Create the router
router := chi.NewRouter()
// Basic CORS
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
// Middlewares
router.Use(cors.Handler)
router.Use(middleware.RequestID)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(middleware.RealIP)
router.Get("/", func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Welcome to GO"))
})
router.Route("/servers", func(r chi.Router) {
r.Get("/", getDomains)
r.Route("/{domain}", func(r chi.Router) {
r.Get("/", getDomainInfo)
})
})
http.ListenAndServe(":3000", router)
}
func getDomains(res http.ResponseWriter, req *http.Request) {
// Open the SQL Connection
db, errdb := sql.Open("postgres", "postgresql://maxroach@localhost:26257/icango?sslmode=disable")
if errdb != nil {
log.Fatal("error connecting to the database: ", errdb)
} else {
// Create the "items" table.
if _, err := db.Exec(
"CREATE TABLE IF NOT EXISTS items (id SERIAL PRIMARY KEY, host STRING, servers STRING, servers_changed BOOL, ssl_grade STRING, previous_ssl_grade STRING, logo STRING, title STRING, is_down BOOL)"); err != nil {
log.Fatal(err)
}
}
// Save the items
items := []Body{}
// Let's search the items
rows, err := db.Query("SELECT * FROM items LIMIT 10")
// If the error is non nil
if err != nil {
http.Error(res, http.StatusText(404), 404)
return
}
defer rows.Close()
// 1 2 3 4 5 6 7 8 9 10
for rows.Next() {
var bodyID int64
body := Body{}
var serverString string
if err := rows.Scan(&bodyID, &body.Host, &serverString, &body.ServersChanged, &body.SslGrade, &body.previousSslGrade, &body.Logo, &body.Title, &body.IsDown); err != nil {
panic(err)
}
// Parse the serverString to Servers
if err := json.Unmarshal([]byte(serverString), &body.Servers); err != nil {
panic(err)
}
items = append(items, body)
}
// Finally, Marshal the json body and response it
response, err := json.Marshal(items)
// If error is non nil
if err != nil {
http.Error(res, http.StatusText(404), 404)
}
res.Write([]byte(response))
}
func getDomainInfo(res http.ResponseWriter, req *http.Request) {
// Open the SQL Connection
db, errdb := sql.Open("postgres", "postgresql://maxroach@localhost:26257/icango?sslmode=disable")
if errdb != nil {
log.Fatal("error connecting to the database: ", errdb)
} else {
// Create the "items" table.
if _, err := db.Exec(
"CREATE TABLE IF NOT EXISTS items (id SERIAL PRIMARY KEY, host STRING, servers STRING, servers_changed BOOL, ssl_grade STRING, previous_ssl_grade STRING, logo STRING, title STRING, is_down BOOL)"); err != nil {
log.Fatal(err)
}
}
// Get the domain param
domain := chi.URLParam(req, "domain")
// Let's search the domain into database
var itemID int64
// Item is for save the result
item := Body{}
// This string save the servers
var serverString string
queryErr := db.QueryRow("SELECT * FROM items WHERE host = $1", domain).Scan(&itemID, &item.Host, &serverString, &item.ServersChanged, &item.SslGrade, &item.previousSslGrade, &item.Logo, &item.Title, &item.IsDown)
// If the error is non nil
if queryErr != nil {
http.Error(res, http.StatusText(404), 404)
return
}
// Then, make the http get request with the domain param like host
result, err := http.Get("https://api.ssllabs.com/api/v3/analyze?host=" + domain)
// If error is non nil
if err != nil {
http.Error(res, http.StatusText(404), 404)
return
}
// When the function ends, let's close the body
defer result.Body.Close()
// Let's get the byte info from Body
body, err := ioutil.ReadAll(result.Body)
// If error is non nil
if err != nil {
http.Error(res, http.StatusText(404), 404)
return
}
// Here we save the json body
var jsonBody Body
var incoming Incoming
// // Decode the body to jsonBody
if err := json.Unmarshal(body, &jsonBody); err != nil {
panic(err)
}
// Decode the body to incoming json
if err := json.Unmarshal(body, &incoming); err != nil {
panic(err)
}
// Let's verify if the domain is already be into database
if itemID != 0 {
// Transform the servers for can save it into database
servers, err := json.Marshal(jsonBody.Servers)
// Parse the serverString to Servers
if err := json.Unmarshal([]byte(servers), &item.Servers); err != nil {
panic(err)
}
// Let's prepare the body
er := prepareBody(&incoming, &item)
// If error is non nil
if er != nil {
http.Error(res, http.StatusText(404), 404)
}
// Update the database
if _, err := db.Exec(
"UPDATE items SET servers = $1 WHERE id = $2", servers, itemID); err != nil {
panic(err)
}
// Finally, Marshal the json body and response it
response, err := json.Marshal(item)
// If error is non nil
if err != nil {
http.Error(res, http.StatusText(404), 404)
}
defer res.Write([]byte(response))
} else {
// Let's prepare the body
err := prepareBody(&incoming, &jsonBody)
// If error is non nil
if err != nil {
panic(err)
}
// Transform the servers for can save it into database
servers, err := json.Marshal(jsonBody.Servers)
// If error is non nil
if err != nil {
panic(err)
}
// Let's save the jsonBody
if _, err := db.Exec("INSERT INTO items (host, servers, servers_changed, ssl_grade, previous_ssl_grade, logo, title, is_down) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", jsonBody.Host, servers, jsonBody.ServersChanged, jsonBody.SslGrade, jsonBody.previousSslGrade, jsonBody.Logo, jsonBody.Title, jsonBody.IsDown); err != nil {
log.Fatal(err)
}
// Finally, Marshal the json body and response it
response, err := json.Marshal(jsonBody)
// If error is non nil
if err != nil {
http.Error(res, http.StatusText(404), 404)
return
}
defer res.Write([]byte(response))
}
}
func prepareBody(incoming *Incoming, body *Body) (err error) {
// If the status is "ERROR" then...
if incoming.Status == "ERROR" {
body.IsDown = true
} else {
body.IsDown = false
}
// Whois
for _, value := range body.Servers {
whoisRaw, err := whois.Whois(body.Host)
if err != nil {
return err
}
whoisParse, err := whoisparser.Parse(whoisRaw)
if err != nil {
return err
}
value.Country = whoisParse.Registrant.Country
value.Owner = whoisParse.Registrant.Organization
}
// Use colly for get logo and title from domain
c := colly.NewCollector()
c.OnHTML("head title", func(e *colly.HTMLElement) {
body.Title = e.Text
})
c.OnHTML("head link", func(e *colly.HTMLElement) {
if strings.HasPrefix(e.Attr("type"), "image/") {
body.Logo = e.Attr("href")
}
})
// Now, let's create a string for visit that site
c.Visit("http://" + body.Host)
return nil
}