func TestContentType(t *testing.T) { proxy := goproxy.NewProxyHttpServer() proxy.OnResponse(goproxy.ContentTypeIs("image/png")).DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { resp.Header.Set("X-Shmoopi", "1") return resp }) client, l := oneShotProxy(proxy, t) defer l.Close() for _, file := range []string{"test_data/panda.png", "test_data/football.png"} { if resp, err := client.Get(localFile(file)); err != nil || resp.Header.Get("X-Shmoopi") != "1" { if err == nil { t.Error("pngs should have X-Shmoopi header = 1, actually", resp.Header.Get("X-Shmoopi")) } else { t.Error("error reading png", err) } } } file := "baby.jpg" if resp, err := client.Get(localFile(file)); err != nil || resp.Header.Get("X-Shmoopi") != "" { if err == nil { t.Error("Non png images should NOT have X-Shmoopi header at all", resp.Header.Get("X-Shmoopi")) } else { t.Error("error reading png", err) } } }
func startProxy() { verbose := flag.Bool("v", false, "should every proxy request be logged to stdout") addr := flag.String("l", ":8080", "on which address should the proxy listen") flag.Parse() proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose if err := os.MkdirAll("db", 0755); err != nil { log.Fatal("Can't create dir", err) } logger, err := NewLogger("db") if err != nil { log.Fatal("can't open log file", err) } proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { logger.LogReq(req, ctx) return req, nil }) proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { logger.LogResp(resp, ctx) return resp }) l, err := net.Listen("tcp", *addr) if err != nil { log.Fatal("listen:", err) } http.Serve(l, proxy) }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() fmt.Fprintln(os.Stderr, " -h : show help usage") } flag.Parse() if *ip == "" && *iface != "" { *ip = getIpFromInterface(*iface) } else { log.Fatal("IP address or Interface must be specified") } var servers []string if *dnsServers == "" { log.Fatal("DNS servers must be specified") } servers = strings.Split(*dnsServers, " ") r := &resolver.Resolver{Servers: servers, LocalAddr: *ip} proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose proxy.Tr.Dial = func(network, addr string) (c net.Conn, err error) { if network == "tcp" { var remoteAddr *net.TCPAddr var err error localAddr, err := net.ResolveTCPAddr(network, *ip+":0") if err != nil { return nil, err } chunks := strings.Split(addr, ":") addrIp, err := r.LookupAddr(chunks[0]) if err != nil { remoteAddr, err = net.ResolveTCPAddr(network, addr) } else { remoteAddr, err = net.ResolveTCPAddr(network, addrIp+":"+chunks[1]) } if err != nil { return nil, err } return net.DialTCP(network, localAddr, remoteAddr) } return net.Dial(network, addr) } go http.ListenAndServe(*listen, proxy) log.Printf("DaisyProxy listen on %s outgoing from %s\n", *listen, *ip) select {} }
func main() { flag.Parse() proxy := goproxy.NewProxyHttpServer() proxy.Verbose = true println("Proxy server listening on " + *port) log.Fatal(http.ListenAndServe(":"+*port, proxy)) }
func Run() { proxy := goproxy.NewProxyHttpServer() proxy.Verbose = true proxy.OnRequest().DoFunc( func(r *Request, ctx *goproxy.ProxyCtx) (*Request, *Response) { sessions[ctx.Session] = NewRoundTrip(time.Now()) for _, f := range onRequestCallbacks { f(ctx.Req) } return r, nil }) proxy.OnResponse().DoFunc( func(r *Response, ctx *goproxy.ProxyCtx) *Response { for _, f := range onResponseCallbacks { f(ctx.Resp) } if ctx.Resp != nil { roundTrip := sessions[ctx.Session] roundTrip.ResponseTime = time.Now() for _, f := range onDoneCallbacks { f(&DoneRequestData{ctx.Req, ctx.Resp, roundTrip}) } } return r }) log.Fatal(ListenAndServe(listen+":"+port, proxy)) }
func main() { var proxy *goproxy.ProxyHttpServer app.Main(func(a app.App) { var sz size.Event for e := range a.Events() { switch e := app.Filter(e).(type) { case lifecycle.Event: if e.Crosses(lifecycle.StageAlive) == lifecycle.CrossOn && proxy == nil { proxy = goproxy.NewProxyHttpServer() //proxy.Verbose = true re := regexp.MustCompile(`.*`) proxy.OnResponse(goproxy.UrlMatches(re)).DoFunc( func(res *http.Response, ctx *goproxy.ProxyCtx) *http.Response { if label != nil { label.Text = fmt.Sprintf("%s\n%s\n", ctx.Req.URL, label.Text) log.Println(ctx.Req.URL) } return res }) go func() { log.Fatal(http.ListenAndServe(":8888", proxy)) }() } case paint.Event: onPaint(sz) a.EndPaint(e) case size.Event: sz = e } } }) }
func main() { proxy := goproxy.NewProxyHttpServer() proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*baidu.com$"))). HandleConnect(goproxy.AlwaysReject) proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*$"))). HandleConnect(goproxy.AlwaysMitm) // enable curl -p for all hosts on port 80 proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*:80$"))). HijackConnect(func(req *http.Request, client net.Conn, ctx *goproxy.ProxyCtx) { defer func() { if e := recover(); e != nil { ctx.Logf("error connecting to remote: %v", e) client.Write([]byte("HTTP/1.1 500 Cannot reach destination\r\n\r\n")) } client.Close() }() clientBuf := bufio.NewReadWriter(bufio.NewReader(client), bufio.NewWriter(client)) remote, err := net.Dial("tcp", req.URL.Host) orPanic(err) remoteBuf := bufio.NewReadWriter(bufio.NewReader(remote), bufio.NewWriter(remote)) for { req, err := http.ReadRequest(clientBuf.Reader) orPanic(err) orPanic(req.Write(remoteBuf)) orPanic(remoteBuf.Flush()) resp, err := http.ReadResponse(remoteBuf.Reader, req) orPanic(err) orPanic(resp.Write(clientBuf.Writer)) orPanic(clientBuf.Flush()) } }) proxy.Verbose = true log.Fatal(http.ListenAndServe(":8080", proxy)) }
func TestProxy(t *testing.T) { c := httpclient.New(nil) if !c.SupportsProxy() { t.Skipf("current run environment does not support support proxies") } const addr = "127.0.0.1:12345" proxy := goproxy.NewProxyHttpServer() count := 0 proxy.OnRequest().DoFunc( func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { count++ return r, nil }) go func() { http.ListenAndServe(addr, proxy) }() time.Sleep(time.Millisecond) c.SetProxy(func(_ *http.Request) (*url.URL, error) { return url.Parse("http://" + addr) }) testUserAgent(t, c, httpclient.DefaultUserAgent) if count != 1 { t.Errorf("expecting 1 proxy request, got %d instead", count) } testUserAgent(t, c.Clone(nil), httpclient.DefaultUserAgent) if count != 2 { t.Errorf("expecting 2 proxy request, got %d instead", count) } }
func TestGoproxyHijackConnect(t *testing.T) { proxy := goproxy.NewProxyHttpServer() proxy.OnRequest(goproxy.ReqHostIs(srv.Listener.Addr().String())). HijackConnect(func(req *http.Request, client net.Conn, ctx *goproxy.ProxyCtx) { t.Logf("URL %+#v\nSTR %s", req.URL, req.URL.String()) resp, err := http.Get("http:" + req.URL.String() + "/bobo") panicOnErr(err, "http.Get(CONNECT url)") panicOnErr(resp.Write(client), "resp.Write(client)") resp.Body.Close() client.Close() }) client, l := oneShotProxy(proxy, t) defer l.Close() proxyAddr := l.Listener.Addr().String() conn, err := net.Dial("tcp", proxyAddr) panicOnErr(err, "conn "+proxyAddr) buf := bufio.NewReader(conn) writeConnect(conn) readConnectResponse(buf) if txt := readResponse(buf); txt != "bobo" { t.Error("Expected bobo for CONNECT /foo, got", txt) } if r := string(getOrFail(https.URL+"/bobo", client, t)); r != "bobo" { t.Error("Expected bobo would keep working with CONNECT", r) } }
func CreateDefault(listen string, configFile string, verbose bool) { proxy := goproxy.NewProxyHttpServer() proxy.Verbose = verbose records := getConfig(configFile) fmt.Println("Configuration from", configFile, ":") for i, config := range records { if i == 0 { // header line continue } defaultUrl := config[0] regexpUrl, e := regexp.Compile(defaultUrl) if e != nil { fmt.Errorf("Can't parse regexp", defaultUrl, e.Error()) } contentType := config[1] newFile := config[2] fmt.Println("- replace", defaultUrl, "by", newFile) proxy.OnRequest(goproxy.UrlMatches(regexpUrl)).DoFunc( func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { fmt.Print("Intercept ", defaultUrl, " and serve ", newFile) response := NewResponse(r, contentType, http.StatusOK, newFile) return r, response }) } fmt.Println("") fmt.Println("Listen", listen) fmt.Println("^C to stop") fmt.Println("") log.Fatal(http.ListenAndServe(listen, proxy)) }
func main() { verbose := flag.Bool("v", false, "should every proxy request be logged to stdout") flag.Parse() proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose log.Fatal(http.ListenAndServe(":8080", proxy)) }
// NewJQueryVersionProxy creates a proxy checking responses HTML content, looks // for scripts referencing jQuery library and emits warnings if different // versions of the library are being used for a given host. func NewJqueryVersionProxy() *goproxy.ProxyHttpServer { proxy := goproxy.NewProxyHttpServer() m := make(map[string]string) jqueryMatcher := regexp.MustCompile(`(?i:jquery\.)`) proxy.OnResponse(goproxy_html.IsHtml).Do(goproxy_html.HandleString( func(s string, ctx *goproxy.ProxyCtx) string { for _, src := range findScriptSrc(s) { if !jqueryMatcher.MatchString(src) { continue } prev, ok := m[ctx.Req.Host] if ok { if prev != src { ctx.Warnf("In %v, Contradicting jqueries %v %v", ctx.Req.URL, prev, src) break } } else { ctx.Warnf("%s uses jquery %s", ctx.Req.Host, src) m[ctx.Req.Host] = src } } return s })) return proxy }
func ProxyThatModifiesResponsesFromURL(url string, modifier func(*http.Response, *goproxy.ProxyCtx) (newResponse *http.Response, newUrl string)) *goproxy.ProxyHttpServer { proxy := goproxy.NewProxyHttpServer() proxy.OnRequest().DoFunc(func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { dump, _ := httputil.DumpRequest(ctx.Req, true) println(string(dump)) return r, nil }) proxy.OnResponse().DoFunc(func(r *http.Response, ctx *goproxy.ProxyCtx) *http.Response { var coloredStatusCode string status := fmt.Sprintf("[%d]", r.StatusCode) if url != "" && strings.HasPrefix(ctx.Req.URL.String(), url) { r, newUrl := modifier(r, ctx) coloredStatusCode = termcolor.ColoredWithBackground(status, termcolor.White, termcolor.BgMagenta, termcolor.Bold) fmt.Printf("%s %s %v => %s\n", coloredStatusCode, termcolor.Colored(ctx.Req.Method, termcolor.White, termcolor.Bold), ctx.Req.URL, newUrl) return r } switch r.StatusCode { case 301, 302: coloredStatusCode = termcolor.Colored(status, termcolor.Cyan) case 200: coloredStatusCode = termcolor.Colored(status, termcolor.White) case 404, 500: coloredStatusCode = termcolor.Colored(status, termcolor.Red) default: coloredStatusCode = termcolor.Colored(status, termcolor.Magenta) } fmt.Printf("%s %s %v\n", coloredStatusCode, termcolor.Colored(ctx.Req.Method, termcolor.White, termcolor.Bold), ctx.Req.URL) return r }) return proxy }
func main() { var templateFilePath string if len(os.Args) == 1 { templateFilePath = "./template.html" } else if len(os.Args) == 2 { templateFilePath = os.Args[1] } else { panic("Unknown number of arguments. Please enter a template file") } content, err := ioutil.ReadFile(templateFilePath) if err != nil { panic(err) } var htmlStr string = string(content) proxy := goproxy.NewProxyHttpServer() proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*$"))).HandleConnect(goproxy.AlwaysMitm) proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { return req, goproxy.NewResponse(req, goproxy.ContentTypeHtml, http.StatusForbidden, htmlStr) }) log.Fatalln(http.ListenAndServe(":8401", proxy)) }
func main() { addr := flag.String("addr", "[fe80::1%lxdbr0]:3128", "proxy listen address") flag.Parse() proxy := goproxy.NewProxyHttpServer() log.Fatal(http.ListenAndServe(*addr, proxy)) }
func TestWithBrowser(t *testing.T) { // an easy way to check if auth works with webserver // to test, run with // $ go test -run TestWithBrowser -- server // configure a browser to use the printed proxy address, use the proxy // and exit with Ctrl-C. It will throw error if your haven't acutally used the proxy if os.Args[len(os.Args)-1] != "server" { return } proxy := goproxy.NewProxyHttpServer() println("proxy localhost port 8082") access := int32(0) proxy.OnRequest().Do(auth.Basic("my_realm", func(user, passwd string) bool { atomic.AddInt32(&access, 1) return user == "user" && passwd == "1234" })) l, err := net.Listen("tcp", "localhost:8082") if err != nil { t.Fatal(err) } ch := make(chan os.Signal) signal.Notify(ch, os.Interrupt) go func() { <-ch l.Close() }() http.Serve(l, proxy) if access <= 0 { t.Error("No one accessed the proxy") } }
func main() { proxy := goproxy.NewProxyHttpServer() timer := make(chan bool) ch := make(chan Count, 10) go func() { for { time.Sleep(20 * time.Second) timer <- true } }() go func() { m := make(map[string]int64) for { select { case c := <-ch: m[c.Id] = m[c.Id] + c.Count case <-timer: fmt.Printf("statistics\n") for k, v := range m { fmt.Printf("%s -> %d\n", k, v) } } } }() // IsWebRelatedText filters on html/javascript/css resources proxy.OnResponse(goproxy_html.IsWebRelatedText).DoFunc(func(resp *Response, ctx *goproxy.ProxyCtx) *Response { resp.Body = &CountReadCloser{ctx.Req.URL.String(), resp.Body, ch, 0} return resp }) fmt.Printf("listening on :8080\n") log.Fatal(ListenAndServe(":8080", proxy)) }
func main() { proxy := goproxy.NewProxyHttpServer() //proxy.Verbose = true timer := make(chan bool) ch := make(chan Count, 10) go func() { for { time.Sleep(time.Minute * 2) timer <- true } }() go func() { m := make(map[string]int64) for { select { case c := <-ch: m[c.Id] = m[c.Id] + c.Count case <-timer: println("statistics") for k, v := range m { println(k, "->", v) } } } }() proxy.OnResponse(goproxy_html.IsWebRelatedText).DoFunc(func(resp *Response, ctx *goproxy.ProxyCtx) *Response { resp.Body = &CountReadCloser{ctx.Req.URL.String(), resp.Body, ch, 0} return resp }) log.Fatal(ListenAndServe(":8080", proxy)) }
func TestBasicAuthWithCurl(t *testing.T) { expected := ":c>" background := httptest.NewServer(ConstantHanlder(expected)) defer background.Close() proxy := goproxy.NewProxyHttpServer() proxy.OnRequest().Do(auth.Basic("my_realm", func(user, passwd string) bool { return user == "user" && passwd == "open sesame" })) _, proxyserver := oneShotProxy(proxy) defer proxyserver.Close() cmd := exec.Command("curl", "--silent", "--show-error", "-x", proxyserver.URL, "-U", "user:open sesame", "--url", background.URL+"/[1-3]", ) out, err := cmd.CombinedOutput() // if curl got error, it'll show up in stderr if err != nil { t.Fatal(err, string(out)) } finalexpected := times(3, expected) if string(out) != finalexpected { t.Error("Expected", finalexpected, "got", string(out)) } }
func initUpstreamProxy() { go func() { proxy := goproxy.NewProxyHttpServer() proxy.OnRequest().DoFunc( func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { if !hasExpectedCustomHeaders(r.Header) { ctx.Logf("missing expected headers: %+v", ctx.Req.Header) return nil, goproxy.NewResponse(r, goproxy.ContentTypeText, http.StatusUnauthorized, "") } return r, nil }) proxy.OnRequest().HandleConnectFunc( func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) { if !hasExpectedCustomHeaders(ctx.Req.Header) { ctx.Logf("missing expected headers: %+v", ctx.Req.Header) return goproxy.RejectConnect, host } return goproxy.OkConnect, host }) err := http.ListenAndServe("127.0.0.1:2161", proxy) if err != nil { fmt.Printf("upstream proxy failed: %s", err) } }() // TODO: wait until listener is active? }
func main() { flag.Parse() font, err := loadFont() if err != nil { log.Println(err) return } fontHeight := int(createContext(font).PointToFix32(*size) >> 8) // two points to output the text and its shadow ptA := freetype.Pt(*indent+1, *indent+1+fontHeight) ptB := freetype.Pt(*indent, *indent+fontHeight) proxy := goproxy.NewProxyHttpServer() proxy.OnResponse().Do(goproxy_image.HandleImage(func(img image.Image, ctx *goproxy.ProxyCtx) image.Image { outImage := image.NewRGBA(img.Bounds()) draw.Copy(outImage, image.ZP, img, img.Bounds(), nil) text := fmt.Sprintf("%dx%d", img.Bounds().Dx(), img.Bounds().Dy()) fontContext := createContext(font) fontContext.SetClip(img.Bounds()) fontContext.SetDst(outImage) drawString(image.White, fontContext, ptA, text) drawString(image.Black, fontContext, ptB, text) return outImage })) proxy.Verbose = *verbose log.Fatal(http.ListenAndServe(":"+*port, proxy)) }
func main() { proxy := goproxy.NewProxyHttpServer() proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*$"))). HandleConnect(goproxy.AlwaysMitm) proxy.Verbose = true log.Fatal(http.ListenAndServe(":8080", proxy)) }
func main() { flag.Parse() conf = client.LoadConf(*confPath) initMimtConnect() proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose if conf.SSlOn { proxy.OnRequest().HandleConnectFunc(alwaysMitm) } else { proxy.OnRequest().HandleConnectFunc(alwaysHTTPMitm) } proxy.OnRequest().DoFunc(requestHanderFunc) proxy.OnResponse().DoFunc(responseHanderFunc) if conf.ParentProxy != "" { proxy.Tr = &http.Transport{ Proxy: func(req *http.Request) (*url.URL, error) { return url.Parse(conf.ParentProxy) }, } } log.Println("proxy client listen at ", *addr) err := http.ListenAndServe(*addr, proxy) log.Fatal(err) }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [options] PORT\n", os.Args[0]) flag.PrintDefaults() } var host string flag.StringVar(&host, "h", "127.0.0.1", "HTTP server bind HOST") flag.Parse() if len(flag.Args()) < 1 { die("You have to specify a port!", 2) } port, err := strconv.Atoi(flag.Args()[0]) if err != nil || port <= 0 { die("Port has to be a positive integer!", 2) } proxy := goproxy.NewProxyHttpServer() proxy.OnRequest().DoFunc( func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { fmt.Println(req.RequestURI) return req, nil }) log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), proxy)) }
func runProxy() { proxy := goproxy.NewProxyHttpServer() proxyAddress := viper.GetString("master.Address") err := http.ListenAndServe(proxyAddress, proxy) if err != nil { log.Fatal(err.Error()) } }
func TestSelfRequest(t *testing.T) { proxy := goproxy.NewProxyHttpServer() _, l := oneShotProxy(proxy, t) defer l.Close() if !strings.Contains(string(getOrFail(l.URL, http.DefaultClient, t)), "non-proxy") { t.Fatal("non proxy requests should fail") } }
func main() { verbose := flag.Bool("v", false, "should every proxy request be logged to stdout") addr := flag.String("addr", ":8080", "proxy listen address") flag.Parse() proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose log.Fatal(http.ListenAndServe(*addr, proxy)) }
func main() { bl := Create() proxy := goproxy.NewProxyHttpServer() proxy.Verbose = false proxy.OnRequest(goproxy.DstHostIs("nogame")).DoFunc(bl.conf) proxy.OnRequest().DoFunc(bl.checkHost) log.Fatal(http.ListenAndServe(":8080", proxy)) }
func oneShotProxy(t *testing.T) (client *http.Client, proxy *goproxy.ProxyHttpServer, s *httptest.Server) { proxy = goproxy.NewProxyHttpServer() s = httptest.NewServer(proxy) proxyUrl, _ := url.Parse(s.URL) tr := &http.Transport{TLSClientConfig: acceptAllCerts, Proxy: http.ProxyURL(proxyUrl)} client = &http.Client{Transport: tr} return }
func oneShotProxy() (client *http.Client, proxy *goproxy.ProxyHttpServer, s *httptest.Server) { proxy = goproxy.NewProxyHttpServer() s = httptest.NewServer(proxy) proxyUrl, _ := url.Parse(s.URL) tr := &http.Transport{Proxy: http.ProxyURL(proxyUrl)} client = &http.Client{Transport: tr} return }