123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package main
- import (
- "encoding/json"
- "net/http"
- "imuslab.com/arozos/ReverseProxy/mod/auth"
- "imuslab.com/arozos/ReverseProxy/mod/utils"
- )
- /*
- API.go
- This file contains all the API called by the web management interface
- */
- func initAPIs() {
- requireAuth := !(*noauth || handler.IsUsingExternalPermissionManager())
- authRouter := auth.NewManagedHTTPRouter(auth.RouterOption{
- AuthAgent: authAgent,
- RequireAuth: requireAuth,
- DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
- http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
- },
- })
- //Register the standard web services urls
- fs := http.FileServer(http.Dir("./web"))
- if requireAuth {
- //Add a layer of middleware for auth control
- authHandler := AuthFsHandler(fs)
- http.Handle("/", authHandler)
- } else {
- http.Handle("/", fs)
- }
- //Auth APIs
- authRouter.HandleFunc("/api/auth/login", authAgent.HandleLogin)
- authRouter.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
- authRouter.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) {
- if requireAuth {
- authAgent.CheckLogin(w, r)
- } else {
- utils.SendJSONResponse(w, "true")
- }
- })
- authRouter.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) {
- username, err := authAgent.GetUserName(w, r)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
- return
- }
- js, _ := json.Marshal(username)
- utils.SendJSONResponse(w, string(js))
- })
- http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) {
- uc := authAgent.GetUserCounts()
- js, _ := json.Marshal(uc)
- utils.SendJSONResponse(w, string(js))
- })
- //Reverse proxy
- authRouter.HandleFunc("/api/proxy/enable", ReverseProxyHandleOnOff)
- authRouter.HandleFunc("/api/proxy/add", ReverseProxyHandleAddEndpoint)
- authRouter.HandleFunc("/api/proxy/status", ReverseProxyStatus)
- authRouter.HandleFunc("/api/proxy/list", ReverseProxyList)
- authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint)
- authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet)
- authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
- //TLS / SSL config
- authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy)
- authRouter.HandleFunc("/api/cert/upload", handleCertUpload)
- authRouter.HandleFunc("/api/cert/list", handleListCertificate)
- authRouter.HandleFunc("/api/cert/checkDefault", handleDefaultCertCheck)
- authRouter.HandleFunc("/api/cert/delete", handleCertRemove)
- //Redirection config
- authRouter.HandleFunc("/api/redirect/list", handleListRedirectionRules)
- authRouter.HandleFunc("/api/redirect/add", handleAddRedirectionRule)
- authRouter.HandleFunc("/api/redirect/delete", handleDeleteRedirectionRule)
- }
|