2022-11-05 02:24:08 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2024-09-11 13:34:59 +08:00
|
|
|
"fmt"
|
2022-11-05 02:24:08 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-09-11 13:34:59 +08:00
|
|
|
|
|
|
|
"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-09-11 13:34:59 +08:00
|
|
|
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo) {
|
2024-10-20 06:01:02 +08:00
|
|
|
userinfo = strings.ReplaceAll(strings.ToLower(userinfo), " ", "")
|
2023-09-29 08:42:57 +08:00
|
|
|
si = new(SubscriptionInfo)
|
2024-09-11 13:34:59 +08:00
|
|
|
|
2023-09-29 08:42:57 +08:00
|
|
|
for _, field := range strings.Split(userinfo, ";") {
|
2024-09-11 13:34:59 +08:00
|
|
|
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 {
|
2023-09-29 08:42:57 +08:00
|
|
|
case "upload":
|
2024-09-11 13:34:59 +08:00
|
|
|
si.Upload = intValue
|
2023-09-29 08:42:57 +08:00
|
|
|
case "download":
|
2024-09-11 13:34:59 +08:00
|
|
|
si.Download = intValue
|
2023-09-29 08:42:57 +08:00
|
|
|
case "total":
|
2024-09-11 13:34:59 +08:00
|
|
|
si.Total = intValue
|
2023-09-29 08:42:57 +08:00
|
|
|
case "expire":
|
2024-09-11 13:34:59 +08:00
|
|
|
si.Expire = intValue
|
2022-11-05 02:24:08 +08:00
|
|
|
}
|
|
|
|
}
|
2024-09-11 13:34:59 +08:00
|
|
|
|
|
|
|
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)
|
2022-11-05 02:24:08 +08:00
|
|
|
}
|