This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lookup.go
65 lines (55 loc) · 1.4 KB
/
lookup.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
package server
import (
"net/http"
"strings"
"github.com/elmasy-com/columbus-sdk/fault"
"github.com/elmasy-com/columbus-server/blacklist"
"github.com/elmasy-com/columbus-server/db"
"github.com/elmasy-com/elnet/domain"
"github.com/gin-gonic/gin"
)
func LookupGet(c *gin.Context) {
// Block blacklisted IPs
if blacklist.IsBlocked(c.ClientIP()) {
if c.GetHeader("Accept") == "text/plain" {
c.String(http.StatusForbidden, fault.ErrBlocked.Err)
} else {
c.JSON(http.StatusForbidden, fault.ErrBlocked)
}
return
}
var err error
d := c.Param("domain")
if !domain.IsValid(d) {
c.Error(fault.ErrInvalidDomain)
if c.GetHeader("Accept") == "text/plain" {
c.String(http.StatusBadRequest, fault.ErrInvalidDomain.Error())
} else {
c.JSON(http.StatusBadRequest, fault.ErrInvalidDomain)
}
return
}
subs, err := db.Lookup(d)
if err != nil {
c.Error(err)
if c.GetHeader("Accept") == "text/plain" {
c.String(http.StatusInternalServerError, err.Error())
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
return
}
if len(subs) == 0 {
if c.GetHeader("Accept") == "text/plain" {
c.String(http.StatusNotFound, fault.ErrNotFound.Err)
} else {
c.JSON(http.StatusNotFound, fault.ErrNotFound)
}
return
}
if c.GetHeader("Accept") == "text/plain" {
c.String(http.StatusOK, strings.Join(subs, "\n"))
} else {
c.JSON(http.StatusOK, subs)
}
}