2020-04-18 09:17:49 +00:00
package main
import (
2024-01-06 08:11:20 +00:00
"crypto/sha512"
2023-03-16 07:40:04 +00:00
"embed"
2020-10-09 21:33:29 +00:00
"flag"
2020-04-23 11:01:40 +00:00
"fmt"
2023-03-16 07:40:04 +00:00
"io/fs"
2023-12-29 09:13:30 +00:00
"net"
2020-10-09 21:33:29 +00:00
"net/http"
2022-07-14 06:40:16 +00:00
"os"
2023-12-25 19:31:11 +00:00
"strings"
"syscall"
2020-10-09 21:33:29 +00:00
"time"
2023-12-25 18:58:31 +00:00
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/ngoduykhanh/wireguard-ui/store"
2023-12-29 08:22:12 +00:00
"github.com/ngoduykhanh/wireguard-ui/telegram"
2023-12-25 18:58:31 +00:00
2021-08-08 17:55:59 +00:00
"github.com/ngoduykhanh/wireguard-ui/emailer"
2020-04-18 09:17:49 +00:00
"github.com/ngoduykhanh/wireguard-ui/handler"
"github.com/ngoduykhanh/wireguard-ui/router"
2021-08-28 13:10:30 +00:00
"github.com/ngoduykhanh/wireguard-ui/store/jsondb"
2020-04-23 11:01:40 +00:00
"github.com/ngoduykhanh/wireguard-ui/util"
2020-04-18 09:17:49 +00:00
)
2020-10-09 21:33:29 +00:00
var (
2021-08-29 18:26:12 +00:00
// command-line banner information
2020-10-09 21:33:29 +00: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 18:26:12 +00:00
// configuration variables
2023-12-29 09:56:37 +00:00
flagDisableLogin = false
flagBindAddress = "0.0.0.0:5000"
flagSmtpHostname = "127.0.0.1"
flagSmtpPort = 25
2023-12-29 08:22:12 +00:00
flagSmtpUsername string
flagSmtpPassword string
2023-12-29 09:56:37 +00:00
flagSmtpAuthType = "NONE"
flagSmtpNoTLSCheck = false
flagSmtpEncryption = "STARTTLS"
flagSmtpHelo = "localhost"
2023-12-29 08:22:12 +00:00
flagSendgridApiKey string
flagEmailFrom string
2023-12-29 09:56:37 +00:00
flagEmailFromName = "WireGuard UI"
2023-12-29 08:22:12 +00:00
flagTelegramToken string
2023-12-29 09:56:37 +00:00
flagTelegramAllowConfRequest = false
flagTelegramFloodWait = 60
flagSessionSecret = util . RandomString ( 32 )
2024-01-06 08:11:20 +00:00
flagSessionMaxDuration = 90
2023-12-29 08:22:12 +00:00
flagWgConfTemplate string
flagBasePath string
flagSubnetRanges string
2020-10-09 21:33:29 +00:00
)
2021-08-08 17:55:59 +00:00
const (
defaultEmailSubject = "Your wireguard configuration"
defaultEmailContent = ` Hi , < / br >
2021-11-09 10:01:42 +00:00
< p > In this email you can find your personal configuration for our wireguard server . < / p >
2021-08-08 17:55:59 +00:00
< p > Best < / p >
`
)
2023-03-16 07:40:04 +00:00
// embed the "templates" directory
//
//go:embed templates/*
var embeddedTemplates embed . FS
// embed the "assets" directory
//
//go:embed assets/*
var embeddedAssets embed . FS
2020-10-09 21:33:29 +00:00
func init ( ) {
2021-08-29 18:26:12 +00:00
// command-line flags and env variables
2021-11-12 20:17:18 +00:00
flag . BoolVar ( & flagDisableLogin , "disable-login" , util . LookupEnvOrBool ( "DISABLE_LOGIN" , flagDisableLogin ) , "Disable authentication on the app. This is potentially dangerous." )
2021-08-29 18:26:12 +00:00
flag . StringVar ( & flagBindAddress , "bind-address" , util . LookupEnvOrString ( "BIND_ADDRESS" , flagBindAddress ) , "Address:Port to which the app will be bound." )
2022-01-29 08:01:37 +00: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" )
2023-12-27 21:42:31 +00:00
flag . StringVar ( & flagSmtpHelo , "smtp-helo" , util . LookupEnvOrString ( "SMTP_HELO" , flagSmtpHelo ) , "SMTP HELO Hostname" )
2022-09-30 08:21:20 +00:00
flag . StringVar ( & flagSmtpUsername , "smtp-username" , util . LookupEnvOrString ( "SMTP_USERNAME" , flagSmtpUsername ) , "SMTP Username" )
2022-01-29 08:01:37 +00:00
flag . BoolVar ( & flagSmtpNoTLSCheck , "smtp-no-tls-check" , util . LookupEnvOrBool ( "SMTP_NO_TLS_CHECK" , flagSmtpNoTLSCheck ) , "Disable TLS verification for SMTP. This is potentially dangerous." )
2023-02-16 16:06:45 +00:00
flag . StringVar ( & flagSmtpEncryption , "smtp-encryption" , util . LookupEnvOrString ( "SMTP_ENCRYPTION" , flagSmtpEncryption ) , "SMTP Encryption : NONE, SSL, SSLTLS, TLS or STARTTLS (by default)" )
2022-12-13 21:44:11 +00:00
flag . StringVar ( & flagSmtpAuthType , "smtp-auth-type" , util . LookupEnvOrString ( "SMTP_AUTH_TYPE" , flagSmtpAuthType ) , "SMTP Auth Type : PLAIN, LOGIN or NONE." )
2021-08-29 18:26:12 +00:00
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." )
2023-12-29 08:22:12 +00:00
flag . StringVar ( & flagTelegramToken , "telegram-token" , util . LookupEnvOrString ( "TELEGRAM_TOKEN" , flagTelegramToken ) , "Telegram bot token for distributing configs to clients." )
flag . BoolVar ( & flagTelegramAllowConfRequest , "telegram-allow-conf-request" , util . LookupEnvOrBool ( "TELEGRAM_ALLOW_CONF_REQUEST" , flagTelegramAllowConfRequest ) , "Allow users to get configs from the bot by sending a message." )
flag . IntVar ( & flagTelegramFloodWait , "telegram-flood-wait" , util . LookupEnvOrInt ( "TELEGRAM_FLOOD_WAIT" , flagTelegramFloodWait ) , "Time in minutes before the next conf request is processed." )
2022-04-24 07:42:08 +00:00
flag . StringVar ( & flagWgConfTemplate , "wg-conf-template" , util . LookupEnvOrString ( "WG_CONF_TEMPLATE" , flagWgConfTemplate ) , "Path to custom wg.conf template." )
2022-04-25 07:17:13 +00:00
flag . StringVar ( & flagBasePath , "base-path" , util . LookupEnvOrString ( "BASE_PATH" , flagBasePath ) , "The base path of the URL" )
2023-12-27 08:08:55 +00:00
flag . StringVar ( & flagSubnetRanges , "subnet-ranges" , util . LookupEnvOrString ( "SUBNET_RANGES" , flagSubnetRanges ) , "IP ranges to choose from when assigning an IP for a client." )
2024-01-06 08:11:20 +00:00
flag . IntVar ( & flagSessionMaxDuration , "session-max-duration" , util . LookupEnvOrInt ( "SESSION_MAX_DURATION" , flagSessionMaxDuration ) , "Max time in days a remembered session is refreshed and valid." )
2023-12-25 18:58:31 +00:00
var (
2023-12-29 09:56:37 +00:00
smtpPasswordLookup = util . LookupEnvOrString ( "SMTP_PASSWORD" , flagSmtpPassword )
sendgridApiKeyLookup = util . LookupEnvOrString ( "SENDGRID_API_KEY" , flagSendgridApiKey )
sessionSecretLookup = util . LookupEnvOrString ( "SESSION_SECRET" , flagSessionSecret )
2023-12-25 18:58:31 +00:00
)
// check empty smtpPassword env var
if smtpPasswordLookup != "" {
flag . StringVar ( & flagSmtpPassword , "smtp-password" , smtpPasswordLookup , "SMTP Password" )
} else {
flag . StringVar ( & flagSmtpPassword , "smtp-password" , util . LookupEnvOrFile ( "SMTP_PASSWORD_FILE" , flagSmtpPassword ) , "SMTP Password File" )
}
2023-12-29 09:56:37 +00:00
// check empty sendgridApiKey env var
if sendgridApiKeyLookup != "" {
flag . StringVar ( & flagSendgridApiKey , "sendgrid-api-key" , sendgridApiKeyLookup , "Your sendgrid api key." )
2023-12-25 18:58:31 +00:00
} else {
flag . StringVar ( & flagSendgridApiKey , "sendgrid-api-key" , util . LookupEnvOrFile ( "SENDGRID_API_KEY_FILE" , flagSendgridApiKey ) , "File containing your sendgrid api key." )
}
// check empty sessionSecret env var
if sessionSecretLookup != "" {
flag . StringVar ( & flagSessionSecret , "session-secret" , sessionSecretLookup , "The key used to encrypt session cookies." )
} else {
flag . StringVar ( & flagSessionSecret , "session-secret" , util . LookupEnvOrFile ( "SESSION_SECRET_FILE" , flagSessionSecret ) , "File containing the key used to encrypt session cookies." )
}
2020-10-09 21:33:29 +00:00
flag . Parse ( )
// update runtime config
2021-08-29 18:26:12 +00:00
util . DisableLogin = flagDisableLogin
util . BindAddress = flagBindAddress
2022-01-29 08:01:37 +00:00
util . SmtpHostname = flagSmtpHostname
util . SmtpPort = flagSmtpPort
2023-12-27 21:42:31 +00:00
util . SmtpHelo = flagSmtpHelo
2022-01-29 08:01:37 +00:00
util . SmtpUsername = flagSmtpUsername
util . SmtpPassword = flagSmtpPassword
util . SmtpAuthType = flagSmtpAuthType
util . SmtpNoTLSCheck = flagSmtpNoTLSCheck
2022-09-30 08:26:17 +00:00
util . SmtpEncryption = flagSmtpEncryption
2021-08-29 18:26:12 +00:00
util . SendgridApiKey = flagSendgridApiKey
util . EmailFrom = flagEmailFrom
util . EmailFromName = flagEmailFromName
2024-01-06 08:11:20 +00:00
util . SessionSecret = sha512 . Sum512 ( [ ] byte ( flagSessionSecret ) )
util . SessionMaxDuration = int64 ( flagSessionMaxDuration ) * 86_400 // Store in seconds
2022-04-24 07:42:08 +00:00
util . WgConfTemplate = flagWgConfTemplate
2022-04-25 07:17:13 +00:00
util . BasePath = util . ParseBasePath ( flagBasePath )
2023-12-27 08:08:55 +00:00
util . SubnetRanges = util . ParseSubnetRanges ( flagSubnetRanges )
2020-06-01 06:03:10 +00:00
2023-12-29 08:22:12 +00:00
lvl , _ := util . ParseLogLevel ( util . LookupEnvOrString ( util . LogLevel , "INFO" ) )
telegram . Token = flagTelegramToken
telegram . AllowConfRequest = flagTelegramAllowConfRequest
telegram . FloodWait = flagTelegramFloodWait
telegram . LogLevel = lvl
2020-06-01 06:03:10 +00:00
2023-03-15 20:29:08 +00:00
// print only if log level is INFO or lower
2023-12-29 08:22:12 +00:00
if lvl <= log . INFO {
2023-03-15 20:29:08 +00: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" )
fmt . Println ( "Authentication\t:" , ! util . DisableLogin )
fmt . Println ( "Bind address\t:" , util . BindAddress )
//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)
fmt . Println ( "Custom wg.conf\t:" , util . WgConfTemplate )
fmt . Println ( "Base path\t:" , util . BasePath + "/" )
2023-12-27 08:08:55 +00:00
fmt . Println ( "Subnet ranges\t:" , util . GetSubnetRangesString ( ) )
2023-03-15 20:29:08 +00:00
}
2020-10-09 21:33:29 +00:00
}
func main ( ) {
2021-08-28 13:10:30 +00:00
db , err := jsondb . New ( "./db" )
if err != nil {
panic ( err )
}
if err := db . Init ( ) ; err != nil {
panic ( err )
}
2020-10-09 21:33:29 +00:00
// set app extra data
2023-05-24 10:04:02 +00:00
extraData := make ( map [ string ] interface { } )
2020-10-09 21:33:29 +00:00
extraData [ "appVersion" ] = appVersion
2023-03-15 20:39:20 +00:00
extraData [ "gitCommit" ] = gitCommit
2022-04-25 07:17:13 +00:00
extraData [ "basePath" ] = util . BasePath
2023-05-24 10:04:02 +00:00
extraData [ "loginDisabled" ] = flagDisableLogin
2020-04-23 11:01:40 +00:00
2023-03-16 07:40:04 +00:00
// strip the "templates/" prefix from the embedded directory so files can be read by their direct name (e.g.
// "base.html" instead of "templates/base.html")
tmplDir , _ := fs . Sub ( fs . FS ( embeddedTemplates ) , "templates" )
2020-05-21 08:51:24 +00:00
2022-07-14 06:40:16 +00:00
// create the wireguard config on start, if it doesn't exist
2023-03-16 07:40:04 +00:00
initServerConfig ( db , tmplDir )
2022-07-14 06:40:16 +00:00
2023-12-27 08:08:55 +00:00
// Check if subnet ranges are valid for the server configuration
// Remove any non-valid CIDRs
if err := util . ValidateAndFixSubnetRanges ( db ) ; err != nil {
panic ( err )
}
// Print valid ranges
if lvl , _ := util . ParseLogLevel ( util . LookupEnvOrString ( util . LogLevel , "INFO" ) ) ; lvl <= log . INFO {
fmt . Println ( "Valid subnet ranges:" , util . GetSubnetRangesString ( ) )
}
2020-04-23 11:01:40 +00:00
// register routes
2023-03-16 07:40:04 +00:00
app := router . New ( tmplDir , extraData , util . SessionSecret )
2020-04-18 09:17:49 +00:00
2024-01-06 08:11:20 +00:00
app . GET ( util . BasePath , handler . WireGuardClients ( db ) , handler . ValidSession , handler . RefreshSession )
2020-10-09 21:33:29 +00:00
2023-12-25 19:03:29 +00:00
// Important: Make sure that all non-GET routes check the request content type using handler.ContentTypeJson to
// mitigate CSRF attacks. This is effective, because browsers don't allow setting the Content-Type header on
// cross-origin requests.
2020-10-09 21:33:29 +00:00
if ! util . DisableLogin {
2022-07-14 06:36:47 +00:00
app . GET ( util . BasePath + "/login" , handler . LoginPage ( ) )
2023-12-25 19:03:29 +00:00
app . POST ( util . BasePath + "/login" , handler . Login ( db ) , handler . ContentTypeJson )
2022-12-21 20:52:00 +00:00
app . GET ( util . BasePath + "/logout" , handler . Logout ( ) , handler . ValidSession )
2024-01-06 08:11:20 +00:00
app . GET ( util . BasePath + "/profile" , handler . LoadProfile ( ) , handler . ValidSession , handler . RefreshSession )
app . GET ( util . BasePath + "/users-settings" , handler . UsersSettings ( ) , handler . ValidSession , handler . RefreshSession , handler . NeedsAdmin )
2023-12-25 19:03:29 +00:00
app . POST ( util . BasePath + "/update-user" , handler . UpdateUser ( db ) , handler . ValidSession , handler . ContentTypeJson )
app . POST ( util . BasePath + "/create-user" , handler . CreateUser ( db ) , handler . ValidSession , handler . ContentTypeJson , handler . NeedsAdmin )
app . POST ( util . BasePath + "/remove-user" , handler . RemoveUser ( db ) , handler . ValidSession , handler . ContentTypeJson , handler . NeedsAdmin )
2023-12-29 09:56:37 +00:00
app . GET ( util . BasePath + "/get-users" , handler . GetUsers ( db ) , handler . ValidSession , handler . NeedsAdmin )
2023-03-15 20:13:53 +00:00
app . GET ( util . BasePath + "/api/user/:username" , handler . GetUser ( db ) , handler . ValidSession )
2020-10-09 21:33:29 +00:00
}
2022-01-29 08:01:37 +00:00
var sendmail emailer . Emailer
if util . SendgridApiKey != "" {
sendmail = emailer . NewSendgridApiMail ( util . SendgridApiKey , util . EmailFromName , util . EmailFrom )
} else {
2023-12-27 21:42:31 +00:00
sendmail = emailer . NewSmtpMail ( util . SmtpHostname , util . SmtpPort , util . SmtpUsername , util . SmtpPassword , util . SmtpHelo , util . SmtpNoTLSCheck , util . SmtpAuthType , util . EmailFromName , util . EmailFrom , util . SmtpEncryption )
2022-01-29 08:01:37 +00:00
}
2021-08-08 17:55:59 +00:00
2023-03-15 20:15:41 +00:00
app . GET ( util . BasePath + "/test-hash" , handler . GetHashesChanges ( db ) , handler . ValidSession )
2023-02-16 16:31:24 +00:00
app . GET ( util . BasePath + "/about" , handler . AboutPage ( ) )
2022-07-14 06:36:47 +00:00
app . GET ( util . BasePath + "/_health" , handler . Health ( ) )
2023-02-16 16:09:24 +00:00
app . GET ( util . BasePath + "/favicon" , handler . Favicon ( ) )
2022-07-14 06:36:47 +00:00
app . POST ( util . BasePath + "/new-client" , handler . NewClient ( db ) , handler . ValidSession , handler . ContentTypeJson )
app . POST ( util . BasePath + "/update-client" , handler . UpdateClient ( db ) , handler . ValidSession , handler . ContentTypeJson )
app . POST ( util . BasePath + "/email-client" , handler . EmailClient ( db , sendmail , defaultEmailSubject , defaultEmailContent ) , handler . ValidSession , handler . ContentTypeJson )
2023-12-29 08:22:12 +00:00
app . POST ( util . BasePath + "/send-telegram-client" , handler . SendTelegramClient ( db ) , handler . ValidSession , handler . ContentTypeJson )
2022-07-14 06:36:47 +00:00
app . POST ( util . BasePath + "/client/set-status" , handler . SetClientStatus ( db ) , handler . ValidSession , handler . ContentTypeJson )
app . POST ( util . BasePath + "/remove-client" , handler . RemoveClient ( db ) , handler . ValidSession , handler . ContentTypeJson )
app . GET ( util . BasePath + "/download" , handler . DownloadClient ( db ) , handler . ValidSession )
2024-01-06 08:11:20 +00:00
app . GET ( util . BasePath + "/wg-server" , handler . WireGuardServer ( db ) , handler . ValidSession , handler . RefreshSession , handler . NeedsAdmin )
2023-03-15 20:13:53 +00:00
app . POST ( util . BasePath + "/wg-server/interfaces" , handler . WireGuardServerInterfaces ( db ) , handler . ValidSession , handler . ContentTypeJson , handler . NeedsAdmin )
app . POST ( util . BasePath + "/wg-server/keypair" , handler . WireGuardServerKeyPair ( db ) , handler . ValidSession , handler . ContentTypeJson , handler . NeedsAdmin )
2024-01-06 08:11:20 +00:00
app . GET ( util . BasePath + "/global-settings" , handler . GlobalSettings ( db ) , handler . ValidSession , handler . RefreshSession , handler . NeedsAdmin )
2023-03-15 20:13:53 +00:00
app . POST ( util . BasePath + "/global-settings" , handler . GlobalSettingSubmit ( db ) , handler . ValidSession , handler . ContentTypeJson , handler . NeedsAdmin )
2024-01-06 08:11:20 +00:00
app . GET ( util . BasePath + "/status" , handler . Status ( db ) , handler . ValidSession , handler . RefreshSession )
2022-07-14 06:36:47 +00:00
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 )
2023-12-27 08:08:55 +00:00
app . GET ( util . BasePath + "/api/subnet-ranges" , handler . GetOrderedSubnetRanges ( ) , handler . ValidSession )
2022-07-14 06:36:47 +00:00
app . GET ( util . BasePath + "/api/suggest-client-ips" , handler . SuggestIPAllocation ( db ) , handler . ValidSession )
2023-03-16 07:40:04 +00:00
app . POST ( util . BasePath + "/api/apply-wg-config" , handler . ApplyServerConfig ( db , tmplDir ) , handler . ValidSession , handler . ContentTypeJson )
2024-01-06 08:11:20 +00:00
app . GET ( util . BasePath + "/wake_on_lan_hosts" , handler . GetWakeOnLanHosts ( db ) , handler . ValidSession , handler . RefreshSession )
2022-07-14 06:36:47 +00:00
app . POST ( util . BasePath + "/wake_on_lan_host" , handler . SaveWakeOnLanHost ( db ) , handler . ValidSession , handler . ContentTypeJson )
app . DELETE ( util . BasePath + "/wake_on_lan_host/:mac_address" , handler . DeleteWakeOnHost ( db ) , handler . ValidSession , handler . ContentTypeJson )
app . PUT ( util . BasePath + "/wake_on_lan_host/:mac_address" , handler . WakeOnHost ( db ) , handler . ValidSession , handler . ContentTypeJson )
2020-05-21 08:51:24 +00:00
2023-03-16 07:40:04 +00:00
// strip the "assets/" prefix from the embedded directory so files can be called directly without the "assets/"
// prefix
assetsDir , _ := fs . Sub ( fs . FS ( embeddedAssets ) , "assets" )
assetHandler := http . FileServer ( http . FS ( assetsDir ) )
// serves other static files
2022-07-14 06:36:47 +00:00
app . GET ( util . BasePath + "/static/*" , echo . WrapHandler ( http . StripPrefix ( util . BasePath + "/static/" , assetHandler ) ) )
2020-05-21 08:51:24 +00:00
2023-12-29 08:22:12 +00:00
initDeps := telegram . TgBotInitDependencies {
DB : db ,
SendRequestedConfigsToTelegram : util . SendRequestedConfigsToTelegram ,
}
initTelegram ( initDeps )
2023-12-25 19:31:11 +00:00
if strings . HasPrefix ( util . BindAddress , "unix://" ) {
// Listen on unix domain socket.
// https://github.com/labstack/echo/issues/830
2023-12-29 09:56:37 +00:00
err := syscall . Unlink ( util . BindAddress [ 6 : ] )
if err != nil {
app . Logger . Fatalf ( "Cannot unlink unix socket: Error: %v" , err )
}
2023-12-25 19:31:11 +00:00
l , err := net . Listen ( "unix" , util . BindAddress [ 6 : ] )
if err != nil {
2023-12-29 09:56:37 +00:00
app . Logger . Fatalf ( "Cannot create unix socket. Error: %v" , err )
2023-12-25 19:31:11 +00:00
}
app . Listener = l
app . Logger . Fatal ( app . Start ( "" ) )
} else {
// Listen on TCP socket
app . Logger . Fatal ( app . Start ( util . BindAddress ) )
}
2020-04-18 09:17:49 +00:00
}
2022-07-14 06:40:16 +00:00
2023-03-16 07:40:04 +00:00
func initServerConfig ( db store . IStore , tmplDir fs . FS ) {
2022-07-14 06:40:16 +00:00
settings , err := db . GetGlobalSettings ( )
if err != nil {
2023-12-29 09:56:37 +00:00
log . Fatalf ( "Cannot get global settings: %v" , err )
2022-07-14 06:40:16 +00:00
}
if _ , err := os . Stat ( settings . ConfigFilePath ) ; err == nil {
// file exists, don't overwrite it implicitly
return
}
server , err := db . GetServer ( )
if err != nil {
2023-12-29 09:56:37 +00:00
log . Fatalf ( "Cannot get server config: %v" , err )
2022-07-14 06:40:16 +00:00
}
clients , err := db . GetClients ( false )
if err != nil {
2023-12-29 09:56:37 +00:00
log . Fatalf ( "Cannot get client config: %v" , err )
2022-07-14 06:40:16 +00:00
}
2023-03-15 20:13:53 +00:00
users , err := db . GetUsers ( )
if err != nil {
2023-12-29 09:56:37 +00:00
log . Fatalf ( "Cannot get user config: %v" , err )
2023-03-15 20:13:53 +00:00
}
2022-07-14 06:40:16 +00:00
// write config file
2023-03-16 07:40:04 +00:00
err = util . WriteWireGuardServerConfig ( tmplDir , server , clients , users , settings )
2022-07-14 06:40:16 +00:00
if err != nil {
2023-12-29 09:56:37 +00:00
log . Fatalf ( "Cannot create server config: %v" , err )
2022-07-14 06:40:16 +00:00
}
}
2023-12-29 08:22:12 +00:00
func initTelegram ( initDeps telegram . TgBotInitDependencies ) {
go func ( ) {
for {
err := telegram . Start ( initDeps )
if err == nil {
break
}
}
} ( )
}