83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package sub
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"s-ui/database"
|
|
"s-ui/database/model"
|
|
"s-ui/service"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type SubService struct {
|
|
service.SettingService
|
|
LinkService
|
|
}
|
|
|
|
func (s *SubService) GetSubs(subId string) (*string, []string, error) {
|
|
var err error
|
|
|
|
db := database.GetDB()
|
|
client := &model.Client{}
|
|
err = db.Model(model.Client{}).Where("enable = true and name = ?", subId).First(client).Error
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
clientInfo := ""
|
|
subShowInfo, _ := s.SettingService.GetSubShowInfo()
|
|
if subShowInfo {
|
|
clientInfo = s.getClientInfo(client)
|
|
}
|
|
|
|
linksArray := s.LinkService.GetLinks(&client.Links, "all", clientInfo)
|
|
result := strings.Join(linksArray, "\n")
|
|
|
|
var headers []string
|
|
updateInterval, _ := s.SettingService.GetSubUpdates()
|
|
headers = append(headers, fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", client.Up, client.Down, client.Volume, client.Expiry))
|
|
headers = append(headers, fmt.Sprintf("%d", updateInterval))
|
|
headers = append(headers, subId)
|
|
|
|
subEncode, _ := s.SettingService.GetSubEncode()
|
|
if subEncode {
|
|
result = base64.StdEncoding.EncodeToString([]byte(result))
|
|
}
|
|
|
|
return &result, headers, nil
|
|
}
|
|
|
|
func (s *SubService) getClientInfo(c *model.Client) string {
|
|
now := time.Now().Unix()
|
|
|
|
var result []string
|
|
if vol := c.Volume - (c.Up + c.Down); vol > 0 {
|
|
result = append(result, fmt.Sprintf("%s%s", s.formatTraffic(vol), "📊"))
|
|
}
|
|
if c.Expiry > 0 {
|
|
result = append(result, fmt.Sprintf("%d%s⏳", (c.Expiry-now)/86400, "Days"))
|
|
}
|
|
if len(result) > 0 {
|
|
return " " + strings.Join(result, " ")
|
|
} else {
|
|
return " ♾"
|
|
}
|
|
}
|
|
|
|
func (s *SubService) formatTraffic(trafficBytes int64) string {
|
|
if trafficBytes < 1024 {
|
|
return fmt.Sprintf("%.2fB", float64(trafficBytes)/float64(1))
|
|
} else if trafficBytes < (1024 * 1024) {
|
|
return fmt.Sprintf("%.2fKB", float64(trafficBytes)/float64(1024))
|
|
} else if trafficBytes < (1024 * 1024 * 1024) {
|
|
return fmt.Sprintf("%.2fMB", float64(trafficBytes)/float64(1024*1024))
|
|
} else if trafficBytes < (1024 * 1024 * 1024 * 1024) {
|
|
return fmt.Sprintf("%.2fGB", float64(trafficBytes)/float64(1024*1024*1024))
|
|
} else if trafficBytes < (1024 * 1024 * 1024 * 1024 * 1024) {
|
|
return fmt.Sprintf("%.2fTB", float64(trafficBytes)/float64(1024*1024*1024*1024))
|
|
} else {
|
|
return fmt.Sprintf("%.2fEB", float64(trafficBytes)/float64(1024*1024*1024*1024*1024))
|
|
}
|
|
}
|