-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp.go
85 lines (75 loc) · 2.18 KB
/
http.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
package main
import (
"encoding/base64"
"log"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func initHTTP() {
e := echo.New()
e.HideBanner = true
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.Recover())
e.Use(middleware.Logger())
e.Use(middleware.CORS())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 9}))
e.Use(middleware.BodyLimit("100K"))
e.GET("/", func(c echo.Context) error {
return c.JSON(200, map[string]interface{}{
"success": true,
"message": "let's reverse the login process",
})
})
/**
* Get the information of an inbox (temp-inbox)
*/
e.GET("/inbox/info/:inboxId", func(c echo.Context) error {
id := c.Param("inboxId")
return c.JSON(200, map[string]interface{}{
"success": true,
"data": map[string]interface{}{
"uuid": id,
"inbox": id + "@" + *flagDomain,
"status": strings.ToLower(redisClient.Get(redisKeyPrefix(id+":status")).Val()) == "done",
"email": redisClient.Get(redisKeyPrefix(id + ":email")).Val(),
"expires": redisClient.Get(redisKeyPrefix(id + ":expires")).Val(),
},
})
})
/**
* Generates a temp-inbox for the specified email
*/
e.GET("/inbox/generate/:email", func(c echo.Context) error {
id := uuid.New().String()
if c.QueryParam("suffix") != "" {
id += "-" + c.QueryParam("suffix")
}
id = base64.RawStdEncoding.EncodeToString([]byte(id))
ttl, _ := strconv.Atoi(c.QueryParam("ttl"))
if ttl < 1 {
ttl = *flagDefaultTTL
}
exp := time.Now().Unix() + int64(ttl)
email := strings.ToLower(c.Param("email"))
redisDur := time.Second * 3600
redisClient.Set(redisKeyPrefix(email+":inbox"), id, redisDur).Val()
redisClient.Set(redisKeyPrefix(id+":status"), "pending", redisDur).Val()
redisClient.Set(redisKeyPrefix(id+":email"), email, redisDur).Val()
redisClient.Set(redisKeyPrefix(id+":expires"), exp, redisDur).Val()
return c.JSON(200, map[string]interface{}{
"success": true,
"data": map[string]interface{}{
"uuid": id,
"inbox": id + "@" + *flagDomain,
"status": false,
"email": email,
"expires": exp,
},
})
})
log.Fatal(e.Start(*flagHTTPAddr))
}