Skip to content

Instantly share code, notes, and snippets.

@JosephRedfern
Created February 15, 2017 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JosephRedfern/b953761fcde9fffb3e996dab12e8b94c to your computer and use it in GitHub Desktop.
Save JosephRedfern/b953761fcde9fffb3e996dab12e8b94c to your computer and use it in GitHub Desktop.
Using Google as HTTP Proxy (limited to GET)
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