mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 12:42:27 +08:00
chore: parse float in subscription info
This commit is contained in:
parent
ecbbf9d220
commit
8230bc8e7d
@ -18,7 +18,6 @@ import (
|
|||||||
"github.com/metacubex/mihomo/component/resource"
|
"github.com/metacubex/mihomo/component/resource"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
types "github.com/metacubex/mihomo/constant/provider"
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
"github.com/metacubex/mihomo/tunnel/statistic"
|
"github.com/metacubex/mihomo/tunnel/statistic"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
@ -149,10 +148,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pp.subscriptionInfo, err = NewSubscriptionInfo(userInfoStr)
|
pp.subscriptionInfo = NewSubscriptionInfo(userInfoStr)
|
||||||
if err != nil {
|
|
||||||
log.Warnln("[Provider] get subscription-userinfo: %e", err)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SubscriptionInfo struct {
|
type SubscriptionInfo struct {
|
||||||
@ -12,28 +15,46 @@ type SubscriptionInfo struct {
|
|||||||
Expire int64
|
Expire int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo, err error) {
|
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo) {
|
||||||
userinfo = strings.ToLower(userinfo)
|
userinfo = strings.ToLower(userinfo)
|
||||||
userinfo = strings.ReplaceAll(userinfo, " ", "")
|
userinfo = strings.ReplaceAll(userinfo, " ", "")
|
||||||
si = new(SubscriptionInfo)
|
si = new(SubscriptionInfo)
|
||||||
|
|
||||||
for _, field := range strings.Split(userinfo, ";") {
|
for _, field := range strings.Split(userinfo, ";") {
|
||||||
switch name, value, _ := strings.Cut(field, "="); name {
|
name, value, ok := strings.Cut(field, "=")
|
||||||
case "upload":
|
if !ok {
|
||||||
si.Upload, err = strconv.ParseInt(value, 10, 64)
|
continue
|
||||||
case "download":
|
|
||||||
si.Download, err = strconv.ParseInt(value, 10, 64)
|
|
||||||
case "total":
|
|
||||||
si.Total, err = strconv.ParseInt(value, 10, 64)
|
|
||||||
case "expire":
|
|
||||||
if value == "" {
|
|
||||||
si.Expire = 0
|
|
||||||
} else {
|
|
||||||
si.Expire, err = strconv.ParseInt(value, 10, 64)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intValue, err := parseValue(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
log.Warnln("[Provider] get subscription-userinfo: %e", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch name {
|
||||||
|
case "upload":
|
||||||
|
si.Upload = intValue
|
||||||
|
case "download":
|
||||||
|
si.Download = intValue
|
||||||
|
case "total":
|
||||||
|
si.Total = intValue
|
||||||
|
case "expire":
|
||||||
|
si.Expire = intValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
return si
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseValue(value string) (int64, error) {
|
||||||
|
if intValue, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||||
|
return intValue, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
|
||||||
|
return int64(floatValue), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, fmt.Errorf("failed to parse value '%s'", value)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user