diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3ce3588 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/Golang/liveurls/huya.go b/Golang/liveurls/huya.go new file mode 100644 index 0000000..1997ff0 --- /dev/null +++ b/Golang/liveurls/huya.go @@ -0,0 +1,91 @@ +// Package liveurls +// @Time:2023/02/05 23:34 +// @File:huya.go +// @SoftWare:Goland +// @Author:feiyang +// @Contact:TG@feiyangdigital + +package liveurls + +import ( + "crypto/md5" + "encoding/base64" + "fmt" + "html" + "io" + "net/http" + "net/url" + "regexp" + "strconv" + "strings" + "time" +) + +type Huya struct { + Rid string +} + +func md5huya(str string) string { + w := md5.New() + io.WriteString(w, str) + md5str := fmt.Sprintf("%x", w.Sum(nil)) + return md5str +} + +func format(e string) string { + i := strings.Split(e, "?")[0] + b := strings.Split(e, "?")[1] + r := strings.Split(i, "/") + re := regexp.MustCompile(".(flv|m3u8)") + s := re.ReplaceAllString(r[len(r)-1], "") + srcAntiCode := html.UnescapeString(b) + c := strings.Split(srcAntiCode, "&") + cc := c[:0] + n := make(map[string]string) + for _, x := range c { + if len(x) > 0 { + cc = append(cc, x) + ss := strings.Split(x, "=") + n[ss[0]] = ss[1] + } + } + c = cc + fm, _ := url.QueryUnescape(n["fm"]) + uu, _ := base64.StdEncoding.DecodeString(fm) + u := string(uu) + p := strings.Split(u, "_")[0] + f := strconv.FormatInt(time.Now().UnixNano()/100, 10) + l := n["wsTime"] + t := "0" + h := p + "_" + t + "_" + s + "_" + f + "_" + l + m := md5huya(h) + url := fmt.Sprintf("%s?wsSecret=%s&wsTime=%s&u=%s&seqid=%s&txyp=%s&fs=%s&sphdcdn=%s&sphdDC=%s&sphd=%s&u=0&t=100&sv=", i, m, l, t, f, n["txyp"], n["fs"], n["sphdcdn"], n["sphdDC"], n["sphd"]) + return url +} + +func (h *Huya) GetLiveUrl() any { + liveurl := "https://m.huya.com/" + h.Rid + client := &http.Client{} + r, _ := http.NewRequest("GET", liveurl, nil) + r.Header.Add("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1") + r.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") + resp, _ := client.Do(r) + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + str := string(body) + freg := regexp.MustCompile(`"(?i)liveLineUrl":"([\s\S]*?)",`) + res := freg.FindStringSubmatch(str) + if res == nil { + return nil + } + nstr, _ := base64.StdEncoding.DecodeString(res[1]) + realstr := string(nstr) + if strings.Contains(realstr, "replay") { + return "https:" + realstr + } else { + liveurl := format(realstr) + liveurl = strings.Replace(liveurl, "hls", "flv", -1) + liveurl = strings.Replace(liveurl, "m3u8", "flv", -1) + return "https:" + liveurl + } +} diff --git a/Golang/main.go b/Golang/main.go index d282f8a..3bf2da0 100644 --- a/Golang/main.go +++ b/Golang/main.go @@ -62,6 +62,17 @@ func setupRouter(adurl string) *gin.Engine { douyuurl = adurl } c.Redirect(http.StatusMovedPermanently, douyuurl) + case "huya": + var huyaurl string + huyaobj := &liveurls.Huya{} + huyaobj.Rid = rid + hyurl := huyaobj.GetLiveUrl() + if str, ok := hyurl.(string); ok { + huyaurl = str + } else { + huyaurl = adurl + } + c.Redirect(http.StatusMovedPermanently, huyaurl) } }) return r