fix netease login

This commit is contained in:
aynakeya
2025-07-23 02:38:12 +08:00
parent c1e6fc35fc
commit 12a3679383
3 changed files with 55 additions and 7 deletions

View File

@@ -46,10 +46,14 @@ func (n *Netease) QrLoginVerify(qrlogin *miaosic.QrLoginSession) (*miaosic.QrLog
}
cookies := make([]*http.Cookie, 0)
for _, c := range (&http.Response{Header: h}).Cookies() {
if c.Name == "MUSIC_U" || c.Name == "__csrf" {
if c.Name == "MUSIC_U" || c.Name == "__csrf" || c.Name == "MUSIC_A" {
cookies = append(cookies, c)
}
}
cookies = append(cookies, &http.Cookie{
Name: "deviceId",
Value: n.deviceId,
})
n.ReqData.Cookies = cookies
return &miaosic.QrLoginResult{
Success: true,
@@ -61,6 +65,7 @@ func (n *Netease) Logout() error {
n.ReqData.Cookies = []*http.Cookie{
{Name: "MUSIC_U", Value: ""},
{Name: "__csrf", Value: ""},
{Name: "deviceId", Value: n.deviceId},
}
return nil
}
@@ -71,10 +76,11 @@ func (n *Netease) SaveSession() string {
data["MUSIC_U"] = ""
data["__csrf"] = ""
for _, c := range n.ReqData.Cookies {
if c.Name == "MUSIC_U" || c.Name == "__csrf" {
if c.Name == "MUSIC_U" || c.Name == "MUSIC_A" || c.Name == "__csrf" || c.Name == "deviceId" {
data[c.Name] = c.Value
}
}
data["deviceId"] = n.deviceId
b, _ := json.Marshal(data)
return base64.StdEncoding.EncodeToString(b)
}
@@ -92,11 +98,20 @@ func (n *Netease) RestoreSession(session string) error {
}
cookies := make([]*http.Cookie, 0)
for name, value := range data {
cookies = append(cookies, &http.Cookie{
Name: name,
Value: value,
})
if name == "MUSIC_U" || name == "MUSIC_A" || name == "__csrf" {
cookies = append(cookies, &http.Cookie{
Name: name,
Value: value,
})
}
if name == "deviceId" {
n.deviceId = value
}
}
cookies = append(cookies, &http.Cookie{
Name: "deviceId",
Value: n.deviceId,
})
n.ReqData.Cookies = cookies
return nil
}

View File

@@ -0,0 +1,19 @@
package netease
import (
"crypto/rand"
"math/big"
)
func generateRandomString(length int) (string, error) {
const charset = "0123456789abcdefghijklmnopqrstuvwxyz"
b := make([]byte, length)
for i := range b {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "", err
}
b[i] = charset[num.Int64()]
}
return string(b), nil
}

View File

@@ -7,6 +7,7 @@ import (
neteaseTypes "github.com/XiaoMengXinX/Music163Api-Go/types"
neteaseUtil "github.com/XiaoMengXinX/Music163Api-Go/utils"
"github.com/spf13/cast"
"net/http"
"regexp"
"slices"
"strconv"
@@ -19,6 +20,7 @@ type Netease struct {
IdRegex1 *regexp.Regexp
PlaylistRegex0 *regexp.Regexp
PlaylistRegex1 *regexp.Regexp
deviceId string
}
func (n *Netease) Qualities() []miaosic.Quality {
@@ -33,6 +35,10 @@ func (n *Netease) Qualities() []miaosic.Quality {
}
func NewNetease() *Netease {
deviceId, err := generateRandomString(32)
if err != nil {
deviceId = "00000000000000000000000000000000"
}
return &Netease{
ReqData: neteaseUtil.RequestData{
Headers: neteaseUtil.Headers{
@@ -40,12 +46,20 @@ func NewNetease() *Netease {
"X-Real-IP",
"118.88.88.88",
},
}},
},
Cookies: []*http.Cookie{
{
Name: "deviceId",
Value: deviceId,
},
},
},
IdRegex0: regexp.MustCompile("^[0-9]+$"),
IdRegex1: regexp.MustCompile("^wy[0-9]+$"),
PlaylistRegex0: regexp.MustCompile("^[0-9]+$"),
// https://music.163.com/playlist?id=2382819181&userid=95906480
PlaylistRegex1: regexp.MustCompile("playlist\\?id=[0-9]+"),
deviceId: deviceId,
}
}