mirror of
https://github.com/gabehf/koito-multi-proxy.git
synced 2026-03-07 13:38:13 -08:00
proxy instead of redirect
This commit is contained in:
parent
6760d0ecff
commit
fe568ea882
1 changed files with 27 additions and 13 deletions
40
main.go
40
main.go
|
|
@ -3,6 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -27,7 +29,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Configure Routes
|
// 2. Configure Routes
|
||||||
http.HandleFunc("/", redirectHandler)
|
http.HandleFunc("/", proxyHandler)
|
||||||
|
|
||||||
// 3. Start Server
|
// 3. Start Server
|
||||||
port := ":4111"
|
port := ":4111"
|
||||||
|
|
@ -60,29 +62,41 @@ func loadConfig(filename string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func redirectHandler(w http.ResponseWriter, r *http.Request) {
|
func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get the Authorization header
|
|
||||||
authHeader := r.Header.Get("Authorization")
|
authHeader := r.Header.Get("Authorization")
|
||||||
|
|
||||||
// Check if header is missing or doesn't start with "Token "
|
|
||||||
if authHeader == "" || !strings.HasPrefix(authHeader, "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", http.StatusUnauthorized)
|
||||||
http.Error(w, "Unauthorized: Missing or malformed token", http.StatusUnauthorized)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the actual token string (remove "Token " prefix)
|
|
||||||
token := strings.TrimPrefix(authHeader, "Token ")
|
token := strings.TrimPrefix(authHeader, "Token ")
|
||||||
|
|
||||||
// Lookup the token in our map
|
targetStr, exists := urlMap[token]
|
||||||
targetURL, exists := urlMap[token]
|
|
||||||
if !exists {
|
if !exists {
|
||||||
log.Printf("Warning: Request received with unknown token: %s", token)
|
|
||||||
http.Error(w, "Unauthorized: Invalid token", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized: Invalid token", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform the redirect
|
// Parse the target URL
|
||||||
// StatusFound (302) is generally used for temporary redirects
|
targetURL, err := url.Parse(targetStr)
|
||||||
http.Redirect(w, r, targetURL, http.StatusFound)
|
if err != nil {
|
||||||
|
http.Error(w, "Bad Gateway", http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a Reverse Proxy
|
||||||
|
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
||||||
|
|
||||||
|
// Update the request to match the target
|
||||||
|
r.URL.Host = targetURL.Host
|
||||||
|
r.URL.Scheme = targetURL.Scheme
|
||||||
|
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
|
||||||
|
r.Host = targetURL.Host
|
||||||
|
|
||||||
|
// Note: The original 'Authorization' header is automatically
|
||||||
|
// passed along by the proxy unless explicitly removed.
|
||||||
|
|
||||||
|
// Serve the request via the proxy
|
||||||
|
proxy.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue