コード例 #1
0
ファイル: proxy_test.go プロジェクト: stoplightio/goproxy
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 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"))
}