diff --git a/admin/static/test.html b/admin/html/test.html similarity index 100% rename from admin/static/test.html rename to admin/html/test.html diff --git a/rvpn/api/setup.go b/rvpn/api/setup.go new file mode 100644 index 0000000..4b70413 --- /dev/null +++ b/rvpn/api/setup.go @@ -0,0 +1,17 @@ +package api + +import ( + "log" + "os" +) + +var ( + loginfo *log.Logger + logdebug *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile +) + +func init() { + loginfo = log.New(os.Stdout, "INFO: api: ", logFlags) + logdebug = log.New(os.Stdout, "DEBUG: api:", logFlags) +} diff --git a/rvpn/genericlistener/api_endpoints.go b/rvpn/genericlistener/api_endpoints.go new file mode 100644 index 0000000..92ffa03 --- /dev/null +++ b/rvpn/genericlistener/api_endpoints.go @@ -0,0 +1,25 @@ +package genericlistener + +import "net/http" + +type apiEndPoint struct { + pack string + endpoint string + method func(w http.ResponseWriter, r *http.Request) +} + +type APIEndPoints struct { + endPoint map[string]*apiEndPoint +} + +//NewAPIEndPoints -- Constructor +func NewAPIEndPoints() (p *APIEndPoints) { + p = new(apiEndPoints) + p.endPoint = make(map[string]*apiEndPoint) + return +} + +func (p *apiEndPoints) add(pack string, endpoint string, method func(w http.ResponseWriter, r *http.Request)) { + + router.HandleFunc("/api/"+rDNSPackageName+"servers", apiServers) +} diff --git a/rvpn/genericlistener/api_interface.go b/rvpn/genericlistener/api_interface.go new file mode 100644 index 0000000..fc6ac23 --- /dev/null +++ b/rvpn/genericlistener/api_interface.go @@ -0,0 +1,79 @@ +package genericlistener + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/gorilla/mux" +) + +const ( + rDNSPackageName = "com.daplie.rvpn" +) + +var connectionTable *Table + +//handleAdminClient - +// - expecting an existing oneConnListener with a qualified wss client connected. +// - auth will happen again since we were just peeking at the token. +func handleAdminClient(ctx context.Context, oneConn *oneConnListener) { + connectionTable = ctx.Value(ctxConnectionTable).(*Table) + router := mux.NewRouter().StrictSlash(true) + + endpoints := make(map[string]string) + + router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + loginfo.Println("HandleFunc /") + switch url := r.URL.Path; url { + case "/": + // check to see if we are using the administrative Host + if strings.Contains(r.Host, "rvpn.daplie.invalid") { + http.Redirect(w, r, "/admin", 301) + } + + default: + http.Error(w, "Not Found", 404) + } + }) + + router.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + fmt.Fprintln(w, "Welcome..press Servers to access stats") + }) + + router.HandleFunc("/api/"+rDNSPackageName+"servers", apiServers) + + s := &http.Server{ + Addr: ":80", + Handler: router, + } + + err := s.Serve(oneConn) + if err != nil { + loginfo.Println("Serve error: ", err) + } + + select { + case <-ctx.Done(): + loginfo.Println("Cancel signal hit") + return + } +} + +func apiServers(w http.ResponseWriter, r *http.Request) { + fmt.Println("here") + serverContainer := NewServerAPIContainer() + + for c := range connectionTable.Connections() { + serverAPI := NewServerAPI(c) + serverContainer.Servers = append(serverContainer.Servers, serverAPI) + + } + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + json.NewEncoder(w).Encode(serverContainer) + +} diff --git a/rvpn/genericlistener/listener_generic.go b/rvpn/genericlistener/listener_generic.go index 07baf22..5ad20c8 100644 --- a/rvpn/genericlistener/listener_generic.go +++ b/rvpn/genericlistener/listener_generic.go @@ -5,8 +5,6 @@ import ( "context" "crypto/tls" "encoding/hex" - "encoding/json" - "fmt" "log" "net" "strconv" @@ -337,65 +335,6 @@ func handleExternalHTTPRequest(ctx context.Context, extConn *WedgeConn, hostname } } -//handleAdminClient - -// - expecting an existing oneConnListener with a qualified wss client connected. -// - auth will happen again since we were just peeking at the token. -func handleAdminClient(ctx context.Context, oneConn *oneConnListener) { - connectionTable := ctx.Value(ctxConnectionTable).(*Table) - - router := mux.NewRouter().StrictSlash(true) - - router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - loginfo.Println("HandleFunc /") - switch url := r.URL.Path; url { - case "/": - // check to see if we are using the administrative Host - if strings.Contains(r.Host, "rvpn.daplie.invalid") { - http.Redirect(w, r, "/admin", 301) - } - - default: - http.Error(w, "Not Found", 404) - } - }) - - router.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - fmt.Fprintln(w, "Welcome..press Servers to access stats") - }) - - router.HandleFunc("/api/servers", func(w http.ResponseWriter, r *http.Request) { - fmt.Println("here") - serverContainer := NewServerAPIContainer() - - for c := range connectionTable.Connections() { - serverAPI := NewServerAPI(c) - serverContainer.Servers = append(serverContainer.Servers, serverAPI) - - } - - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - json.NewEncoder(w).Encode(serverContainer) - - }) - - s := &http.Server{ - Addr: ":80", - Handler: router, - } - - err := s.Serve(oneConn) - if err != nil { - loginfo.Println("Serve error: ", err) - } - - select { - case <-ctx.Done(): - loginfo.Println("Cancel signal hit") - return - } -} - //handleWssClient - // - expecting an existing oneConnListener with a qualified wss client connected. // - auth will happen again since we were just peeking at the token.