working pre-unique
This commit is contained in:
parent
9566e0666e
commit
2da51b1344
1 changed files with 94 additions and 34 deletions
|
@ -2,6 +2,7 @@ package keydbextension
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -14,18 +15,26 @@ import (
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const authTokenHeader = "X-GuardianInternal-Token"
|
||||||
|
|
||||||
|
// init registers the KeyDBHandler module with Caddy and registers the "keydb" directive
|
||||||
|
// for use in the Caddyfile configuration.
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterModule(KeyDBHandler{})
|
caddy.RegisterModule(KeyDBHandler{})
|
||||||
httpcaddyfile.RegisterHandlerDirective("keydb", parseCaddyfile)
|
httpcaddyfile.RegisterHandlerDirective("keydb", parseCaddyfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KeyDBHandler is a Caddy module that provides a HTTP handler for interacting with a KeyDB server.
|
||||||
|
// It allows retrieving values from the KeyDB server based on a provided hash parameter.
|
||||||
type KeyDBHandler struct {
|
type KeyDBHandler struct {
|
||||||
Address string `json:"address"`
|
Address string `json:"address"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
DB int `json:"db"`
|
DB int `json:"db"`
|
||||||
client *redis.Client
|
client *redis.Client
|
||||||
|
validTokens map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CaddyModule returns module information for use by Caddy.
|
||||||
func (KeyDBHandler) CaddyModule() caddy.ModuleInfo {
|
func (KeyDBHandler) CaddyModule() caddy.ModuleInfo {
|
||||||
return caddy.ModuleInfo{
|
return caddy.ModuleInfo{
|
||||||
ID: "http.handlers.keydb",
|
ID: "http.handlers.keydb",
|
||||||
|
@ -33,75 +42,126 @@ func (KeyDBHandler) CaddyModule() caddy.ModuleInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *KeyDBHandler) Provision(ctx caddy.Context) error {
|
// Provision initializes the KeyDBHandler by creating a new Redis client with the configured address, password, and database index.
|
||||||
if h.DB < 0 || h.DB > 15 {
|
// The Redis client is the most robust and is backwards compatible with KeyDB.
|
||||||
return fmt.Errorf("invalid db value: %d", h.DB)
|
func (handler *KeyDBHandler) Provision(ctx caddy.Context) error {
|
||||||
|
// The database index must be between 0 and 15 (inclusive).
|
||||||
|
if handler.DB < 0 || handler.DB > 15 {
|
||||||
|
return fmt.Errorf("invalid db value: %d", handler.DB)
|
||||||
}
|
}
|
||||||
h.client = redis.NewClient(&redis.Options{
|
handler.client = redis.NewClient(&redis.Options{
|
||||||
Addr: h.Address,
|
Addr: handler.Address,
|
||||||
Password: h.Password,
|
Password: handler.Password,
|
||||||
DB: h.DB, // Directly use h.DB
|
DB: handler.DB,
|
||||||
})
|
})
|
||||||
|
handler.validTokens = map[string]bool{
|
||||||
|
"token1": true,
|
||||||
|
"token2": true,
|
||||||
|
"token3": true,
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h KeyDBHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
// ServeHTTP is the HTTP handler for the KeyDBHandler module. It retrieves a value from the KeyDB server
|
||||||
hash := r.URL.Query().Get("hash")
|
// based on the "q" query parameter provided in the request. If the q parameter is missing, it returns
|
||||||
if hash == "" {
|
// a 400 Bad Request error. If the value is not found in the KeyDB server, it returns a 404 Not Found error.
|
||||||
http.Error(w, "Missing hash parameter", http.StatusBadRequest)
|
// If there is an error interacting with the KeyDB server, it returns a 500 Internal Server Error.
|
||||||
|
func (handler KeyDBHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request, next caddyhttp.Handler) error {
|
||||||
|
authToken := request.Header.Get(authTokenHeader)
|
||||||
|
if authToken == "" {
|
||||||
|
sendJSONError(writer, http.StatusUnauthorized, "No auth token provided")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !handler.validTokens[authToken] {
|
||||||
|
sendJSONError(writer, http.StatusForbidden, "Unauthorized access")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
question := request.URL.Query().Get("q")
|
||||||
|
if question == "" {
|
||||||
|
sendJSONError(writer, http.StatusBadRequest, "Request was incomplete")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
val, err := h.client.Get(ctx, hash).Result()
|
val, err := handler.client.Get(ctx, question).Result()
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
http.Error(w, "Cache miss", http.StatusNotFound)
|
sendJSONError(writer, http.StatusNotFound, "Connection to GuardianPT failed")
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
sendJSONError(writer, http.StatusInternalServerError, "Request could not be processed")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintln(w, val)
|
// Append the auth token to the response content
|
||||||
|
responseContent := fmt.Sprintf("%s\nAuth Token: %s", val, authToken)
|
||||||
|
|
||||||
|
// Write the returned value with the appended auth token to the response writer
|
||||||
|
fmt.Fprintln(writer, responseContent)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *KeyDBHandler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
// UnmarshalCaddyfile parses the Caddyfile configuration for the KeyDBHandler module.
|
||||||
for d.Next() {
|
// It sets the address, password, and database index for the KeyDB connection.
|
||||||
for d.NextBlock(0) {
|
// The database index must be between 0 and 15 (inclusive) and is converted to an integer here for the Redis client.
|
||||||
switch d.Val() {
|
func (handler *KeyDBHandler) UnmarshalCaddyfile(dispenser *caddyfile.Dispenser) error {
|
||||||
|
for dispenser.Next() {
|
||||||
|
for dispenser.NextBlock(0) {
|
||||||
|
switch dispenser.Val() {
|
||||||
case "address":
|
case "address":
|
||||||
if !d.Args(&h.Address) {
|
if !dispenser.Args(&handler.Address) {
|
||||||
return d.ArgErr()
|
return dispenser.ArgErr()
|
||||||
}
|
}
|
||||||
case "password":
|
case "password":
|
||||||
if !d.Args(&h.Password) {
|
if !dispenser.Args(&handler.Password) {
|
||||||
return d.ArgErr()
|
return dispenser.ArgErr()
|
||||||
}
|
}
|
||||||
case "db":
|
case "db":
|
||||||
var dbString string
|
var dbString string
|
||||||
if !d.Args(&dbString) {
|
if !dispenser.Args(&dbString) {
|
||||||
return d.ArgErr()
|
return dispenser.ArgErr()
|
||||||
}
|
}
|
||||||
db, err := strconv.Atoi(dbString)
|
db, err := strconv.Atoi(dbString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
h.DB = db
|
handler.DB = db
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
// parseCaddyfile creates a new KeyDBHandler instance and initializes it by parsing the Caddyfile configuration.
|
||||||
var m KeyDBHandler
|
// This function is used by Caddy to load the KeyDBHandler module from the Caddyfile. It's called once during init.
|
||||||
err := m.UnmarshalCaddyfile(h.Dispenser)
|
func parseCaddyfile(helper httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
return m, err
|
var handler KeyDBHandler
|
||||||
|
err := handler.UnmarshalCaddyfile(helper.Dispenser)
|
||||||
|
return handler, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sendJSONError writes a JSON-formatted error response to the provided http.ResponseWriter.
|
||||||
|
func sendJSONError(w http.ResponseWriter, code int, message string) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(code)
|
||||||
|
|
||||||
|
// Spooky high error codes
|
||||||
|
mysteriousCode := fmt.Sprintf("0x%08X", 0xC0043293+code)
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||||
|
"error": true,
|
||||||
|
"code": mysteriousCode,
|
||||||
|
"message": message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// The KeyDBHandler type implements several interfaces that allow it to be used as a Caddy module:
|
||||||
|
// - caddy.Provisioner: Allows the module to be provisioned and configured.
|
||||||
|
// - caddyhttp.MiddlewareHandler: Allows the module to be used as HTTP middleware.
|
||||||
|
// - caddyfile.Unmarshaler: Allows the module to be configured from a Caddyfile.
|
||||||
var (
|
var (
|
||||||
_ caddy.Provisioner = (*KeyDBHandler)(nil)
|
_ caddy.Provisioner = (*KeyDBHandler)(nil)
|
||||||
_ caddyhttp.MiddlewareHandler = (*KeyDBHandler)(nil)
|
_ caddyhttp.MiddlewareHandler = (*KeyDBHandler)(nil)
|
||||||
|
|
Loading…
Reference in a new issue