Compare commits

...

2 Commits

Author SHA1 Message Date
肥羊 fe7daf9d3c
fix huya.php 2 years ago
肥羊 fdbfea3108
fix huya 2 years ago
  1. 66
      Golang/liveurls/huya.go
  2. 29
      PHP/huya.php

@ -22,6 +22,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/tidwall/gjson"
) )
type Huya struct { type Huya struct {
@ -128,23 +130,34 @@ func processAntiCode(antiCode string, uid int, streamName string) string {
return q.Encode() return q.Encode()
} }
func format(liveArr map[string]any, uid int) map[string]any { func format(jsonStr string, uid int) map[string]any {
streamInfo := map[string]any{"flv": make(map[string]string), "hls": make(map[string]string)} streamInfo := map[string]any{"flv": make(map[string]string), "hls": make(map[string]string)}
cdnType := map[string]string{"HY": "hycdn", "AL": "alicdn", "TX": "txcdn", "HW": "hwcdn", "HS": "hscdn", "WS": "wscdn"} cdnType := map[string]string{"HY": "hycdn", "AL": "alicdn", "TX": "txcdn", "HW": "hwcdn", "HS": "hscdn", "WS": "wscdn"}
for _, s := range liveArr["roomInfo"].(map[string]any)["tLiveInfo"].(map[string]any)["tLiveStreamInfo"].(map[string]any)["vStreamInfo"].(map[string]any)["value"].([]any) { ojsonStr := gjson.Get(jsonStr, "roomInfo.tLiveInfo.tLiveStreamInfo.vStreamInfo").String()
sMap := s.(map[string]any) fmt.Println(gjson.Get(ojsonStr, "value"))
if sMap["sFlvUrl"] != nil { qreg := regexp.MustCompile(`(?i){"_proto"[\s\S]*?"value":([\s\S]*),"_classname"`)
streamInfo["flv"].(map[string]string)[cdnType[sMap["sCdnType"].(string)]] = sMap["sFlvUrl"].(string) + "/" + sMap["sStreamName"].(string) + "." + sMap["sFlvUrlSuffix"].(string) + "?" + processAntiCode(sMap["sFlvAntiCode"].(string), uid, sMap["sStreamName"].(string)) qres := qreg.FindStringSubmatch(ojsonStr)
} gjson.Parse(qres[1]).ForEach(func(_, value gjson.Result) bool {
if sMap["sHlsUrl"] != nil { sFlvUrl := value.Get("sFlvUrl").String()
streamInfo["hls"].(map[string]string)[cdnType[sMap["sCdnType"].(string)]] = sMap["sHlsUrl"].(string) + "/" + sMap["sStreamName"].(string) + "." + sMap["sHlsUrlSuffix"].(string) + "?" + processAntiCode(sMap["sHlsAntiCode"].(string), uid, sMap["sStreamName"].(string)) sFlvUrlSuffix := value.Get("sFlvUrlSuffix").String()
} sHlsUrl := value.Get("sHlsUrl").String()
} sHlsUrlSuffix := value.Get("sHlsUrlSuffix").String()
sStreamName := value.Get("sStreamName").String()
sCdnType := value.Get("sCdnType").String()
sFlvAntiCode := value.Get("sFlvAntiCode").String()
sHlsAntiCode := value.Get("sHlsAntiCode").String()
if sFlvUrl != "" {
streamInfo["flv"].(map[string]string)[cdnType[sCdnType]] = sFlvUrl + "/" + sStreamName + "." + sFlvUrlSuffix + "?" + processAntiCode(sFlvAntiCode, uid, sStreamName)
}
if sHlsUrl != "" {
streamInfo["hls"].(map[string]string)[cdnType[sCdnType]] = sHlsUrl + "/" + sStreamName + "." + sHlsUrlSuffix + "?" + processAntiCode(sHlsAntiCode, uid, sStreamName)
}
return true
})
return streamInfo return streamInfo
} }
func (h *Huya) GetLiveUrl() any { func (h *Huya) GetLiveUrl() any {
var liveArr map[string]any
liveurl := "https://m.huya.com/" + h.Rid liveurl := "https://m.huya.com/" + h.Rid
client := &http.Client{} client := &http.Client{}
r, _ := http.NewRequest("GET", liveurl, nil) r, _ := http.NewRequest("GET", liveurl, nil)
@ -154,41 +167,40 @@ func (h *Huya) GetLiveUrl() any {
defer resp.Body.Close() defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
str := string(body) str := string(body)
freg := regexp.MustCompile(`(?i)<script> window.HNF_GLOBAL_INIT = (.*) </script>`) freg := regexp.MustCompile(`(?i)<script>[\s\S]window.HNF_GLOBAL_INIT = ([\s\S]*?) </script>`)
res := freg.FindStringSubmatch(str) res := freg.FindStringSubmatch(str)
if len(res) > 1 {
json.Unmarshal([]byte(res[1]), &liveArr) jsonStr := res[1]
liveStatus := gjson.Get(jsonStr, "roomInfo.eLiveStatus").Int()
var mediaurl any var mediaurl any
if roomInfo, ok := liveArr["roomInfo"].(map[string]any); ok {
if liveStatus, ok := roomInfo["eLiveStatus"].(float64); ok && liveStatus == 2 { if liveStatus == 2 {
realurl := format(liveArr, uid) realurl := format(jsonStr, uid)
if h.Type == "display" { if h.Type == "display" {
return realurl return realurl
} }
for k, v := range realurl { for k, v := range realurl {
switch k { if k == h.Media {
case h.Media:
if urlarr, ok := v.(map[string]string); ok { if urlarr, ok := v.(map[string]string); ok {
for k, v := range urlarr { for k, v := range urlarr {
switch k { if k == h.Cdn {
case h.Cdn:
mediaurl = strings.Replace(v, "http://", "https://", 1) mediaurl = strings.Replace(v, "http://", "https://", 1)
} }
} }
} }
} }
} }
} else if liveStatus == 3 {
} else if liveStatus, ok := roomInfo["eLiveStatus"].(float64); ok && liveStatus == 3 { liveLineUrl := gjson.Get(jsonStr, "roomProfile.liveLineUrl").String()
if roomProfile, ok := liveArr["roomProfile"].(map[string]any); ok { if liveLineUrl != "" {
if liveLineUrl, ok := roomProfile["liveLineUrl"].(string); ok {
decodedLiveLineUrl, _ := base64.StdEncoding.DecodeString(liveLineUrl) decodedLiveLineUrl, _ := base64.StdEncoding.DecodeString(liveLineUrl)
mediaurl = "https:" + string(decodedLiveLineUrl) mediaurl = "https:" + string(decodedLiveLineUrl)
} }
}
} else { } else {
mediaurl = nil mediaurl = nil
} }
}
return mediaurl return mediaurl
} }
return nil
}

@ -1,10 +1,11 @@
<?php <?php
date_default_timezone_set("Asia/Shanghai"); date_default_timezone_set("Asia/Shanghai");
$type = empty($_GET['type']) ? "nodisplay" : trim($_GET['type']); $type = empty($_GET['type']) ? "nodisplay" : trim($_GET['type']);
$id = empty($_GET['id']) ? "shangdi" : trim($_GET['id']); $id = empty($_GET['id']) ? "11342412" : trim($_GET['id']);
$cdn = empty($_GET['cdn']) ? "hwcdn" : trim($_GET['cdn']); $cdn = empty($_GET['cdn']) ? "hwcdn" : trim($_GET['cdn']);
$media = empty($_GET['media']) ? "flv" : trim($_GET['media']); $media = empty($_GET['media']) ? "flv" : trim($_GET['media']);
$roomurl = "https://m.huya.com/" . $id; $roomurl = "https://mp.huya.com/cache.php?m=Live&do=profileRoom&roomid=" . $id;
function get_content($apiurl, $flag) function get_content($apiurl, $flag)
{ {
@ -44,6 +45,10 @@ function get_content($apiurl, $flag)
return $data; return $data;
} }
$jsonStr = json_decode(get_content($roomurl, "mobile"), true);
$realdata = $jsonStr["data"];
$uid = json_decode(get_content("https://udblgn.huya.com/web/anonymousLogin", "uid"), true)["data"]["uid"];
function aes_decrypt($ciphertext, $key, $iv) function aes_decrypt($ciphertext, $key, $iv)
{ {
return openssl_decrypt($ciphertext, 'AES-256-CBC', $key, 0, $iv); return openssl_decrypt($ciphertext, 'AES-256-CBC', $key, 0, $iv);
@ -53,7 +58,6 @@ $key = "abcdefghijklmnopqrstuvwxyz123456";
$iv = "1234567890123456"; $iv = "1234567890123456";
$mediaurl = aes_decrypt("fIuPMpBI1RpRnM2JhbYHzvwCvwhHBF7Q+8k14m9h3N5ZfubHcDCEk08TnLwHoMI/SG7bxpqT6Rh+gZunSpYHf1JM/RmEC/S1SjRYWw6rwc3gGo3Rrsl3sojPujI2aZsb", $key, $iv); $mediaurl = aes_decrypt("fIuPMpBI1RpRnM2JhbYHzvwCvwhHBF7Q+8k14m9h3N5ZfubHcDCEk08TnLwHoMI/SG7bxpqT6Rh+gZunSpYHf1JM/RmEC/S1SjRYWw6rwc3gGo3Rrsl3sojPujI2aZsb", $key, $iv);
$uid = json_decode(get_content("https://udblgn.huya.com/web/anonymousLogin", "uid"), true)["data"]["uid"];
function get_uuid() function get_uuid()
{ {
@ -84,11 +88,11 @@ function process_anticode($anticode, $uid, $streamname)
return http_build_query($q); return http_build_query($q);
} }
function format($livearr, $uid) function format($realdata, $uid)
{ {
$stream_info = ['flv' => [], 'hls' => []]; $stream_info = ['flv' => [], 'hls' => []];
$cdn_type = ['HY' => 'hycdn', 'AL' => 'alicdn', 'TX' => 'txcdn', 'HW' => 'hwcdn', 'HS' => 'hscdn', 'WS' => 'wscdn']; $cdn_type = ['HY' => 'hycdn', 'TX' => 'txcdn', 'HW' => 'hwcdn', 'HS' => 'hscdn', 'WS' => 'wscdn'];
foreach ($livearr["roomInfo"]["tLiveInfo"]["tLiveStreamInfo"]["vStreamInfo"]["value"] as $s) { foreach ($realdata["stream"]["baseSteamInfoList"] as $s) {
if ($s["sFlvUrl"]) { if ($s["sFlvUrl"]) {
$stream_info["flv"][$cdn_type[$s["sCdnType"]]] = $s["sFlvUrl"] . '/' . $s["sStreamName"] . '.' . $s["sFlvUrlSuffix"] . '?' . process_anticode($s["sFlvAntiCode"], $uid, $s["sStreamName"]); $stream_info["flv"][$cdn_type[$s["sCdnType"]]] = $s["sFlvUrl"] . '/' . $s["sStreamName"] . '.' . $s["sFlvUrlSuffix"] . '?' . process_anticode($s["sFlvAntiCode"], $uid, $s["sStreamName"]);
} }
@ -99,15 +103,7 @@ function format($livearr, $uid)
return $stream_info; return $stream_info;
} }
$res = get_content($roomurl, "mobile"); if ($jsonStr["status"] == 200) {
$reg = "/\<script\> window.HNF_GLOBAL_INIT = (.*) \<\/script\>/i";
preg_match($reg, $res, $realres);
$realdata = json_decode($realres[1], true);
if (array_key_exists("exceptionType", $realdata)) {
header('location:' . $mediaurl);
exit();
} elseif ($realdata["roomInfo"]["eLiveStatus"] == 2) {
$realurl = format($realdata, $uid); $realurl = format($realdata, $uid);
if ($type == "display") { if ($type == "display") {
print_r($realurl); print_r($realurl);
@ -135,9 +131,6 @@ if (array_key_exists("exceptionType", $realdata)) {
} }
header('location:' . $mediaurl); header('location:' . $mediaurl);
exit(); exit();
} elseif ($realdata["roomInfo"]["eLiveStatus"] == 3) {
header('location:' . "https:" . base64_decode($realdata["roomProfile"]["liveLineUrl"]));
exit();
} else { } else {
header('location:' . $mediaurl); header('location:' . $mediaurl);
exit(); exit();

Loading…
Cancel
Save