Clash.Meta/adapter/provider/subscription_info.go

57 lines
1.1 KiB
Go
Raw Normal View History

2022-11-05 02:24:08 +08:00
package provider
import (
"fmt"
2022-11-05 02:24:08 +08:00
"strconv"
"strings"
"github.com/metacubex/mihomo/log"
2022-11-05 02:24:08 +08:00
)
type SubscriptionInfo struct {
2022-11-08 22:30:50 +08:00
Upload int64
Download int64
Total int64
Expire int64
2022-11-05 02:24:08 +08:00
}
2024-12-25 10:23:55 +08:00
func (info *SubscriptionInfo) Update(userinfo string) {
userinfo = strings.ReplaceAll(strings.ToLower(userinfo), " ", "")
for _, field := range strings.Split(userinfo, ";") {
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":
2024-12-25 10:23:55 +08:00
info.Upload = intValue
case "download":
2024-12-25 10:23:55 +08:00
info.Download = intValue
case "total":
2024-12-25 10:23:55 +08:00
info.Total = intValue
case "expire":
2024-12-25 10:23:55 +08:00
info.Expire = intValue
2022-11-05 02:24:08 +08:00
}
}
}
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)
2022-11-05 02:24:08 +08:00
}