Skip to content

Commit

Permalink
Added proxy endpoints and API design [#67 #58 #63]
Browse files Browse the repository at this point in the history
  • Loading branch information
RicYaben committed Jul 13, 2022
1 parent 268f736 commit a221f04
Show file tree
Hide file tree
Showing 15 changed files with 604 additions and 190 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,10 @@ Note: the list will be updated on support for additional scanning sources.
## 2. Requirements

Make sure that you abide by the following software and platform requirements before running RIoTPot,
Make sure that you abide by the following software and platform requirements before running RIoTPot:

- Ubuntu 18.04 or higher
- gcc 9.3.0 or higher
- GNU make 4.2.1 or higher
- Go version go1.16.4 or higher
- MongoDB v4.4.8 or higher, having a username as `superuser` and password as `password`
- Docker version 20.10.2 or higher
- Docker-compose version 1.29.2 or higher
- Ubuntu
- [Golang ^V1.16](https://go.dev/dl/)

## 3. Installation

Expand All @@ -114,7 +109,7 @@ Follow the steps to get the RIoTPot project first:
$ git clone [email protected]:aau-network-security/riotpot.git

# 2. Navigate to the newly created directory with the repository
$ cd RIoTPot
$ cd riotpot
```

### 3.1 Local Build
Expand Down
147 changes: 147 additions & 0 deletions api/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package proxy

import (
"log"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/riotpot/api"
"github.com/riotpot/internal/proxy"
"github.com/riotpot/pkg/services"
)

// Structures used to serialize data
type Proxy struct {
ID int `json:"id" binding:"required" gorm:"primary_key"`
Port int `json:"port" binding:"required"`
Protocol string `json:"protocol" binding:"required"`
Status bool `json:"status"`
Service *Service `json:"service"`
}

type Service struct {
ID int `json:"id" binding:"required" gorm:"primary_key"`
Port int `json:"port" binding:"required"`
Name string `json:"name" binding:"required"`
Host string `json:"host" binding:"required"`
}

// Routes
var (
// Proxy Routes
proxyRoutes = []api.Route{
// Get proxies
api.NewRoute("/", "GET", getProxies),
// Get a proxy by port
api.NewRoute("/proxy/:port", "GET", getProxy),
// Post proxy by port
api.NewRoute("/proxy/:port", "POST", postProxy),
// Delete a proxy by port
api.NewRoute("/proxy/:port", "DELETE", delProxy),
// Patch (Not update) a proxy by port
api.NewRoute("/proxy/:port", "PATCH", patchProxy),
}
)

// Routers
var (
ProxyRouter = api.NewRouter("/proxies", proxyRoutes, nil)
)

func newService(serv services.Service) (sv *Service) {
if serv != nil {
sv = &Service{
Port: serv.GetPort(),
Name: serv.GetName(),
Host: serv.GetName(),
}
}

return
}

func newProxy(px proxy.Proxy) *Proxy {
serv := newService(px.Service())

return &Proxy{
Port: px.Port(),
Protocol: px.Protocol(),
Status: px.Alive(),
Service: serv,
}
}

// GET proxies registered
// Contains a filter to get proxies by port
func getProxies(ctx *gin.Context) {
casted := []Proxy{}

// Iterate through the proxies registered
for _, px := range proxy.Proxies.Proxies() {
// Serialize the proxy
pr := newProxy(px)
// Append the proxy tot he casted
casted = append(casted, *pr)
}

// Set the header and transform the struct to JSON format
ctx.JSON(http.StatusOK, casted)

}

// GET proxy by port ":port"
func getProxy(ctx *gin.Context) {
port, err := strconv.Atoi(ctx.Param("port"))
if err != nil {
log.Fatal(err)
}

// Get the proxy
px, err := proxy.Proxies.GetProxy(port)

// If the proxy could not be found, let the user know
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// Serialize the proxy and send it as a response
pr := newProxy(px)
ctx.JSON(http.StatusOK, pr)
}

// POST a proxy by port ":port"
func postProxy(ctx *gin.Context) {}

// DELETE a proxy by port ":port"
func delProxy(ctx *gin.Context) {}

// PATCH proxy by port ":port"
// Can update port, protocol, status and service
func patchProxy(ctx *gin.Context) {}

// Routers
/*
var (
ProxyRouter = &api.AbstractRouter{
path: "proxy",
routes: []Route{
GetProxies,
GetProxy,
},
}
)
*/

/*
func getProxies(ctx *gin.Context) {
// List of proxies
var proxies []Proxy
// Send the response with the proxies
ctx.Header("Content-Type", "application/json")
ctx.JSON(http.StatusOK, proxies)
}
*/
136 changes: 118 additions & 18 deletions api/router.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,129 @@
package api

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
)

// Proxy struct
type Proxy struct {
ID int `json:"id" binding:"required"`
Port int `json:"port" binding:"required"`
// Router interface
type Router interface {
// Returns the list of routes registered in the router
Routes() []Route
// Parent path for the router
Path() string
// Group the registered path
Group(parentGroup *gin.RouterGroup) *gin.RouterGroup
}

type AbstractRouter struct {
// Inherits the Router
Router
// Routes registered in the router
routes []Route
// Parent path
path string
// Child Routers
// A router may have child routers
// For example:
// - /v1
// .. - /router1
// .. .. - /r1
// .. .. - /r2
// .. - /router2
childs []Router
}

// Returns the parent path
func (r *AbstractRouter) Path() string {
return r.path
}

// Returns the list of routes registered in the router
func (r *AbstractRouter) Routes() []Route {
return r.routes
}

// Add the handlers to a router group
func (r *AbstractRouter) addHandlers(parentGroup *gin.RouterGroup) *gin.RouterGroup {
// Iterate the routes and add the handlers registered in the
for _, route := range r.Routes() {
parentGroup.Handle(route.Method(), route.Path(), route.Handlers()...)
}

return parentGroup
}

// Add all the child routers to a group
func (r *AbstractRouter) addChilds(parentGroup *gin.RouterGroup) *gin.RouterGroup {
// Iterate through the child routers to add the routes
if len(r.childs) > 0 {
for _, child := range r.childs {
child.Group(parentGroup)
}
}

return parentGroup
}

// Create the group routes inside of the router
func (r *AbstractRouter) Group(parentGroup *gin.RouterGroup) *gin.RouterGroup {
// Create a group inside of the parent group for this child
childGroup := parentGroup.Group(r.Path())
// Add the routes handlers for the current group
childGroup = r.addHandlers(childGroup)
// Add the child groups to the router
childGroup = r.addChilds(childGroup)

return childGroup
}

func NewRouter(path string, routes []Route, childs []Router) *AbstractRouter {
return &AbstractRouter{
path: path,
routes: routes,
childs: childs,
}
}

// Route that will be handled by the API
type Route interface {
// Raw function to handle the request
Handlers() gin.HandlersChain
// (Sub)Path to the route
Path() string
// Method used for the path
Method() string
}

type AbstractRoute struct {
Route
path string
method string
handlers gin.HandlersChain
}

func (ar *AbstractRoute) Path() string {
return ar.path
}

func (ar *AbstractRoute) Method() string {
return ar.method
}

func (ar *AbstractRoute) Handlers() gin.HandlersChain {
return ar.handlers
}

func NewRoute(path string, method string, handlers ...gin.HandlerFunc) *AbstractRoute {
return &AbstractRoute{
path: path,
method: method,
handlers: handlers,
}
}

// This function creates a new router that listens for new connections in
// the designated port
/*
func NewRouter(port int) {
// New default Gin router
router := gin.Default()
Expand All @@ -23,19 +132,10 @@ func NewRouter(port int) {
api := router.Group("/api")
{
// Proxies
api.GET("/proxies", getProxies)
api.GET("/proxies", nil)
}
router.NoRoute(func(ctx *gin.Context) { ctx.JSON(http.StatusNotFound, gin.H{}) })
router.Run(fmt.Sprintf(":%d", port))
}

func getProxies(ctx *gin.Context) {
// List of proxies
var proxies []Proxy

// Send the response with the proxies
ctx.Header("Content-Type", "application/json")
ctx.JSON(http.StatusOK, proxies)

}
*/
27 changes: 21 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,39 @@ module github.com/riotpot
go 1.18

require (
github.com/buger/goterm v1.0.4
github.com/gobuffalo/packr v1.30.1
github.com/google/uuid v1.3.0
github.com/plgd-dev/go-coap/v2 v2.6.0
github.com/stretchr/testify v1.7.1
github.com/traetox/pty v0.0.0-20141209045113-df6c8cd2e0e6
github.com/xiegeo/modbusone v0.2.4-0.20200428173500-797d647e237d
go.mongodb.org/mongo-driver v1.9.1
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

require (
github.com/dsnet/golib/memfile v1.0.0 // indirect
github.com/gin-gonic/gin v1.8.1
github.com/go-stack/stack v1.8.1 // indirect
github.com/gobuffalo/envy v1.10.1 // indirect
github.com/gobuffalo/packd v1.0.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand All @@ -30,12 +45,12 @@ require (
github.com/pion/udp v0.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/plgd-dev/kit/v2 v2.0.0-20211006190727-057b33161b90 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
Expand Down
Loading

0 comments on commit a221f04

Please sign in to comment.