chore: improve subscription userinfo parsing (#781)
do not use regex parsing for `Subscription-UserInfo` header field
This commit is contained in:
parent
c2b06a02bf
commit
0ed3c5a5ec
1 changed files with 16 additions and 38 deletions
|
@ -1,7 +1,6 @@
|
||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dlclark/regexp2"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -13,45 +12,24 @@ type SubscriptionInfo struct {
|
||||||
Expire int64
|
Expire int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriptionInfo(str string) (si *SubscriptionInfo, err error) {
|
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo, err error) {
|
||||||
si = &SubscriptionInfo{}
|
userinfo = strings.ToLower(userinfo)
|
||||||
str = strings.ToLower(str)
|
userinfo = strings.ReplaceAll(userinfo, " ", "")
|
||||||
reTraffic := regexp2.MustCompile("upload=(\\d+); download=(\\d+); total=(\\d+)", 0)
|
si = new(SubscriptionInfo)
|
||||||
reExpire := regexp2.MustCompile("expire=(\\d+)", 0)
|
for _, field := range strings.Split(userinfo, ";") {
|
||||||
|
switch name, value, _ := strings.Cut(field, "="); name {
|
||||||
match, err := reTraffic.FindStringMatch(str)
|
case "upload":
|
||||||
if err != nil || match == nil {
|
si.Upload, err = strconv.ParseInt(value, 10, 64)
|
||||||
return nil, err
|
case "download":
|
||||||
}
|
si.Download, err = strconv.ParseInt(value, 10, 64)
|
||||||
group := match.Groups()
|
case "total":
|
||||||
si.Upload, err = str2uint64(group[1].String())
|
si.Total, err = strconv.ParseInt(value, 10, 64)
|
||||||
if err != nil {
|
case "expire":
|
||||||
return nil, err
|
si.Expire, err = strconv.ParseInt(value, 10, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
si.Download, err = str2uint64(group[2].String())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
si.Total, err = str2uint64(group[3].String())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
match, _ = reExpire.FindStringMatch(str)
|
|
||||||
if match != nil {
|
|
||||||
group = match.Groups()
|
|
||||||
si.Expire, err = str2uint64(group[1].String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func str2uint64(str string) (int64, error) {
|
|
||||||
i, err := strconv.ParseInt(str, 10, 64)
|
|
||||||
return i, err
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue