Skip to content

Commit

Permalink
feat(server): add insert API endoint
Browse files Browse the repository at this point in the history
  • Loading branch information
g0rbe committed Oct 5, 2023
1 parent 3c1c5af commit 291db0d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
38 changes: 37 additions & 1 deletion frontend/static/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ info:
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 0.15.1
version: dev
servers:
- url: https://columbus.elmasy.com
tags:
Expand Down Expand Up @@ -306,6 +306,42 @@ paths:
'504':
description: Gateway Timeout. Upstream response takes too long.

/api/insert/{domain}:
put:
tags:
- domain
operationId: PutAPIInsert
summary: Insert domain into the database.
description: |
This endpoint technically suggest a domain to the server.
It is required to have at least one valid DNS record for the domain to insert (eg.: `A` or `AAAA`).
The domain is sent to an underlying channel, so returns fast, but the client will not get informed about the result.
If the underlying channel is full, hangs until there is free space to send.
This endpoint uses blacklist and rate limiter to prevent garbage and resource exhaustion
(eg.: sending invalid domain results a block for some time).
parameters:
- name: domain
in: path
description: Domain to insert.
required: true
schema:
type: string
responses:
'200':
description: OK
'400':
description: Invalid domain.
'403':
description: Client IP blocked.
'500':
description: Internal Server Error.
'502':
description: Bad Gateway. Upstream failed.
'504':
description: Gateway Timeout. Upstream response takes too long.

/api/stat:
get:
tags:
Expand Down
36 changes: 36 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"time"

"github.com/elmasy-com/elnet/blocklist"
"github.com/elmasy-com/elnet/dns"
"gopkg.in/yaml.v3"
)
Expand All @@ -20,6 +21,10 @@ type conf struct {
DNSServers []string `yaml:"DNSServers"`
DomainWorker int `yaml:"DomainWorker"`
DomainBuffer int `yaml:"DomainBuffer"`
InsertWorker int `yaml:"InsertWorker"`
InsertBuffer int `yaml:"InsertBuffer"`
BlocklistSize int `yaml:"BlocklistSize"`
BlockTime int `yaml:"BlockTime"`
}

var (
Expand All @@ -32,6 +37,11 @@ var (
DNSServers []string
DomainWorker int
DomainBuffer int
InsertWorker int
InsertBuffer int
BlocklistSize int
BlockTime time.Duration
Blocklist *blocklist.Blocklist
)

// Parse parses the config file in path and gill the global variables.
Expand Down Expand Up @@ -87,5 +97,31 @@ func Parse(path string) error {

DomainBuffer = c.DomainBuffer

if c.InsertWorker == 0 {
c.InsertWorker = runtime.NumCPU()
}

InsertWorker = c.InsertWorker

if c.InsertBuffer == 0 {
c.InsertBuffer = 10000
}

InsertBuffer = c.InsertBuffer

if c.BlocklistSize == 0 {
c.BlocklistSize = 1000
}

BlocklistSize = c.BlocklistSize

if c.BlockTime == 0 {
c.BlockTime = 600
}

BlockTime = time.Duration(c.BlockTime) * time.Second

Blocklist = blocklist.NewBlocklist(BlockTime, int64(BlocklistSize))

return nil
}
33 changes: 33 additions & 0 deletions server/route/api/insert/insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package insert

import (
"fmt"
"net/http"

"github.com/elmasy-com/columbus/db"
"github.com/elmasy-com/columbus/server/config"
"github.com/elmasy-com/elnet/dns"
"github.com/elmasy-com/elnet/validator"
"github.com/gin-gonic/gin"
)

func PutApiInsert(c *gin.Context) {

if config.Blocklist.IsBlocked(c.ClientIP()) {
c.Status(http.StatusForbidden)
return
}

d := dns.Clean(c.Param("domain"))

if !validator.Domain(d) {
config.Blocklist.Block(c.ClientIP())
c.Error(fmt.Errorf("invalid domain: %s", d))
c.Status(http.StatusBadRequest)
return
}

db.UpdaterChan <- db.UpdateableDomain{Domain: d, Type: db.InsertNewDomain}

c.Status(http.StatusOK)
}
8 changes: 7 additions & 1 deletion server/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ DNSServers: ["upd://8.8.8.8:53", "udp://8.8.4.4:53", "udp://1.1.1.1:53", "udp://
DomainWorker: 4

# Buffer for record updater (default: 1000)
DomainBuffer: 10000
DomainBuffer: 10000

# Size of the blocklist (default: 1000)
BlocklistSize: 1000

# Number of seconds to block remote IP on bad behaviour (default: 600)
BlockTime: 600
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/elmasy-com/columbus/server/config"
"github.com/elmasy-com/columbus/server/route/api"
"github.com/elmasy-com/columbus/server/route/api/history"
"github.com/elmasy-com/columbus/server/route/api/insert"
"github.com/elmasy-com/columbus/server/route/api/lookup"
"github.com/elmasy-com/columbus/server/route/api/starts"
"github.com/elmasy-com/columbus/server/route/api/statistics"
Expand Down Expand Up @@ -91,6 +92,8 @@ func ServerRun() error {
router.GET("/api/tools/subdomain/:fqdn", tools.ToolsSubdomainGet)
router.GET("/api/tools/isvalid/:fqdn", tools.ToolsIsValidGet)

router.PUT("/api/insert/:domain", insert.PutApiInsert)

// Redirect to /search/:domain
router.GET("/lookup/:domain", lookup.RedirectLookup)

Expand Down

0 comments on commit 291db0d

Please sign in to comment.