Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created November 16, 2011 01:52
Show Gist options
  • Select an option

  • Save kylelemons/1369014 to your computer and use it in GitHub Desktop.

Select an option

Save kylelemons/1369014 to your computer and use it in GitHub Desktop.
A potential way to kill all connections in a proxy server
package main
import (
"http"
"net"
"os"
"url"
)
var reverse = http.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: "google.com"})
var cancel = make(chan bool)
func proxy(w http.ResponseWriter, r *http.Request) {
var proxyconn net.Conn
dial := func(network, addr string) (c net.Conn, err os.Error) {
c, err = net.Dial(network, addr)
proxyconn = c
return
}
t := &http.Transport{Dial: dial}
p := &http.ReverseProxy{Transport: t, Director: reverse.Director}
servedone := make(chan bool)
go func() {
p.ServeHTTP(w, r)
close(servedone)
}()
select {
case <-servedone:
case <-cancel:
// close the proxy connection
proxyconn.Close()
// close the client connection
if h, ok := w.(http.Hijacker); ok {
if conn, _, err := h.Hijack(); err == nil {
conn.Close()
}
}
}
}
func main() {
http.HandleFunc("/", proxy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment