Created
November 16, 2011 01:52
-
-
Save kylelemons/1369014 to your computer and use it in GitHub Desktop.
A potential way to kill all connections in a proxy server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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