diff --git a/adapter/provider/adapter.go b/adapter/provider/adapter.go index 162506a8..3be6ad7a 100644 --- a/adapter/provider/adapter.go +++ b/adapter/provider/adapter.go @@ -289,11 +289,21 @@ func uniquifyTags(opts []option.Outbound) { func removeEmojisFromTags(opts []option.Outbound) { for i, opt := range opts { - cleaned := emojiRegex.ReplaceAllString(opt.Tag, "") + cleaned := flagRegex.ReplaceAllStringFunc(opt.Tag, flagToCountryCode) + cleaned = emojiRegex.ReplaceAllString(cleaned, "") cleaned = multiSpaceRegex.ReplaceAllString(cleaned, " ") opts[i].Tag = strings.TrimSpace(cleaned) } } +func flagToCountryCode(flag string) string { + runes := []rune(flag) + if len(runes) == 2 { + return string(rune(runes[0]-0x1F1E6+'A')) + string(rune(runes[1]-0x1F1E6+'A')) + " " + } + return "" +} + +var flagRegex = regexp.MustCompile(`[\x{1F1E6}-\x{1F1FF}]{2}`) var emojiRegex = regexp.MustCompile(`[\x{1F1E0}-\x{1F1FF}\x{1F300}-\x{1F9FF}\x{2600}-\x{27BF}\x{FE00}-\x{FE0F}\x{200D}]+`) var multiSpaceRegex = regexp.MustCompile(`\s{2,}`) diff --git a/adapter/provider/adapter_test.go b/adapter/provider/adapter_test.go new file mode 100644 index 00000000..e855f293 --- /dev/null +++ b/adapter/provider/adapter_test.go @@ -0,0 +1,45 @@ +package provider + +import ( + "testing" + + "github.com/sagernet/sing-box/option" +) + +func TestFlagToCountryCodeAllFlags(t *testing.T) { + for first := 'A'; first <= 'Z'; first++ { + for second := 'A'; second <= 'Z'; second++ { + flag := string(rune(0x1F1E6+(first-'A'))) + string(rune(0x1F1E6+(second-'A'))) + expected := string(first) + string(second) + result := flagToCountryCode(flag) + // flagToCountryCode appends a space + if result != expected+" " { + t.Errorf("flagToCountryCode(%q) = %q, want %q", expected, result, expected+" ") + } + } + } +} + +func TestRemoveEmojisFromTags(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"πŸ‡ΊπŸ‡Έ United States", "US United States"}, + {"πŸ‡·πŸ‡Ί Россия", "RU Россия"}, + {"πŸ‡©πŸ‡ͺ Germany πŸš€", "DE Germany"}, + {"πŸ‡«πŸ‡·πŸ‡¬πŸ‡§ France-UK", "FR GB France-UK"}, + {"No emojis here", "No emojis here"}, + {"🌍 World", "World"}, + {"πŸ‡―πŸ‡΅ Tokyo ⚑ Fast", "JP Tokyo Fast"}, + {"Germany πŸ‡©πŸ‡ͺ", "Germany DE"}, + {"Server πŸ‡ΊπŸ‡Έ Node", "Server US Node"}, + } + for _, tt := range tests { + opts := []option.Outbound{{Tag: tt.input}} + removeEmojisFromTags(opts) + if opts[0].Tag != tt.expected { + t.Errorf("removeEmojisFromTags(%q) = %q, want %q", tt.input, opts[0].Tag, tt.expected) + } + } +} diff --git a/go.mod b/go.mod index 103f8535..46501dc3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/sagernet/sing-box -go 1.26.1 +go 1.26.4 require ( github.com/AliRizaAynaci/gorl/v2 v2.2.0