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 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() { func Demo() {
//raw, t, _ := download("https://github.com/Loyalsoldier/v2ray-rules-dat/releases/download/202309082208/geosite.dat", "") //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", "")
//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", "socks5://192.168.2.2:1084")
raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "") //raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "")
logger.Infof("%v", t) //logger.Infof("%v", t)
ret, _ := tryExtract(raw) //ret, _ := tryExtract(raw)
logger.Debugf("content size -> %d", len(ret)) //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 ( import (
"XProxy/next/logger" "XProxy/next/logger"
"bytes" "bytes"
"errors"
"github.com/andybalholm/brotli" "github.com/andybalholm/brotli"
"github.com/go-http-utils/headers" "github.com/go-http-utils/headers"
"github.com/klauspost/compress/flate" "github.com/klauspost/compress/flate"
@ -114,6 +115,10 @@ func download(url string, proxy string) ([]byte, *time.Time, error) {
return nil, nil, err return nil, nil, err
} }
defer resp.Body.Close() 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") logger.Debugf("Remote data downloaded successfully")
var content []byte var content []byte

Loading…
Cancel
Save