2020-05-01 05:47:46 +00:00
|
|
|
package admin
|
2017-03-13 16:43:28 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-01 05:47:46 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2017-03-13 16:43:28 +00:00
|
|
|
"net/http"
|
2017-03-13 21:46:11 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2017-03-13 16:43:28 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
"git.coolaj86.com/coolaj86/go-telebitd/relay/api"
|
2020-05-01 06:12:16 +00:00
|
|
|
"git.coolaj86.com/coolaj86/go-telebitd/relay/mplexy"
|
2020-05-01 05:47:46 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-03-13 16:43:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-04-28 06:26:00 +00:00
|
|
|
endPointPrefix = "/api/org.rootprojects.tunnel/"
|
2017-03-13 16:43:28 +00:00
|
|
|
)
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
var connectionTable *api.Table
|
|
|
|
var serverStatus *api.Status
|
|
|
|
var serverStatusAPI *api.Status
|
2017-03-13 16:43:28 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
//ListenAndServe -
|
2017-03-13 16:43:28 +00:00
|
|
|
// - expecting an existing oneConnListener with a qualified wss client connected.
|
|
|
|
// - auth will happen again since we were just peeking at the token.
|
2020-05-01 06:12:16 +00:00
|
|
|
func ListenAndServe(mx *mplexy.MPlexy, adminListener net.Listener) error {
|
2020-05-01 05:47:46 +00:00
|
|
|
//serverStatus = mx.ctx.Value(ctxServerStatus).(*Status)
|
2017-03-20 00:04:47 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
connectionTable = mx.Status.ConnectionTable
|
|
|
|
serverStatusAPI = mx.Status
|
2017-03-13 16:43:28 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
2017-03-15 00:26:01 +00:00
|
|
|
router.PathPrefix("/admin/").Handler(http.StripPrefix("/admin/", http.FileServer(http.Dir("html/admin"))))
|
2017-03-13 16:43:28 +00:00
|
|
|
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("HandleFunc /")
|
|
|
|
|
|
|
|
_, err := mx.AuthorizeAdmin(r)
|
|
|
|
if err == nil {
|
|
|
|
// TODO
|
|
|
|
w.Write([]byte("TODO: handle bad auth"))
|
|
|
|
return
|
|
|
|
}
|
2017-03-23 23:10:49 +00:00
|
|
|
|
|
|
|
serverStatus.AdminStats.IncRequests()
|
|
|
|
|
2017-03-13 16:43:28 +00:00
|
|
|
switch url := r.URL.Path; url {
|
|
|
|
case "/":
|
2020-05-05 04:12:11 +00:00
|
|
|
http.Redirect(w, r, "/admin", 301)
|
|
|
|
serverStatus.AdminStats.IncResponses()
|
|
|
|
return
|
2017-03-13 16:43:28 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "Not Found", 404)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-03-14 01:22:44 +00:00
|
|
|
router.HandleFunc(endPointPrefix+"domains", getDomainsEndpoint).Methods("GET")
|
|
|
|
router.HandleFunc(endPointPrefix+"domain/", getDomainEndpoint).Methods("GET")
|
|
|
|
router.HandleFunc(endPointPrefix+"domain/{domain-name}", getDomainEndpoint).Methods("GET")
|
2017-03-13 21:46:11 +00:00
|
|
|
router.HandleFunc(endPointPrefix+"servers", getServersEndpoint).Methods("GET")
|
|
|
|
router.HandleFunc(endPointPrefix+"server/", getServerEndpoint).Methods("GET")
|
|
|
|
router.HandleFunc(endPointPrefix+"server/{server-id}", getServerEndpoint).Methods("GET")
|
2017-03-20 00:04:47 +00:00
|
|
|
router.HandleFunc(endPointPrefix+"status/", getStatusEndpoint).Methods("GET")
|
2017-03-13 16:43:28 +00:00
|
|
|
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: ":80",
|
|
|
|
Handler: router,
|
|
|
|
}
|
2020-05-01 05:47:46 +00:00
|
|
|
return s.Serve(adminListener)
|
2017-03-13 16:43:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 00:04:47 +00:00
|
|
|
func getStatusEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pc, _, _, _ := runtime.Caller(0)
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println(runtime.FuncForPC(pc).Name())
|
2017-03-20 00:04:47 +00:00
|
|
|
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncRequests()
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
statusContainer := api.NewStatusAPI(serverStatusAPI)
|
2017-03-20 00:04:47 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
env := NewResponse("domains/GET")
|
2017-03-20 00:04:47 +00:00
|
|
|
env.Result = statusContainer
|
|
|
|
env.GenerateWriter(w)
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncResponses()
|
2017-03-20 00:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 01:22:44 +00:00
|
|
|
func getDomainsEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pc, _, _, _ := runtime.Caller(0)
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println(runtime.FuncForPC(pc).Name())
|
2017-03-14 01:22:44 +00:00
|
|
|
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncRequests()
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
domainsContainer := api.NewDomainsAPI(connectionTable.Domains)
|
2017-03-14 01:22:44 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
env := NewResponse("domains/GET")
|
2017-03-14 01:22:44 +00:00
|
|
|
env.Result = domainsContainer
|
|
|
|
env.GenerateWriter(w)
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncResponses()
|
2017-03-14 01:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDomainEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pc, _, _, _ := runtime.Caller(0)
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println(runtime.FuncForPC(pc).Name())
|
2017-03-14 01:22:44 +00:00
|
|
|
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncRequests()
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
env := NewResponse("domain/GET")
|
2017-03-14 01:22:44 +00:00
|
|
|
|
|
|
|
params := mux.Vars(r)
|
|
|
|
if id, ok := params["domain-name"]; !ok {
|
|
|
|
env.Error = "domain-name is missing"
|
|
|
|
env.ErrorURI = r.RequestURI
|
|
|
|
env.ErrorDescription = "domain API requires a domain-name"
|
|
|
|
} else {
|
|
|
|
domainName := id
|
2020-05-01 05:47:46 +00:00
|
|
|
if domainLB, ok := connectionTable.Domains[domainName]; !ok {
|
2017-03-14 01:22:44 +00:00
|
|
|
env.Error = "domain-name was not found"
|
|
|
|
env.ErrorURI = r.RequestURI
|
|
|
|
env.ErrorDescription = "domain-name not found"
|
|
|
|
} else {
|
2020-05-01 05:47:46 +00:00
|
|
|
var domainAPIContainer []*api.ServerDomainAPI
|
2017-03-25 20:01:57 +00:00
|
|
|
conns := domainLB.Connections()
|
|
|
|
for pos := range conns {
|
|
|
|
conn := conns[pos]
|
2020-05-01 05:47:46 +00:00
|
|
|
domainAPI := api.NewServerDomainAPI(conn, conn.DomainTrack[domainName])
|
2017-03-25 20:01:57 +00:00
|
|
|
domainAPIContainer = append(domainAPIContainer, domainAPI)
|
|
|
|
}
|
|
|
|
env.Result = domainAPIContainer
|
2017-03-14 01:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
env.GenerateWriter(w)
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncResponses()
|
2017-03-14 01:22:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 21:46:11 +00:00
|
|
|
func getServersEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pc, _, _, _ := runtime.Caller(0)
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println(runtime.FuncForPC(pc).Name())
|
2017-03-13 21:46:11 +00:00
|
|
|
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncRequests()
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
serverContainer := api.NewServerAPIContainer()
|
2017-03-13 16:43:28 +00:00
|
|
|
|
|
|
|
for c := range connectionTable.Connections() {
|
2020-05-01 05:47:46 +00:00
|
|
|
serverAPI := api.NewServersAPI(c)
|
2017-03-13 16:43:28 +00:00
|
|
|
serverContainer.Servers = append(serverContainer.Servers, serverAPI)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
env := NewResponse("servers/GET")
|
2017-03-13 21:46:11 +00:00
|
|
|
env.Result = serverContainer
|
|
|
|
env.GenerateWriter(w)
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncResponses()
|
2017-03-13 21:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getServerEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pc, _, _, _ := runtime.Caller(0)
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println(runtime.FuncForPC(pc).Name())
|
2017-03-13 21:46:11 +00:00
|
|
|
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncRequests()
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
env := NewResponse("server/GET")
|
2017-03-13 21:46:11 +00:00
|
|
|
|
|
|
|
params := mux.Vars(r)
|
|
|
|
if id, ok := params["server-id"]; !ok {
|
|
|
|
env.Error = "server-id is missing"
|
|
|
|
env.ErrorURI = r.RequestURI
|
|
|
|
env.ErrorDescription = "server API requires a server-id"
|
|
|
|
} else {
|
|
|
|
serverID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
env.Error = "server-id is not an integer"
|
|
|
|
env.ErrorURI = r.RequestURI
|
|
|
|
env.ErrorDescription = "server API requires a server-id"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
conn, err := connectionTable.GetConnection(int64(serverID))
|
|
|
|
if err != nil {
|
|
|
|
env.Error = "server-id was not found"
|
|
|
|
env.ErrorURI = r.RequestURI
|
|
|
|
env.ErrorDescription = "missing server-id, make sure desired service-id is in servers"
|
|
|
|
} else {
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("test")
|
|
|
|
serverAPI := api.NewServerAPI(conn)
|
2017-03-13 21:46:11 +00:00
|
|
|
env.Result = serverAPI
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
env.GenerateWriter(w)
|
2017-03-23 23:10:49 +00:00
|
|
|
serverStatus.AdminStats.IncResponses()
|
2017-03-13 16:43:28 +00:00
|
|
|
}
|