Browse Source

feat: remote assets modification time

feature/assets
Dnomd343 1 year ago
parent
commit
8c896624a8
  1. 8
      next/assets/assets.go
  2. 28
      next/assets/remote.go

8
next/assets/assets.go

@ -3,9 +3,11 @@ package assets
import "XProxy/next/logger"
func Demo() {
raw, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat", "")
//raw, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat", "socks5://192.168.2.2:1084")
//raw, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "")
//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))
}

28
next/assets/remote.go

@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"time"
)
// broltiDecode handles brolti encoding in http responses.
@ -81,23 +82,36 @@ func createClient(remoteUrl string, proxyUrl string) (http.Client, error) {
}, nil
}
// download obtains resource file from the remote server and supports proxy.
func download(url string, proxy string) ([]byte, error) {
// assetDate attempts to obtain the last modification time of the remote
// file and returns nil if it does not exist or is invalid.
func assetDate(resp *http.Response) *time.Time {
date, err := http.ParseTime(resp.Header.Get(headers.LastModified))
if err != nil {
logger.Warnf("Unable to get remote data modification time")
return nil
}
logger.Debugf("Remote data modification time -> `%v`", date)
return &date
}
// download obtains resource file from the remote server, gets its
// modification time, and supports proxy acquisition.
func download(url string, proxy string) ([]byte, *time.Time, error) {
client, err := createClient(url, proxy)
if err != nil {
return nil, err
return nil, nil, err
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logger.Errorf("Failed to create http request -> %v", err)
return nil, err
return nil, nil, err
}
req.Header.Set(headers.AcceptEncoding, "gzip, deflate, br")
resp, err := client.Do(req)
if err != nil {
logger.Errorf("Failed to execute http request -> %v", err)
return nil, err
return nil, nil, err
}
defer resp.Body.Close()
logger.Debugf("Remote data downloaded successfully")
@ -117,8 +131,8 @@ func download(url string, proxy string) ([]byte, error) {
content, err = nonDecode(resp.Body)
}
if err != nil {
return nil, err
return nil, nil, err
}
logger.Debugf("Download `%s` successfully -> %d bytes", url, len(content))
return content, nil
return content, assetDate(resp), nil
}

Loading…
Cancel
Save