1
1
package provider
2
2
3
3
import (
4
+ "fmt"
4
5
"strconv"
5
6
"strings"
7
+
8
+ "github.com/metacubex/mihomo/log"
6
9
)
7
10
8
11
type SubscriptionInfo struct {
@@ -12,28 +15,46 @@ type SubscriptionInfo struct {
12
15
Expire int64
13
16
}
14
17
15
- func NewSubscriptionInfo (userinfo string ) (si * SubscriptionInfo , err error ) {
18
+ func NewSubscriptionInfo (userinfo string ) (si * SubscriptionInfo ) {
16
19
userinfo = strings .ToLower (userinfo )
17
20
userinfo = strings .ReplaceAll (userinfo , " " , "" )
18
21
si = new (SubscriptionInfo )
22
+
19
23
for _ , field := range strings .Split (userinfo , ";" ) {
20
- switch name , value , _ := strings .Cut (field , "=" ); name {
24
+ name , value , ok := strings .Cut (field , "=" )
25
+ if ! ok {
26
+ continue
27
+ }
28
+
29
+ intValue , err := parseValue (value )
30
+ if err != nil {
31
+ log .Warnln ("[Provider] get subscription-userinfo: %e" , err )
32
+ continue
33
+ }
34
+
35
+ switch name {
21
36
case "upload" :
22
- si .Upload , err = strconv . ParseInt ( value , 10 , 64 )
37
+ si .Upload = intValue
23
38
case "download" :
24
- si .Download , err = strconv . ParseInt ( value , 10 , 64 )
39
+ si .Download = intValue
25
40
case "total" :
26
- si .Total , err = strconv . ParseInt ( value , 10 , 64 )
41
+ si .Total = intValue
27
42
case "expire" :
28
- if value == "" {
29
- si .Expire = 0
30
- } else {
31
- si .Expire , err = strconv .ParseInt (value , 10 , 64 )
32
- }
33
- }
34
- if err != nil {
35
- return
43
+ si .Expire = intValue
36
44
}
37
45
}
38
- return
46
+
47
+ return si
48
+ }
49
+
50
+ func parseValue (value string ) (int64 , error ) {
51
+ if intValue , err := strconv .ParseInt (value , 10 , 64 ); err == nil {
52
+ return intValue , nil
53
+ }
54
+
55
+ if floatValue , err := strconv .ParseFloat (value , 64 ); err == nil {
56
+ return int64 (floatValue ), nil
57
+ }
58
+
59
+ return 0 , fmt .Errorf ("failed to parse value '%s'" , value )
39
60
}
0 commit comments