2020-01-30 06:45:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/danielkov/gin-helmet"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
|
|
"github.com/gin-contrib/static"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/api"
|
2020-02-19 07:19:16 +00:00
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/core"
|
2020-01-30 06:45:49 +00:00
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.SetFormatter(&log.TextFormatter{})
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-02-25 08:45:44 +00:00
|
|
|
log.Infof("Starting Wg Gen Web version: %s", util.Version)
|
2020-02-25 06:56:04 +00:00
|
|
|
|
2020-01-30 06:45:49 +00:00
|
|
|
// load .env environment variables
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
2020-03-18 06:57:15 +00:00
|
|
|
}).Fatal("failed to load .env file")
|
2020-01-30 06:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check directories or create it
|
|
|
|
if !util.DirectoryExists(filepath.Join(os.Getenv("WG_CONF_DIR"))) {
|
|
|
|
err = os.Mkdir(filepath.Join(os.Getenv("WG_CONF_DIR")), 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
"dir": filepath.Join(os.Getenv("WG_CONF_DIR")),
|
2020-03-18 06:57:15 +00:00
|
|
|
}).Fatal("failed to create directory")
|
2020-01-30 06:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 06:57:15 +00:00
|
|
|
// check if server.json exists otherwise create it with default values
|
2020-02-20 02:08:47 +00:00
|
|
|
if !util.FileExists(filepath.Join(os.Getenv("WG_CONF_DIR"), "server.json")) {
|
|
|
|
_, err = core.ReadServer()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Fatal("server.json doesnt not exists and can not read it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:19:24 +00:00
|
|
|
if os.Getenv("GIN_MODE") == "debug" {
|
2020-02-06 06:07:15 +00:00
|
|
|
// set gin release debug
|
2020-02-03 07:19:24 +00:00
|
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
} else {
|
2020-01-30 06:45:49 +00:00
|
|
|
// set gin release mode
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
// disable console color
|
|
|
|
gin.DisableConsoleColor()
|
2020-02-25 06:56:04 +00:00
|
|
|
// log level info
|
|
|
|
log.SetLevel(log.InfoLevel)
|
2020-01-30 06:45:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 07:19:16 +00:00
|
|
|
// migrate
|
2020-03-18 06:57:15 +00:00
|
|
|
err = core.MigrateInitialStructChange()
|
2020-02-19 07:19:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
2020-03-18 06:57:15 +00:00
|
|
|
}).Fatal("failed to migrate initial struct changes")
|
|
|
|
}
|
|
|
|
err = core.MigratePresharedKey()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Fatal("failed to migrate preshared key struct changes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump wg config file
|
|
|
|
err = core.UpdateServerConfigWg()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Fatal("failed to dump wg config file")
|
2020-02-19 07:19:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 06:45:49 +00:00
|
|
|
// creates a gin router with default middleware: logger and recovery (crash-free) middleware
|
|
|
|
app := gin.Default()
|
|
|
|
|
2020-03-18 06:57:15 +00:00
|
|
|
// cors middleware
|
2020-01-30 06:45:49 +00:00
|
|
|
config := cors.DefaultConfig()
|
|
|
|
config.AllowAllOrigins = true
|
|
|
|
app.Use(cors.New(config))
|
|
|
|
|
2020-03-18 06:57:15 +00:00
|
|
|
// protection middleware
|
2020-01-30 06:45:49 +00:00
|
|
|
app.Use(helmet.Default())
|
|
|
|
|
|
|
|
// no route redirect to frontend app
|
|
|
|
app.NoRoute(func(c *gin.Context) {
|
|
|
|
c.Redirect(301, "/index.html")
|
|
|
|
})
|
|
|
|
|
|
|
|
// serve static files
|
|
|
|
app.Use(static.Serve("/", static.LocalFile("./ui/dist", false)))
|
|
|
|
|
|
|
|
// apply api router
|
|
|
|
api.ApplyRoutes(app)
|
|
|
|
|
|
|
|
err = app.Run(fmt.Sprintf("%s:%s", os.Getenv("SERVER"), os.Getenv("PORT")))
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Fatal("failed to start server")
|
|
|
|
}
|
|
|
|
}
|