Skip to content

Instantly share code, notes, and snippets.

@marten-seemann
Created May 14, 2024 03:41
Show Gist options
  • Save marten-seemann/33db22a3f7f7d957803ca1d574bfeae7 to your computer and use it in GitHub Desktop.
Save marten-seemann/33db22a3f7f7d957803ca1d574bfeae7 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
)
func main() {
var urls []string
for i := 0; i < 1000; i++ {
urls = append(urls, fmt.Sprintf("http://google.com/%d.html", i))
}
proxyURL, err := url.Parse("http://localhost:8080")
if err != nil {
log.Fatal("Error parsing proxy URL:", err)
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
for _, link := range urls {
req, err := http.NewRequest("GET", link, nil)
if err != nil {
log.Println("Error creating request for:", link, err)
continue
}
resp, err := client.Do(req)
if err != nil {
log.Println("Error making request for:", link, err)
continue
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println("Error reading response from:", link, err)
continue
}
log.Printf("URL: %s, Status: %s, Body: %v bytes\n", link, resp.Status, len(body))
}
select {}
}
package main
import (
"io"
"log"
"net/http"
_ "net/http/pprof"
"net/url"
"github.com/quic-go/quic-go/http3"
)
func main() {
client := &http.Client{
Transport: &http3.RoundTripper{},
}
proxyHandler := func(w http.ResponseWriter, r *http.Request) {
// Use the incoming request's host for the upstream URL
upstreamHost := r.Host
upstreamURL, err := url.Parse("https://" + upstreamHost)
if err != nil {
http.Error(w, "Error parsing upstream URL", http.StatusInternalServerError)
return
}
r.URL = upstreamURL
r.Host = upstreamURL.Host
r.RequestURI = "" // Clear the RequestURI to prevent errors in outgoing requests
log.Println("handling request to", r.URL)
// Forward the request using HTTP/3
resp, err := client.Do(r)
if err != nil {
http.Error(w, "Error making HTTP/3 request", http.StatusBadGateway)
return
}
defer resp.Body.Close()
for key, values := range resp.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}
w.WriteHeader(resp.StatusCode)
if _, err := io.Copy(w, resp.Body); err != nil {
log.Println("Error copying response body:", err)
}
}
http.HandleFunc("/", proxyHandler)
log.Println("Starting proxy server on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment