Add /waygates page

This commit is contained in:
Anders Pitman
2022-03-10 19:39:47 -07:00
parent bd186fb331
commit b7438a05bd
5 changed files with 53 additions and 9 deletions

14
api.go
View File

@@ -11,6 +11,8 @@ import (
"net/url"
"strconv"
"strings"
"github.com/takingnames/waygate-go"
)
type Api struct {
@@ -750,3 +752,15 @@ func (a *Api) DeleteDomain(tokenData TokenData, domainName string) error {
return nil
}
func (a *Api) GetWaygates(tokenData TokenData) map[string]waygate.Waygate {
user, _ := a.db.GetUser(tokenData.Owner)
waygates := a.db.GetWaygates()
if user.IsAdmin {
return waygates
} else {
return map[string]waygate.Waygate{}
}
}

View File

@@ -390,7 +390,7 @@ func (d *Database) DeleteDomain(domain string) {
d.persist()
}
func (d *Database) AddWaygate(domains []string) (string, error) {
func (d *Database) AddWaygate(wg waygate.Waygate) (string, error) {
d.mutex.Lock()
defer d.mutex.Unlock()
@@ -399,7 +399,7 @@ func (d *Database) AddWaygate(domains []string) (string, error) {
return "", errors.New("Could not generate waygate id")
}
for _, domainName := range domains {
for _, domainName := range wg.Domains {
for _, waygate := range d.Waygates {
for _, waygateDomainName := range waygate.Domains {
if domainName == waygateDomainName {
@@ -409,11 +409,7 @@ func (d *Database) AddWaygate(domains []string) (string, error) {
}
}
waygate := waygate.Waygate{
Domains: domains,
}
d.Waygates[id] = waygate
d.Waygates[id] = wg
d.persist()

View File

@@ -25,6 +25,7 @@
<div class='menu'>
<a class='menu-item' href='/tunnels'>Tunnels</a>
<a class='menu-item' href='/edit-tunnel'>Add Tunnel</a>
<a class='menu-item' href='/waygates'>Waygates</a>
<a class='menu-item' href='/tokens'>Tokens</a>
<a class='menu-item' href='/clients'>Clients</a>
<a class='menu-item' href='/domains'>Domains</a>

View File

@@ -40,14 +40,14 @@ main {
font-weight: bold;
}
.tn-tunnel-table, .tn-tunnel-table__cell {
.tn-tunnel-table, .tn-tunnel-table__cell, .tn-waygate-table, .tn-waygate-table__cell {
border: 1px solid var(--main-color);
border-collapse: collapse;
text-align: center;
padding: 10px;
word-wrap: break-word;
}
.tn-tunnel-table {
.tn-tunnel-table, .tn-waygate-table {
width: 100%;
}
.tn-tunnel-table__link {

View File

@@ -14,6 +14,8 @@ import (
"strings"
"sync"
"time"
"github.com/takingnames/waygate-go"
)
//go:embed logo.png templates
@@ -240,6 +242,8 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
h.handleClients(w, r, user, tokenData)
case "/domains":
h.handleDomains(w, r, user, tokenData)
case "/waygates":
h.handleWaygates(w, r, user, tokenData)
case "/confirm-delete-token":
h.confirmDeleteToken(w, r)
case "/delete-token":
@@ -531,6 +535,35 @@ func (h *WebUiHandler) handleDomains(w http.ResponseWriter, r *http.Request, use
}
}
func (h *WebUiHandler) handleWaygates(w http.ResponseWriter, r *http.Request, user User, tokenData TokenData) {
r.ParseForm()
switch r.Method {
case "GET":
waygates := h.api.GetWaygates(tokenData)
templateData := struct {
User User
Waygates map[string]waygate.Waygate
}{
User: user,
Waygates: waygates,
}
err := h.tmpl.ExecuteTemplate(w, "waygates.tmpl", templateData)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
default:
w.WriteHeader(405)
h.alertDialog(w, r, "Invalid method for /waygates", "/waygates")
return
}
}
func (h *WebUiHandler) handleLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {