-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (104 loc) · 2.38 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
package main
import (
"github.com/undiabler/golang-whois"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-lambda-go/lambda"
"context"
"strings"
"os"
"strconv"
"errors"
)
type Event struct {
Domain string `json:"domain"`
}
type Response struct {
success bool
message string
}
var is_lambda bool
var verbose bool
func init() {
// setup environment variables
is_lambda = false
ISLAMBDA := strings.ToLower(os.Getenv("ISLAMBDA"))
if len(ISLAMBDA) > 0 {
is_lambda, _ = strconv.ParseBool(ISLAMBDA)
}
verbose = false
VERBOSE := strings.ToLower(os.Getenv("VERBOSE"))
if len(VERBOSE) > 0 {
verbose, _ = strconv.ParseBool(VERBOSE)
}
}
func init() {
// setup logging
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
if verbose {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.ErrorLevel)
}
}
func IsAvailable(domain string) (bool, error) {
log.Trace("inside IsAvailable")
if strings.Count(domain, ".") > 1 {
log.Info("need to remove the subdomain")
subdomainIndex := strings.Index(domain, ".")
domain = domain[subdomainIndex+1:]
}
log.Debug("domain = " + domain)
if len(domain) > 0 {
result, err := whois.GetWhois(domain)
if err != nil || len(whois.ParseDomainStatus(result)) == 0 {
log.Info(domain+" is available")
return true, nil
} else {
log.Warning("got a response. "+domain+" is not available.")
return false, nil
}
} else {
log.Error("no domain provided")
return false, errors.New("no domain provided")
}
}
func LambdaHandler(ctx context.Context, req Event) (Response, error) {
log.Trace("in LambdaHandler")
is_available, err := IsAvailable(req.Domain)
if err != nil {
return Response{success: false, message: err.Error()}, err
} else if is_available {
return Response{success: true, message: req.Domain+" is available"}, nil
} else {
return Response{success: true, message: "got a response. "+req.Domain+" is not available."}, nil
}
}
func CommandLine() {
log.Trace("in CommandLine")
args := os.Args[1:]
if len(args) < 1 {
log.Error("Need a domain")
os.Exit(1)
}
domain := args[0]
is_available, _ := IsAvailable(domain)
if is_available {
// domain is available
os.Exit(0)
} else {
// domain is unavailable
os.Exit(2)
}
}
func main() {
log.Info("starting")
if is_lambda {
// run in lambda function
lambda.Start(LambdaHandler)
} else {
CommandLine()
}
}