Created
February 15, 2017 13:18
-
-
Save JosephRedfern/b953761fcde9fffb3e996dab12e8b94c to your computer and use it in GitHub Desktop.
Using Google as HTTP Proxy (limited to GET)
This file contains 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 ( | |
"bytes" | |
"fmt" | |
"github.com/elazarl/goproxy" | |
"gopkg.in/h2non/filetype.v1" | |
"log" | |
"net/http" | |
) | |
func main() { | |
proxy := goproxy.NewProxyHttpServer() | |
proxy_url := "https://feedback.googleusercontent.com/gadgets/proxy?container=fbk&url=%s" | |
proxy.OnRequest().DoFunc( | |
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { | |
proxy_req_url := fmt.Sprintf(proxy_url, r.URL.String()) | |
resp, _ := http.Get(proxy_req_url) | |
bodyBuffer := new(bytes.Buffer) | |
bodyBuffer.ReadFrom(resp.Body) | |
body := bodyBuffer.String() | |
kind, unknown := filetype.Match(bodyBuffer.Bytes()) | |
if unknown == nil { | |
log.Printf("Unknown content type for URL: %s, using %s", r.URL.String(), kind.MIME.Value) | |
} | |
return r, goproxy.NewResponse(r, | |
kind.MIME.Value, resp.StatusCode, | |
body) | |
}) | |
log.Fatal(http.ListenAndServe(":8080", proxy)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment