diff --git a/service/common/parseGeoIP/parser.go b/service/common/parseGeoIP/parser.go index 2303ff38..e45c217a 100644 --- a/service/common/parseGeoIP/parser.go +++ b/service/common/parseGeoIP/parser.go @@ -24,13 +24,17 @@ type parserCacheKey struct { countryCode string } -var parserCache sync.Map +var parserCache = make(map[parserCacheKey]parserResult) +var parserCacheMu sync.Mutex func Parser(filename string, countryCode string) ([]string, []string, error) { realpath, err := asset.GetV2rayLocationAsset(filename) if err != nil { return nil, nil, err } + // Serialize misses, including stat, so a waiter cannot restore an old generation. + parserCacheMu.Lock() + defer parserCacheMu.Unlock() info, err := os.Stat(realpath) if err != nil { return nil, nil, err @@ -42,10 +46,14 @@ func Parser(filename string, countryCode string) ([]string, []string, error) { modTime: info.ModTime().UnixNano(), countryCode: countryCode, } - if cached, ok := parserCache.Load(cacheKey); ok { - result := cached.(parserResult) + if result, ok := parserCache[cacheKey]; ok { return result.ipv4, result.ipv6, nil } + for key := range parserCache { + if key.path == realpath && (key.size != cacheKey.size || key.modTime != cacheKey.modTime) { + delete(parserCache, key) + } + } data, err := os.ReadFile(realpath) if err != nil { @@ -57,7 +65,7 @@ func Parser(filename string, countryCode string) ([]string, []string, error) { return ipv4List, ipv6List, err } - parserCache.Store(cacheKey, parserResult{ipv4: ipv4List, ipv6: ipv6List}) + parserCache[cacheKey] = parserResult{ipv4: ipv4List, ipv6: ipv6List} return ipv4List, ipv6List, nil } diff --git a/service/common/parseGeoIP/parser_test.go b/service/common/parseGeoIP/parser_test.go index b19b48fe..519c29f1 100644 --- a/service/common/parseGeoIP/parser_test.go +++ b/service/common/parseGeoIP/parser_test.go @@ -2,12 +2,98 @@ package parseGeoIP import ( "net" + "os" + "path/filepath" "reflect" + "sync" "testing" + "time" "google.golang.org/protobuf/proto" ) +func TestParserCache(t *testing.T) { + dir := t.TempDir() + t.Setenv("XRAY_LOCATION_ASSET", dir) + parserCache = make(map[parserCacheKey]parserResult) + t.Cleanup(func() { parserCache = make(map[parserCacheKey]parserResult) }) + write := func(name string, generation int) { + t.Helper() + data, err := proto.Marshal(&GeoIPList{Entry: []*GeoIP{ + {CountryCode: "A", Cidr: []*CIDR{{Ip: []byte{10, byte(generation), 0, 0}, Prefix: 16}}}, + {CountryCode: "B", Cidr: []*CIDR{{Ip: []byte{192, 0, 2, 0}, Prefix: 24}}}, + }}) + if err != nil { + t.Fatal(err) + } + path := filepath.Join(dir, name) + if err := os.WriteFile(path, data, 0600); err != nil { + t.Fatal(err) + } + stamp := time.Unix(int64(generation), 0) + if err := os.Chtimes(path, stamp, stamp); err != nil { + t.Fatal(err) + } + } + write("other.dat", 9) + if _, _, err := Parser("other.dat", "A"); err != nil { + t.Fatal(err) + } + for generation := 1; generation <= 3; generation++ { + write("test.dat", generation) + var wg sync.WaitGroup + results := make([][]string, 16) + for i := range results { + wg.Add(1) + go func(i int) { + defer wg.Done() + var err error + results[i], _, err = Parser("test.dat", "A") + if err != nil { + t.Error(err) + } + }(i) + } + wg.Wait() + want := net.IP{10, byte(generation), 0, 0}.String() + "/16" + for _, result := range results { + if len(result) != 1 || result[0] != want { + t.Fatalf("got %v, want %s", result, want) + } + // Re-parsing allocates another slice; concurrent callers must share the cached result. + if &result[0] != &results[0][0] { + t.Fatal("concurrent miss parsed more than once") + } + } + if got, _, err := Parser("test.dat", "B"); err != nil || !reflect.DeepEqual(got, []string{"192.0.2.0/24"}) { + t.Fatalf("second country: %v, %v", got, err) + } + if len(parserCache) != 3 { + t.Fatalf("cache retains %d entries, want 3", len(parserCache)) + } + } + if got, _, err := Parser("other.dat", "A"); err != nil || !reflect.DeepEqual(got, []string{"10.9.0.0/16"}) { + t.Fatalf("other file: %v, %v", got, err) + } + + if _, _, err := Parser("missing.dat", "A"); err == nil { + t.Fatal("missing file accepted") + } + if err := os.WriteFile(filepath.Join(dir, "missing.dat"), []byte{0x0a, 0x02, 0xff}, 0600); err != nil { + t.Fatal(err) + } + if _, _, err := Parser("missing.dat", "A"); err == nil { + t.Fatal("malformed file accepted") + } + if len(parserCache) != 3 { + t.Fatal("failed parse cached") + } + write("missing.dat", 4) + if got, _, err := Parser("missing.dat", "A"); err != nil || !reflect.DeepEqual(got, []string{"10.4.0.0/16"}) { + t.Fatalf("retry: %v, %v", got, err) + } +} + func TestParseGeoIPStopsAtRequestedEntry(t *testing.T) { data, err := proto.Marshal(&GeoIPList{Entry: []*GeoIP{ {