github.com/yaling888/clash@v1.53.0/adapter/provider/subscription.go (about)

     1  package provider
     2  
     3  import (
     4  	"net/textproto"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  type Subscription struct {
    10  	Upload   int64 `json:"upload"`
    11  	Download int64 `json:"download"`
    12  	Total    int64 `json:"total"`
    13  	Expire   int64 `json:"expire"`
    14  }
    15  
    16  func (s *Subscription) parse(userinfo string) {
    17  	s.Upload = 0
    18  	s.Download = 0
    19  	s.Total = 0
    20  	s.Expire = 0
    21  
    22  	// Subscription-Userinfo:
    23  	// upload=2859205172; download=319670653638; total=322122547200; expire=1711870550
    24  	parts := strings.Split(textproto.TrimString(userinfo), ";")
    25  	if len(parts) == 1 && parts[0] == "" {
    26  		return
    27  	}
    28  
    29  	for i := 0; i < len(parts); i++ {
    30  		parts[i] = textproto.TrimString(parts[i])
    31  		key, value, ok := strings.Cut(parts[i], "=")
    32  		if !ok {
    33  			continue
    34  		}
    35  
    36  		val, err := strconv.ParseInt(textproto.TrimString(value), 10, 64)
    37  		if err != nil {
    38  			continue
    39  		}
    40  
    41  		key = textproto.TrimString(key)
    42  		switch key {
    43  		case "upload":
    44  			s.Upload = val
    45  		case "download":
    46  			s.Download = val
    47  		case "total":
    48  			s.Total = val
    49  		case "expire":
    50  			s.Expire = val
    51  		}
    52  	}
    53  }