diff options
| author | Shishkevich D. <135337715+shishkevichd@users.noreply.github.com> | 2025-05-06 19:10:58 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-06 19:10:58 +0300 |
| commit | 1aed2d8cdcd7b971d3bc055d428a7958a71c9226 (patch) | |
| tree | d70860e2eb0e5ddd85dcf69839c547cddb78af76 /web/service | |
| parent | c3084aaecea6e2ddc309c899a5def77244b901f1 (diff) | |
feat: implement geofiles update in panel (#2971)
solves #2672
Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
Diffstat (limited to 'web/service')
| -rw-r--r-- | web/service/server.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/web/service/server.go b/web/service/server.go index 99e2a97b..23167fb3 100644 --- a/web/service/server.go +++ b/web/service/server.go @@ -591,6 +591,66 @@ func (s *ServerService) ImportDB(file multipart.File) error { return nil } +func (s *ServerService) UpdateGeofile(fileName string) error { + files := []struct { + URL string + FileName string + }{ + {"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat", "geoip.dat"}, + {"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat", "geosite.dat"}, + {"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat", "geoip_IR.dat"}, + {"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat", "geosite_IR.dat"}, + {"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat", "geoip_RU.dat"}, + {"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat", "geosite_RU.dat"}, + } + + downloadFile := func(url, destPath string) error { + resp, err := http.Get(url) + if err != nil { + return common.NewErrorf("Failed to download Geofile from %s: %v", url, err) + } + defer resp.Body.Close() + + file, err := os.Create(destPath) + if err != nil { + return common.NewErrorf("Failed to create Geofile %s: %v", destPath, err) + } + defer file.Close() + + _, err = io.Copy(file, resp.Body) + if err != nil { + return common.NewErrorf("Failed to save Geofile %s: %v", destPath, err) + } + + return nil + } + + var fileURL string + for _, file := range files { + if file.FileName == fileName { + fileURL = file.URL + break + } + } + + if fileURL == "" { + return common.NewErrorf("File '%s' not found in the list of Geofiles", fileName) + } + + destPath := fmt.Sprintf("%s/%s", config.GetBinFolderPath(), fileName) + + if err := downloadFile(fileURL, destPath); err != nil { + return common.NewErrorf("Error downloading Geofile '%s': %v", fileName, err) + } + + err := s.RestartXrayService() + if err != nil { + return common.NewErrorf("Updated Geofile '%s' but Failed to start Xray: %v", fileName, err) + } + + return nil +} + func (s *ServerService) GetNewX25519Cert() (any, error) { // Run the command cmd := exec.Command(xray.GetBinaryPath(), "x25519") |
