mirror of
https://github.com/boringproxy/boringproxy.git
synced 2025-02-25 18:55:29 -06:00
Implement deleting waygates
This commit is contained in:
parent
c91b322a23
commit
967099e3a1
19
api.go
19
api.go
@ -764,3 +764,22 @@ func (a *Api) GetWaygates(tokenData TokenData) map[string]waygate.Waygate {
|
||||
return map[string]waygate.Waygate{}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Api) DeleteWaygate(tokenData TokenData, waygateId string) error {
|
||||
|
||||
waygate, err := a.db.GetBoringProxyWaygate(waygateId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if tokenData.Owner != waygate.OwnerId {
|
||||
user, _ := a.db.GetUser(tokenData.Owner)
|
||||
if !user.IsAdmin {
|
||||
return errors.New("Unauthorized")
|
||||
}
|
||||
}
|
||||
|
||||
a.db.DeleteWaygate(waygateId)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
44
database.go
44
database.go
@ -13,6 +13,11 @@ import (
|
||||
|
||||
var DBFolderPath string
|
||||
|
||||
type Waygate struct {
|
||||
OwnerId string `json:"owner_id"`
|
||||
Waygate waygate.Waygate `json:"waygate"`
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
AdminDomain string `json:"admin_domain"`
|
||||
Tokens map[string]TokenData `json:"tokens"`
|
||||
@ -20,7 +25,7 @@ type Database struct {
|
||||
Users map[string]User `json:"users"`
|
||||
Domains map[string]Domain `json:"domains"`
|
||||
dnsRequests map[string]namedrop.DNSRequest `json:"dns_requests"`
|
||||
Waygates map[string]waygate.Waygate `json:"waygates"`
|
||||
Waygates map[string]Waygate `json:"waygates"`
|
||||
WaygateTokens map[string]waygate.TokenData `json:"waygate_tokens"`
|
||||
waygateCodes map[string]string `json:"waygate_codes"`
|
||||
mutex *sync.Mutex
|
||||
@ -110,7 +115,7 @@ func NewDatabase(path string) (*Database, error) {
|
||||
}
|
||||
|
||||
if db.Waygates == nil {
|
||||
db.Waygates = make(map[string]waygate.Waygate)
|
||||
db.Waygates = make(map[string]Waygate)
|
||||
}
|
||||
if db.WaygateTokens == nil {
|
||||
db.WaygateTokens = make(map[string]waygate.TokenData)
|
||||
@ -390,7 +395,7 @@ func (d *Database) DeleteDomain(domain string) {
|
||||
d.persist()
|
||||
}
|
||||
|
||||
func (d *Database) AddWaygate(wg waygate.Waygate) (string, error) {
|
||||
func (d *Database) AddWaygate(ownerId string, wg waygate.Waygate) (string, error) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
|
||||
@ -401,7 +406,7 @@ func (d *Database) AddWaygate(wg waygate.Waygate) (string, error) {
|
||||
|
||||
for _, domainName := range wg.Domains {
|
||||
for _, waygate := range d.Waygates {
|
||||
for _, waygateDomainName := range waygate.Domains {
|
||||
for _, waygateDomainName := range waygate.Waygate.Domains {
|
||||
if domainName == waygateDomainName {
|
||||
return "", errors.New("Domain already used by another waygate")
|
||||
}
|
||||
@ -409,7 +414,10 @@ func (d *Database) AddWaygate(wg waygate.Waygate) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
d.Waygates[id] = wg
|
||||
d.Waygates[id] = Waygate{
|
||||
OwnerId: ownerId,
|
||||
Waygate: wg,
|
||||
}
|
||||
|
||||
d.persist()
|
||||
|
||||
@ -419,12 +427,12 @@ func (d *Database) GetWaygate(id string) (waygate.Waygate, error) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
|
||||
tun, exists := d.Waygates[id]
|
||||
wg, exists := d.Waygates[id]
|
||||
if !exists {
|
||||
return waygate.Waygate{}, errors.New("No such waygate")
|
||||
}
|
||||
|
||||
return tun, nil
|
||||
return wg.Waygate, nil
|
||||
}
|
||||
func (d *Database) GetWaygates() map[string]waygate.Waygate {
|
||||
d.mutex.Lock()
|
||||
@ -433,12 +441,32 @@ func (d *Database) GetWaygates() map[string]waygate.Waygate {
|
||||
wgs := make(map[string]waygate.Waygate)
|
||||
|
||||
for id, wg := range d.Waygates {
|
||||
wgs[id] = wg
|
||||
wgs[id] = wg.Waygate
|
||||
}
|
||||
|
||||
return wgs
|
||||
}
|
||||
|
||||
func (d *Database) GetBoringProxyWaygate(id string) (Waygate, error) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
|
||||
wg, exists := d.Waygates[id]
|
||||
if !exists {
|
||||
return Waygate{}, errors.New("No such waygate")
|
||||
}
|
||||
|
||||
return wg, nil
|
||||
|
||||
}
|
||||
|
||||
func (d *Database) DeleteWaygate(id string) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
|
||||
delete(d.Waygates, id)
|
||||
|
||||
}
|
||||
func (d *Database) AddWaygateToken(waygateId string) (string, error) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
|
@ -29,7 +29,7 @@
|
||||
<td class='tn-waygate-table__cell'>
|
||||
<div class='button-row'>
|
||||
<a class='button' href="/waygates/{{$waygateId}}">Edit</a>
|
||||
<a class='button' href="/confirm-delete-waygate?id={{$waygateId}}">Delete</a>
|
||||
<a class='button' href="/waygate-confirm-delete?waygate-id={{$waygateId}}">Delete</a>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -301,11 +301,15 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
|
||||
case "/waygate-add-wildcard-domain":
|
||||
h.handleWaygateAddWildcardDomain(w, r)
|
||||
case "/waygate-delete-selected":
|
||||
h.handleWaygateDeleteSelected(w, r)
|
||||
h.handleWaygateDeleteSelectedDomain(w, r)
|
||||
case "/waygate-create":
|
||||
h.handleWaygateCreate(w, r)
|
||||
h.handleWaygateCreate(w, r, tokenData)
|
||||
case "/waygate-connect-existing":
|
||||
h.handleWaygateConnectExisting(w, r)
|
||||
case "/waygate-confirm-delete":
|
||||
h.confirmDeleteWaygate(w, r)
|
||||
case "/waygate-delete":
|
||||
h.deleteWaygate(w, r, tokenData)
|
||||
|
||||
default:
|
||||
if strings.HasPrefix(r.URL.Path, "/tunnels/") {
|
||||
|
43
waygate.go
43
waygate.go
@ -176,7 +176,7 @@ func (h *WebUiHandler) handleWaygateAddDomain(w http.ResponseWriter, r *http.Req
|
||||
|
||||
h.handleWaygateEdit(w, r)
|
||||
}
|
||||
func (h *WebUiHandler) handleWaygateDeleteSelected(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *WebUiHandler) handleWaygateDeleteSelectedDomain(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
w.WriteHeader(405)
|
||||
io.WriteString(w, "Invalid method")
|
||||
@ -249,7 +249,7 @@ func (h *WebUiHandler) handleWaygateEdit(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *WebUiHandler) handleWaygateCreate(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *WebUiHandler) handleWaygateCreate(w http.ResponseWriter, r *http.Request, tokenData TokenData) {
|
||||
if r.Method != "POST" {
|
||||
w.WriteHeader(405)
|
||||
io.WriteString(w, "Invalid method")
|
||||
@ -295,7 +295,8 @@ func (h *WebUiHandler) handleWaygateCreate(w http.ResponseWriter, r *http.Reques
|
||||
Domains: selectedDomains,
|
||||
Description: description,
|
||||
}
|
||||
_, err = h.db.AddWaygate(wg)
|
||||
|
||||
_, err = h.db.AddWaygate(tokenData.Owner, wg)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprintf(w, err.Error())
|
||||
@ -360,3 +361,39 @@ func (h *WebUiHandler) completeAuth(w http.ResponseWriter, r *http.Request, wayg
|
||||
http.Redirect(w, r, url, 303)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *WebUiHandler) confirmDeleteWaygate(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
r.ParseForm()
|
||||
|
||||
waygateId := r.Form.Get("waygate-id")
|
||||
|
||||
data := &ConfirmData{
|
||||
Head: h.headHtml,
|
||||
Message: "Are you sure you want to delete Waygate?",
|
||||
ConfirmUrl: fmt.Sprintf("/waygate-delete?waygate-id=%s", waygateId),
|
||||
CancelUrl: "/waygates",
|
||||
}
|
||||
|
||||
err := h.tmpl.ExecuteTemplate(w, "confirm.tmpl", data)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
h.alertDialog(w, r, err.Error(), "/waygates")
|
||||
return
|
||||
}
|
||||
}
|
||||
func (h *WebUiHandler) deleteWaygate(w http.ResponseWriter, r *http.Request, tokenData TokenData) {
|
||||
|
||||
r.ParseForm()
|
||||
|
||||
waygateId := r.Form.Get("waygate-id")
|
||||
|
||||
err := h.api.DeleteWaygate(tokenData, waygateId)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
h.alertDialog(w, r, err.Error(), "/waygates")
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/waygates", 303)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user