Пример #1
0
func TestGoproxyHijackConnect(t *testing.T) {
	proxy := goproxy.NewProxyHttpServer()

	hijackHandler := goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next {
		req := ctx.Req

		client := ctx.HijackConnect()

		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()

		return goproxy.DONE
	})
	proxy.HandleRequest(goproxy.RequestHostIsIn(srv.Listener.Addr().String())(hijackHandler))

	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)
	}
}
Пример #2
0
func TestGoproxyThroughProxy(t *testing.T) {
	proxy := goproxy.NewProxyHttpServer()
	proxy2 := goproxy.NewProxyHttpServer()
	doubleString := goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next {
		resp := ctx.Resp
		b, err := ioutil.ReadAll(resp.Body)
		panicOnErr(err, "readAll resp")
		resp.Body = ioutil.NopCloser(bytes.NewBufferString(string(b) + " " + string(b)))
		return goproxy.NEXT
	})
	proxy.HandleConnect(goproxy.AlwaysMitm)
	proxy.HandleResponse(doubleString)

	_, l := oneShotProxy(proxy, t)
	defer l.Close()

	proxy2.ConnectDial = proxy2.NewConnectDialToProxy(l.URL)

	client, l2 := oneShotProxy(proxy2, t)
	defer l2.Close()
	if r := string(getOrFail(https.URL+"/bobo", client, t)); r != "bobo bobo" {
		t.Error("Expected bobo doubled twice, got", r)
	}

}
Пример #3
0
func TestContentType(t *testing.T) {
	proxy := goproxy.NewProxyHttpServer()

	mangleImage := goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next {
		ctx.Resp.Header.Set("X-Shmoopi", "1")
		return goproxy.NEXT
	})
	proxy.HandleResponse(goproxy.RespContentTypeIs("image/png")(mangleImage))

	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)
		}
	}
}
Пример #4
0
func main() {
	proxy := goproxy.NewProxyHttpServer()

	daytimeBlocker := goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next {
		if h, _, _ := time.Now().Clock(); h >= 8 && h <= 17 {
			ctx.NewResponse(http.StatusForbidden, "text/plain", "Don't waste your time!")
			return goproxy.FORWARD
		}
		return goproxy.NEXT
	})
	proxy.HandleRequest(goproxy.RequestHostIsIn("www.reddit.com")(daytimeBlocker))

	log.Fatalln(proxy.ListenAndServe(":8080"))
}
Пример #5
0
func TestSimpleConditionalHook(t *testing.T) {
	proxy := goproxy.NewProxyHttpServer()

	mangleRequestPath := goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next {
		ctx.Req.URL.Path = "/bobo"
		return goproxy.NEXT
	})
	proxy.HandleRequest(goproxy.RemoteAddrIs("127.0.0.1")(mangleRequestPath))
	client, l := oneShotProxy(proxy, t)
	defer l.Close()

	if result := string(getOrFail(srv.URL+("/momo"), client, t)); result != "bobo" {
		t.Error("Redirecting all requests from 127.0.0.1 to bobo, didn't work." +
			" (Might break if Go's client sets RemoteAddr to IPv6 address). Got: " +
			result)
	}
}
Пример #6
0
func TestMitmIsFiltered(t *testing.T) {
	proxy := goproxy.NewProxyHttpServer()
	//proxy.Verbose = true
	proxy.HandleConnect(goproxy.AlwaysMitm)
	// PREVIOUSLY: proxy.OnRequest(goproxy.ReqHostIs(https.Listener.Addr().String())).HandleConnect(goproxy.AlwaysMitm)
	proxy.HandleRequest(goproxy.UrlIsIn("/momo")(goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next {
		ctx.NewTextResponse("koko")
		return goproxy.FORWARD
	})))

	client, l := oneShotProxy(proxy, t)
	defer l.Close()

	if resp := string(getOrFail(https.URL+"/momo", client, t)); resp != "koko" {
		t.Error("Proxy should capture /momo to be koko and not", resp)
	}

	if resp := string(getOrFail(https.URL+"/bobo", client, t)); resp != "bobo" {
		t.Error("But still /bobo should be 'bobo' and not", resp)
	}
}
Пример #7
0
func TestReplaceReponseForUrl(t *testing.T) {
	proxy := goproxy.NewProxyHttpServer()
	proxy.HandleResponse(goproxy.UrlIsIn("/koko")(goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next {
		if ctx.Resp == nil {
			return goproxy.NEXT
		}
		ctx.Resp.StatusCode = http.StatusOK
		ctx.SetResponseBody([]byte("chico"))

		return goproxy.NEXT
	})))

	client, l := oneShotProxy(proxy, t)
	defer l.Close()

	if result := string(getOrFail(srv.URL+("/koko"), client, t)); result != "chico" {
		t.Error("hooked 'koko', should be chico, instead:", result)
	}
	if result := string(getOrFail(srv.URL+("/bobo"), client, t)); result != "bobo" {
		t.Error("still, bobo should stay as usual, instead:", result)
	}
}