[Go]基於GoProxy實現HTTP代理,攔截智慧樹注入JS實現自動刷課


這幾天各種網課都快截至了,鄙人也是被逼得煩的不行

雖然網上有刷課的瀏覽器,但是每次換個地方就得重新拷貝一邊

所以就想能不能寫一個HTTP代理,丟在服務器上,自己不管到哪里,設置上代理就能刷課

從本質來說其實都是注入JS,跟瀏覽器相比換個注入方式而已

既然要寫HTTP代理,第一時間就想到了大名鼎鼎的GoProxy

首先執行go get來下載庫

go get github.com/snail007/goproxy

  接下來,不多說直接上代碼

package main
import (
    "strings"
    "fmt"
    "bytes"
    "net/http"
    "io/ioutil"
    "github.com/elazarl/goproxy"
)
var Code string = `<script>function query() {
    if ($("#popbox_title").length > 0) {
        $(".popboxes_close")[0].click();
        console.log('關閉窗口');
    }

    if ($("#chapterList .time_ico.fl").nextAll()[2].children[0].style.width === "100%" || $("video").get(0).ended) {
        var num = -1;
        var text = $("#chapterList .time_ico.fl").parent().nextAll()[++num].id;
        while (text === "" ||
               text.substr(0, 5) != "video" ||
               text.substr(0, 7) === "video-0") {
            text = $("#chapterList .time_ico.fl").parent().nextAll()[++num].id;
        }
        $("#chapterList .time_ico.fl").parent().nextAll()[num].click();
    }

    if ($("video").length > 0 && $("video").get(0).playbackRate != 1.5) {
        console.log('切換到1.5倍');
        $(".speedTab15")[0].click();
    }

    if ($("video").get(0).volume > 0) {
        $(".volumeIcon").click();
    }
}
var divObj=document.createElement("div"); 
divObj.innerHTML='<h1 style="font-size:25px;">技術支持來自:Lee QQ:925776327<br>項目整體開源,歡迎Fork和Star <a href="https://github.com/leeli73/ZhiHuiShuShuaKe">github.com/leeli73/ZhiHuiShuShuaKe</a><br>你也可以通過Tampermonkey、Fiddler等軟件,向網頁中注入上面Git項目中的Code.js<br>本地版程序下載(無訪問限制版)<a href="https://www.lanzous.com/i237o3e">點擊下載</a></h1>'; 
var first=document.body.firstChild;
document.body.insertBefore(divObj,first);
window.setInterval(query, 1000);</script>`

func main() {
    proxy := goproxy.NewProxyHttpServer()
    fmt.Println("Proxy已經成功啟動...智慧樹課程頁面監控中...")
    fmt.Println("請將你的瀏覽器代理設定為127.0.0.1:8080  設置方法請百度,很簡單!")
    fmt.Println("支持在內網和外網通過IP訪問代理服務")
    fmt.Println("技術支持來自:Lee QQ:925776327")
    fmt.Println("項目整體開源,歡迎Fork和Star 項目地址:https://github.com/leeli73/ZhiHuiShuShuaKe")
    fmt.Println("你也可以通過Tampermonkey、Fiddler等軟件,向網頁中注入上面Git項目中的Code.js")
    fmt.Println("如果需要Server版,請聯系上面的QQ")
    fmt.Println("如果出現報錯代碼請不用管他。程序會自動熱重啟。")
    proxy.Verbose = false
    
    //proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
    proxy.OnRequest().DoFunc(
        func(r *http.Request,ctx *goproxy.ProxyCtx)(*http.Request,*http.Response) {
            r.Header.Set("X-GoProxy","yxorPoG-X")
            //fmt.Println(r.Host)
            if r.URL.Scheme == "https" && !strings.Contains(r.Host,"zhihuishu"){
                return r,goproxy.NewResponse(r,
                    goproxy.ContentTypeText,http.StatusForbidden,
                    "為了保證服務器的能為每個同學提供刷課服務,服務器已經關閉了你的這個HTTPS請求!tips:人心險惡,為了保證你的數據安全,請盡量避免使用未知的Proxy。")
            }
            if !strings.Contains(r.Host,"zhihuishu"){
                return r,goproxy.NewResponse(r,
                    goproxy.ContentTypeText,http.StatusForbidden,
                    "為了保證服務器的能為每個同學提供刷課服務,服務器已經關閉了你的這個HTTP請求!tips:人心險惡,為了保證你的數據安全,請盡量避免使用未知的Proxy。")
            }
            return r,nil
    })
    /*proxy.OnRequest(goproxy.DstHostIs("eol.qhu.edu.cn")).DoFunc(
    func(r *http.Request,ctx *goproxy.ProxyCtx)(*http.Request,*http.Response) {
        return r,goproxy.NewResponse(r,
            goproxy.ContentTypeText,http.StatusForbidden,
            "Don't waste your time!")
    })*/
    proxy.OnResponse().DoFunc(
        func(r *http.Response, ctx *goproxy.ProxyCtx)*http.Response{
            if strings.Contains(r.Request.URL.Path,"learning/videoList"){
                bs, _ := ioutil.ReadAll(r.Body)
                sbs := string(bs)
                sbs = sbs + Code
                fmt.Println("發現課程頁面,刷課代碼已經成功注入!")
                bs = []byte(sbs)
                r.Body = ioutil.NopCloser(bytes.NewReader(bs))
            }
            return r
    })
    http.ListenAndServe(":8080", proxy)
}

然后就可以啦,我默認禁止了部分請求來降低服務器負載,大家可以自主刪除

下面再給大家附帶一個編譯好的二進制版

https://www.lanzous.com/i237o3e


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM