-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
160 lines (133 loc) · 3.41 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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
)
var availableHost []string
var testQueue = make(chan int, 10)
func getAppIDAndSecret() (string, string) {
err := godotenv.Load()
if err != nil {
panic("Error when load .env file: " + err.Error())
}
//这个ID和Secret可能被重置了, 请到 https://search.censys.io/account/api 获取
apiID := os.Getenv("API_ID")
apiSecret := os.Getenv("SECRET")
if apiID == "" || apiSecret == "" {
panic("Please set API_ID and SECRET in .env file")
}
return apiID, apiSecret
}
func queryFromCensys() (string, error) {
url := "https://search.censys.io/api/v2/hosts/search?q=service.service_name:%20HTTP%20AND%20services.http.response.headers.location:%20account.jetbrains.com/fls-auth&per_page=50&virtual_hosts=EXCLUDE"
method := "GET"
//避免x509: certificate signed by unknown authority
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return "", err
}
req.Header.Add("Accept", "application/json")
id, secret := getAppIDAndSecret()
req.SetBasicAuth(id, secret)
res, err := client.Do(req)
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return "", err
}
return string(body), nil
}
func TestHost(ip string, port int) {
testQueue <- 1
url := "http://" + ip + ":" + strconv.Itoa(port) + "/"
//避免x509: certificate signed by unknown authority
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Timeout: time.Second * 5,
Transport: tr,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
<-testQueue
return
}
response, err := client.Do(req)
if err != nil {
log.Println(url, " http get error: ", err)
<-testQueue
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println(url, " http close error: ", err)
}
}(response.Body)
if response.StatusCode == 200 && response.Request.URL.Host == "account.jetbrains.com" && response.Request.URL.Path == "/login" {
log.Println(url, " is ok")
availableHost = append(availableHost, url)
}
<-testQueue
}
func parseCensysResult(body string) {
fmt.Println(body)
fmt.Print("\n\n------------------\n\n")
fmt.Println("耐心等待...")
var result HostSearchResult
err := json.Unmarshal([]byte(body), &result)
if err != nil {
fmt.Println("error when unmarshal json: ", err)
}
if result.Code != 200 && result.Status != "OK" {
fmt.Println("error code from censys: ", result.Status)
return
}
for _, hit := range result.Result.Hits {
for _, service := range hit.Services {
if service.ServiceName == "HTTP" {
fmt.Printf("testing: %s %s:%d \n", hit.AutonomousSystem.Name, hit.IP, service.Port)
go TestHost(hit.IP, service.Port)
}
}
}
}
func main() {
jsonString, err := queryFromCensys()
if err != nil {
fmt.Println(err)
return
}
parseCensysResult(jsonString)
for len(testQueue) > 0 {
time.Sleep(time.Second)
}
fmt.Print("\n\n------------------\n\n")
fmt.Println("可用的服务器:")
for _, host := range availableHost {
fmt.Println(host)
}
fmt.Println("\n\ndone!!!")
}