forked from trumail/trumail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (70 loc) · 2.04 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
package main
import (
"log"
"net"
"os"
"strings"
"github.com/entrik/httpclient"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/sdwolfe32/trumail/api"
"github.com/sdwolfe32/trumail/verifier"
)
var (
// port defines the port used by the api server
port = getEnv("PORT", "8080")
// sourceAddr defines the address used on verifier
sourceAddr = getEnv("SOURCE_ADDR", "[email protected]")
)
func main() {
// Declare the router
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Define the API Services
v := verifier.NewVerifier(retrievePTR(), sourceAddr)
// Bind the API endpoints to router
e.GET("/v1/:format/:email", api.LookupHandler(v), authMiddleware)
e.GET("/v1/health", api.HealthHandler(), authMiddleware)
// Listen and Serve
e.Logger.Fatal(e.Start(":" + port))
}
// RetrievePTR attempts to retrieve the PTR record for the IP
// address retrieved via an API call on api.ipify.org
func retrievePTR() string {
// Request the IP from ipify
ip, err := httpclient.GetString("https://api.ipify.org/")
if err != nil {
log.Fatal("Failed to retrieve public IP")
}
// Retrieve the PTR record for our IP and return without a trailing dot
names, err := net.LookupAddr(ip)
if err != nil {
return ip
}
return strings.TrimSuffix(names[0], ".")
}
// authMiddleware verifies the auth token on the request matches the
// one defined in the environment
func authMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
// authToken is the token that must be used on all requests
authToken := getEnv("AUTH_TOKEN", "")
// Return the Handlerfunc that asserts the auth token
return func(c echo.Context) error {
if authToken != "" {
if c.Request().Header.Get("X-Auth-Token") == authToken {
return next(c)
}
return echo.ErrUnauthorized
}
return next(c)
}
}
// getEnv retrieves variables from the environment and falls back
// to a passed fallback variable if it isn't set
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}