Skip to content

Instantly share code, notes, and snippets.

@Apurer
Created May 12, 2024 05:12
Show Gist options
  • Save Apurer/bf4e0f65f58cc970358be01beff00749 to your computer and use it in GitHub Desktop.
Save Apurer/bf4e0f65f58cc970358be01beff00749 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/tls"
"encoding/base64"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
// Define the target server URL to which all requests will be forwarded
target := "https://example.com" // Ensure to use HTTPS, change to your target server URL
// Username and Password for Basic Authentication
username := "your_username"
password := "your_password"
// Encode credentials
credentials := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
// Parse the target URL
url, err := url.Parse(target)
if err != nil {
panic(err)
}
// Create a reverse proxy handler
proxy := httputil.NewSingleHostReverseProxy(url)
proxy.Director = func(req *http.Request) {
req.URL.Scheme = url.Scheme
req.URL.Host = url.Host
req.Host = url.Host // Optionally set the 'Host' header to the target host
// Set the Authorization header
req.Header.Set("Authorization", "Basic " + credentials)
}
// Custom HTTP transport to skip SSL certificate validation
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Start an HTTP server with the proxy handler
http.ListenAndServe(":8080", proxy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment