|
|
@ -1,7 +1,7 @@ |
|
|
|
package assets |
|
|
|
|
|
|
|
import ( |
|
|
|
. "XProxy/next/logger" |
|
|
|
"XProxy/next/logger" |
|
|
|
"bytes" |
|
|
|
"github.com/andybalholm/brotli" |
|
|
|
"github.com/go-http-utils/headers" |
|
|
@ -17,7 +17,7 @@ func broltiDecode(stream io.Reader) ([]byte, error) { |
|
|
|
var buffer bytes.Buffer |
|
|
|
_, err := io.Copy(&buffer, brotli.NewReader(stream)) |
|
|
|
if err != nil { |
|
|
|
Logger.Errorf("Failed to decode http responses with brolti encoding -> %v", err) |
|
|
|
logger.Errorf("Failed to decode http responses with brolti encoding -> %v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return buffer.Bytes(), nil |
|
|
@ -27,14 +27,14 @@ func broltiDecode(stream io.Reader) ([]byte, error) { |
|
|
|
func gzipDecode(stream io.Reader) ([]byte, error) { |
|
|
|
reader, err := gzip.NewReader(stream) |
|
|
|
if err != nil { |
|
|
|
Logger.Errorf("Failed to decode http responses with gzip encoding -> %v", err) |
|
|
|
logger.Errorf("Failed to decode http responses with gzip encoding -> %v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
var buffer bytes.Buffer |
|
|
|
_, err = io.Copy(&buffer, reader) |
|
|
|
if err != nil { |
|
|
|
Logger.Errorf("Failed to handle gzip reader -> %v", err) |
|
|
|
logger.Errorf("Failed to handle gzip reader -> %v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return buffer.Bytes(), nil |
|
|
@ -45,7 +45,7 @@ func deflateDecode(stream io.Reader) ([]byte, error) { |
|
|
|
var buffer bytes.Buffer |
|
|
|
_, err := io.Copy(&buffer, flate.NewReader(stream)) |
|
|
|
if err != nil { |
|
|
|
Logger.Errorf("Failed to decode http responses with deflate encoding -> %v", err) |
|
|
|
logger.Errorf("Failed to decode http responses with deflate encoding -> %v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return buffer.Bytes(), nil |
|
|
@ -56,7 +56,7 @@ func nonDecode(stream io.Reader) ([]byte, error) { |
|
|
|
var buffer bytes.Buffer |
|
|
|
_, err := io.Copy(&buffer, stream) |
|
|
|
if err != nil { |
|
|
|
Logger.Errorf("Failed to read http responses -> %v", err) |
|
|
|
logger.Errorf("Failed to read http responses -> %v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return buffer.Bytes(), nil |
|
|
@ -65,13 +65,13 @@ func nonDecode(stream io.Reader) ([]byte, error) { |
|
|
|
// createClient build http client based on http or socks proxy url.
|
|
|
|
func createClient(remoteUrl string, proxyUrl string) (http.Client, error) { |
|
|
|
if proxyUrl == "" { |
|
|
|
Logger.Infof("Downloading `%s` without proxy", remoteUrl) |
|
|
|
logger.Infof("Downloading `%s` without proxy", remoteUrl) |
|
|
|
return http.Client{}, nil |
|
|
|
} |
|
|
|
Logger.Infof("Downloading `%s` via `%s`", remoteUrl, proxyUrl) |
|
|
|
logger.Infof("Downloading `%s` via `%s`", remoteUrl, proxyUrl) |
|
|
|
proxy, err := url.Parse(proxyUrl) |
|
|
|
if err != nil { |
|
|
|
Logger.Errorf("Invalid proxy url `%s` -> %v", proxyUrl, err) |
|
|
|
logger.Errorf("Invalid proxy url `%s` -> %v", proxyUrl, err) |
|
|
|
return http.Client{}, err |
|
|
|
} |
|
|
|
return http.Client{ |
|
|
@ -81,8 +81,8 @@ 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) { |
|
|
|
// download obtains resource file from the remote server and supports proxy.
|
|
|
|
func download(url string, proxy string) ([]byte, error) { |
|
|
|
client, err := createClient(url, proxy) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
@ -90,28 +90,28 @@ func Download(url string, proxy string) ([]byte, error) { |
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, 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 |
|
|
|
} |
|
|
|
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) |
|
|
|
logger.Errorf("Failed to execute http request -> %v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
defer resp.Body.Close() |
|
|
|
Logger.Debugf("Remote data downloaded successfully") |
|
|
|
logger.Debugf("Remote data downloaded successfully") |
|
|
|
|
|
|
|
var content []byte |
|
|
|
switch resp.Header.Get(headers.ContentEncoding) { |
|
|
|
case "br": |
|
|
|
Logger.Debugf("Downloaded content using brolti encoding") |
|
|
|
logger.Debugf("Downloaded content using brolti encoding") |
|
|
|
content, err = broltiDecode(resp.Body) |
|
|
|
case "gzip": |
|
|
|
Logger.Debugf("Downloaded content using gzip encoding") |
|
|
|
logger.Debugf("Downloaded content using gzip encoding") |
|
|
|
content, err = gzipDecode(resp.Body) |
|
|
|
case "deflate": |
|
|
|
Logger.Debugf("Downloaded content using deflate encoding") |
|
|
|
logger.Debugf("Downloaded content using deflate encoding") |
|
|
|
content, err = deflateDecode(resp.Body) |
|
|
|
default: |
|
|
|
content, err = nonDecode(resp.Body) |
|
|
@ -119,6 +119,6 @@ func Download(url string, proxy string) ([]byte, error) { |
|
|
|
if err != nil { |
|
|
|
return 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 |
|
|
|
} |
|
|
|