mirror of
https://github.com/boringproxy/boringproxy.git
synced 2025-02-25 18:55:29 -06:00
Create UI tunnels through API
This commit is contained in:
parent
a4fa2862e4
commit
fb8d379663
32
api.go
32
api.go
@ -3,9 +3,12 @@ package main
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Api struct {
|
||||
@ -52,6 +55,35 @@ func (a *Api) GetTunnels(tokenData TokenData) map[string]Tunnel {
|
||||
return tunnels
|
||||
}
|
||||
|
||||
func (a *Api) CreateTunnel(tokenData TokenData, params url.Values) (*Tunnel, error) {
|
||||
|
||||
if len(params["domain"]) != 1 {
|
||||
return nil, errors.New("Invalid domain parameter")
|
||||
}
|
||||
domain := params["domain"][0]
|
||||
|
||||
if len(params["client-name"]) != 1 {
|
||||
return nil, errors.New("Invalid client-name parameter")
|
||||
}
|
||||
clientName := params["client-name"][0]
|
||||
|
||||
if len(params["client-port"]) != 1 {
|
||||
return nil, errors.New("Invalid client-port parameter")
|
||||
}
|
||||
|
||||
clientPort, err := strconv.Atoi(params["client-port"][0])
|
||||
if err != nil {
|
||||
return nil, errors.New("Invalid client-port parameter")
|
||||
}
|
||||
|
||||
tunnel, err := a.tunMan.CreateTunnelForClient(domain, tokenData.Owner, clientName, clientPort)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tunnel, nil
|
||||
}
|
||||
|
||||
func (a *Api) handleTunnels(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
token, err := extractToken("access_token", r)
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -23,7 +22,7 @@ type WebUiHandler struct {
|
||||
menuHtml template.HTML
|
||||
}
|
||||
|
||||
type IndexData struct {
|
||||
type TunnelsData struct {
|
||||
Head template.HTML
|
||||
Menu template.HTML
|
||||
Tunnels map[string]Tunnel
|
||||
@ -171,30 +170,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
case "/":
|
||||
|
||||
indexTemplate, err := box.String("index.tmpl")
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
io.WriteString(w, "Error reading index.tmpl")
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.New("index").Parse(indexTemplate)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
log.Println(err)
|
||||
io.WriteString(w, "Error compiling index.tmpl")
|
||||
return
|
||||
}
|
||||
|
||||
indexData := IndexData{
|
||||
Head: h.headHtml,
|
||||
Menu: h.menuHtml,
|
||||
Tunnels: h.api.GetTunnels(tokenData),
|
||||
}
|
||||
|
||||
tmpl.Execute(w, indexData)
|
||||
|
||||
http.Redirect(w, r, "/tunnels", 302)
|
||||
case "/tunnels":
|
||||
h.handleTunnels(w, r, tokenData)
|
||||
case "/confirm-delete-tunnel":
|
||||
@ -350,6 +326,29 @@ func (h *WebUiHandler) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *WebUiHandler) handleTunnels(w http.ResponseWriter, r *http.Request, tokenData TokenData) {
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
tunnelsTemplate, err := h.box.String("tunnels.tmpl")
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
io.WriteString(w, "Error reading tunnels.tmpl")
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.New("tunnels").Parse(tunnelsTemplate)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
log.Println(err)
|
||||
io.WriteString(w, "Error compiling tunnels.tmpl")
|
||||
return
|
||||
}
|
||||
|
||||
tunnelsData := TunnelsData{
|
||||
Head: h.headHtml,
|
||||
Menu: h.menuHtml,
|
||||
Tunnels: h.api.GetTunnels(tokenData),
|
||||
}
|
||||
|
||||
tmpl.Execute(w, tunnelsData)
|
||||
case "POST":
|
||||
h.handleCreateTunnel(w, r, tokenData)
|
||||
default:
|
||||
@ -363,35 +362,7 @@ func (h *WebUiHandler) handleCreateTunnel(w http.ResponseWriter, r *http.Request
|
||||
|
||||
r.ParseForm()
|
||||
|
||||
if len(r.Form["domain"]) != 1 {
|
||||
w.WriteHeader(400)
|
||||
h.alertDialog(w, r, "Invalid domain parameter", "/")
|
||||
return
|
||||
}
|
||||
domain := r.Form["domain"][0]
|
||||
|
||||
if len(r.Form["client-name"]) != 1 {
|
||||
w.WriteHeader(400)
|
||||
h.alertDialog(w, r, "Invalid client-name parameter", "/")
|
||||
return
|
||||
}
|
||||
clientName := r.Form["client-name"][0]
|
||||
|
||||
if len(r.Form["client-port"]) != 1 {
|
||||
w.WriteHeader(400)
|
||||
h.alertDialog(w, r, "Invalid client-port parameter", "/")
|
||||
return
|
||||
}
|
||||
|
||||
clientPort, err := strconv.Atoi(r.Form["client-port"][0])
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
h.alertDialog(w, r, "Invalid client-port parameter", "/")
|
||||
return
|
||||
}
|
||||
|
||||
//fmt.Println(domain, clientName, clientPort)
|
||||
_, err = h.tunMan.CreateTunnelForClient(domain, tokenData.Owner, clientName, clientPort)
|
||||
_, err := h.api.CreateTunnel(tokenData, r.Form)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
h.alertDialog(w, r, err.Error(), "/")
|
||||
|
@ -1,7 +1,7 @@
|
||||
<label id='menu-label' for='menu-toggle'>Menu</label>
|
||||
<input type='checkbox' id='menu-toggle'/>
|
||||
<div class='menu'>
|
||||
<a class='menu-item' href='/'>Tunnels</a>
|
||||
<a class='menu-item' href='/tunnels'>Tunnels</a>
|
||||
<a class='menu-item' href='/tokens'>Tokens</a>
|
||||
{{if .IsAdmin}}
|
||||
<a class='menu-item' href='/users'>Users</a>
|
||||
|
Loading…
Reference in New Issue
Block a user