fix: issue where api calls would 404 sometimes

This commit is contained in:
0x1a8510f2 2022-11-25 01:15:31 +00:00
parent 50472980b7
commit 291b4c6dc5
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D
3 changed files with 84 additions and 73 deletions

View File

@ -84,77 +84,83 @@ func main() {
panic(err)
}
pm.SetWebserverHandlers(map[string]http.Handler{
"/X/": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.EscapedPath(), "/X/")
pm.SetWebserverHandlers([]pmanager.WebserverHandler{
{
Path: "/X/",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.EscapedPath(), "/X/")
switch path {
case "about":
// Require auth.
if !StatusInGroup(AuthStatus(r), AUTH_STATUS_A, AUTH_STATUS_V) {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch path {
case "about":
// Require auth.
if !StatusInGroup(AuthStatus(r), AUTH_STATUS_A, AUTH_STATUS_V) {
w.WriteHeader(http.StatusUnauthorized)
return
}
handleAbout(w)
case "checkauth":
if !StatusInGroup(AuthStatus(r), AUTH_STATUS_A, AUTH_STATUS_V) {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.WriteHeader(http.StatusNoContent)
case "auth":
// Make sure we haven't exceeded the limit for failed logins.
if c.attemptsUntilLockout.Load() <= 0 {
handleAbout(w)
case "checkauth":
if !StatusInGroup(AuthStatus(r), AUTH_STATUS_A, AUTH_STATUS_V) {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.WriteHeader(http.StatusNoContent)
case "auth":
// Make sure we haven't exceeded the limit for failed logins.
if c.attemptsUntilLockout.Load() <= 0 {
w.WriteHeader(http.StatusTeapot)
return
}
// Get the data from the request body.
reqbody, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
reqdata := authRequest{}
err = json.Unmarshal(reqbody, &reqdata)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Validate credential.
outtoken, expiry, status, ok := TradeTokens(c, reqdata)
if !ok {
c.attemptsUntilLockout.Add(-1)
w.WriteHeader(http.StatusUnauthorized)
return
}
// Reset failed attempts counter on successful login.
c.attemptsUntilLockout.Store(STARTING_ATTEMPTS_UNTIL_LOCKOUT)
// Create a response.
response, err := json.Marshal(authSuccessResponse{
Token: string(outtoken),
Expiry: expiry,
Access: status,
})
// Not much we can do.
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
// 200.
w.Write(response)
default:
// If someone makes an API call we don't recognise, we're a teapot.
w.WriteHeader(http.StatusTeapot)
return
}
// Get the data from the request body.
reqbody, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
reqdata := authRequest{}
err = json.Unmarshal(reqbody, &reqdata)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Validate credential.
outtoken, expiry, status, ok := TradeTokens(c, reqdata)
if !ok {
c.attemptsUntilLockout.Add(-1)
w.WriteHeader(http.StatusUnauthorized)
return
}
// Reset failed attempts counter on successful login.
c.attemptsUntilLockout.Store(STARTING_ATTEMPTS_UNTIL_LOCKOUT)
// Create a response.
response, err := json.Marshal(authSuccessResponse{
Token: string(outtoken),
Expiry: expiry,
Access: status,
})
// Not much we can do.
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
// 200.
w.Write(response)
default:
// If someone makes an API call we don't recognise, we're a teapot.
w.WriteHeader(http.StatusTeapot)
}
}),
"/": http.FileServer(http.FS(ui)),
}),
},
{
Path: "/",
Handler: http.FileServer(http.FS(ui)),
},
})
// Start pinecone.

View File

@ -47,7 +47,7 @@ type configSnapshot struct {
// efficiency, to allow nodes which also need to run a regular webserver to
// use the one used by pinecone for websockets. This saves allocating another
// port and other system resources.
webserverHandlers map[string]http.Handler
webserverHandlers []WebserverHandler
}
// This struct represents the configuration for a pinecone manager. Values can be
@ -66,6 +66,11 @@ type config struct {
configSnapshot
}
type WebserverHandler struct {
Path string
Handler http.Handler
}
//
// Internal
//
@ -144,7 +149,7 @@ func (pm *manager) SetStaticPeers(u []string) {
pm.conf.staticPeers = u
}
func (pm *manager) SetWebserverHandlers(u map[string]http.Handler) {
func (pm *manager) SetWebserverHandlers(u []WebserverHandler) {
defer pm.conf.autolock()()
pm.conf.webserverHandlers = u
@ -196,7 +201,7 @@ func (pm *manager) GetStaticPeers() []string {
return pm.conf.staticPeers
}
func (pm *manager) GetWebserverHandlers() map[string]http.Handler {
func (pm *manager) GetWebserverHandlers() []WebserverHandler {
defer pm.conf.autorlock()()
return pm.conf.webserverHandlers

View File

@ -217,8 +217,8 @@ func (pm *manager) Start() {
}
// If additional handlers are configured for the webserver, add them.
for route, handler := range c.webserverHandlers {
httpRouter.PathPrefix(route).Handler(handler)
for _, handler := range c.webserverHandlers {
httpRouter.PathPrefix(handler.Path).Handler(handler.Handler)
}
// Non-pinecone HTTP server.
@ -423,7 +423,7 @@ func GetInstance() *manager {
webserverDebugPath: "",
useMulticast: false,
staticPeers: []string{},
webserverHandlers: map[string]http.Handler{},
webserverHandlers: []WebserverHandler{},
}
// Set default config values to ensure that the config is never