Browse Source

feat: local assets extract

feature/assets
Dnomd343 1 year ago
parent
commit
631a6ac573
  1. 83
      next/assets/assets.go
  2. 1
      next/assets/remote.go

83
next/assets/assets.go

@ -3,8 +3,35 @@ package assets
import ( import (
"XProxy/next/logger" "XProxy/next/logger"
"os" "os"
"time"
) )
// saveAsset is used to write to a local file and specify its last modify
// time. If the file exists, it will be replaced.
func saveAsset(file string, content []byte, date *time.Time) error {
fp, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
logger.Errorf("Failed to open file `%s` -> %v", file, err)
return err
}
defer fp.Close()
_, err = fp.Write(content)
if err != nil {
logger.Errorf("Failed to save file `%s` -> %v", file, err)
return err
}
if date != nil { // change local file last modify time
if err := os.Chtimes(file, *date, *date); err != nil {
logger.Warnf("Failed to change asset modification time")
} else {
logger.Debugf("Change `%s` modification time to `%v`", file, date)
}
}
return nil
}
// updateRemoteAsset will download remote asset via the optional proxy and // updateRemoteAsset will download remote asset via the optional proxy and
// save them locally. Local files will be overwritten if they exist. // save them locally. Local files will be overwritten if they exist.
func updateRemoteAsset(file string, url string, proxy string) error { func updateRemoteAsset(file string, url string, proxy string) error {
@ -14,38 +41,50 @@ func updateRemoteAsset(file string, url string, proxy string) error {
logger.Errorf("Failed to download remote asset `%s`", url) logger.Errorf("Failed to download remote asset `%s`", url)
return err return err
} }
asset, err = tryExtract(asset) if asset, err = tryExtract(asset); err != nil {
return err
}
if err := saveAsset(file, asset, date); err != nil {
return err
}
logger.Infof("Successfully obtained remote asset `%s`", file)
return nil
}
fp, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) // updateLocalAsset will extract local asset and save them locally. If the
// local file already exists, it will be skipped.
func updateLocalAsset(file string, src string) error {
_, err := os.Stat(file)
if err == nil {
logger.Debugf("Local asset `%s` already exist", file)
return nil // skip local asset extract
}
var date = time.Now()
if stat, err := os.Stat(src); err == nil {
date = stat.ModTime() // using last modify time of src file
}
logger.Debugf("Start extracting local asset `%s`", file)
asset, err := os.ReadFile(src)
if err != nil { if err != nil {
logger.Errorf("Failed to open file `%s` -> %v", file, err) logger.Errorf("Failed to read local asset -> %v", err)
return err return err
} }
defer fp.Close() if asset, err = tryExtract(asset); err != nil {
_, err = fp.Write(asset)
if err != nil {
logger.Errorf("Failed to save file `%s` -> %v", file, err)
return err return err
} }
if err := saveAsset(file, asset, &date); err != nil {
if err := os.Chtimes(file, *date, *date); err != nil { return err
logger.Warnf("Failed to change asset modification time")
} else {
logger.Debugf("Change `%s` modification time to `%v`", file, date)
} }
logger.Infof("Successfully obtained remote asset `%s`", file) logger.Infof("Successfully extracted local asset `%s`", file)
return nil return nil
} }
func Demo() { func Demo() {
//raw, t, _ := download("https://github.com/Loyalsoldier/v2ray-rules-dat/releases/download/202309082208/geosite.dat", "")
//raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat", "") //updateRemoteAsset("geosite.dat", "http://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "")
//raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat", "socks5://192.168.2.2:1084") //updateRemoteAsset("geosite.dat", "http://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "socks5://192.168.2.2:1084")
//raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "") updateLocalAsset("geosite.dat", "geosite.dat.xz")
//logger.Infof("%v", t)
//ret, _ := tryExtract(raw)
//logger.Debugf("content size -> %d", len(ret))
updateRemoteAsset("geosite.dat", "http://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "")
} }

1
next/assets/remote.go

@ -90,6 +90,7 @@ func createClient(remoteUrl string, proxyUrl string) (http.Client, error) {
}, nil }, nil
} }
// doRequest initiates http request and allows retries.
func doRequest(client http.Client, req *http.Request) (*http.Response, error) { func doRequest(client http.Client, req *http.Request) (*http.Response, error) {
maxRetry := retry.Attempts(DownloadRetry) maxRetry := retry.Attempts(DownloadRetry)
maxDelay := retry.MaxDelay(DownloadTimeout * time.Second) maxDelay := retry.MaxDelay(DownloadTimeout * time.Second)

Loading…
Cancel
Save