Пример #1
0
func newProxyHttpServer() *goproxy.ProxyHttpServer {
	proxy := goproxy.NewProxyHttpServer()
	// proxy.Verbose = true
	proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if req.Host == "" {
			http.Error(w, "Cannot handle requests without Host header, e.g., HTTP 1.0", 500)
			return
		}
		req.URL.Scheme = "http"
		req.URL.Host = req.Host
		proxy.ServeHTTP(w, req)
	})

	proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
		if detect.Activate() && !utils.IsIgnoreRequest(ctx.Req.URL.RequestURI(), resp.Header.Get("Content-Type"), resp.StatusCode) {
			reqDumpHeader, errReq := httputil.DumpRequest(ctx.Req, false)
			reqDumpBody, errReq := ioutil.ReadAll(ctx.Req.Body)
			ctx.Req.Body = ioutil.NopCloser(bytes.NewBuffer(reqDumpBody))
			// ignore exceed 100KB body size
			if len(reqDumpBody) > 100*1024 {
				reqDumpBody = []byte("<content too long>")
			}

			respDumpHeader, errResp := httputil.DumpResponse(resp, false)
			respDumpBody, errResp := ioutil.ReadAll(resp.Body)
			resp.Body = ioutil.NopCloser(bytes.NewBuffer(respDumpBody))
			// ignore exceed 10KB body size
			if len(respDumpBody) > 100*1024 {
				respDumpBody = []byte("<content too long>")
			}

			if errReq == nil && errResp == nil {
				detect.SendEvent(map[string]interface{}{
					"request":  map[string]string{"header": string(reqDumpHeader), "body": string(reqDumpBody)},
					"response": map[string]string{"header": string(respDumpHeader), "body": string(respDumpBody)},
				})
			}
		}

		return resp
	})

	return proxy
}
Пример #2
0
// content type:  //mockjs  //javascript
// content object:
// $request.params  object
// $request.url     string
// $response.body   string
// $output
func (c *ErrorController) Error404() {
	c.EnableRender = false

	if ruleCache == nil {
		refreshRuleCache()
	}

	if !detect.Activate() {
		// check rule match and return content
		var result string
		var err error

		if rule, match := c.matchRequest(); match && rule != nil {
			content := rule.Contents[0]
			body := content.Content
			c.Ctx.Output.SetStatus(200)

			if result, err = c.parseMockjsContent(content.Content); err == nil {
				if content.DelaySeconds > 0 {
					time.Sleep(time.Duration(content.DelaySeconds) * time.Millisecond)
				}
				c.Ctx.Output.Body([]byte(result))
				return
			} else {
				if err.Error() != "not mockjs content" {
					fmt.Println(err)
					body = err.Error() + "\n\n" + body
				}
			}
			if result, err = c.parseJavascriptContent(content.Content); err == nil {
				if content.DelaySeconds > 0 {
					time.Sleep(time.Duration(content.DelaySeconds) * time.Millisecond)
				}
				c.Ctx.Output.Body([]byte(result))
				return
			} else {
				if err.Error() != "not javascript content" {
					fmt.Println(err)
					body = err.Error() + "\n\n" + body
				}
			}

			if err == nil || err.Error() == "not mockjs content" || err.Error() == "not javascript content" {
				body = c.parseBackendRender(rule, body)
			}

			if content.DelaySeconds > 0 {
				time.Sleep(time.Duration(content.DelaySeconds) * time.Millisecond)
			}
			c.Ctx.Output.Body([]byte(body))
			return
		}
	}

	if strings.Contains(c.Ctx.Request.Host, "localhost") || strings.Contains(c.Ctx.Request.Host, "127.0.0.1") ||
		strings.Contains(c.Ctx.Request.Host, utils.GetHostIp()) || strings.Contains(c.Ctx.Request.URL.Path, "/mocku") {
		c.Ctx.Output.SetStatus(404)
		c.Ctx.Output.Body([]byte("page not found"))
	} else {
		c.Ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(c.Ctx.Input.RequestBody))
		proxy.ServeHTTP(c.Ctx.ResponseWriter.ResponseWriter, c.Ctx.Request)
	}
}