Skip to content

Commit

Permalink
Require that param must be map[string]string
Browse files Browse the repository at this point in the history
  • Loading branch information
boxie123 committed Dec 28, 2023
1 parent 2aac434 commit 07cc056
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 85 deletions.
102 changes: 97 additions & 5 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package app

import (
"fmt"
"github.com/go-resty/resty/v2"
"strconv"
"testing"
"time"
)

func TestParamSign(t *testing.T) {
param := map[string]interface{}{
"id": 114514,
param := map[string]string{
"id": "114514",
"str": "1919810",
"test": "いいよ,こいよ",
}
Expand All @@ -19,11 +22,100 @@ func TestParamSign(t *testing.T) {
}

func TestGetVersions(t *testing.T) {
build, _, err := GetVersions("")
device := &Device{}
err := device.GetVersions("")
if err != nil {
t.Error(err)
}
if build != "7580300" {
t.Errorf("获取版本号失败: build = %s", build)
if device.VersionCode != "7580300" {
t.Errorf("获取版本号失败: build = %s", device.VersionCode)
}
}

func TestDevice(t *testing.T) {
device, err := NewDevice("JKM-AL00", "9")
if err != nil {
t.Error(err)
return
}
device.GenerateFakeBuvid()
err = device.GetVersions("")
if err != nil {
t.Error(err)
return
}
fmt.Println(device)
}

func TestDevice_BuildUserAgent(t *testing.T) {
device, err := NewDevice("JKM-AL00", "9")
if err != nil {
t.Error(err)
return
}
ua, err := device.BuildUserAgent()
if err != nil {
t.Error(err)
return
}
fmt.Println(ua)
}

func TestRequest(t *testing.T) {
type Result struct {
Code int `json:"code"`
Message string `json:"message"`
}
device, err := NewDevice("JKM-AL00", "9")
if err != nil {
t.Error(err)
return
}
client := resty.New()
result := Result{}
timeStamp := time.Now().Unix()
params := map[string]string{
"actionKey": "appkey",
"build": device.VersionCode,
"c_locale": "zh-Hans_CN",
"device": MobiApp,
"device_model": device.AndroidModel,
"disable_rcmd": "0",
"jump_from": "24001",
"mobi_app": MobiApp,
"platform": Platform,
"room_id": "1184275",
"s_locale": "zh-Hans_CN",
"statistics": fmt.Sprintf(`{"appId":1,"version":"%s","abtest":"","platform":1}`, device.VersionName),
"ts": strconv.FormatInt(timeStamp, 10),
}
params = ParamSign(params)
ua, err := device.BuildUserAgent()
if err != nil {
t.Error(err)
return
}
headers := map[string]string{
"Host": "api.live.bilibili.com",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"APP-KEY": MobiApp,
"Buvid": device.BilibiliBuvid,
"User-Agent": ua,
"ENV": "prod",
"Session_ID": BuildSessionID(),
"x-bili-trace-id": BuildXBiliTraceID(timeStamp),
"Accept-Encoding": "gzip, deflate, br",
}
_, err = client.R().
SetResult(&result).
SetQueryParams(params).
SetHeaders(headers).
Get("https://api.live.bilibili.com/xlive/app-room/v1/index/getInfoByRoom")
if err != nil {
t.Error(err)
}
if result.Code != 0 {
t.Errorf("Code: %d, Message: %s", result.Code, result.Message)
}
}
112 changes: 53 additions & 59 deletions app/device.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
package app

import (
"encoding/base64"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"log"
"math"
"strconv"
"strings"
"unicode"
)

// NewDevice
//
// @Description: 生成新的设备信息
// @param AndroidModel 手机型号
// @param AndroidBuild 安卓版本
// @return *Device
// @return error 错误处理
func NewDevice(AndroidModel string, AndroidBuild string) (*Device, error) {
device := &Device{
AndroidModel: AndroidModel,
AndroidBuild: AndroidBuild,
}
device.GenerateFakeBuvid()
err := device.GetVersions("")
if err != nil {
return nil, err
}
_, err = device.GetSdkInt()
if err != nil {
return nil, err
}
return device, nil
}

// GenerateFakeBuvid
//
// @Description: 伪造设备标识码
Expand All @@ -28,7 +47,7 @@ func (d *Device) GenerateFakeBuvid() {
// GetVersions
//
// @Description: 获取最新版本的 build 和 version
// @param mod 传参, 若为空则传入 "android"
// @param mod 传参, 若为空则传入 MobiApp
// @return string build
// @return string version
// @return error 错误处理
Expand All @@ -38,13 +57,13 @@ func (d *Device) GetVersions(mod string) error {
}
client := resty.New()
resp, err := client.R().
SetResult(&AppVersionResponse{}).
SetResult(&BilibiliVersionResponse{}).
SetQueryParam("mobi_app", mod).
Get("https://app.bilibili.com/x/v2/version")
if err != nil {
return err
}
result := resp.Result().(*AppVersionResponse)
result := resp.Result().(*BilibiliVersionResponse)
if len(result.Data) > 0 {
d.VersionCode = fmt.Sprintf("%d", result.Data[0].Build)
d.VersionName = result.Data[0].Version
Expand All @@ -60,82 +79,57 @@ func (d *Device) GetVersions(mod string) error {
// @param AndroidVersion 安卓系统版本
// @return string sdk版本
// @return error 错误处理
func (d *Device) GetSdkInt() string {
func (d *Device) GetSdkInt() (string, error) {
AndroidVersion := d.AndroidBuild
if AndroidVersion == "" {
return "", fmt.Errorf("请先指定安卓系统版本")
}
buildList := strings.Split(AndroidVersion, ".")
var sdkInt string
data := SystemSdkIntMap
for _, li := range buildList {
value, ok := data[li]
if !ok {
if sdkInt != "" {
return sdkInt
return sdkInt, nil
}
log.Println("未找到" + AndroidVersion + "的sdk_int")
return ""
return "", fmt.Errorf("未找到 %s 的sdk_int", AndroidVersion)
}
sdkInt = value["value"].(string)
data = map[string]map[string]interface{}{
li: value,
}
}
return sdkInt
}

// BuildXBiliAuroraEID
//
// @Description: 生成 x-bili-aurora-eid
// @param mid 用户 uid
// @return string x-bili-aurora-eid
func BuildXBiliAuroraEID(mid string) string {
length := len(mid)
byteArr := make([]byte, length)

if length-1 < 0 {
return ""
}

for i := 0; i < length; i++ {
s := unicode.ToLower(rune("ad1va46a7lza"[i%12]))
byteArr[i] = byte(mid[i]) ^ byte(s)
}

return base64.StdEncoding.EncodeToString(byteArr)
}

// BuildXBiliTraceID
//
// @Description: 生成 x-bili-trace-id
// @param timeStamp
// @return string
func BuildXBiliTraceID(timeStamp int64) string {
back6 := strconv.FormatInt(int64(math.Round(float64(timeStamp)/256)), 16)
front := strings.ReplaceAll(uuid.New().String(), "-", "")
_data1 := front[6:] + back6[2:]
_data2 := front[22:] + back6[2:]

return fmt.Sprintf("%v:%v:0:0", _data1, _data2)
}

// BuildSessionID
//
// @Description: 构造随机 Session ID
// @return string SessionID
func BuildSessionID() string {
return strings.ReplaceAll(uuid.New().String(), "-", "")[:8]
return sdkInt, nil
}

// BuildUserAgent
//
// @Description: 构造User-Agent
// @param device 设备信息
func (d *Device) BuildUserAgent() string {
func (d *Device) BuildUserAgent() (string, error) {
sdkInt, err := d.GetSdkInt()
if err != nil {
return "", err
}
if d.VersionCode == "" || d.VersionName == "" {
err = d.GetVersions("")
if err != nil {
return "", err
}
}
if d.AndroidModel == "" {
return "", fmt.Errorf("AndroidModel为空")
}
if d.BilibiliBuvid == "" {
d.GenerateFakeBuvid()
}
varMap := map[string]string{
"ANDROID_BUILD": d.AndroidBuild,
"ANDROID_MODEL": d.AndroidModel,
"ANDROID_BUILD_M": BuildM,
"BUVID": d.BilibiliBuvid,
"SDK_INT": d.GetSdkInt(),
"SDK_INT": sdkInt,
"VERSION_CODE": d.VersionCode,
"CHANNEL": Channel,
"SESSION_ID": BuildSessionID(),
Expand All @@ -148,5 +142,5 @@ func (d *Device) BuildUserAgent() string {
strTemplate = strings.ReplaceAll(strTemplate, "{"+k+"}", v)
}

return strTemplate
return strTemplate, nil
}
54 changes: 54 additions & 0 deletions app/header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package app

import (
"encoding/base64"
"fmt"
"github.com/google/uuid"
"math"
"strconv"
"strings"
"unicode"
)

// BuildXBiliAuroraEID
//
// @Description: 生成 x-bili-aurora-eid
// @param mid 用户 uid
// @return string x-bili-aurora-eid
func BuildXBiliAuroraEID(mid string) string {
length := len(mid)
byteArr := make([]byte, length)

if length-1 < 0 {
return ""
}

for i := 0; i < length; i++ {
s := unicode.ToLower(rune("ad1va46a7lza"[i%12]))
byteArr[i] = byte(mid[i]) ^ byte(s)
}

return base64.StdEncoding.EncodeToString(byteArr)
}

// BuildXBiliTraceID
//
// @Description: 生成 x-bili-trace-id
// @param timeStamp
// @return string
func BuildXBiliTraceID(timeStamp int64) string {
back6 := strconv.FormatInt(int64(math.Round(float64(timeStamp)/256)), 16)
front := strings.ReplaceAll(uuid.New().String(), "-", "")
_data1 := front[6:] + back6[2:]
_data2 := front[22:] + back6[2:]

return fmt.Sprintf("%v:%v:0:0", _data1, _data2)
}

// BuildSessionID
//
// @Description: 构造随机 Session ID
// @return string SessionID
func BuildSessionID() string {
return strings.ReplaceAll(uuid.New().String(), "-", "")[:8]
}
10 changes: 1 addition & 9 deletions app/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ import (
"github.com/boxie123/BilibiliAuthorize/utils"
)

const (
AppKey = "1d8b6e7d45233436"
AppSec = "560c52ccd288fed045859ed18bffd973"
Platform = "android"
NeuronAppId = 1
MobiApp = "android"
)

// ParamSign
//
// @Description: 向参数映射中添加 appkey 和 sign 鉴权签名
// @param param 需要加签名的参数映射
// @return map[string]interface{}
func ParamSign(param map[string]interface{}) map[string]interface{} {
func ParamSign(param map[string]string) map[string]string {
_, ok := param["appkey"]
if !ok {
param["appkey"] = AppKey
Expand Down
6 changes: 3 additions & 3 deletions app/type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package app

type AppVersionResponse struct {
Data []AppVersionData `json:"data"`
type BilibiliVersionResponse struct {
Data []BilibiliVersionData `json:"data"`
}

type AppVersionData struct {
type BilibiliVersionData struct {
Build int `json:"build"`
Version string `json:"version"`
}
Expand Down
Loading

0 comments on commit 07cc056

Please sign in to comment.