Skip to content

Commit a221f04

Browse files
committed
Added proxy endpoints and API design [#67 #58 #63]
1 parent 268f736 commit a221f04

15 files changed

Lines changed: 604 additions & 190 deletions

File tree

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,10 @@ Note: the list will be updated on support for additional scanning sources.
8686
8787
## 2. Requirements
8888

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

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

9994
## 3. Installation
10095

@@ -114,7 +109,7 @@ Follow the steps to get the RIoTPot project first:
114109
$ git clone git@github.com:aau-network-security/riotpot.git
115110

116111
# 2. Navigate to the newly created directory with the repository
117-
$ cd RIoTPot
112+
$ cd riotpot
118113
```
119114

120115
### 3.1 Local Build

api/proxy/proxy.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package proxy
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"strconv"
7+
8+
"github.com/gin-gonic/gin"
9+
"github.com/riotpot/api"
10+
"github.com/riotpot/internal/proxy"
11+
"github.com/riotpot/pkg/services"
12+
)
13+
14+
// Structures used to serialize data
15+
type Proxy struct {
16+
ID int `json:"id" binding:"required" gorm:"primary_key"`
17+
Port int `json:"port" binding:"required"`
18+
Protocol string `json:"protocol" binding:"required"`
19+
Status bool `json:"status"`
20+
Service *Service `json:"service"`
21+
}
22+
23+
type Service struct {
24+
ID int `json:"id" binding:"required" gorm:"primary_key"`
25+
Port int `json:"port" binding:"required"`
26+
Name string `json:"name" binding:"required"`
27+
Host string `json:"host" binding:"required"`
28+
}
29+
30+
// Routes
31+
var (
32+
// Proxy Routes
33+
proxyRoutes = []api.Route{
34+
// Get proxies
35+
api.NewRoute("/", "GET", getProxies),
36+
// Get a proxy by port
37+
api.NewRoute("/proxy/:port", "GET", getProxy),
38+
// Post proxy by port
39+
api.NewRoute("/proxy/:port", "POST", postProxy),
40+
// Delete a proxy by port
41+
api.NewRoute("/proxy/:port", "DELETE", delProxy),
42+
// Patch (Not update) a proxy by port
43+
api.NewRoute("/proxy/:port", "PATCH", patchProxy),
44+
}
45+
)
46+
47+
// Routers
48+
var (
49+
ProxyRouter = api.NewRouter("/proxies", proxyRoutes, nil)
50+
)
51+
52+
func newService(serv services.Service) (sv *Service) {
53+
if serv != nil {
54+
sv = &Service{
55+
Port: serv.GetPort(),
56+
Name: serv.GetName(),
57+
Host: serv.GetName(),
58+
}
59+
}
60+
61+
return
62+
}
63+
64+
func newProxy(px proxy.Proxy) *Proxy {
65+
serv := newService(px.Service())
66+
67+
return &Proxy{
68+
Port: px.Port(),
69+
Protocol: px.Protocol(),
70+
Status: px.Alive(),
71+
Service: serv,
72+
}
73+
}
74+
75+
// GET proxies registered
76+
// Contains a filter to get proxies by port
77+
func getProxies(ctx *gin.Context) {
78+
casted := []Proxy{}
79+
80+
// Iterate through the proxies registered
81+
for _, px := range proxy.Proxies.Proxies() {
82+
// Serialize the proxy
83+
pr := newProxy(px)
84+
// Append the proxy tot he casted
85+
casted = append(casted, *pr)
86+
}
87+
88+
// Set the header and transform the struct to JSON format
89+
ctx.JSON(http.StatusOK, casted)
90+
91+
}
92+
93+
// GET proxy by port ":port"
94+
func getProxy(ctx *gin.Context) {
95+
port, err := strconv.Atoi(ctx.Param("port"))
96+
if err != nil {
97+
log.Fatal(err)
98+
}
99+
100+
// Get the proxy
101+
px, err := proxy.Proxies.GetProxy(port)
102+
103+
// If the proxy could not be found, let the user know
104+
if err != nil {
105+
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
106+
return
107+
}
108+
109+
// Serialize the proxy and send it as a response
110+
pr := newProxy(px)
111+
ctx.JSON(http.StatusOK, pr)
112+
}
113+
114+
// POST a proxy by port ":port"
115+
func postProxy(ctx *gin.Context) {}
116+
117+
// DELETE a proxy by port ":port"
118+
func delProxy(ctx *gin.Context) {}
119+
120+
// PATCH proxy by port ":port"
121+
// Can update port, protocol, status and service
122+
func patchProxy(ctx *gin.Context) {}
123+
124+
// Routers
125+
/*
126+
var (
127+
ProxyRouter = &api.AbstractRouter{
128+
path: "proxy",
129+
routes: []Route{
130+
GetProxies,
131+
GetProxy,
132+
},
133+
}
134+
)
135+
*/
136+
137+
/*
138+
func getProxies(ctx *gin.Context) {
139+
// List of proxies
140+
var proxies []Proxy
141+
142+
// Send the response with the proxies
143+
ctx.Header("Content-Type", "application/json")
144+
ctx.JSON(http.StatusOK, proxies)
145+
146+
}
147+
*/

api/router.go

Lines changed: 118 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,129 @@
11
package api
22

33
import (
4-
"fmt"
5-
"net/http"
6-
74
"github.com/gin-gonic/gin"
85
)
96

10-
// Proxy struct
11-
type Proxy struct {
12-
ID int `json:"id" binding:"required"`
13-
Port int `json:"port" binding:"required"`
7+
// Router interface
8+
type Router interface {
9+
// Returns the list of routes registered in the router
10+
Routes() []Route
11+
// Parent path for the router
12+
Path() string
13+
// Group the registered path
14+
Group(parentGroup *gin.RouterGroup) *gin.RouterGroup
15+
}
16+
17+
type AbstractRouter struct {
18+
// Inherits the Router
19+
Router
20+
// Routes registered in the router
21+
routes []Route
22+
// Parent path
23+
path string
24+
// Child Routers
25+
// A router may have child routers
26+
// For example:
27+
// - /v1
28+
// .. - /router1
29+
// .. .. - /r1
30+
// .. .. - /r2
31+
// .. - /router2
32+
childs []Router
33+
}
34+
35+
// Returns the parent path
36+
func (r *AbstractRouter) Path() string {
37+
return r.path
38+
}
39+
40+
// Returns the list of routes registered in the router
41+
func (r *AbstractRouter) Routes() []Route {
42+
return r.routes
43+
}
44+
45+
// Add the handlers to a router group
46+
func (r *AbstractRouter) addHandlers(parentGroup *gin.RouterGroup) *gin.RouterGroup {
47+
// Iterate the routes and add the handlers registered in the
48+
for _, route := range r.Routes() {
49+
parentGroup.Handle(route.Method(), route.Path(), route.Handlers()...)
50+
}
51+
52+
return parentGroup
53+
}
54+
55+
// Add all the child routers to a group
56+
func (r *AbstractRouter) addChilds(parentGroup *gin.RouterGroup) *gin.RouterGroup {
57+
// Iterate through the child routers to add the routes
58+
if len(r.childs) > 0 {
59+
for _, child := range r.childs {
60+
child.Group(parentGroup)
61+
}
62+
}
63+
64+
return parentGroup
65+
}
66+
67+
// Create the group routes inside of the router
68+
func (r *AbstractRouter) Group(parentGroup *gin.RouterGroup) *gin.RouterGroup {
69+
// Create a group inside of the parent group for this child
70+
childGroup := parentGroup.Group(r.Path())
71+
// Add the routes handlers for the current group
72+
childGroup = r.addHandlers(childGroup)
73+
// Add the child groups to the router
74+
childGroup = r.addChilds(childGroup)
75+
76+
return childGroup
77+
}
78+
79+
func NewRouter(path string, routes []Route, childs []Router) *AbstractRouter {
80+
return &AbstractRouter{
81+
path: path,
82+
routes: routes,
83+
childs: childs,
84+
}
85+
}
86+
87+
// Route that will be handled by the API
88+
type Route interface {
89+
// Raw function to handle the request
90+
Handlers() gin.HandlersChain
91+
// (Sub)Path to the route
92+
Path() string
93+
// Method used for the path
94+
Method() string
95+
}
96+
97+
type AbstractRoute struct {
98+
Route
99+
path string
100+
method string
101+
handlers gin.HandlersChain
102+
}
103+
104+
func (ar *AbstractRoute) Path() string {
105+
return ar.path
106+
}
107+
108+
func (ar *AbstractRoute) Method() string {
109+
return ar.method
110+
}
111+
112+
func (ar *AbstractRoute) Handlers() gin.HandlersChain {
113+
return ar.handlers
114+
}
115+
116+
func NewRoute(path string, method string, handlers ...gin.HandlerFunc) *AbstractRoute {
117+
return &AbstractRoute{
118+
path: path,
119+
method: method,
120+
handlers: handlers,
121+
}
14122
}
15123

16124
// This function creates a new router that listens for new connections in
17125
// the designated port
126+
/*
18127
func NewRouter(port int) {
19128
// New default Gin router
20129
router := gin.Default()
@@ -23,19 +132,10 @@ func NewRouter(port int) {
23132
api := router.Group("/api")
24133
{
25134
// Proxies
26-
api.GET("/proxies", getProxies)
135+
api.GET("/proxies", nil)
27136
}
28137
29138
router.NoRoute(func(ctx *gin.Context) { ctx.JSON(http.StatusNotFound, gin.H{}) })
30139
router.Run(fmt.Sprintf(":%d", port))
31140
}
32-
33-
func getProxies(ctx *gin.Context) {
34-
// List of proxies
35-
var proxies []Proxy
36-
37-
// Send the response with the proxies
38-
ctx.Header("Content-Type", "application/json")
39-
ctx.JSON(http.StatusOK, proxies)
40-
41-
}
141+
*/

go.mod

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@ module github.com/riotpot
33
go 1.18
44

55
require (
6-
github.com/buger/goterm v1.0.4
7-
github.com/gobuffalo/packr v1.30.1
86
github.com/google/uuid v1.3.0
97
github.com/plgd-dev/go-coap/v2 v2.6.0
8+
github.com/stretchr/testify v1.7.1
109
github.com/traetox/pty v0.0.0-20141209045113-df6c8cd2e0e6
1110
github.com/xiegeo/modbusone v0.2.4-0.20200428173500-797d647e237d
1211
go.mongodb.org/mongo-driver v1.9.1
1312
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
1413
gopkg.in/yaml.v3 v3.0.1
1514
)
1615

16+
require (
17+
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/gin-contrib/sse v0.1.0 // indirect
19+
github.com/go-playground/locales v0.14.0 // indirect
20+
github.com/go-playground/universal-translator v0.18.0 // indirect
21+
github.com/go-playground/validator/v10 v10.10.0 // indirect
22+
github.com/goccy/go-json v0.9.7 // indirect
23+
github.com/json-iterator/go v1.1.12 // indirect
24+
github.com/leodido/go-urn v1.2.1 // indirect
25+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
26+
github.com/modern-go/reflect2 v1.0.2 // indirect
27+
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
28+
github.com/pmezard/go-difflib v1.0.0 // indirect
29+
github.com/ugorji/go/codec v1.2.7 // indirect
30+
google.golang.org/protobuf v1.28.0 // indirect
31+
gopkg.in/yaml.v2 v2.4.0 // indirect
32+
)
33+
1734
require (
1835
github.com/dsnet/golib/memfile v1.0.0 // indirect
36+
github.com/gin-gonic/gin v1.8.1
1937
github.com/go-stack/stack v1.8.1 // indirect
20-
github.com/gobuffalo/envy v1.10.1 // indirect
21-
github.com/gobuffalo/packd v1.0.1 // indirect
2238
github.com/golang/snappy v0.0.4 // indirect
23-
github.com/joho/godotenv v1.4.0 // indirect
2439
github.com/klauspost/compress v1.15.6 // indirect
2540
github.com/mattn/go-colorable v0.1.12 // indirect
2641
github.com/mattn/go-isatty v0.0.14 // indirect
@@ -30,12 +45,12 @@ require (
3045
github.com/pion/udp v0.1.1 // indirect
3146
github.com/pkg/errors v0.9.1 // indirect
3247
github.com/plgd-dev/kit/v2 v2.0.0-20211006190727-057b33161b90 // indirect
33-
github.com/rogpeppe/go-internal v1.8.1 // indirect
3448
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
3549
github.com/xdg-go/scram v1.1.1 // indirect
3650
github.com/xdg-go/stringprep v1.0.3 // indirect
3751
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
3852
go.uber.org/atomic v1.9.0 // indirect
53+
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
3954
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect
4055
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
4156
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect

0 commit comments

Comments
 (0)