一个朋友公司须要在现有的bs架构上添加一个新的功能,想在查询某些特定用户,弹出一个对话框,提醒操做者,可是现有的架构无法动。html
方法1:jquery
一开始和朋友没有沟通好,我用go作了一个代理服务器,劫持中间的数据,篡改返回的数据。git
作完之后发现问题了,github
1:他们现有一个代理服务器,不能同时设置2个代理服务器。服务器
2:万一作到这个代理服务器出现问题,就是大范围的瘫痪了。架构
放弃该方案。app
代码以下:测试
package main import ( "github.com/elazarl/goproxy" "github.com/elazarl/goproxy/ext/html" "log" "net/http" "regexp" "strings" "net/url" ) const url2="127.0.0.1/11/readme.html" const url1="www.baidu.com" const htmkey=`<div id="content_left">` func findScriptSrc(html string) []string { // who said we can't parse HTML with regexp? // rawqueryMatcher := regexp.MustCompile(`(?i:wd=\s+)`) // srcAttrMatcher := regexp.MustCompile(`^(?i:[^>]*\ssrc=["']([^"']*)["'])`) srcs := make([]string, 0) // matches := scriptMatcher.FindAllStringIndex(html, -1) // for _, match := range matches { // //println("Match",html[match[0]:match[1]]) // // -1 to capture the whitespace at the end of the script tag // srcMatch := srcAttrMatcher.FindStringSubmatch(html[match[1]-1:]) // if srcMatch != nil { // srcs = append(srcs, srcMatch[1]) // } // } return srcs } func NewJqueryVersionProxy() *goproxy.ProxyHttpServer { proxy := goproxy.NewProxyHttpServer() // m := make(map[string]string) // jqueryMatcher := regexp.MustCompile(`(?i:jquery\.)`) proxy.OnResponse(goproxy_html.IsHtml).Do(goproxy_html.HandleString( func(s string, ctx *goproxy.ProxyCtx) string { // ctx.Req.Form if (strings.Contains(ctx.Req.Host,url1)){ rawqueryMatcher := regexp.MustCompile(`wd=([^&]+)`) matches := rawqueryMatcher.FindStringSubmatch(ctx.Req.URL.RawQuery) if (matches==nil){ return s }else{ s1,_:=url.QueryUnescape(matches[1]) return strings.Replace(s,htmkey,htmkey+ `<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr id='TopArea'><td>测试添加内容:`+ s1 + "</td></tr></table><br>",1) } // s1:=matches[1] } // ctx.Warnf("Charset %v by %v",ctx.Charset(),ctx.Req.Header["Content-Type"]) // for _, src := range findScriptSrc(s) { // if jqueryMatcher.MatchString(src) { // prev, ok := m[ctx.Req.Host] // if ok && prev != src { // ctx.Warnf("In %v, Contradicting jqueries %v %v", ctx.Req.URL, prev, src) // break // } // m[ctx.Req.Host] = src // } // } return s })) return proxy } func main() { proxy := NewJqueryVersionProxy() //proxy.Verbose = true log.Fatal(http.ListenAndServe(":8080", proxy)) }