Browse Source

feat: download asset via proxy

v1.x.x
dnomd343 2 years ago
parent
commit
b20e89cd72
  1. 11
      cmd/asset/update.go
  2. 33
      cmd/common/file.go
  3. 8
      cmd/config/decode.go
  4. 1
      cmd/xproxy.go

11
cmd/asset/update.go

@ -8,11 +8,12 @@ import (
) )
type Config struct { type Config struct {
Cron string `yaml:"cron" json:"cron"` Proxy string `yaml:"proxy" json:"proxy"`
Url map[string]string `yaml:"url" json:"url"` Cron string `yaml:"cron" json:"cron"`
Url map[string]string `yaml:"url" json:"url"`
} }
func updateAsset(urls map[string]string, assetDir string) { // download new assets func updateAsset(urls map[string]string, assetDir string, updateProxy string) { // download new assets
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Errorf("Update failed -> %v", err) log.Errorf("Update failed -> %v", err)
@ -21,7 +22,7 @@ func updateAsset(urls map[string]string, assetDir string) { // download new asse
if len(urls) != 0 { if len(urls) != 0 {
log.Info("Start update assets") log.Info("Start update assets")
for file, url := range urls { for file, url := range urls {
common.DownloadFile(url, path.Join(assetDir, file)) // maybe override old asset common.DownloadFile(url, path.Join(assetDir, file), updateProxy) // maybe override old asset
} }
} }
} }
@ -30,7 +31,7 @@ func AutoUpdate(update *Config, assetDir string) { // set cron task for auto upd
if update.Cron != "" { if update.Cron != "" {
autoUpdate := cron.New() autoUpdate := cron.New()
_ = autoUpdate.AddFunc(update.Cron, func() { // cron function _ = autoUpdate.AddFunc(update.Cron, func() { // cron function
updateAsset(update.Url, assetDir) updateAsset(update.Url, assetDir, update.Proxy)
}) })
autoUpdate.Start() autoUpdate.Start()
} }

33
cmd/common/file.go

@ -5,6 +5,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"os" "os"
"strings" "strings"
) )
@ -69,21 +70,35 @@ func CopyFile(source string, target string) {
} }
} }
func DownloadFile(url string, file string) { func DownloadFile(fileUrl string, filePath string, proxyUrl string) {
log.Debugf("File download `%s` => `%s`", url, file) log.Debugf("File download `%s` => `%s`", fileUrl, filePath)
resp, err := http.Get(url) client := http.Client{}
defer resp.Body.Close() if proxyUrl != "" { // use proxy for download
log.Infof("File download via proxy -> %s", proxyUrl)
rawUrl, _ := url.Parse(proxyUrl)
client = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(rawUrl),
},
}
}
resp, err := client.Get(fileUrl)
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil { if err != nil {
log.Errorf("Download `%s` error -> %v", url, err) log.Errorf("Download `%s` error -> %v", fileUrl, err)
return return
} }
output, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) output, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
defer output.Close() defer output.Close()
if err != nil { if err != nil {
log.Panicf("Open `%s` error -> %v", file, err) log.Panicf("Open `%s` error -> %v", filePath, err)
} }
if _, err = io.Copy(output, resp.Body); err != nil { if _, err = io.Copy(output, resp.Body); err != nil {
log.Panicf("File `%s` save error -> %v", file, err) log.Panicf("File `%s` save error -> %v", filePath, err)
} }
log.Infof("Download success `%s` => `%s`", url, file) log.Infof("Download success `%s` => `%s`", fileUrl, filePath)
} }

8
cmd/config/decode.go

@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"net/url"
) )
type NetConfig struct { type NetConfig struct {
@ -146,6 +147,13 @@ func decodeRadvd(rawConfig *RawConfig, config *Config) {
func decodeUpdate(rawConfig *RawConfig, config *Config) { func decodeUpdate(rawConfig *RawConfig, config *Config) {
config.Update = rawConfig.Update config.Update = rawConfig.Update
if config.Update.Proxy != "" {
_, err := url.Parse(config.Update.Proxy) // check proxy info
if err != nil {
log.Panicf("Invalid update proxy -> %s", config.Update.Proxy)
}
}
log.Debugf("Update proxy -> %s", config.Update.Proxy)
log.Debugf("Update cron -> %s", config.Update.Cron) log.Debugf("Update cron -> %s", config.Update.Cron)
log.Debugf("Update urls -> %v", config.Update.Url) log.Debugf("Update urls -> %v", config.Update.Url)
} }

1
cmd/xproxy.go

@ -90,7 +90,6 @@ func main() {
config.Load(configFile, &settings) config.Load(configFile, &settings)
loadNetwork(&settings) loadNetwork(&settings)
loadProxy(&settings) loadProxy(&settings)
// TODO: update assets via proxy
loadAsset(&settings) loadAsset(&settings)
loadRadvd(&settings) loadRadvd(&settings)

Loading…
Cancel
Save