-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkURL.go
120 lines (109 loc) · 3.08 KB
/
checkURL.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
package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
type ConfigurationFile struct {
Httpslist string `json:"httpslist"`
Outputfile string `json:"outputfile"`
Search string `json:"search"`
ErrorCodes []string `json:"errorCodes"`
}
func main() {
fromConfig := LoadConfiguration("urlCheckConfig.json")
Httpslist := fromConfig.Httpslist
Outputfile := fromConfig.Outputfile
Search := fromConfig.Search
ErrorCodes := fromConfig.ErrorCodes
DeleteOutputFile(Outputfile)
ListAllURIs(Httpslist, Search, Outputfile)
fmt.Print("Starting to check URI's\n")
RunUrlTest(Outputfile, ErrorCodes)
}
//Read json Config File
func LoadConfiguration(file string) ConfigurationFile {
var config ConfigurationFile
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return config
}
//Checkes if the Message has error type that indicats that the site doesn't exists
func checkMessage(message string, errorCodes []string, site string) {
colorRed := "\033[31m"
colorGreen := "\033[32m"
for _, errorCodes := range errorCodes {
if strings.Contains(message, errorCodes) {
fmt.Println(string(colorRed), "****** Not Exists ****** "+site, message, colorGreen)
return
}
}
fmt.Println("Exists "+site, message)
}
//Openes the URL list file that was created and sends each line to CheckMessage function
func RunUrlTest(URIList string, errorCodes []string) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
data, err := os.Open(URIList)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(data)
for scanner.Scan() {
site := scanner.Text()
resp, err := http.Get(site)
if err != nil {
checkMessage(err.Error(), errorCodes, site)
}
if resp != nil {
checkMessage(resp.Status, errorCodes, site)
}
}
}
//Deletes the OutPutFile in case it exists, Done only once
func DeleteOutputFile(fileToDelete string) {
if _, err := os.Stat(fileToDelete); os.IsNotExist(err) {
return
}
err := os.Remove(fileToDelete)
if err != nil {
panic(err)
}
fmt.Print("****** Old copy of Output file deleted ******\n")
}
//Add URI string from the Bookmarks.yaml list to output file
func UpdateOutputFile(lineToAdd string, outputfile string) {
output, err := os.OpenFile(outputfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic((err))
}
defer output.Close()
if _, err := output.WriteString(lineToAdd + "\n"); err != nil {
panic(err)
}
}
//Taks the list of URI's and runs line by line to search for uri, in case found send it to be written
func ListAllURIs(URIList string, search string, outputfile string) {
data, err := os.Open(URIList)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(data)
for scanner.Scan() {
line := scanner.Text()
answer := strings.Contains(line, search)
if answer == true {
CleanLine := strings.Replace(line, "-", "", 1)
FixedLine := strings.Replace(CleanLine, " ", "", 10)
UpdateOutputFile(FixedLine, outputfile)
}
}
}