mirror of
https://github.com/vx3r/wg-gen-web.git
synced 2024-11-04 17:31:17 +00:00
114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/auth"
|
||
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/core"
|
||
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
|
||
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/template"
|
||
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/version"
|
||
|
"golang.org/x/oauth2"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
// ApplyRoutes applies router to gin Router
|
||
|
func ApplyRoutes(r *gin.RouterGroup) {
|
||
|
g := r.Group("/server")
|
||
|
{
|
||
|
g.GET("", readServer)
|
||
|
g.PATCH("", updateServer)
|
||
|
g.GET("/config", configServer)
|
||
|
g.GET("/version", versionStr)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func readServer(c *gin.Context) {
|
||
|
client, err := core.ReadServer()
|
||
|
if err != nil {
|
||
|
log.WithFields(log.Fields{
|
||
|
"err": err,
|
||
|
}).Error("failed to read client")
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, client)
|
||
|
}
|
||
|
|
||
|
func updateServer(c *gin.Context) {
|
||
|
var data model.Server
|
||
|
|
||
|
if err := c.ShouldBindJSON(&data); err != nil {
|
||
|
log.WithFields(log.Fields{
|
||
|
"err": err,
|
||
|
}).Error("failed to bind")
|
||
|
c.AbortWithStatus(http.StatusUnprocessableEntity)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// get update user from token and add to server infos
|
||
|
oauth2Token := c.MustGet("oauth2Token").(*oauth2.Token)
|
||
|
oauth2Client := c.MustGet("oauth2Client").(auth.Auth)
|
||
|
user, err := oauth2Client.UserInfo(oauth2Token)
|
||
|
if err != nil {
|
||
|
log.WithFields(log.Fields{
|
||
|
"oauth2Token": oauth2Token,
|
||
|
"err": err,
|
||
|
}).Error("failed to get user with oauth token")
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
data.UpdatedBy = user.Name
|
||
|
|
||
|
server, err := core.UpdateServer(&data)
|
||
|
if err != nil {
|
||
|
log.WithFields(log.Fields{
|
||
|
"err": err,
|
||
|
}).Error("failed to update client")
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, server)
|
||
|
}
|
||
|
|
||
|
func configServer(c *gin.Context) {
|
||
|
clients, err := core.ReadClients()
|
||
|
if err != nil {
|
||
|
log.WithFields(log.Fields{
|
||
|
"err": err,
|
||
|
}).Error("failed to read clients")
|
||
|
c.AbortWithStatus(http.StatusUnprocessableEntity)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
server, err := core.ReadServer()
|
||
|
if err != nil {
|
||
|
log.WithFields(log.Fields{
|
||
|
"err": err,
|
||
|
}).Error("failed to read server")
|
||
|
c.AbortWithStatus(http.StatusUnprocessableEntity)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
configData, err := template.DumpServerWg(clients, server)
|
||
|
if err != nil {
|
||
|
log.WithFields(log.Fields{
|
||
|
"err": err,
|
||
|
}).Error("failed to dump wg config")
|
||
|
c.AbortWithStatus(http.StatusUnprocessableEntity)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// return config as txt file
|
||
|
c.Header("Content-Disposition", "attachment; filename=wg0.conf")
|
||
|
c.Data(http.StatusOK, "application/config", configData)
|
||
|
}
|
||
|
|
||
|
func versionStr(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"version": version.Version,
|
||
|
})
|
||
|
}
|