Skip to content

Commit

Permalink
update sign
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora233 committed Jul 1, 2023
1 parent 24533b2 commit efbc144
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
4 changes: 1 addition & 3 deletions lsp/bilibili/XSpaceAccInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const (
type XSpaceAccInfoRequest struct {
Mid int64 `json:"mid"`
Platform string `json:"platform"`
Jsonp string `json:"jsonp"`
Token string `json:"token"`
WebLocation string `json:"web_location"`
}
Expand Down Expand Up @@ -47,10 +46,9 @@ func XSpaceAccInfo(mid int64) (*XSpaceAccInfoResponse, error) {
logger.WithField("FuncName", utils.FuncName()).Tracef("cost %v", ed.Sub(st))
}()
url := BPath(PathXSpaceAccInfo)
params, err := utils.ToParams(&XSpaceAccInfoRequest{
params, err := utils.ToDatas(&XSpaceAccInfoRequest{
Mid: mid,
Platform: "web",
Jsonp: "jsonp",
WebLocation: "1550101",
})
if err != nil {
Expand Down
50 changes: 28 additions & 22 deletions lsp/bilibili/XWebInterfaceNav.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/Sora233/DDBOT/proxy_pool"
"github.com/Sora233/DDBOT/requests"
"github.com/Sora233/DDBOT/utils"
"github.com/guonaihong/gout"
"github.com/samber/lo"
"sort"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -40,30 +40,36 @@ func getWbi() (imgKey string, subKey string) {
subKey = getKey(wbi.SubUrl)
return
}
func getMixinKey(orig string) string {
var str strings.Builder
for _, v := range mixinKeyEncTab {
if v < len(orig) {
str.WriteByte(orig[v])
}
}
return str.String()[:32]
}

func signWbi(params gout.H) {
func signWbi(params map[string]string) map[string]string {
imgKey, subKey := getWbi()
var sb strings.Builder
var orig = imgKey + subKey
for _, r := range mixinKeyEncTab {
sb.WriteRune(rune(orig[r]))
mixinKey := getMixinKey(imgKey + subKey)
currTime := strconv.FormatInt(time.Now().Unix(), 10)
params["wts"] = currTime
// Sort keys
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
salt := sb.String()[:32]
params["wts"] = time.Now().Unix()
p := lo.MapToSlice(params, func(key string, value any) lo.Tuple2[string, any] {
return lo.Tuple2[string, any]{A: key, B: value}
})
sort.Slice(p, func(i, j int) bool {
return p[i].A < p[j].A
})

query := strings.Join(lo.Map(p, func(item lo.Tuple2[string, any], _ int) string {
return fmt.Sprintf("%v=%v", item.A, item.B)
}), "&")
hash := md5.New()
hash.Write([]byte(query + salt))
wbiSign := hex.EncodeToString(hash.Sum(nil))
params["w_rid"] = wbiSign
sort.Strings(keys)
// Build URL parameters
var str strings.Builder
for _, k := range keys {
str.WriteString(fmt.Sprintf("%s=%s&", k, params[k]))
}
query := strings.TrimSuffix(str.String(), "&")
hash := md5.Sum([]byte(query + mixinKey))
params["w_rid"] = hex.EncodeToString(hash[:])
return params
}

func XWebInterfaceNav(login bool) (*WebInterfaceNavResponse, error) {
Expand Down
2 changes: 1 addition & 1 deletion lsp/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var (
}
})
}()
mixinKeyEncTab = []rune{
mixinKeyEncTab = []int{
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40,
61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11,
Expand Down
9 changes: 3 additions & 6 deletions lsp/bilibili/concern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ func TestConcern_FindUserLiving(t *testing.T) {
assert.NotNil(t, err)
assert.Nil(t, liveInfo)

const testMid int64 = 2
const testMid int64 = 97505

userInfo, err := c.FindOrLoadUser(testMid)
assert.Nil(t, err)
assert.Equal(t, testMid, userInfo.Mid)
assert.Equal(t, "碧诗", userInfo.Name)

liveInfo, err = c.FindUserLiving(testMid, true)
assert.Nil(t, err)
Expand Down Expand Up @@ -109,12 +108,11 @@ func TestConcern_FindUserNews(t *testing.T) {
assert.NotNil(t, err)
assert.Nil(t, newsInfo)

const testMid int64 = 2
const testMid int64 = 97505

userInfo, err := c.FindOrLoadUser(testMid)
assert.Nil(t, err)
assert.Equal(t, testMid, userInfo.Mid)
assert.Equal(t, "碧诗", userInfo.Name)

userInfo2, err := c.FindOrLoadUser(testMid)
assert.Nil(t, err)
Expand All @@ -135,12 +133,11 @@ func TestConcern_StatUserWithCache(t *testing.T) {

c := initConcern(t)

const testMid int64 = 2
const testMid int64 = 97505

userInfo, err := c.FindOrLoadUser(testMid)
assert.Nil(t, err)
assert.Equal(t, testMid, userInfo.Mid)
assert.Equal(t, "碧诗", userInfo.Name)

stat, err := c.StatUserWithCache(testMid, time.Hour)
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion lsp/bilibili/feedList.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func FeedList(opt ...FeedOpt) (*FeedListResponse, error) {
}

url := BPath(PathRelationFeedList)
params, err := utils.ToParams(&RelationFeedRequest{
params, err := utils.ToDatas(&RelationFeedRequest{
Page: p["page"],
PageSize: p["pageSize"],
})
Expand Down
2 changes: 1 addition & 1 deletion requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func Do(f func(*gout.Client) *dataflow.DataFlow, out interface{}, options ...Opt
return nil
}

func Get(url string, params gout.H, out interface{}, options ...Option) error {
func Get(url string, params interface{}, out interface{}, options ...Option) error {
return Do(func(gcli *gout.Client) *dataflow.DataFlow {
return gcli.GET(url).SetQuery(params)
}, out, options...)
Expand Down

0 comments on commit efbc144

Please sign in to comment.