在沒法改動bs架構的基礎上,添加新的功能(1) 代理服務器

一個朋友公司須要在現有的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))
}
相關文章
相關標籤/搜索