[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

posted @ 2018-12-02 22:28  leeli73  阅读(3361)  评论(0编辑  收藏  举报