Fixes security issue & Adds support to sent configuration via email (#83)

This commit is contained in:
Georgios Komninos 2021-08-08 20:55:59 +03:00 committed by GitHub
parent 7edcd1b80c
commit 1711530dda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 335 additions and 76 deletions

55
main.go
View file

@ -4,10 +4,13 @@ import (
"flag"
"fmt"
"net/http"
"os"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/labstack/echo/v4"
"github.com/ngoduykhanh/wireguard-ui/emailer"
"github.com/ngoduykhanh/wireguard-ui/handler"
"github.com/ngoduykhanh/wireguard-ui/router"
"github.com/ngoduykhanh/wireguard-ui/util"
@ -21,6 +24,15 @@ var (
buildTime = fmt.Sprintf(time.Now().UTC().Format("01-02-2006 15:04:05"))
)
const (
defaultEmailSubject = "Your wireguard configuration"
defaultEmailContent = `Hi,</br>
<p>in this email you can file your personal configuration for our wireguard server.</p>
<p>Best</p>
`
)
func init() {
// command-line flags
flagDisableLogin := flag.Bool("disable-login", false, "Disable login page. Turn off authentication.")
@ -30,6 +42,10 @@ func init() {
// update runtime config
util.DisableLogin = *flagDisableLogin
util.BindAddress = *flagBindAddress
util.SendgridApiKey = os.Getenv("SENDGRID_API_KEY")
util.EmailFrom = os.Getenv("EMAIL_FROM")
util.EmailFromName = os.Getenv("EMAIL_FROM_NAME")
util.SessionSecret = []byte(os.Getenv("SESSION_SECRET"))
// print app information
fmt.Println("Wireguard UI")
@ -60,31 +76,34 @@ func main() {
assetHandler := http.FileServer(rice.MustFindBox("assets").HTTPBox())
// register routes
app := router.New(tmplBox, extraData)
app := router.New(tmplBox, extraData, util.SessionSecret)
app.GET("/", handler.WireGuardClients())
app.GET("/", handler.WireGuardClients(), handler.ValidSession)
if !util.DisableLogin {
app.GET("/login", handler.LoginPage())
app.POST("/login", handler.Login())
}
app.GET("/logout", handler.Logout())
app.POST("/new-client", handler.NewClient())
app.POST("/update-client", handler.UpdateClient())
app.POST("/client/set-status", handler.SetClientStatus())
app.POST("/remove-client", handler.RemoveClient())
app.GET("/download", handler.DownloadClient())
app.GET("/wg-server", handler.WireGuardServer())
app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces())
app.POST("wg-server/keypair", handler.WireGuardServerKeyPair())
app.GET("/global-settings", handler.GlobalSettings())
app.POST("/global-settings", handler.GlobalSettingSubmit())
app.GET("/api/clients", handler.GetClients())
app.GET("/api/client/:id", handler.GetClient())
app.GET("/api/machine-ips", handler.MachineIPAddresses())
app.GET("/api/suggest-client-ips", handler.SuggestIPAllocation())
app.GET("/api/apply-wg-config", handler.ApplyServerConfig(tmplBox))
sendmail := emailer.NewSendgridApiMail(util.SendgridApiKey, util.EmailFromName, util.EmailFrom)
app.GET("/logout", handler.Logout(), handler.ValidSession)
app.POST("/new-client", handler.NewClient(), handler.ValidSession)
app.POST("/update-client", handler.UpdateClient(), handler.ValidSession)
app.POST("/email-client", handler.EmailClient(sendmail, defaultEmailSubject, defaultEmailContent), handler.ValidSession)
app.POST("/client/set-status", handler.SetClientStatus(), handler.ValidSession)
app.POST("/remove-client", handler.RemoveClient(), handler.ValidSession)
app.GET("/download", handler.DownloadClient(), handler.ValidSession)
app.GET("/wg-server", handler.WireGuardServer(), handler.ValidSession)
app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces(), handler.ValidSession)
app.POST("wg-server/keypair", handler.WireGuardServerKeyPair(), handler.ValidSession)
app.GET("/global-settings", handler.GlobalSettings(), handler.ValidSession)
app.POST("/global-settings", handler.GlobalSettingSubmit(), handler.ValidSession)
app.GET("/api/clients", handler.GetClients(), handler.ValidSession)
app.GET("/api/client/:id", handler.GetClient(), handler.ValidSession)
app.GET("/api/machine-ips", handler.MachineIPAddresses(), handler.ValidSession)
app.GET("/api/suggest-client-ips", handler.SuggestIPAllocation(), handler.ValidSession)
app.GET("/api/apply-wg-config", handler.ApplyServerConfig(tmplBox), handler.ValidSession)
// servers other static files
app.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))