/
main.go
169 lines (144 loc) · 3.91 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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/openrdap/rdap/bootstrap"
"log"
"io/ioutil"
"encoding/json"
)
func main() {
http.HandleFunc("/", handle)
http.ListenAndServe(":80", nil)
}
func handle(w http.ResponseWriter, r *http.Request) {
/*
if r.Method != "GET" {
http.Error(w, "Method not supported", 501)
return
}
*/
w.Header().Add("Access-Control-Allow-Origin", "*")
s := http.NewServeMux()
// Static files (index.html, img/*, etc.)
s.Handle("/", http.FileServer(http.Dir("www")))
s.HandleFunc("/_ah/health", healthCheckHandler)
s.HandleFunc("/ip/", handleIPQuery)
s.HandleFunc("/autnum/", handleAutnumQuery)
s.HandleFunc("/domain/", handleDomainQuery)
s.HandleFunc("/help", handleHelpQuery)
s.ServeHTTP(w, r)
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "ok")
}
func handleHelpQuery(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/rdap+json")
helpJSON := `{
"rdapConformance" :
[
"rdap_level_0"
],
"notices" :
[
{
"title" : "rdap.biznetgio.com help",
"description" :
[
"rdap.biznetgio.com provides a free RDAP query service."
],
"links" :
[
{
"value" : "https://rdap.biznetgio.com",
"rel" : "alternate",
"type" : "text/html",
"href" : "https://rdap.biznetgio.com"
}
]
}
]
}`
fmt.Fprintf(w, "%s", helpJSON)
}
func handleIPQuery(w http.ResponseWriter, r *http.Request) {
autnum := strings.TrimPrefix(r.URL.Path, "/ip/")
regType := bootstrap.IPv4
if strings.ContainsAny(autnum, ":") {
regType = bootstrap.IPv6
}
processBootstrappedQuery(w, r, regType, autnum)
}
func handleDomainQuery(w http.ResponseWriter, r *http.Request) {
domain := strings.TrimPrefix(r.URL.Path, "/domain/")
processBootstrappedQuery(w, r, bootstrap.DNS, domain)
}
func handleAutnumQuery(w http.ResponseWriter, r *http.Request) {
autnum := strings.TrimPrefix(r.URL.Path, "/autnum/")
processBootstrappedQuery(w, r, bootstrap.ASN, autnum)
}
func processBootstrappedQuery(w http.ResponseWriter, r *http.Request, regType bootstrap.RegistryType, query string) {
ctx := r.Context()
// Empty query?
if query == "" {
http.NotFound(w, r)
return
}
// Question for bootstrap client.
question := &bootstrap.Question{
RegistryType: regType,
Query: query,
}
question = question.WithContext(ctx)
client := &bootstrap.Client{
Verbose: func(s string) { fmt.Fprintf(os.Stderr, "%s\n", s) },
}
// Lookup question.
answer, err := client.Lookup(question)
// Not found?
if err != nil || len(answer.URLs) == 0 {
http.NotFound(w, r)
return
}
// Build redirect URL.
u := answer.URLs[0]
u.Path = strings.TrimRight(u.Path, "/")
u.Path += r.URL.Path
u.RawQuery = r.URL.RawQuery
// Redirect...
//http.Redirect(w, r, u.String(), 302)
resp, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var entities map[string]interface{}
json.Unmarshal(bodyBytes, &entities)
handleCode := entities["entities"].([]interface{})[0].(map[string]interface{})["handle"]
fmt.Println("handle code : ",handleCode)
if handleCode != "3773" {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
notFoundMsg := `{"errorCode":404,"title":"Not Found","description":"Domain not found"}`
fmt.Fprintf(w, "%s", notFoundMsg)
} else {
bodyString := string(bodyBytes)
w.Header().Set("Content-Type", "application/rdap+json")
fmt.Fprintf(w, "%s", bodyString)
}
} else if resp.StatusCode == http.StatusNotFound {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(resp.StatusCode)
notFoundMsg := `{"errorCode":404,"title":"Not Found","description":"Domain not found"}`
fmt.Fprintf(w, "%s", notFoundMsg)
}else {
w.WriteHeader(resp.StatusCode)
}
}