From 143f2557017fcff2bc3b17be6546f410c5dfd62a Mon Sep 17 00:00:00 2001 From: Gabe Farrell Date: Mon, 19 Jan 2026 12:52:25 -0500 Subject: [PATCH] first --- README.md | 3 ++ go.mod | 5 ++++ go.sum | 3 ++ main.go | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..03df832 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# koito-multi-proxy + +Proxies requests based on the token provided in the authorization header. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..855fb15 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/gabehf/koito-multi-proxy + +go 1.25.5 + +require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4bc0337 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..41be685 --- /dev/null +++ b/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "log" + "net/http" + "os" + "strings" + + "gopkg.in/yaml.v3" +) + +// Config structure to match the yaml file +type Config struct { + Mappings []struct { + Token string `yaml:"token"` + URL string `yaml:"url"` + } `yaml:"mappings"` +} + +// Global map to store token -> url associations for fast lookup +var urlMap map[string]string + +func main() { + // 1. Load and Parse Config + if err := loadConfig("config.yml"); err != nil { + log.Fatalf("Failed to load configuration: %v", err) + } + + // 2. Configure Routes + http.HandleFunc("/", redirectHandler) + + // 3. Start Server + port := ":8080" + log.Printf("Server starting on port %s", port) + if err := http.ListenAndServe(port, nil); err != nil { + log.Fatalf("Server failed: %v", err) + } +} + +func loadConfig(filename string) error { + // Read file + data, err := os.ReadFile(filename) + if err != nil { + return err + } + + // Unmarshal YAML + var cfg Config + if err := yaml.Unmarshal(data, &cfg); err != nil { + return err + } + + // Build the map + urlMap = make(map[string]string) + for _, m := range cfg.Mappings { + urlMap[m.Token] = m.URL + } + + log.Printf("Loaded %d token mappings", len(urlMap)) + return nil +} + +func redirectHandler(w http.ResponseWriter, r *http.Request) { + // Get the Authorization header + authHeader := r.Header.Get("Authorization") + + // Check if header is missing or doesn't start with "Token " + if authHeader == "" || !strings.HasPrefix(authHeader, "Token ") { + log.Printf("Warning: Request received with missing or malformed Authorization header from %s", r.RemoteAddr) + http.Error(w, "Unauthorized: Missing or malformed token", http.StatusUnauthorized) + return + } + + // Extract the actual token string (remove "Token " prefix) + token := strings.TrimPrefix(authHeader, "Token ") + + // Lookup the token in our map + targetURL, exists := urlMap[token] + if !exists { + log.Printf("Warning: Request received with unknown token: %s", token) + http.Error(w, "Unauthorized: Invalid token", http.StatusUnauthorized) + return + } + + // Perform the redirect + // StatusFound (302) is generally used for temporary redirects + http.Redirect(w, r, targetURL, http.StatusFound) +}