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" import "XProxy/next/logger"
func Demo() { func Demo() {
raw, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat", "") //raw, t, _ := download("https://github.com/Loyalsoldier/v2ray-rules-dat/releases/download/202309082208/geosite.dat", "")
//raw, _ := 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", "")
//raw, _ := download("https://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")
raw, t, _ := download("https://cdn.dnomd343.top/v2ray-rules-dat/geosite.dat.xz", "")
logger.Infof("%v", t)
ret, _ := tryExtract(raw) ret, _ := tryExtract(raw)
logger.Debugf("content size -> %d", len(ret)) logger.Debugf("content size -> %d", len(ret))
} }

28
next/assets/remote.go

@ -10,6 +10,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"time"
) )
// broltiDecode handles brolti encoding in http responses. // broltiDecode handles brolti encoding in http responses.
@ -81,23 +82,36 @@ func createClient(remoteUrl string, proxyUrl string) (http.Client, error) {
}, nil }, nil
} }
// download obtains resource file from the remote server and supports proxy. // assetDate attempts to obtain the last modification time of the remote
func download(url string, proxy string) ([]byte, error) { // 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) client, err := createClient(url, proxy)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
logger.Errorf("Failed to create http request -> %v", err) logger.Errorf("Failed to create http request -> %v", err)
return nil, err return nil, nil, err
} }
req.Header.Set(headers.AcceptEncoding, "gzip, deflate, br") req.Header.Set(headers.AcceptEncoding, "gzip, deflate, br")
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
logger.Errorf("Failed to execute http request -> %v", err) logger.Errorf("Failed to execute http request -> %v", err)
return nil, err return nil, nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
logger.Debugf("Remote data downloaded successfully") logger.Debugf("Remote data downloaded successfully")
@ -117,8 +131,8 @@ func download(url string, proxy string) ([]byte, error) {
content, err = nonDecode(resp.Body) content, err = nonDecode(resp.Body)
} }
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
logger.Debugf("Download `%s` successfully -> %d bytes", url, len(content)) logger.Debugf("Download `%s` successfully -> %d bytes", url, len(content))
return content, nil return content, assetDate(resp), nil
} }

Loading…
Cancel
Save