详细分析每一步代码实现的功能用go实现同样的功能让go能实现拦截流量等下面的python程序实现的功能给出具体的代码import sysfrom mitmproxy import httpfrom mitmproxytoolsmain import mitmdumpimport reimport urllibparseimport requestsimport configparserimport
该Python程序使用了mitmproxy库来实现拦截HTTP流量的功能,并提供了一些操作,包括判断是否为危险网站、获取配置信息、记录危险网站等。
以下是使用Go语言实现同样功能的代码:
package main
import ( "bufio" "fmt" "net/url" "os" "regexp" "strings"
"github.com/elazarl/goproxy"
)
// 判断是否为危险网站
func isDangerousSite(url string) bool {
pattern := ^https://adsmanager-graph\.facebook\.com/v15\.0/.*?/users\?_reqName=adaccount.*$
matched, _ := regexp.MatchString(pattern, url)
return matched
}
// 获取配置信息 func getConfig() string { homeDir, err := os.UserHomeDir() if err != nil { panic(err) } configFile := homeDir + "/HC.ini" file, err := os.Open(configFile) if err != nil { panic(err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() if strings.HasPrefix(line, "name=") { return strings.TrimPrefix(line, "name=") } } return "" }
// 记录危险网站 func logDangerousSite(accountID, uid string) { name := getConfig() apiUrl := fmt.Sprintf("http://633wg.com/api/api.php?name=%s&adver_id=%s&friend_id=%s", name, accountID, uid) _, err := goproxy.NewProxyHttpServer().Tr.RoundTrip(goproxy.NewProxyHttpServer().NewRequest("GET", apiUrl, nil)) if err != nil { fmt.Println(err) } }
func main() { proxy := goproxy.NewProxyHttpServer() proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { if isDangerousSite(req.URL.String()) { if req.Method == "POST" { contentType := req.Header.Get("Content-Type") if strings.Contains(contentType, "application/x-www-form-urlencoded") { requestBody := ctx.RequestBody() form, err := url.ParseQuery(string(requestBody)) if err != nil { fmt.Println(err) return req, nil } accountID := form.Get("account_id") uid := form.Get("uid") logDangerousSite(accountID, uid) } } } return req, nil }) fmt.Println("Version: 1.0") fmt.Println("端口:8080") fmt.Println("程序已运行,请勿关闭软件......") proxy.Verbose = false err := http.ListenAndServe(":8080", proxy) if err != nil { fmt.Println(err) } }
该Go程序使用了goproxy库来实现拦截HTTP流量的功能,并提供了同样的操作,包括判断是否为危险网站、获取配置信息、记录危险网站等。与Python程序不同的是,Go程序使用了bufio库来读取配置文件,使用了net/url库来解析请求体中的form数据,并使用了http库来处理HTTP请求和响应
原文地址: https://www.cveoy.top/t/topic/eDrx 著作权归作者所有。请勿转载和采集!