Skip to content

Commit

Permalink
feat(api): add unit-openvpn API
Browse files Browse the repository at this point in the history
Store OpenVPN configuration to be used
inside the VPN dashboard
  • Loading branch information
gsanchietti committed Sep 18, 2024
1 parent a213de9 commit 4a9667b
Show file tree
Hide file tree
Showing 4 changed files with 72 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_config 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_config (uuid, instance, name, device, type) VALUES ($1, $2, $3, $4, $5)", c.MustGet("UnitId").(string), server.Instance, server.Name, server.Device, server.Type)
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
13 changes: 13 additions & 0 deletions api/models/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,16 @@ type DpiStatsRequest struct {
type UnitNameRequest struct {
Name string `json:"name" binding:"required"`
}

type OpenVPNConfiguration struct {
Instance string `json:"instance" binding:"required"`
Name string `json:"name" binding:"required"`
Device string `json:"device" binding:"required"`
Type string `json:"type"` // valid values are: rw (for roadwarrior), client (for tunnel client), server (for tunnel server)
}

type OpenVPNConfigurations []OpenVPNConfiguration

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

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

-- General retention policies

-- Keep raw data for 30 days
Expand Down

0 comments on commit 4a9667b

Please sign in to comment.