本文整理匯總了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)
}
}
示例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")
}
}
示例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)
}
}
}
示例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))
}
}
示例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))
}
示例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")
}
}
示例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")
}
}
示例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)
}
示例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))
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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")
}
}