From 633ad79d6a822c347fb47c0750c3a82e3246b784 Mon Sep 17 00:00:00 2001 From: strnk Date: Tue, 7 Dec 2021 15:44:52 +0100 Subject: [PATCH 1/3] Add base URL configuration to the webserver to support reverse-proxy setups --- README.md | 3 +++ main.go | 38 ++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 67344ca..2069316 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,9 @@ Flags: --listen.port="8080" port for ovpn-admin (or $OVPN_LISTEN_PROT) + --listen.base-url="/" base URL for ovpn-admin web files + (or $OVPN_LISTEN_BASE_URL) + --role="master" server role, master or slave (or $OVPN_ROLE) diff --git a/main.go b/main.go index 327b14b..5dcaadb 100644 --- a/main.go +++ b/main.go @@ -31,8 +31,6 @@ const ( usernameRegexp = `^([a-zA-Z0-9_.-@])+$` passwordRegexp = `^([a-zA-Z0-9_.-@])+$` passwordMinLength = 6 - downloadCertsApiUrl = "/api/data/certs/download" - downloadCcdApiUrl = "/api/data/ccd/download" certsArchiveFileName = "certs.tar.gz" ccdArchiveFileName = "ccd.tar.gz" indexTxtDateLayout = "060102150405Z" @@ -46,6 +44,7 @@ const ( var ( listenHost = kingpin.Flag("listen.host","host for ovpn-admin").Default("0.0.0.0").Envar("OVPN_LISTEN_HOST").String() listenPort = kingpin.Flag("listen.port","port for ovpn-admin").Default("8080").Envar("OVPN_LISTEN_PORT").String() + listenBaseUrl = kingpin.Flag("listen.base-url", "base url for ovpn-admin").Default("/").Envar("OVPN_LISTEN_BASE_URL").String() serverRole = kingpin.Flag("role","server role, master or slave").Default("master").Envar("OVPN_ROLE").HintOptions("master", "slave").String() masterHost = kingpin.Flag("master.host","URL for the master server").Default("http://127.0.0.1").Envar("OVPN_MASTER_HOST").String() masterBasicAuthUser = kingpin.Flag("master.basic-auth.user","user for master server's Basic Auth").Default("").Envar("OVPN_MASTER_USER").String() @@ -69,6 +68,9 @@ var ( debug = kingpin.Flag("debug", "enable debug mode").Default("false").Envar("OVPN_DEBUG").Bool() verbose = kingpin.Flag("verbose", "enable verbose mode").Default("false").Envar("OVPN_VERBOSE").Bool() + downloadCertsApiUrl = *listenBaseUrl + "api/data/certs/download" + downloadCcdApiUrl = *listenBaseUrl + "api/data/ccd/download" + certsArchivePath = "/tmp/" + certsArchiveFileName ccdArchivePath = "/tmp/" + ccdArchiveFileName @@ -443,30 +445,30 @@ func main() { staticBox := packr.New("static", "./frontend/static") static := CacheControlWrapper(http.FileServer(staticBox)) - http.Handle("/", static) - http.HandleFunc("/api/server/settings", ovpnAdmin.serverSettingsHandler) - http.HandleFunc("/api/users/list", ovpnAdmin.userListHandler) - http.HandleFunc("/api/user/create", ovpnAdmin.userCreateHandler) - http.HandleFunc("/api/user/change-password", ovpnAdmin.userChangePasswordHandler) - http.HandleFunc("/api/user/revoke", ovpnAdmin.userRevokeHandler) - http.HandleFunc("/api/user/unrevoke", ovpnAdmin.userUnrevokeHandler) - http.HandleFunc("/api/user/config/show", ovpnAdmin.userShowConfigHandler) - http.HandleFunc("/api/user/disconnect", ovpnAdmin.userDisconnectHandler) - http.HandleFunc("/api/user/statistic", ovpnAdmin.userStatisticHandler) - http.HandleFunc("/api/user/ccd", ovpnAdmin.userShowCcdHandler) - http.HandleFunc("/api/user/ccd/apply", ovpnAdmin.userApplyCcdHandler) + http.Handle(*listenBaseUrl, http.StripPrefix(strings.TrimRight(*listenBaseUrl, "/"), static)) + http.HandleFunc(*listenBaseUrl + "api/server/settings", ovpnAdmin.serverSettingsHandler) + http.HandleFunc(*listenBaseUrl + "api/users/list", ovpnAdmin.userListHandler) + http.HandleFunc(*listenBaseUrl + "api/user/create", ovpnAdmin.userCreateHandler) + http.HandleFunc(*listenBaseUrl + "api/user/change-password", ovpnAdmin.userChangePasswordHandler) + http.HandleFunc(*listenBaseUrl + "api/user/revoke", ovpnAdmin.userRevokeHandler) + http.HandleFunc(*listenBaseUrl + "api/user/unrevoke", ovpnAdmin.userUnrevokeHandler) + http.HandleFunc(*listenBaseUrl + "api/user/config/show", ovpnAdmin.userShowConfigHandler) + http.HandleFunc(*listenBaseUrl + "api/user/disconnect", ovpnAdmin.userDisconnectHandler) + http.HandleFunc(*listenBaseUrl + "api/user/statistic", ovpnAdmin.userStatisticHandler) + http.HandleFunc(*listenBaseUrl + "api/user/ccd", ovpnAdmin.userShowCcdHandler) + http.HandleFunc(*listenBaseUrl + "api/user/ccd/apply", ovpnAdmin.userApplyCcdHandler) - http.HandleFunc("/api/sync/last/try", ovpnAdmin.lastSyncTimeHandler) - http.HandleFunc("/api/sync/last/successful", ovpnAdmin.lastSuccessfulSyncTimeHandler) + http.HandleFunc(*listenBaseUrl + "api/sync/last/try", ovpnAdmin.lastSyncTimeHandler) + http.HandleFunc(*listenBaseUrl + "api/sync/last/successful", ovpnAdmin.lastSuccessfulSyncTimeHandler) http.HandleFunc(downloadCertsApiUrl, ovpnAdmin.downloadCertsHandler) http.HandleFunc(downloadCcdApiUrl, ovpnAdmin.downloadCcdHandler) http.Handle(*metricsPath, promhttp.HandlerFor(ovpnAdmin.promRegistry, promhttp.HandlerOpts{})) - http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(*listenBaseUrl + "ping", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "pong") }) - log.Printf("Bind: http://%s:%s\n", *listenHost, *listenPort) + log.Printf("Bind: http://%s:%s%s\n", *listenHost, *listenPort, *listenBaseUrl) log.Fatal(http.ListenAndServe(*listenHost+":"+*listenPort, nil)) } From f73626dd7b05cca9433fc7a28ce1c03b005e67f8 Mon Sep 17 00:00:00 2001 From: strnk Date: Tue, 7 Dec 2021 15:57:31 +0100 Subject: [PATCH 2/3] Add configuration parameter for the easyrsa script path --- main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 5dcaadb..971a8b4 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,7 @@ var ( metricsPath = kingpin.Flag("metrics.path", "URL path for exposing collected metrics").Default("/metrics").Envar("OVPN_METRICS_PATH").String() easyrsaDirPath = kingpin.Flag("easyrsa.path", "path to easyrsa dir").Default("./easyrsa/").Envar("EASYRSA_PATH").String() indexTxtPath = kingpin.Flag("easyrsa.index-path", "path to easyrsa index file").Default("./easyrsa/pki/index.txt").Envar("OVPN_INDEX_PATH").String() + easyrsaBinPath = kingpin.Flag("easyrsa.bin-path", "path to easyrsa script").Default("easyrsa").Envar("EASYRSA_BIN_PATH").String() ccdEnabled = kingpin.Flag("ccd", "enable client-config-dir").Default("false").Envar("OVPN_CCD").Bool() ccdDir = kingpin.Flag("ccd.path", "path to client-config-dir").Default("./ccd").Envar("OVPN_CCD_PATH").String() clientConfigTemplatePath = kingpin.Flag("templates.clientconfig-path", "path to custom client.conf.tpl").Default("").Envar("OVPN_TEMPLATES_CC_PATH").String() @@ -850,7 +851,7 @@ func (oAdmin *OvpnAdmin) userCreate(username, password string) (bool, string) { } } - o := runBash(fmt.Sprintf("date +%%Y-%%m-%%d\\ %%H:%%M:%%S && cd %s && easyrsa build-client-full %s nopass", *easyrsaDirPath, username)) + o := runBash(fmt.Sprintf("date +%%Y-%%m-%%d\\ %%H:%%M:%%S && cd %s && %s build-client-full %s nopass", *easyrsaDirPath, *easyrsaBinPath, username)) log.Println(o) if *authByPassword { @@ -911,7 +912,7 @@ func (oAdmin *OvpnAdmin) getUserStatistic(username string) clientStatus { func (oAdmin *OvpnAdmin) userRevoke(username string) string { if checkUserExist(username) { // check certificate valid flag 'V' - o := runBash(fmt.Sprintf("date +%%Y-%%m-%%d\\ %%H:%%M:%%S && cd %s && echo yes | easyrsa revoke %s && easyrsa gen-crl", *easyrsaDirPath, username)) + o := runBash(fmt.Sprintf("date +%%Y-%%m-%%d\\ %%H:%%M:%%S && cd %s && echo yes | %s revoke %s && %s gen-crl", *easyrsaDirPath, *easyrsaBinPath, username, *easyrsaBinPath)) if *authByPassword { o = runBash(fmt.Sprintf("openvpn-user revoke --db-path %s --user %s", *authDatabase, username)) //fmt.Println(o) @@ -949,7 +950,7 @@ func (oAdmin *OvpnAdmin) userUnrevoke(username string) string { //fmt.Println(o) fWrite(*indexTxtPath, renderIndexTxt(usersFromIndexTxt)) //fmt.Print(renderIndexTxt(usersFromIndexTxt)) - o = runBash(fmt.Sprintf("cd %s && easyrsa gen-crl", *easyrsaDirPath)) + o = runBash(fmt.Sprintf("cd %s && %s gen-crl", *easyrsaDirPath, *easyrsaBinPath)) //fmt.Println(o) if *authByPassword { o = runBash(fmt.Sprintf("openvpn-user restore --db-path %s --user %s", *authDatabase, username)) From 0ee9be5744cb397917b217aacedd90c395e3dcae Mon Sep 17 00:00:00 2001 From: strnk Date: Tue, 7 Dec 2021 16:12:57 +0100 Subject: [PATCH 3/3] Fix certs and ccd slave download API endpoints --- main.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 971a8b4..043c940 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,8 @@ const ( indexTxtDateLayout = "060102150405Z" stringDateFormat = "2006-01-02 15:04:05" ovpnStatusDateLayout = "2006-01-02 15:04:05" + downloadCertsApiUrl = "api/data/certs/download" + downloadCcdApiUrl = "api/data/ccd/download" kubeTokenFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/token" kubeNamespaceFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" @@ -69,15 +71,15 @@ var ( debug = kingpin.Flag("debug", "enable debug mode").Default("false").Envar("OVPN_DEBUG").Bool() verbose = kingpin.Flag("verbose", "enable verbose mode").Default("false").Envar("OVPN_VERBOSE").Bool() - downloadCertsApiUrl = *listenBaseUrl + "api/data/certs/download" - downloadCcdApiUrl = *listenBaseUrl + "api/data/ccd/download" - certsArchivePath = "/tmp/" + certsArchiveFileName ccdArchivePath = "/tmp/" + ccdArchiveFileName version = "1.7.5" ) +var ( +) + var ( ovpnServerCertExpire = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "ovpn_server_cert_expire", @@ -461,8 +463,8 @@ func main() { http.HandleFunc(*listenBaseUrl + "api/sync/last/try", ovpnAdmin.lastSyncTimeHandler) http.HandleFunc(*listenBaseUrl + "api/sync/last/successful", ovpnAdmin.lastSuccessfulSyncTimeHandler) - http.HandleFunc(downloadCertsApiUrl, ovpnAdmin.downloadCertsHandler) - http.HandleFunc(downloadCcdApiUrl, ovpnAdmin.downloadCcdHandler) + http.HandleFunc(*listenBaseUrl + downloadCertsApiUrl, ovpnAdmin.downloadCertsHandler) + http.HandleFunc(*listenBaseUrl + downloadCcdApiUrl, ovpnAdmin.downloadCcdHandler) http.Handle(*metricsPath, promhttp.HandlerFor(ovpnAdmin.promRegistry, promhttp.HandlerOpts{})) http.HandleFunc(*listenBaseUrl + "ping", func(w http.ResponseWriter, r *http.Request) { @@ -1076,7 +1078,7 @@ func (oAdmin *OvpnAdmin) downloadCerts() bool { if fExist(certsArchivePath) { fDelete(certsArchivePath) } - err := fDownload(certsArchivePath, *masterHost+downloadCertsApiUrl+"?token="+oAdmin.masterSyncToken, oAdmin.masterHostBasicAuth) + err := fDownload(certsArchivePath, *masterHost+*listenBaseUrl+downloadCertsApiUrl+"?token="+oAdmin.masterSyncToken, oAdmin.masterHostBasicAuth) if err != nil { log.Println(err) return false @@ -1090,7 +1092,7 @@ func (oAdmin *OvpnAdmin) downloadCcd() bool { fDelete(ccdArchivePath) } - err := fDownload(ccdArchivePath, *masterHost+downloadCcdApiUrl+"?token="+oAdmin.masterSyncToken, oAdmin.masterHostBasicAuth) + err := fDownload(ccdArchivePath, *masterHost+*listenBaseUrl+downloadCcdApiUrl+"?token="+oAdmin.masterSyncToken, oAdmin.masterHostBasicAuth) if err != nil { log.Println(err) return false