2020-04-18 11:17:49 +02:00
package main
import (
2020-10-09 23:33:29 +02:00
"flag"
2020-04-23 13:01:40 +02:00
"fmt"
2022-03-20 10:03:27 +01:00
"github.com/labstack/echo/v4"
2020-10-09 23:33:29 +02:00
"net/http"
"time"
2020-05-21 10:51:24 +02:00
rice "github.com/GeertJohan/go.rice"
2021-08-08 19:55:59 +02:00
"github.com/ngoduykhanh/wireguard-ui/emailer"
2020-04-18 11:17:49 +02:00
"github.com/ngoduykhanh/wireguard-ui/handler"
"github.com/ngoduykhanh/wireguard-ui/router"
2021-08-28 15:10:30 +02:00
"github.com/ngoduykhanh/wireguard-ui/store/jsondb"
2020-04-23 13:01:40 +02:00
"github.com/ngoduykhanh/wireguard-ui/util"
2020-04-18 11:17:49 +02:00
)
2020-10-09 23:33:29 +02:00
var (
2021-08-29 20:26:12 +02:00
// command-line banner information
2020-10-09 23:33:29 +02:00
appVersion = "development"
gitCommit = "N/A"
gitRef = "N/A"
buildTime = fmt . Sprintf ( time . Now ( ) . UTC ( ) . Format ( "01-02-2006 15:04:05" ) )
2021-08-29 20:26:12 +02:00
// configuration variables
flagDisableLogin bool = false
flagBindAddress string = "0.0.0.0:5000"
2022-01-29 09:01:37 +01:00
flagSmtpHostname string = "127.0.0.1"
flagSmtpPort int = 25
flagSmtpUsername string
flagSmtpPassword string
flagSmtpAuthType string = "None"
flagSmtpNoTLSCheck bool = false
2021-08-29 20:26:12 +02:00
flagSendgridApiKey string
flagEmailFrom string
flagEmailFromName string = "WireGuard UI"
flagSessionSecret string
2022-04-24 09:42:08 +02:00
flagWgConfTemplate string
2022-04-25 09:17:13 +02:00
flagBasePath string
2020-10-09 23:33:29 +02:00
)
2021-08-08 19:55:59 +02:00
const (
defaultEmailSubject = "Your wireguard configuration"
defaultEmailContent = ` Hi , < / br >
2021-11-09 11:01:42 +01:00
< p > In this email you can find your personal configuration for our wireguard server . < / p >
2021-08-08 19:55:59 +02:00
< p > Best < / p >
`
)
2020-10-09 23:33:29 +02:00
func init ( ) {
2021-08-29 20:26:12 +02:00
// command-line flags and env variables
2021-11-12 21:17:18 +01:00
flag . BoolVar ( & flagDisableLogin , "disable-login" , util . LookupEnvOrBool ( "DISABLE_LOGIN" , flagDisableLogin ) , "Disable authentication on the app. This is potentially dangerous." )
2021-08-29 20:26:12 +02:00
flag . StringVar ( & flagBindAddress , "bind-address" , util . LookupEnvOrString ( "BIND_ADDRESS" , flagBindAddress ) , "Address:Port to which the app will be bound." )
2022-01-29 09:01:37 +01:00
flag . StringVar ( & flagSmtpHostname , "smtp-hostname" , util . LookupEnvOrString ( "SMTP_HOSTNAME" , flagSmtpHostname ) , "SMTP Hostname" )
flag . IntVar ( & flagSmtpPort , "smtp-port" , util . LookupEnvOrInt ( "SMTP_PORT" , flagSmtpPort ) , "SMTP Port" )
flag . StringVar ( & flagSmtpUsername , "smtp-username" , util . LookupEnvOrString ( "SMTP_USERNAME" , flagSmtpUsername ) , "SMTP Password" )
flag . StringVar ( & flagSmtpPassword , "smtp-password" , util . LookupEnvOrString ( "SMTP_PASSWORD" , flagSmtpPassword ) , "SMTP Password" )
flag . BoolVar ( & flagSmtpNoTLSCheck , "smtp-no-tls-check" , util . LookupEnvOrBool ( "SMTP_NO_TLS_CHECK" , flagSmtpNoTLSCheck ) , "Disable TLS verification for SMTP. This is potentially dangerous." )
flag . StringVar ( & flagSmtpAuthType , "smtp-auth-type" , util . LookupEnvOrString ( "SMTP_AUTH_TYPE" , flagSmtpAuthType ) , "SMTP Auth Type : Plain or None." )
2021-08-29 20:26:12 +02:00
flag . StringVar ( & flagSendgridApiKey , "sendgrid-api-key" , util . LookupEnvOrString ( "SENDGRID_API_KEY" , flagSendgridApiKey ) , "Your sendgrid api key." )
flag . StringVar ( & flagEmailFrom , "email-from" , util . LookupEnvOrString ( "EMAIL_FROM_ADDRESS" , flagEmailFrom ) , "'From' email address." )
flag . StringVar ( & flagEmailFromName , "email-from-name" , util . LookupEnvOrString ( "EMAIL_FROM_NAME" , flagEmailFromName ) , "'From' email name." )
flag . StringVar ( & flagSessionSecret , "session-secret" , util . LookupEnvOrString ( "SESSION_SECRET" , flagSessionSecret ) , "The key used to encrypt session cookies." )
2022-04-24 09:42:08 +02:00
flag . StringVar ( & flagWgConfTemplate , "wg-conf-template" , util . LookupEnvOrString ( "WG_CONF_TEMPLATE" , flagWgConfTemplate ) , "Path to custom wg.conf template." )
2022-04-25 09:17:13 +02:00
flag . StringVar ( & flagBasePath , "base-path" , util . LookupEnvOrString ( "BASE_PATH" , flagBasePath ) , "The base path of the URL" )
2020-10-09 23:33:29 +02:00
flag . Parse ( )
// update runtime config
2021-08-29 20:26:12 +02:00
util . DisableLogin = flagDisableLogin
util . BindAddress = flagBindAddress
2022-01-29 09:01:37 +01:00
util . SmtpHostname = flagSmtpHostname
util . SmtpPort = flagSmtpPort
util . SmtpUsername = flagSmtpUsername
util . SmtpPassword = flagSmtpPassword
util . SmtpAuthType = flagSmtpAuthType
util . SmtpNoTLSCheck = flagSmtpNoTLSCheck
2021-08-29 20:26:12 +02:00
util . SendgridApiKey = flagSendgridApiKey
util . EmailFrom = flagEmailFrom
util . EmailFromName = flagEmailFromName
util . SessionSecret = [ ] byte ( flagSessionSecret )
2022-04-24 09:42:08 +02:00
util . WgConfTemplate = flagWgConfTemplate
2022-04-25 09:17:13 +02:00
util . BasePath = util . ParseBasePath ( flagBasePath )
2020-06-01 08:03:10 +02:00
// print app information
fmt . Println ( "Wireguard UI" )
fmt . Println ( "App Version\t:" , appVersion )
fmt . Println ( "Git Commit\t:" , gitCommit )
fmt . Println ( "Git Ref\t\t:" , gitRef )
fmt . Println ( "Build Time\t:" , buildTime )
fmt . Println ( "Git Repo\t:" , "https://github.com/ngoduykhanh/wireguard-ui" )
2020-10-09 23:33:29 +02:00
fmt . Println ( "Authentication\t:" , ! util . DisableLogin )
2020-10-10 16:01:35 +02:00
fmt . Println ( "Bind address\t:" , util . BindAddress )
2021-08-29 20:26:12 +02:00
//fmt.Println("Sendgrid key\t:", util.SendgridApiKey)
fmt . Println ( "Email from\t:" , util . EmailFrom )
fmt . Println ( "Email from name\t:" , util . EmailFromName )
//fmt.Println("Session secret\t:", util.SessionSecret)
2022-04-24 09:42:08 +02:00
fmt . Println ( "Custom wg.conf\t:" , util . WgConfTemplate )
2022-04-25 09:17:13 +02:00
fmt . Println ( "Base path\t:" , util . BasePath + "/" )
2020-10-09 23:33:29 +02:00
}
func main ( ) {
2021-08-28 15:10:30 +02:00
db , err := jsondb . New ( "./db" )
if err != nil {
panic ( err )
}
if err := db . Init ( ) ; err != nil {
panic ( err )
}
2020-10-09 23:33:29 +02:00
// set app extra data
extraData := make ( map [ string ] string )
extraData [ "appVersion" ] = appVersion
2022-04-25 09:17:13 +02:00
extraData [ "basePath" ] = util . BasePath
2020-04-23 13:01:40 +02:00
2020-05-21 10:51:24 +02:00
// create rice box for embedded template
tmplBox := rice . MustFindBox ( "templates" )
// rice file server for assets. "assets" is the folder where the files come from.
assetHandler := http . FileServer ( rice . MustFindBox ( "assets" ) . HTTPBox ( ) )
2020-04-23 13:01:40 +02:00
// register routes
2021-08-08 19:55:59 +02:00
app := router . New ( tmplBox , extraData , util . SessionSecret )
2020-04-18 11:17:49 +02:00
2022-04-25 09:17:13 +02:00
app . GET ( util . BasePath , handler . WireGuardClients ( db ) , handler . ValidSession )
2020-10-09 23:33:29 +02:00
if ! util . DisableLogin {
2022-04-25 09:17:13 +02:00
app . GET ( util . BasePath + "/login" , handler . LoginPage ( ) )
app . POST ( util . BasePath + "/login" , handler . Login ( db ) )
2020-10-09 23:33:29 +02:00
}
2022-01-29 09:01:37 +01:00
var sendmail emailer . Emailer
if util . SendgridApiKey != "" {
sendmail = emailer . NewSendgridApiMail ( util . SendgridApiKey , util . EmailFromName , util . EmailFrom )
} else {
sendmail = emailer . NewSmtpMail ( util . SmtpHostname , util . SmtpPort , util . SmtpUsername , util . SmtpPassword , util . SmtpNoTLSCheck , util . SmtpAuthType , util . EmailFromName , util . EmailFrom )
}
2021-08-08 19:55:59 +02:00
2022-04-25 09:17:13 +02:00
app . GET ( util . BasePath + "/_health" , handler . Health ( ) )
app . GET ( util . BasePath + "/logout" , handler . Logout ( ) , handler . ValidSession )
app . POST ( util . BasePath + "/new-client" , handler . NewClient ( db ) , handler . ValidSession )
app . POST ( util . BasePath + "/update-client" , handler . UpdateClient ( db ) , handler . ValidSession )
app . POST ( util . BasePath + "/email-client" , handler . EmailClient ( db , sendmail , defaultEmailSubject , defaultEmailContent ) , handler . ValidSession )
app . POST ( util . BasePath + "/client/set-status" , handler . SetClientStatus ( db ) , handler . ValidSession )
app . POST ( util . BasePath + "/remove-client" , handler . RemoveClient ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/download" , handler . DownloadClient ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/wg-server" , handler . WireGuardServer ( db ) , handler . ValidSession )
app . POST ( util . BasePath + "/wg-server/interfaces" , handler . WireGuardServerInterfaces ( db ) , handler . ValidSession )
app . POST ( util . BasePath + "/wg-server/keypair" , handler . WireGuardServerKeyPair ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/global-settings" , handler . GlobalSettings ( db ) , handler . ValidSession )
app . POST ( util . BasePath + "/global-settings" , handler . GlobalSettingSubmit ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/status" , handler . Status ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/api/clients" , handler . GetClients ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/api/client/:id" , handler . GetClient ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/api/machine-ips" , handler . MachineIPAddresses ( ) , handler . ValidSession )
app . GET ( util . BasePath + "/api/suggest-client-ips" , handler . SuggestIPAllocation ( db ) , handler . ValidSession )
app . GET ( util . BasePath + "/api/apply-wg-config" , handler . ApplyServerConfig ( db , tmplBox ) , handler . ValidSession )
app . GET ( util . BasePath + "/wake_on_lan_hosts" , handler . GetWakeOnLanHosts ( db ) , handler . ValidSession )
app . POST ( util . BasePath + "/wake_on_lan_host" , handler . SaveWakeOnLanHost ( db ) , handler . ValidSession )
app . DELETE ( util . BasePath + "/wake_on_lan_host/:mac_address" , handler . DeleteWakeOnHost ( db ) , handler . ValidSession )
app . PUT ( util . BasePath + "/wake_on_lan_host/:mac_address" , handler . WakeOnHost ( db ) , handler . ValidSession )
2020-05-21 10:51:24 +02:00
// servers other static files
2022-04-25 09:17:13 +02:00
app . GET ( util . BasePath + "/static/*" , echo . WrapHandler ( http . StripPrefix ( util . BasePath + "/static/" , assetHandler ) ) )
2020-05-21 10:51:24 +02:00
2020-10-10 16:01:35 +02:00
app . Logger . Fatal ( app . Start ( util . BindAddress ) )
2020-04-18 11:17:49 +02:00
}