diff options
| author | MHSanaei <ho3ein.sanaei@gmail.com> | 2023-04-18 21:04:06 +0300 |
|---|---|---|
| committer | MHSanaei <ho3ein.sanaei@gmail.com> | 2023-04-18 21:04:06 +0300 |
| commit | 3e0faecaaebd8394a1a9d18ec11b5a60f210b654 (patch) | |
| tree | 70226a9e7a73155d05410bda80a38673f8db2a83 /web/service/sub.go | |
| parent | dc7dbae14a37492ac3a7e3822b3e0b250e248173 (diff) | |
improve reality setting
split xtls from tls - remove iran warp - remove old setting reality from franzkafka (it was a messy code) -and other improvement
Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
Diffstat (limited to 'web/service/sub.go')
| -rw-r--r-- | web/service/sub.go | 70 |
1 files changed, 56 insertions, 14 deletions
diff --git a/web/service/sub.go b/web/service/sub.go index 06c597e8..3041b721 100644 --- a/web/service/sub.go +++ b/web/service/sub.go @@ -8,6 +8,7 @@ import ( "x-ui/database" "x-ui/database/model" "x-ui/logger" + "x-ui/xray" "github.com/goccy/go-json" "gorm.io/gorm" @@ -18,12 +19,15 @@ type SubService struct { inboundService InboundService } -func (s *SubService) GetSubs(subId string, host string) ([]string, error) { +func (s *SubService) GetSubs(subId string, host string) ([]string, string, error) { s.address = host var result []string + var header string + var traffic xray.ClientTraffic + var clientTraffics []xray.ClientTraffic inbounds, err := s.getInboundsBySubId(subId) if err != nil { - return nil, err + return nil, "", err } for _, inbound := range inbounds { clients, err := s.inboundService.getClients(inbound) @@ -37,22 +41,60 @@ func (s *SubService) GetSubs(subId string, host string) ([]string, error) { if client.SubID == subId { link := s.getLink(inbound, client.Email) result = append(result, link) + clientTraffics = append(clientTraffics, s.getClientTraffics(inbound.ClientStats, client.Email)) + } + } + } + for index, clientTraffic := range clientTraffics { + if index == 0 { + traffic.Up = clientTraffic.Up + traffic.Down = clientTraffic.Down + traffic.Total = clientTraffic.Total + if clientTraffic.ExpiryTime > 0 { + traffic.ExpiryTime = clientTraffic.ExpiryTime + } + } else { + traffic.Up += clientTraffic.Up + traffic.Down += clientTraffic.Down + if traffic.Total == 0 || clientTraffic.Total == 0 { + traffic.Total = 0 + } else { + traffic.Total += clientTraffic.Total + } + if clientTraffic.ExpiryTime != traffic.ExpiryTime { + traffic.ExpiryTime = 0 } } } - return result, nil + header = fmt.Sprintf("upload=%d;download=%d", traffic.Up, traffic.Down) + if traffic.Total > 0 { + header = header + fmt.Sprintf(";total=%d", traffic.Total) + } + if traffic.ExpiryTime > 0 { + header = header + fmt.Sprintf(";expire=%d", traffic.ExpiryTime) + } + return result, header, nil } func (s *SubService) getInboundsBySubId(subId string) ([]*model.Inbound, error) { db := database.GetDB() var inbounds []*model.Inbound - err := db.Model(model.Inbound{}).Where("settings like ?", fmt.Sprintf(`%%"subId": "%s"%%`, subId)).Find(&inbounds).Error + err := db.Model(model.Inbound{}).Preload("ClientStats").Where("settings like ?", fmt.Sprintf(`%%"subId": "%s"%%`, subId)).Find(&inbounds).Error if err != nil && err != gorm.ErrRecordNotFound { return nil, err } return inbounds, nil } +func (s *SubService) getClientTraffics(traffics []xray.ClientTraffic, email string) xray.ClientTraffic { + for _, traffic := range traffics { + if traffic.Email == email { + return traffic + } + } + return xray.ClientTraffic{} +} + func (s *SubService) getLink(inbound *model.Inbound, email string) string { switch inbound.Protocol { case "vmess": @@ -296,7 +338,7 @@ func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string { if security == "xtls" { params["security"] = "xtls" - xtlsSetting, _ := stream["XTLSSettings"].(map[string]interface{}) + xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{}) alpns, _ := xtlsSetting["alpn"].([]interface{}) var alpn []string for _, a := range alpns { @@ -306,15 +348,15 @@ func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string { params["alpn"] = strings.Join(alpn, ",") } - XTLSSettings, _ := searchKey(xtlsSetting, "settings") + xtlsSettings, _ := searchKey(xtlsSetting, "settings") if xtlsSetting != nil { - if sniValue, ok := searchKey(XTLSSettings, "serverName"); ok { + if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok { params["sni"], _ = sniValue.(string) } - if fpValue, ok := searchKey(XTLSSettings, "fingerprint"); ok { + if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok { params["fp"], _ = fpValue.(string) } - if insecure, ok := searchKey(XTLSSettings, "allowInsecure"); ok { + if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok { if insecure.(bool) { params["allowInsecure"] = "1" } @@ -465,7 +507,7 @@ func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string if security == "xtls" { params["security"] = "xtls" - xtlsSetting, _ := stream["XTLSSettings"].(map[string]interface{}) + xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{}) alpns, _ := xtlsSetting["alpn"].([]interface{}) var alpn []string for _, a := range alpns { @@ -475,15 +517,15 @@ func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string params["alpn"] = strings.Join(alpn, ",") } - XTLSSettings, _ := searchKey(xtlsSetting, "settings") + xtlsSettings, _ := searchKey(xtlsSetting, "settings") if xtlsSetting != nil { - if sniValue, ok := searchKey(XTLSSettings, "serverName"); ok { + if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok { params["sni"], _ = sniValue.(string) } - if fpValue, ok := searchKey(XTLSSettings, "fingerprint"); ok { + if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok { params["fp"], _ = fpValue.(string) } - if insecure, ok := searchKey(XTLSSettings, "allowInsecure"); ok { + if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok { if insecure.(bool) { params["allowInsecure"] = "1" } |
