mirror of
https://github.com/safing/portbase
synced 2025-09-02 10:40:39 +00:00
Release to master
This commit is contained in:
commit
d8bf015caa
2 changed files with 34 additions and 20 deletions
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -16,6 +17,10 @@ var (
|
||||||
|
|
||||||
authFnLock sync.Mutex
|
authFnLock sync.Mutex
|
||||||
authFn Authenticator
|
authFn Authenticator
|
||||||
|
|
||||||
|
// ErrAPIAccessDeniedMessage should be returned by Authenticator functions in
|
||||||
|
// order to signify a blocked request, including a error message for the user.
|
||||||
|
ErrAPIAccessDeniedMessage = errors.New("")
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -28,7 +33,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Authenticator is a function that can be set as the authenticator for the API endpoint. If none is set, all requests will be allowed.
|
// Authenticator is a function that can be set as the authenticator for the API endpoint. If none is set, all requests will be allowed.
|
||||||
type Authenticator func(s *http.Server, r *http.Request) (grantAccess bool, err error)
|
type Authenticator func(s *http.Server, r *http.Request) (err error)
|
||||||
|
|
||||||
// SetAuthenticator sets an authenticator function for the API endpoint. If none is set, all requests will be allowed.
|
// SetAuthenticator sets an authenticator function for the API endpoint. If none is set, all requests will be allowed.
|
||||||
func SetAuthenticator(fn Authenticator) error {
|
func SetAuthenticator(fn Authenticator) error {
|
||||||
|
@ -79,15 +84,15 @@ func authMiddleware(next http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get auth decision
|
// get auth decision
|
||||||
grantAccess, err := authenticator(server, r)
|
err = authenticator(server, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("api: authenticator failed: %s", err)
|
if errors.Is(err, ErrAPIAccessDeniedMessage) {
|
||||||
http.Error(w, "Bad Request: Could not identify client", http.StatusBadRequest)
|
log.Warningf("api: denying api access to %s", r.RemoteAddr)
|
||||||
return
|
http.Error(w, err.Error(), http.StatusForbidden)
|
||||||
}
|
} else {
|
||||||
if !grantAccess {
|
log.Warningf("api: authenticator failed: %s", err)
|
||||||
log.Warningf("api: denying api access to %s", r.RemoteAddr)
|
http.Error(w, "Internal server error during authentication.", http.StatusInternalServerError)
|
||||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
31
run/main.go
31
run/main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
|
@ -36,6 +37,10 @@ func Run() int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if printStackOnExit {
|
||||||
|
printStackTo(os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
_ = modules.Shutdown()
|
_ = modules.Shutdown()
|
||||||
return modules.GetExitStatusCode()
|
return modules.GetExitStatusCode()
|
||||||
}
|
}
|
||||||
|
@ -78,20 +83,13 @@ signalLoop:
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if printStackOnExit {
|
if printStackOnExit {
|
||||||
fmt.Println("=== PRINTING TRACES ===")
|
printStackTo(os.Stdout)
|
||||||
fmt.Println("=== GOROUTINES ===")
|
|
||||||
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
||||||
fmt.Println("=== BLOCKING ===")
|
|
||||||
_ = pprof.Lookup("block").WriteTo(os.Stdout, 1)
|
|
||||||
fmt.Println("=== MUTEXES ===")
|
|
||||||
_ = pprof.Lookup("mutex").WriteTo(os.Stdout, 1)
|
|
||||||
fmt.Println("=== END TRACES ===")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(60 * time.Second)
|
time.Sleep(3 * time.Minute)
|
||||||
fmt.Fprintln(os.Stderr, "===== TAKING TOO LONG FOR SHUTDOWN - PRINTING STACK TRACES =====")
|
fmt.Fprintln(os.Stderr, "===== TAKING TOO LONG FOR SHUTDOWN =====")
|
||||||
_ = pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
|
printStackTo(os.Stderr)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -124,3 +122,14 @@ func inputSignals(signalCh chan os.Signal) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printStackTo(writer io.Writer) {
|
||||||
|
fmt.Fprintln(writer, "=== PRINTING TRACES ===")
|
||||||
|
fmt.Fprintln(writer, "=== GOROUTINES ===")
|
||||||
|
_ = pprof.Lookup("goroutine").WriteTo(writer, 1)
|
||||||
|
fmt.Fprintln(writer, "=== BLOCKING ===")
|
||||||
|
_ = pprof.Lookup("block").WriteTo(writer, 1)
|
||||||
|
fmt.Fprintln(writer, "=== MUTEXES ===")
|
||||||
|
_ = pprof.Lookup("mutex").WriteTo(writer, 1)
|
||||||
|
fmt.Fprintln(writer, "=== END TRACES ===")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue