Skip to content

Commit

Permalink
chore: parse float in subscription info
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Sep 11, 2024
1 parent ecbbf9d commit 8230bc8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
6 changes: 1 addition & 5 deletions adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/metacubex/mihomo/component/resource"
C "github.com/metacubex/mihomo/constant"
types "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/tunnel/statistic"

"github.com/dlclark/regexp2"
Expand Down Expand Up @@ -149,10 +148,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
return
}
}
pp.subscriptionInfo, err = NewSubscriptionInfo(userInfoStr)
if err != nil {
log.Warnln("[Provider] get subscription-userinfo: %e", err)
}
pp.subscriptionInfo = NewSubscriptionInfo(userInfoStr)
}()
}

Expand Down
49 changes: 35 additions & 14 deletions adapter/provider/subscription_info.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package provider

import (
"fmt"
"strconv"
"strings"

"github.com/metacubex/mihomo/log"
)

type SubscriptionInfo struct {
Expand All @@ -12,28 +15,46 @@ type SubscriptionInfo struct {
Expire int64
}

func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo, err error) {
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo) {
userinfo = strings.ToLower(userinfo)
userinfo = strings.ReplaceAll(userinfo, " ", "")
si = new(SubscriptionInfo)

for _, field := range strings.Split(userinfo, ";") {
switch name, value, _ := strings.Cut(field, "="); name {
name, value, ok := strings.Cut(field, "=")
if !ok {
continue
}

intValue, err := parseValue(value)
if err != nil {
log.Warnln("[Provider] get subscription-userinfo: %e", err)
continue
}

switch name {
case "upload":
si.Upload, err = strconv.ParseInt(value, 10, 64)
si.Upload = intValue
case "download":
si.Download, err = strconv.ParseInt(value, 10, 64)
si.Download = intValue
case "total":
si.Total, err = strconv.ParseInt(value, 10, 64)
si.Total = intValue
case "expire":
if value == "" {
si.Expire = 0
} else {
si.Expire, err = strconv.ParseInt(value, 10, 64)
}
}
if err != nil {
return
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)
}

0 comments on commit 8230bc8

Please sign in to comment.