Add iran.dat when updating xray (#870)

we can download iran.dat, because it's one of the required files in our settings
This commit is contained in:
Hamidreza 2023-08-08 22:21:02 +03:30 committed by GitHub
parent 22cf278ce2
commit 24eb36715a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 10 deletions

View File

@ -254,7 +254,6 @@ func (s *ServerService) GetXrayVersions() ([]string, error) {
} }
func (s *ServerService) StopXrayService() (string error) { func (s *ServerService) StopXrayService() (string error) {
err := s.xrayService.StopXray() err := s.xrayService.StopXray()
if err != nil { if err != nil {
logger.Error("stop xray failed:", err) logger.Error("stop xray failed:", err)
@ -265,7 +264,6 @@ func (s *ServerService) StopXrayService() (string error) {
} }
func (s *ServerService) RestartXrayService() (string error) { func (s *ServerService) RestartXrayService() (string error) {
s.xrayService.StopXray() s.xrayService.StopXray()
defer func() { defer func() {
err := s.xrayService.RestartXray(true) err := s.xrayService.RestartXray(true)
@ -363,18 +361,48 @@ func (s *ServerService) UpdateXray(version string) error {
return err return err
} }
err = copyZipFile("xray", xray.GetBinaryPath()) downloadFile := func(fileName string, url string) error {
os.Remove(fileName)
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, fs.ModePerm)
if err != nil { if err != nil {
return err return err
} }
err = copyZipFile("geosite.dat", xray.GetGeositePath()) defer file.Close()
resp, err := http.Get(url)
if err != nil { if err != nil {
return err return err
} }
err = copyZipFile("geoip.dat", xray.GetGeoipPath()) defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download file failed: %s", resp.Status)
}
_, err = io.Copy(file, resp.Body)
return err
}
copyFiles := map[string]string{
"xray": xray.GetBinaryPath(),
"geosite.dat": xray.GetGeositePath(),
"geoip.dat": xray.GetGeoipPath(),
}
downloadFiles := map[string]string{
xray.GetIranPath(): "https://github.com/MasterKia/iran-hosted-domains/releases/latest/download/iran.dat",
}
for fileName, filePath := range copyFiles {
err := copyZipFile(fileName, filePath)
if err != nil { if err != nil {
return err return err
} }
}
for fileName, filePath := range downloadFiles {
err := downloadFile(fileName, filePath)
if err != nil {
return err
}
}
return nil return nil
} }

View File

@ -13,6 +13,7 @@ import (
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
"x-ui/config" "x-ui/config"
"x-ui/logger" "x-ui/logger"
"x-ui/util/common" "x-ui/util/common"
@ -40,6 +41,10 @@ func GetGeoipPath() string {
return config.GetBinFolderPath() + "/geoip.dat" return config.GetBinFolderPath() + "/geoip.dat"
} }
func GetIranPath() string {
return config.GetBinFolderPath() + "/iran.dat"
}
func GetIPLimitLogPath() string { func GetIPLimitLogPath() string {
return config.GetLogFolder() + "/3xipl.log" return config.GetLogFolder() + "/3xipl.log"
} }