2020-05-14 19:13:33 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-30 09:25:21 -05:00
|
|
|
"database/sql"
|
|
|
|
"embed"
|
2020-05-14 19:13:33 -04:00
|
|
|
"fmt"
|
2022-12-30 09:25:21 -05:00
|
|
|
"github.com/flant/ovpn-admin/backend"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
ou "github.com/pashcovich/openvpn-user/src"
|
2021-07-21 18:06:34 -04:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2021-12-30 03:24:44 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-07-21 18:06:34 -04:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2022-12-30 09:25:21 -05:00
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-10-15 12:12:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-12-30 09:25:21 -05:00
|
|
|
version = "2.1.0"
|
2020-11-27 02:23:59 -05:00
|
|
|
)
|
|
|
|
|
2021-12-30 03:24:44 -05:00
|
|
|
var logLevels = map[string]log.Level{
|
2022-01-20 11:42:36 -05:00
|
|
|
"trace": log.TraceLevel,
|
2021-12-30 03:24:44 -05:00
|
|
|
"debug": log.DebugLevel,
|
|
|
|
"info": log.InfoLevel,
|
|
|
|
"warn": log.WarnLevel,
|
|
|
|
"error": log.ErrorLevel,
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:42:36 -05:00
|
|
|
var logFormats = map[string]log.Formatter{
|
|
|
|
"text": &log.TextFormatter{},
|
|
|
|
"json": &log.JSONFormatter{},
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
//go:embed frontend/static
|
|
|
|
var staticFS embed.FS
|
2021-07-21 18:06:34 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
//go:embed templates
|
|
|
|
var templatesFS embed.FS
|
2021-07-21 18:06:34 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
var app backend.OpenVPNPKI
|
2021-12-30 03:24:44 -05:00
|
|
|
|
2020-05-14 19:13:33 -04:00
|
|
|
func main() {
|
2021-02-20 07:48:41 -05:00
|
|
|
kingpin.Version(version)
|
|
|
|
kingpin.Parse()
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
log.SetLevel(logLevels[*backend.LogLevel])
|
|
|
|
log.SetFormatter(logFormats[*backend.LogFormat])
|
2021-07-21 18:06:34 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin := new(backend.OvpnAdmin)
|
|
|
|
ovpnAdmin.OUser = new(ou.OpenvpnUser)
|
2020-11-17 12:48:26 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.StorageBackend == "kubernetes.secrets" {
|
|
|
|
err := app.Run()
|
2022-08-02 10:19:27 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2021-07-21 18:06:34 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 12:12:31 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.IndexTxtPath == "" {
|
|
|
|
*backend.IndexTxtPath = *backend.EasyrsaDirPath + "/pki/index.txt"
|
2021-12-30 03:24:44 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin.LastSyncTime = "unknown"
|
|
|
|
ovpnAdmin.Role = *backend.ServerRole
|
|
|
|
ovpnAdmin.LastSuccessfulSyncTime = "unknown"
|
|
|
|
ovpnAdmin.MasterSyncToken = *backend.MasterSyncToken
|
|
|
|
ovpnAdmin.PromRegistry = prometheus.NewRegistry()
|
|
|
|
ovpnAdmin.Modules = []string{}
|
|
|
|
ovpnAdmin.CreateUserMutex = &sync.Mutex{}
|
|
|
|
ovpnAdmin.MgmtInterfaces = make(map[string]string)
|
2022-01-20 11:42:36 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
for _, mgmtInterface := range *backend.MgmtAddress {
|
|
|
|
parts := strings.SplitN(mgmtInterface, "=", 2)
|
|
|
|
ovpnAdmin.MgmtInterfaces[parts[0]] = parts[len(parts)-1]
|
2022-08-12 06:52:45 -04:00
|
|
|
}
|
2020-05-14 19:13:33 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.MasterBasicAuthPassword != "" && *backend.MasterBasicAuthUser != "" {
|
|
|
|
ovpnAdmin.MasterHostBasicAuth = true
|
2021-02-15 01:03:38 -05:00
|
|
|
} else {
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin.MasterHostBasicAuth = false
|
2020-11-27 02:23:59 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin.Modules = append(ovpnAdmin.Modules, "core")
|
2020-05-14 19:13:33 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.AuthByPassword {
|
|
|
|
db, err := sql.Open("sqlite3", *backend.AuthDatabase)
|
2021-12-30 03:24:44 -05:00
|
|
|
if err != nil {
|
2022-12-30 09:25:21 -05:00
|
|
|
kingpin.Fatalf(err.Error())
|
2020-11-27 02:23:59 -05:00
|
|
|
}
|
2022-12-30 09:25:21 -05:00
|
|
|
defer func(db *sql.DB) {
|
|
|
|
err = db.Close()
|
2021-12-30 03:24:44 -05:00
|
|
|
if err != nil {
|
2022-12-30 09:25:21 -05:00
|
|
|
kingpin.Fatalf(err.Error())
|
2021-12-30 03:24:44 -05:00
|
|
|
}
|
2022-12-30 09:25:21 -05:00
|
|
|
}(db)
|
|
|
|
ovpnAdmin.OUser.Database = db
|
2021-12-30 03:24:44 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin.Modules = append(ovpnAdmin.Modules, "passwdAuth")
|
2021-10-05 11:09:29 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.AuthTFA {
|
|
|
|
ovpnAdmin.Modules = append(ovpnAdmin.Modules, "totpAuth")
|
2021-10-05 11:09:29 -04:00
|
|
|
}
|
2020-05-14 19:13:33 -04:00
|
|
|
}
|
2022-07-21 11:17:53 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.CcdEnabled {
|
|
|
|
ovpnAdmin.Modules = append(ovpnAdmin.Modules, "ccd")
|
2020-05-14 19:13:33 -04:00
|
|
|
}
|
2022-08-02 10:19:27 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if ovpnAdmin.Role == "slave" {
|
|
|
|
ovpnAdmin.SyncDataFromMaster()
|
|
|
|
go ovpnAdmin.SyncWithMaster()
|
2022-07-21 11:17:53 -04:00
|
|
|
}
|
2020-11-27 02:23:59 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
templatesRoot, err := fs.Sub(templatesFS, "templates")
|
2020-11-17 12:48:26 -05:00
|
|
|
if err != nil {
|
2022-12-30 09:25:21 -05:00
|
|
|
log.Fatal(err)
|
2022-01-20 11:42:36 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
staticRoot, err := fs.Sub(staticFS, "frontend/static")
|
2021-07-21 18:06:34 -04:00
|
|
|
if err != nil {
|
2022-12-30 09:25:21 -05:00
|
|
|
log.Fatal(err)
|
2020-10-29 06:50:19 -04:00
|
|
|
}
|
2020-11-17 12:48:26 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin.Templates = templatesRoot
|
|
|
|
static := CacheControlWrapper(http.FileServer(http.FS(staticRoot)))
|
2020-10-29 06:50:19 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin.MgmtSetTimeFormat()
|
2020-10-29 06:50:19 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
ovpnAdmin.RegisterMetrics()
|
|
|
|
ovpnAdmin.SetState()
|
2020-10-29 06:50:19 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
go ovpnAdmin.UpdateState()
|
2020-11-17 12:48:26 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
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/rotate", ovpnAdmin.UserRotateHandler)
|
|
|
|
http.HandleFunc("/api/user/delete", ovpnAdmin.UserDeleteHandler)
|
|
|
|
http.HandleFunc("/api/user/revoke", ovpnAdmin.UserRevokeHandler)
|
|
|
|
http.HandleFunc("/api/user/unrevoke", ovpnAdmin.UserUnrevokeHandler)
|
|
|
|
http.HandleFunc("/api/user/config/show", ovpnAdmin.UserShowConfigHandler)
|
2021-10-05 11:09:29 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
http.HandleFunc("/api/user/disconnect", ovpnAdmin.UserDisconnectHandler)
|
|
|
|
http.HandleFunc("/api/user/statistic", ovpnAdmin.UserStatisticHandler)
|
2021-10-05 11:09:29 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.CcdEnabled {
|
|
|
|
http.HandleFunc("/api/user/ccd", ovpnAdmin.UserShowCcdHandler)
|
|
|
|
http.HandleFunc("/api/user/ccd/apply", ovpnAdmin.UserApplyCcdHandler)
|
2021-10-05 11:09:29 -04:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.AuthByPassword {
|
|
|
|
http.HandleFunc("/api/user/change-password", ovpnAdmin.UserChangePasswordHandler)
|
|
|
|
http.HandleFunc("/api/auth/check", ovpnAdmin.AuthCheckHandler)
|
2022-01-20 11:42:36 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if *backend.AuthTFA {
|
|
|
|
http.HandleFunc("/api/user/2fa/secret", ovpnAdmin.UserGetSecretHandler)
|
|
|
|
http.HandleFunc("/api/user/2fa/register", ovpnAdmin.UserSetupTFAHandler)
|
|
|
|
http.HandleFunc("/api/user/2fa/reset", ovpnAdmin.UserResetTFAHandler)
|
2022-01-20 11:42:36 -05:00
|
|
|
}
|
2022-04-27 06:42:59 -04:00
|
|
|
}
|
2021-10-05 11:09:29 -04:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
http.HandleFunc("/api/sync/last/try", ovpnAdmin.LastSyncTimeHandler)
|
|
|
|
http.HandleFunc("/api/sync/last/successful", ovpnAdmin.LastSuccessfulSyncTimeHandler)
|
|
|
|
http.HandleFunc(backend.DownloadCertsApiUrl, ovpnAdmin.DownloadCertsHandler)
|
|
|
|
http.HandleFunc(backend.DownloadCcdApiUrl, ovpnAdmin.DownloadCcdHandler)
|
2022-01-20 09:49:03 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
http.Handle(*backend.MetricsPath, promhttp.HandlerFor(ovpnAdmin.PromRegistry, promhttp.HandlerOpts{}))
|
|
|
|
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "pong")
|
|
|
|
})
|
2020-11-27 02:23:59 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
log.Printf("Bind: http://%s:%s", *backend.ListenHost, *backend.ListenPort)
|
|
|
|
log.Fatal(http.ListenAndServe(*backend.ListenHost+":"+*backend.ListenPort, nil))
|
2020-11-27 02:23:59 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func CacheControlWrapper(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Cache-Control", "max-age=2592000") // 30 days
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2020-11-27 02:23:59 -05:00
|
|
|
}
|