Move /tunnels GET to /api

This commit is contained in:
Anders Pitman 2020-10-05 23:37:03 -06:00
parent 5b5f474853
commit 6ee5a5d3f4
3 changed files with 22 additions and 12 deletions

8
api.go
View File

@ -44,6 +44,14 @@ func (a *Api) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (a *Api) handleTunnels(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
body, err := json.Marshal(a.tunMan.GetTunnels())
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Error encoding tunnels"))
return
}
w.Write([]byte(body))
case "POST":
a.validateSession(http.HandlerFunc(a.handleCreateTunnel)).ServeHTTP(w, r)
default:

View File

@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"fmt"
"github.com/GeertJohan/go.rice"
"html/template"
@ -119,17 +118,10 @@ func (p *BoringProxy) handleTunnels(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
if r.Method == "GET" {
body, err := json.Marshal(p.db.GetTunnels())
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Error encoding tunnels"))
return
}
w.Write([]byte(body))
} else if r.Method == "POST" {
switch r.Method {
case "POST":
p.handleCreateTunnel(w, r)
} else if r.Method == "DELETE" {
case "DELETE":
if len(query["host"]) != 1 {
w.WriteHeader(400)
w.Write([]byte("Invalid host parameter"))
@ -138,7 +130,11 @@ func (p *BoringProxy) handleTunnels(w http.ResponseWriter, r *http.Request) {
host := query["host"][0]
p.tunMan.DeleteTunnel(host)
}
default:
w.WriteHeader(405)
w.Write([]byte("Invalid method for /tunnels"))
return
}
}
func (p *BoringProxy) handleLogin(w http.ResponseWriter, r *http.Request) {

View File

@ -45,6 +45,10 @@ func NewTunnelManager(db *Database, certConfig *certmagic.Config) *TunnelManager
return &TunnelManager{db, nextPort, mutex, certConfig, user}
}
func (m *TunnelManager) GetTunnels() map[string]Tunnel {
return m.db.GetTunnels()
}
func (m *TunnelManager) SetTunnel(host string, port int) error {
err := m.certConfig.ManageSync([]string{host})
if err != nil {
@ -95,6 +99,8 @@ func (m *TunnelManager) DeleteTunnel(domain string) error {
return errors.New("Tunnel doesn't exist")
}
m.db.DeleteTunnel(domain)
authKeysPath := fmt.Sprintf("%s/.ssh/authorized_keys", m.user.HomeDir)
akBytes, err := ioutil.ReadFile(authKeysPath)