/
check_http.go
144 lines (132 loc) · 3 KB
/
check_http.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
package main
import (
"io/ioutil"
"net"
"strings"
"os"
"sync"
"log"
"regexp"
"crypto/tls"
// "encoding/json"
"gopkg.in/yaml.v2"
"github.com/olekukonko/tablewriter"
)
type Domain struct{
Domain string `yaml:"domain"`
Find string `yaml:"regex"`
}
type Settings struct {
Ipv4 bool `yaml:"ipv4"`
Ipv6 bool `yaml:"ipv6"`
Domains []Domain `yaml:"domains"`
}
type StatusDomain struct{
Domain string
Ipv4_http int
Ipv6_http int
Ipv4_https int
Ipv6_https int
}
var (
statusoutput = make(chan StatusDomain,5)
outputwg = sync.WaitGroup{}
domainwg = sync.WaitGroup{}
config = &Settings{}
)
func main() {
readConfig()
outputwg.Add(1)
go printTable()
for _,domain := range config.Domains{
domainwg.Add(1)
fetchDomain(domain)
}
domainwg.Wait()
close(statusoutput)
outputwg.Wait()
}
func fetchDomain(domain Domain){
n := StatusDomain{Domain:domain.Domain}
find,err := regexp.Compile(domain.Find)
if err !=nil{
log.Println(err)
}
if config.Ipv4 {
n.Ipv4_http = requestHttp("tcp4",domain.Domain,find)
n.Ipv4_https = requestHttps("tcp4",domain.Domain,find)
}
if config.Ipv6 {
n.Ipv6_http = requestHttp("tcp6",domain.Domain,find)
n.Ipv6_https = requestHttps("tcp6",domain.Domain,find)
}
statusoutput <- n
domainwg.Done()
}
func printTable(){
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Domain", "IPv4", "IPv6", "SSL-IPv4", "SSL-IPv6"})
for v := range statusoutput {
table.Append([]string{v.Domain,printStatus(v.Ipv4_http),printStatus(v.Ipv6_http),printStatus(v.Ipv4_https),printStatus(v.Ipv6_https)})
}
table.Render()
outputwg.Done()
}
func printStatus(s int) string{
switch s {
case 1:
return "0";
case 200:
return "W";
case 300:
return "30";
}
return "E";
}
func requestHttp(tcp, domain string,find *regexp.Regexp) int {
conn, err := net.Dial(tcp, domain+":http")
handleError(err)
_, err = conn.Write([]byte("GET / HTTP/1.0\nHOST: "+domain+"\r\n\r\n"))
handleError(err)
result, _ := ioutil.ReadAll(conn)
return requestOutputInterpreter(result,find,false,false)
}
func requestHttps(tcp, domain string,find *regexp.Regexp) int {
conf := tls.Config{}
conn, err := tls.Dial(tcp, domain+":https",&conf)
handleError(err)
_, err = conn.Write([]byte("GET / HTTP/1.0\nHOST: "+domain+"\r\n\r\n"))
handleError(err)
result, _ := ioutil.ReadAll(conn)
ssl := false;
out := requestOutputInterpreter(result,find,true,ssl)
return out;
}
func requestOutputInterpreter(result []byte,find *regexp.Regexp,ssl bool, sslwork bool) int{
result_s := string(result)
if strings.Contains(result_s,"HTTP/1.1 200") || strings.Contains(result_s,"HTTP/1.1 30"){
if find.Match(result){
return 1;
}else{
if strings.Contains(result_s,"HTTP/1.1 200"){
return 200
}else{
return 300
}
}
}
return -1;
}
func readConfig() {
file, _ := ioutil.ReadFile("/home/geno/.checkhttprc")
err := yaml.Unmarshal(file,&config)
if err != nil {
log.Panic("error:", err)
}
}
func handleError(err error){
if err != nil {
log.Println(err)
os.Exit(1)
}
}