Skip to content

Commit

Permalink
api: add unit-openvpnrw API
Browse files Browse the repository at this point in the history
Store OpenVPN Road Warrior configuration to be used
inside the VPN dashboard
  • Loading branch information
gsanchietti committed Sep 18, 2024
1 parent a213de9 commit b37a303
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func main() {
reports.POST("/ovpnrw-connections", methods.UpdateOvpnConnections)
reports.POST("/dpi-stats", methods.UpdateDpiStats)
reports.POST("/unit-name", methods.SetUnitName)
reports.POST("/unit-openvpnrw", methods.SetUnitOpenVPNRW)
}

// handle missing endpoint
Expand Down
49 changes: 49 additions & 0 deletions api/methods/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,55 @@ func SetUnitName(c *gin.Context) {
}
}

func SetUnitOpenVPNRW(c *gin.Context) {
var req models.UnitOpenVPNRWRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, structs.Map(response.StatusBadRequest{
Code: 400,
Message: "Invalid request",
Data: err.Error(),
}))
return
}

if checkUnitId(c.MustGet("UnitId").(string)) != nil {
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Unit not found",
Data: nil,
Code: 404,
}))
return
}

dbpool, dbctx := storage.ReportInstance()

// Remove all previous data
_, err := dbpool.Exec(dbctx, "DELETE FROM openvpn_rw WHERE uuid = $1", c.MustGet("UnitId").(string))
if err != nil {
logs.Logs.Println("[ERR][UNITOPENVPNRW] error deleting previous data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Code: 500,
Message: "Error deleting previous data",
Data: err.Error(),
}))
return
}

// insert inside OpenVPN table
for _, server := range req.Data {
_, err := dbpool.Exec(dbctx, "INSERT INTO openvpn_rw (uuid, instance, name, device) VALUES ($1, $2, $3, $4)", c.MustGet("UnitId").(string), server.Instance, server.Name, server.Device)
if err != nil {
logs.Logs.Println("[ERR][UNITOPENVPNRW] error inserting data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Code: 500,
Message: "Error inserting data",
Data: err.Error(),
}))
return
}
}
}

func checkUnitId(unitId string) error {
if unitId == "" {
return errors.New("uuid is empty")
Expand Down
12 changes: 12 additions & 0 deletions api/models/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ type DpiStatsRequest struct {
type UnitNameRequest struct {
Name string `json:"name" binding:"required"`
}

type OpenVPNRWServer struct {
Instance string `json:"instance" binding:"required"`
Name string `json:"name" binding:"required"`
Device string `json:"device" binding:"required"`
}

type OpenVPNRWServers []OpenVPNRWServer

type UnitOpenVPNRWRequest struct {
Data OpenVPNRWServers `json:"data" binding:"required"`
}
8 changes: 8 additions & 0 deletions api/storage/report_schema.sql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ CREATE TABLE IF NOT EXISTS units (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS openvpn_rw (
uuid UUID NOT NULL references units(uuid),
instance TEXT NOT NULL,
name TEXT,
device TEXT NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- General retention policies

-- Keep raw data for 30 days
Expand Down

0 comments on commit b37a303

Please sign in to comment.