本文整理汇总了Golang中github.com/elazarl/goproxy.NewResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang NewResponse函数的具体用法?Golang NewResponse怎么用?Golang NewResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewResponse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleDialDirectConnection
func handleDialDirectConnection(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
// see who we're gonna call
req.ParseForm()
switch req.Method {
case "POST":
connectionID := req.Form.Get("connectionID")
if connectionID == "" {
log.Printf("Missing connectionID")
return nil, goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusBadRequest,
"Missing connectionID.")
}
log.Printf("handleDirectConnection has connectionID: %s", connectionID)
invitation := getInvitation(connectionID)
if invitation == nil {
log.Printf("Wrong connectionID")
return nil, goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusBadRequest,
"Proxy has been restarted since this page was served. Please log in again.")
}
//log.Printf("invitation is: %#v", invitation)
inviteeName, inviteeHost, err := eccentric.ParseCN(invitation.InviteeCN)
check(err)
log.Printf("invitee is: %v, %v", inviteeName, inviteeHost)
// fetch our own identity
ourCreds := getLoggedInCreds(inviteeHost)
if ourCreds == nil {
// should not happen, as we need to be logged in to get the dial-button, but anyway
log.Println("Site says to dial a direct connection but you are not logged in.")
return nil, goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusInternalServerError,
"Ecca Proxy error: Site says to dial a direct connection but you haven't logged in. Please log in first, then try again.")
}
log.Printf("our creds are: %v\n", ourCreds.CN)
ourCert, err := tls.X509KeyPair(ourCreds.Cert, ourCreds.Priv)
check(err)
//log.Printf("ourCert is: %#v", ourCert)
// call out and show the response
response := dialDirectConnection(invitation, ourCert)
return nil, goproxy.NewResponse(req, goproxy.ContentTypeText, http.StatusOK, response)
}
log.Printf("Unexpected method: %#v", req.Method)
return nil, nil
}
示例2: requestHanderFunc
func requestHanderFunc(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
urlOld := r.URL.String()
log.Println("raw_url->", urlOld)
r.Header.Set("Connection", "Close")
r.Header.Del("Proxy-Connection")
if conf.IsProxyHost(urlOld) {
log.Println("direct IsProxyHost->", urlOld)
return r, nil
}
proxy := conf.GetOneProxy()
if proxy == nil {
log.Println("no proxy")
return r, goproxy.NewResponse(r, goproxy.ContentTypeHtml, http.StatusBadGateway, "no proxy")
}
urlNew, _, err := proxy.GenReqUrl(urlOld)
if err != nil {
log.Println("encryptURL", urlOld, "failed", err)
return r, goproxy.NewResponse(r, goproxy.ContentTypeHtml, http.StatusBadGateway, "encrypt url failed")
}
log.Println(urlOld, "--->", urlNew)
// var err error
r.URL, err = url.Parse(urlNew)
if err != nil {
log.Println("parse new url failed", err)
return r, goproxy.NewResponse(r, goproxy.ContentTypeHtml, http.StatusBadGateway, "create url failed,check proxy url")
}
r.Host = r.URL.Host
r.Header.Add("is_client", "1")
r.Header.Set(kxKey, proxy.SecertKey)
if conf.HiddenIp {
r.Header.Set("hidden_ip", "1")
}
kxutil.HeaderEnc(r.Header)
// body:=r.Body
// reader := kxutil.CipherStreamReader(proxy.SecertKey, encodeURL, body)
// r.Body = ioutil.NopCloser(reader)
// r.Header.Set("_kx_enc_","1")
// panic("a")
return r, nil
}
示例3: OnRequest
func (h *FilteringHandler) OnRequest(r *http.Request, ctx *goproxy.ProxyCtx) (
*http.Request, *http.Response) {
host := r.URL.Host
if host == "" {
host = r.Host
}
rq := &adblock.Request{
URL: r.URL.String(),
Domain: host,
OriginDomain: getReferrerDomain(r),
}
rules := h.Cache.Rules()
start := time.Now()
matched, id := rules.Matcher.Match(rq)
end := time.Now()
duration := end.Sub(start) / time.Millisecond
if matched {
rule := rules.Rules[id]
log.Printf("rejected in %dms: %s\n", duration, r.URL.String())
log.Printf(" by %s\n", rule)
return r, goproxy.NewResponse(r, goproxy.ContentTypeText,
http.StatusNotFound, "Not Found")
}
ctx.UserData = &ProxyState{
Duration: duration,
URL: r.URL.String(),
}
return r, nil
}
示例4: main
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))
}
示例5: initUpstreamProxy
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?
}
示例6: listPackages
func listPackages(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
val, _, _, err := cn.Get("packages")
if err != nil {
return r, nil
}
log.Println("MEMCACHED FROM GO SERVER")
return r, goproxy.NewResponse(r, "application/json", http.StatusOK, val)
}
示例7: conf
// display the configuration
func (bl *BlockClock) conf(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
bl.update()
return r, goproxy.NewResponse(r,
goproxy.ContentTypeText,
http.StatusOK,
fmt.Sprintf("hosts %s\nstart blocking at: %s", bl.Hosts, bl.LockAt))
}
示例8: getPackage
func getPackage(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
elements := strings.Split(r.URL.Path, "/")
packageName := elements[len(elements)-1]
var name, url string
if err := pool.QueryRow("getPackage", packageName).Scan(&name, &url); err != nil {
if err == pgx.ErrNoRows {
return r, goproxy.NewResponse(r, "text/html", http.StatusNotFound, "Package not found")
}
return r, goproxy.NewResponse(r, "text/html", http.StatusInternalServerError, "Internal server error")
}
data, err := json.Marshal(Package{Name: name, URL: url})
if err != nil {
return r, goproxy.NewResponse(r, "text/html", http.StatusInternalServerError, "Internal server error")
}
return r, goproxy.NewResponse(r, "application/json", http.StatusOK, string(data))
}
示例9: getResponse
// getResponse returns stored response from cache
func (d *DBClient) getResponse(req *http.Request) *http.Response {
log.Info("Returning response")
key := getRequestFingerprint(req)
var payload Payload
payloadBts, err := redis.Bytes(d.cache.get(key))
if err == nil {
log.Info("Decoding bytes")
// getting cache response
err = json.Unmarshal(payloadBts, &payload)
if err != nil {
log.Error(err)
// what now?
}
newResponse := &http.Response{}
newResponse.Request = req
// adding headers
newResponse.Header = make(http.Header)
if len(payload.Response.Headers) > 0 {
for k, values := range payload.Response.Headers {
// headers is a map, appending each value
for _, v := range values {
newResponse.Header.Add(k, v)
}
}
}
newResponse.Header.Set("Gen-Proxy", "Playback")
// adding body
buf := bytes.NewBuffer(payload.Response.Body)
newResponse.ContentLength = int64(buf.Len())
newResponse.Body = ioutil.NopCloser(buf)
newResponse.StatusCode = payload.Response.Status
log.WithFields(log.Fields{
"key": key,
"status": payload.Response.Status,
"bodyLength": newResponse.ContentLength,
}).Info("Response found, returning")
return newResponse
} else {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Failed to retrieve response from cache")
// return error? if we return nil - proxy forwards request to original destination
return goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusPreconditionFailed,
"Coudldn't find recorded request, please record it first!")
}
}
示例10: eccaProxy
// eccaProxy: proxy the user requests and authenticate with the credentials we know.
func eccaProxy(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
//log.Println("\n\n\nRequest is ", req.Method, req.URL.String())
ctx.Logf("Start-of-eccaProxy handler")
for _, c := range req.Cookies() {
ctx.Logf("Cookie send by the client is: %#v", c.Name)
}
// set the scheme to https so we connect upstream securely
if req.URL.Scheme == "http" {
req.URL.Scheme = "https"
}
// Copy the body because we need it untouched. But we also need to parse
// the POST parameters and that eats the original buffer with the body
body, err := ioutil.ReadAll(req.Body)
check(err)
req.Body.Close() // close it before replacing. Prevents leaking file descriptors.
// give the data back immedeately.
req.Body = ioutil.NopCloser(bytes.NewReader(body))
// Read the parameters
req.ParseForm() // eats req.Body
req.Body = ioutil.NopCloser(bytes.NewReader(body)) // copy back in again
// Check for POST method with 'encrypt', 'sign' or 'initiate-direct-connection' parameter.
if req.Method == "POST" {
if req.Form.Get("initiate-direct-connection") == "required" {
// create a direct connection listener, awaiting reply
return initiateDirectConnection(req)
} else if req.Form.Get("encrypt") == "required" {
// transparantly encrypt and sign for a private message
return encryptMessage(req)
} else if req.Form.Get("sign") == "required" || req.Form.Get("sign") == "optional" {
// transparently sign the message before publication
return signMessage(req, ctx)
}
}
// Fetch the request from upstream
resp, err := fetchRequest(req, ctx)
if err != nil {
ctx.Warnf("There was an error fetching the users' request: %v", err)
return req, goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusInternalServerError,
"Some server error!")
}
ctx.Logf("response is %#v", resp)
for _, c := range resp.Cookies() {
ctx.Logf("Cookie send by the server is: %#v\n", c.Name)
}
ctx.Logf("End-of-eccaProxy handler")
//log.Printf("Sleeping for 10 seconds...\n")
//time.Sleep(10 * time.Second)
return nil, resp // let goproxy send our response
}
示例11: OnResponse
func (h *FilteringHandler) OnResponse(r *http.Response,
ctx *goproxy.ProxyCtx) *http.Response {
if r == nil {
// Happens if RoundTrip fails
return r
}
state, ok := ctx.UserData.(*ProxyState)
if !ok {
// The request was rejected by the previous handler
return r
}
duration2 := time.Duration(0)
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err == nil && len(mediaType) > 0 {
host := ctx.Req.URL.Host
if host == "" {
host = ctx.Req.Host
}
rq := &adblock.Request{
URL: ctx.Req.URL.String(),
Domain: host,
OriginDomain: getReferrerDomain(ctx.Req),
ContentType: mediaType,
Timeout: h.MatchTimeout,
}
// Second level filtering, based on returned content
rules := h.Cache.Rules()
start := time.Now()
matched, id, err := rules.Matcher.Match(rq)
if err != nil {
log.Printf("error: matching %s with domain=%s, origin=%, content-type: %s, "+
"failed: %s", rq.URL, rq.Domain, rq.OriginDomain, rq.ContentType, err)
}
end := time.Now()
duration2 = end.Sub(start) / time.Millisecond
if matched {
r.Body.Close()
rule := rules.Rules[id]
log.Printf("rejected in %d/%dms: %s\n", state.Duration, duration2,
state.URL)
log.Printf(" by %s\n", rule)
return goproxy.NewResponse(ctx.Req, goproxy.ContentTypeText,
http.StatusNotFound, "Not Found")
}
}
if atomic.LoadUint64(logRequests)%2 == 1 {
logRequest(ctx.Req)
logResponse(r)
}
log.Printf("accepted in %d/%dms: %s\n", state.Duration, duration2, state.URL)
return r
}
示例12: responseFromResponseRecorder
func responseFromResponseRecorder(req *http.Request, w *httptest.ResponseRecorder) (*http.Request, *http.Response) {
resp := goproxy.NewResponse(req, "", w.Code, w.Body.String())
resp.Header = make(http.Header)
for key, vals := range w.HeaderMap {
for _, val := range vals {
resp.Header.Add(key, val)
}
}
return req, resp
}
示例13: main
func main() {
verbose := flag.Bool("v", false, "should every proxy request be logged to stdout")
docRoot := flag.String("root", ".", "document root directory")
address := flag.String("http", ":8080", `HTTP service address (e.g., ":8080")`)
flag.Parse()
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = *verbose
proxy.OnRequest(reqMethodIs("GET", "HEAD")).DoFunc(
func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
filename := path.Join(*docRoot, ctx.Req.URL.Path)
if !exists(filename) {
return req, nil
}
bytes, err := ioutil.ReadFile(filename)
if err != nil {
ctx.Warnf("%s", err)
return req, nil
}
resp := goproxy.NewResponse(req, "application/octet-stream",
http.StatusOK, string(bytes))
ctx.Logf("return response from local %s", filename)
return req, resp
})
proxy.OnResponse(respReqMethodIs("GET", "HEAD")).Do(
goproxy.HandleBytes(
func(b []byte, ctx *goproxy.ProxyCtx) []byte {
if ctx.Req.Method != "GET" || hasRespHeader(ctx.Resp, "Location") {
return b
}
filename := path.Join(*docRoot, ctx.Req.URL.Path)
if exists(filename) {
return b
}
dir := path.Dir(filename)
err := os.MkdirAll(dir, 0755)
if err != nil {
ctx.Warnf("cannot create directory: %s", dir)
}
err = ioutil.WriteFile(filename, b, 0644)
if err != nil {
ctx.Warnf("cannot write file: %s", filename)
}
ctx.Logf("save cache to %s", filename)
return b
}))
log.Fatal(http.ListenAndServe(*address, proxy))
}
示例14: getResponse
// getResponse returns stored response from cache
func (d *DBClient) getResponse(req *http.Request) *http.Response {
reqBody, err := ioutil.ReadAll(req.Body)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Got error when reading request body")
}
key := getRequestFingerprint(req, reqBody)
payloadBts, err := d.cache.Get([]byte(key))
if err == nil {
// getting cache response
payload, err := decodePayload(payloadBts)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Failed to decode payload")
}
c := NewConstructor(req, *payload)
if d.cfg.middleware != "" {
_ = c.ApplyMiddleware(d.cfg.middleware)
}
response := c.reconstructResponse()
log.WithFields(log.Fields{
"key": key,
"status": payload.Response.Status,
"bodyLength": response.ContentLength,
}).Info("Response found, returning")
return response
}
log.WithFields(log.Fields{
"error": err.Error(),
"query": req.URL.RawQuery,
"path": req.URL.RawPath,
"destination": req.Host,
"method": req.Method,
}).Warn("Failed to retrieve response from cache")
// return error? if we return nil - proxy forwards request to original destination
return goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusPreconditionFailed,
"Coudldn't find recorded request, please record it first!")
}
示例15: newCachedResponse
func newCachedResponse(shared bool, cacheRequest *cacheRequest, resource *Resource) *http.Response {
statusCode, contentLength, headers, body, err := newResponseParameters(shared, cacheRequest, resource)
if err != nil {
return goproxy.NewResponse(cacheRequest.Request, goproxy.ContentTypeText, http.StatusInternalServerError, "Error calculating age: "+err.Error())
}
cachedResponse := newResponse(cacheRequest.Request, statusCode, contentLength, headers, body)
return cachedResponse
}