better freedom/blackhole tags handling

This commit is contained in:
fgsfds 2025-08-05 02:49:53 +05:00
parent 3c4905db9a
commit ddd47d69da
2 changed files with 61 additions and 9 deletions

View file

@ -17,7 +17,8 @@ var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
type ServerController struct { type ServerController struct {
BaseController BaseController
serverService service.ServerService serverService service.ServerService
settingService service.SettingService
lastStatus *service.Status lastStatus *service.Status
lastGetStatusTime time.Time lastGetStatusTime time.Time
@ -141,7 +142,41 @@ func (a *ServerController) getXrayLogs(c *gin.Context) {
showDirect := c.PostForm("showDirect") showDirect := c.PostForm("showDirect")
showBlocked := c.PostForm("showBlocked") showBlocked := c.PostForm("showBlocked")
showProxy := c.PostForm("showProxy") showProxy := c.PostForm("showProxy")
logs := a.serverService.GetXrayLogs(count, filter, showDirect, showBlocked, showProxy)
var freedoms []string
var blackholes []string
//getting tags for freedom and blackhole outbounds
config, err := a.settingService.GetDefaultXrayConfig()
if err == nil && config != nil {
if cfgMap, ok := config.(map[string]interface{}); ok {
if outbounds, ok := cfgMap["outbounds"].([]interface{}); ok {
for _, outbound := range outbounds {
if obMap, ok := outbound.(map[string]interface{}); ok {
switch obMap["protocol"] {
case "freedom":
if tag, ok := obMap["tag"].(string); ok {
freedoms = append(freedoms, tag)
}
case "blackhole":
if tag, ok := obMap["tag"].(string); ok {
blackholes = append(blackholes, tag)
}
}
}
}
}
}
}
if len(freedoms) == 0 {
freedoms = []string{"direct"}
}
if len(blackholes) == 0 {
blackholes = []string{"blocked"}
}
logs := a.serverService.GetXrayLogs(count, filter, showDirect, showBlocked, showProxy, freedoms, blackholes)
jsonObj(c, logs, nil) jsonObj(c, logs, nil)
} }

View file

@ -482,7 +482,14 @@ func (s *ServerService) GetLogs(count string, level string, syslog string) []str
return lines return lines
} }
func (s *ServerService) GetXrayLogs(count string, filter string, showDirect string, showBlocked string, showProxy string) []string { func (s *ServerService) GetXrayLogs(
count string,
filter string,
showDirect string,
showBlocked string,
showProxy string,
freedoms []string,
blackholes []string) []string {
c, _ := strconv.Atoi(count) c, _ := strconv.Atoi(count)
var lines []string var lines []string
@ -498,22 +505,23 @@ func (s *ServerService) GetXrayLogs(count string, filter string, showDirect stri
defer file.Close() defer file.Close()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.TrimSpace(line) == "" || strings.Contains(line, "api -> api") { for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.Contains(line, "api -> api") {
continue continue
} }
if filter != "" && !strings.Contains(line, filter) { if filter != "" && !strings.Contains(line, filter) {
continue continue
} }
if showDirect == "false" && strings.HasSuffix(line, "direct]") {
if showDirect == "false" && hasSuffix(line, freedoms) {
continue continue
} }
if showBlocked == "false" && strings.HasSuffix(line, "blocked]") { if showBlocked == "false" && hasSuffix(line, blackholes) {
continue continue
} }
if showProxy == "false" && !strings.HasSuffix(line, "blocked]") && !strings.HasSuffix(line, "direct]") { if showProxy == "false" && !hasSuffix(line, append(freedoms, blackholes...)) {
continue continue
} }
@ -527,6 +535,15 @@ func (s *ServerService) GetXrayLogs(count string, filter string, showDirect stri
return lines return lines
} }
func hasSuffix(line string, suffixes []string) bool {
for _, sfx := range suffixes {
if strings.HasSuffix(line, sfx+"]") {
return true
}
}
return false
}
func (s *ServerService) GetConfigJson() (any, error) { func (s *ServerService) GetConfigJson() (any, error) {
config, err := s.xrayService.GetXrayConfig() config, err := s.xrayService.GetXrayConfig()
if err != nil { if err != nil {