72 lines
1.3 KiB
Go
72 lines
1.3 KiB
Go
package hub
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/base64"
|
|
"html/template"
|
|
"net/http"
|
|
"path/filepath"
|
|
"vppn/hub/api"
|
|
|
|
"git.crumpington.com/lib/keyedmutex"
|
|
"git.crumpington.com/lib/webutil"
|
|
)
|
|
|
|
//go:embed static
|
|
var staticFS embed.FS
|
|
|
|
//go:embed templates
|
|
var templateFS embed.FS
|
|
|
|
type Config struct {
|
|
RootDir string
|
|
ListenAddr string
|
|
Insecure bool
|
|
}
|
|
|
|
type App struct {
|
|
api *api.API
|
|
mux *http.ServeMux
|
|
tmpl map[string]*template.Template
|
|
insecure bool
|
|
|
|
// Per-remote address sign-in serialization lock.
|
|
signInLock *keyedmutex.KeyedMutex[string]
|
|
}
|
|
|
|
func NewApp(conf Config) (*App, error) {
|
|
api, err := api.New(filepath.Join(conf.RootDir, "db.sqlite3"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
app := &App{
|
|
api: api,
|
|
mux: http.NewServeMux(),
|
|
tmpl: webutil.ParseTemplateSet(templateFuncs, templateFS),
|
|
insecure: conf.Insecure,
|
|
signInLock: keyedmutex.New[string](),
|
|
}
|
|
|
|
app.registerRoutes()
|
|
|
|
return app, nil
|
|
}
|
|
|
|
func (app *App) Handler() http.Handler {
|
|
cop := http.NewCrossOriginProtection()
|
|
return cop.Handler(app.mux)
|
|
}
|
|
|
|
var templateFuncs = template.FuncMap{
|
|
"ipToString": ipBytesTostring,
|
|
"wgKeyString": wgKeyString,
|
|
}
|
|
|
|
func wgKeyString(key []byte) string {
|
|
if len(key) == 0 {
|
|
return "not set"
|
|
}
|
|
return base64.StdEncoding.EncodeToString(key)
|
|
}
|