当前位置: 首页>>代码示例>>Golang>>正文


Golang gopensslproxy.NewProxyHttpServer函数代码示例

本文整理汇总了Golang中github.com/g3rk6/gopensslproxy.NewProxyHttpServer函数的典型用法代码示例。如果您正苦于以下问题:Golang NewProxyHttpServer函数的具体用法?Golang NewProxyHttpServer怎么用?Golang NewProxyHttpServer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewProxyHttpServer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestgopensslproxyHijackConnect

func TestgopensslproxyHijackConnect(t *testing.T) {
	proxy := gopensslproxy.NewProxyHttpServer()
	proxy.OnRequest(gopensslproxy.ReqHostIs(srv.Listener.Addr().String())).
		HijackConnect(func(req *http.Request, client net.Conn, ctx *gopensslproxy.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)
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:27,代码来源:proxy_test.go

示例2: TestWithBrowser

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 := gopensslproxy.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")
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:31,代码来源:basic_test.go

示例3: TestContentType

func TestContentType(t *testing.T) {
	proxy := gopensslproxy.NewProxyHttpServer()
	proxy.OnResponse(gopensslproxy.ContentTypeIs("image/png")).DoFunc(func(resp *http.Response, ctx *gopensslproxy.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)
		}
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:29,代码来源:proxy_test.go

示例4: TestBasicAuthWithCurl

func TestBasicAuthWithCurl(t *testing.T) {
	expected := ":c>"
	background := httptest.NewServer(ConstantHanlder(expected))
	defer background.Close()
	proxy := gopensslproxy.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))
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:26,代码来源:basic_test.go

示例5: main

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 := gopensslproxy.NewProxyHttpServer()
	proxy.Verbose = *verbose
	log.Fatal(http.ListenAndServe(*addr, proxy))
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:8,代码来源:main.go

示例6: TestSelfRequest

func TestSelfRequest(t *testing.T) {
	proxy := gopensslproxy.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")
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:8,代码来源:proxy_test.go

示例7: TestHeadReqHasContentLength

func TestHeadReqHasContentLength(t *testing.T) {
	client, l := oneShotProxy(gopensslproxy.NewProxyHttpServer(), t)
	defer l.Close()

	resp, err := client.Head(localFile("test_data/panda.png"))
	panicOnErr(err, "resp to HEAD")
	if resp.Header.Get("Content-Length") == "" {
		t.Error("Content-Length should exist on HEAD requests")
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:10,代码来源:proxy_test.go

示例8: TestNoProxyHeadersHttps

func TestNoProxyHeadersHttps(t *testing.T) {
	s := httptest.NewTLSServer(VerifyNoProxyHeaders{t})
	proxy := gopensslproxy.NewProxyHttpServer()
	proxy.OnRequest().HandleConnect(gopensslproxy.AlwaysMitm)
	client, l := oneShotProxy(proxy, t)
	defer l.Close()
	req, err := http.NewRequest("GET", s.URL, nil)
	panicOnErr(err, "bad request")
	req.Header.Add("Connection", "close")
	req.Header.Add("Proxy-Connection", "close")
	client.Do(req)
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:12,代码来源:proxy_test.go

示例9: main

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 := gopensslproxy.NewProxyHttpServer()
	proxy.OnRequest().HandleConnect(gopensslproxy.AlwaysMitm)
	proxy.OnRequest().DoFunc(func(req *http.Request, ctx *gopensslproxy.ProxyCtx) (*http.Request, *http.Response) {
		return req, nil
	})
	proxy.Verbose = *verbose
	log.Fatal(http.ListenAndServe(*addr, proxy))
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:12,代码来源:mitm.go

示例10: TestConnectHandler

func TestConnectHandler(t *testing.T) {
	proxy := gopensslproxy.NewProxyHttpServer()
	althttps := httptest.NewTLSServer(ConstantHanlder("althttps"))
	proxy.OnRequest().HandleConnectFunc(func(host string, ctx *gopensslproxy.ProxyCtx) (*gopensslproxy.ConnectAction, string) {
		u, _ := url.Parse(althttps.URL)
		return gopensslproxy.OkConnect, u.Host
	})

	client, l := oneShotProxy(proxy, t)
	defer l.Close()
	if resp := string(getOrFail(https.URL+"/alturl", client, t)); resp != "althttps" {
		t.Error("Proxy should redirect CONNECT requests to local althttps server, expected 'althttps' got ", resp)
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:14,代码来源:proxy_test.go

示例11: TestReplaceResponse

func TestReplaceResponse(t *testing.T) {
	proxy := gopensslproxy.NewProxyHttpServer()
	proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *gopensslproxy.ProxyCtx) *http.Response {
		resp.StatusCode = http.StatusOK
		resp.Body = ioutil.NopCloser(bytes.NewBufferString("chico"))
		return resp
	})

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

	if result := string(getOrFail(srv.URL+("/momo"), client, t)); result != "chico" {
		t.Error("hooked response, should be chico, instead:", result)
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:15,代码来源:proxy_test.go

示例12: TestAlwaysHook

func TestAlwaysHook(t *testing.T) {
	proxy := gopensslproxy.NewProxyHttpServer()
	proxy.OnRequest().DoFunc(func(req *http.Request, ctx *gopensslproxy.ProxyCtx) (*http.Request, *http.Response) {
		req.URL.Path = "/bobo"
		return req, nil
	})
	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)
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:15,代码来源:proxy_test.go

示例13: TestgopensslproxyThroughProxy

func TestgopensslproxyThroughProxy(t *testing.T) {
	proxy := gopensslproxy.NewProxyHttpServer()
	proxy2 := gopensslproxy.NewProxyHttpServer()
	doubleString := func(resp *http.Response, ctx *gopensslproxy.ProxyCtx) *http.Response {
		b, err := ioutil.ReadAll(resp.Body)
		panicOnErr(err, "readAll resp")
		resp.Body = ioutil.NopCloser(bytes.NewBufferString(string(b) + " " + string(b)))
		return resp
	}
	proxy.OnRequest().HandleConnect(gopensslproxy.AlwaysMitm)
	proxy.OnResponse().DoFunc(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)
	}

}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:24,代码来源:proxy_test.go

示例14: TestFirstHandlerMatches

func TestFirstHandlerMatches(t *testing.T) {
	proxy := gopensslproxy.NewProxyHttpServer()
	proxy.OnRequest().DoFunc(func(req *http.Request, ctx *gopensslproxy.ProxyCtx) (*http.Request, *http.Response) {
		return nil, gopensslproxy.TextResponse(req, "koko")
	})
	proxy.OnRequest().DoFunc(func(req *http.Request, ctx *gopensslproxy.ProxyCtx) (*http.Request, *http.Response) {
		panic("should never get here, previous response is no null")
	})

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

	if resp := string(getOrFail(srv.URL+"/", client, t)); resp != "koko" {
		t.Error("should return always koko and not", resp)
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:16,代码来源:proxy_test.go

示例15: TestSimpleHttpReqWithProxy

func TestSimpleHttpReqWithProxy(t *testing.T) {

	client, s := oneShotProxy(gopensslproxy.NewProxyHttpServer(), t)
	defer s.Close()

	if r := string(getOrFail(srv.URL+"/bobo", client, t)); r != "bobo" {
		t.Error("proxy server does not serve constant handlers", r)
	}

	if r := string(getOrFail(srv.URL+"/bobo", client, t)); r != "bobo" {
		t.Error("proxy server does not serve constant handlers", r)
	}

	if string(getOrFail(https.URL+"/bobo", client, t)) != "bobo" {
		t.Error("TLS server does not serve constant handlers, when proxy is used")
	}
}
开发者ID:g3rk6,项目名称:gopensslproxy,代码行数:17,代码来源:proxy_test.go


注:本文中的github.com/g3rk6/gopensslproxy.NewProxyHttpServer函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。