This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (167 loc) · 4.64 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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"bitbucket.org/nndi/phada"
)
var (
sessionStore = make(map[string]*phada.UssdRequestSession, 0)
ErrFailedToCheckDomain = errors.New("Failed to check if domain is available")
ApiKey = "__YOUR_API_KEY__" // changed during+ build with build tags -ldApiKey=""
bindAddress string
)
const (
JSON_WHOIS_API = "https://api.jsonwhois.io/whois/domain"
STATE_ENTRY = 1
STATE_PROMPT = 2
STATE_PROMPT_INPUT = 3
STATE_ABOUT = 4
ENTRY_MENU = `Welcome to USSD WhoIs
1. Check for domain
2. About
`
CHECK_FOR_DOMAIN_PROMPT = `Check for domain
Enter the name of a domain. e.g. mywebsite.com
`
DOMAIN_ALREADY_REGISTERED_TPL = `%s is already registered.
Owner: %s
Updated: %s
Name Servers: %s
`
DOMAIN_AVAILABLE = `The domain %s is available.
Purchase now before someone else gets their hands on it.
`
ABOUT_SERVICE = `This service is brought to you by Payrope and NNDI, who specialize in developing SMS, USSD and Web applications.
Find us online: https://payrope.com https://nndi-tech.com
`
)
type JsonWhoIsResult struct {
Data struct {
Name string `json:"name"`
Created string `json:"created"`
Changed string `json:"changed"`
Expires string `json:"expires"`
Dnssec string `json:"dnssec"`
Registered bool `json:"registered"`
NameServers []string `json:"nameservers"`
} `json:"result"`
}
func (j *JsonWhoIsResult) NameServersList() string {
return strings.Join(j.Data.NameServers, ",")
}
func ussdContinue(text string) string {
return fmt.Sprintf("CON %s", text)
}
func ussdEnd(text string) string {
return fmt.Sprintf("END %s", text)
}
func checkDomainAvailability(domainName string) (*JsonWhoIsResult, error) {
client := &http.Client{}
whoIsResult := &JsonWhoIsResult{}
requestUrl, _ := url.Parse(JSON_WHOIS_API)
q := requestUrl.Query()
q.Add("key", ApiKey)
q.Add("domain", domainName)
requestUrl.RawQuery = q.Encode()
log.Print("Sending domain request check for domain: " + domainName)
res, err := client.Get(requestUrl.String())
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(res.Body)
log.Print(fmt.Sprintf(
"Got Status: %s for domain request check for: %s: body=%s",
res.StatusCode,
domainName,
data,
))
if err != nil {
return nil, err
}
json.Unmarshal(data, whoIsResult)
if whoIsResult == nil {
return nil, ErrFailedToCheckDomain
}
return whoIsResult, nil
}
func handlerFunc(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
session, err := phada.ParseUssdRequest(req)
if err != nil {
log.Print("Failed to parse request to UssdRequestSession")
fmt.Fprintf(w, ussdEnd("Failed to process request"))
return
}
existingSession, ok := sessionStore[session.SessionID]
if ok {
existingSession.RecordHop(session.Text)
session = existingSession
} else {
sessionStore[session.SessionID] = session // store/persist the session
}
prevState := session.State
session.SetState(STATE_ENTRY)
if prevState == STATE_PROMPT {
session.SetState(STATE_PROMPT_INPUT)
}
if session.ReadIn() == "1" {
session.SetState(STATE_PROMPT)
}
if session.ReadIn() == "2" {
session.SetState(STATE_ABOUT)
}
// fmt.Println("Text", session.ReadIn(), "Current state", session.State, "Previous state", prevState)
switch session.State {
case STATE_ENTRY:
fmt.Fprintf(w, ussdContinue(ENTRY_MENU))
break
case STATE_PROMPT:
fmt.Fprintf(w, ussdContinue(CHECK_FOR_DOMAIN_PROMPT))
break
case STATE_PROMPT_INPUT:
domainName := session.ReadIn()
domainWhoIs, err := checkDomainAvailability(domainName)
if err != nil {
log.Println("Error while processing request", err)
fmt.Fprintf(w, ussdEnd("Failed to process request"))
break
}
if !domainWhoIs.Data.Registered {
fmt.Fprintf(w, ussdEnd(fmt.Sprintf(DOMAIN_AVAILABLE, domainName)))
break
}
fmt.Fprintf(w, ussdEnd(fmt.Sprintf(
DOMAIN_ALREADY_REGISTERED_TPL,
domainName,
domainWhoIs.Data.Name,
domainWhoIs.Data.Changed,
domainWhoIs.NameServersList(),
)))
break
case STATE_ABOUT:
fmt.Fprintf(w, ussdEnd(ABOUT_SERVICE))
break
default:
fmt.Fprintf(w, ussdEnd("Failed to process"+session.ReadIn()))
break
}
sessionStore[session.SessionID] = session
}
func init() {
flag.StringVar(&bindAddress, "b", "localhost:8773", "interface and port to bind server to, e.g. localhost:8080")
}
func main() {
flag.Parse()
if bindAddress == "" {
bindAddress = "localhost:8773"
}
http.HandleFunc("/", handlerFunc)
log.Fatalf("Failed to start server. Error %s", http.ListenAndServe(bindAddress, nil))
}