Browse Source

feat: support update remote assets

feature/assets
Dnomd343 1 year ago
parent
commit
b33d539850
  1. 46
      next/assets/assets.go
  2. 5
      next/assets/remote.go

46
next/assets/assets.go

@ -1,13 +1,49 @@
package assets
import "XProxy/next/logger"
import (
"XProxy/next/logger"
"os"
)
func updateRemoteAsset(file string, url string, proxy string) error {
logger.Debugf("Start downloading remote asset `%s` to `%s`", url, file)
asset, date, err := download(url, proxy)
if err != nil {
logger.Errorf("Failed to download remote asset `%s`", url)
return err
}
asset, err = tryExtract(asset)
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(asset)
if err != nil {
logger.Errorf("Failed to save file `%s` -> %v", file, err)
return err
}
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)
}
logger.Infof("Successfully obtained remote asset `%s`", file)
return nil
}
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", "")
//raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat", "socks5://192.168.2.2:1084")
raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "")
logger.Infof("%v", t)
ret, _ := tryExtract(raw)
logger.Debugf("content size -> %d", len(ret))
//raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-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", "")
}

5
next/assets/remote.go

@ -3,6 +3,7 @@ package assets
import (
"XProxy/next/logger"
"bytes"
"errors"
"github.com/andybalholm/brotli"
"github.com/go-http-utils/headers"
"github.com/klauspost/compress/flate"
@ -114,6 +115,10 @@ func download(url string, proxy string) ([]byte, *time.Time, error) {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
logger.Errorf("Download `%s` with http status code -> %d", url, resp.StatusCode)
return nil, nil, errors.New("http status code non-2xx")
}
logger.Debugf("Remote data downloaded successfully")
var content []byte

Loading…
Cancel
Save