merge upstream

Signed-off-by: wozulong <>
This commit is contained in:
wozulong
2024-05-28 11:57:05 +08:00
parent cc020d6a40
commit 74392efbed
6 changed files with 87 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ var StartTime = time.Now().Unix() // unit: second
var Version = "v0.0.0" // this hard coding will be replaced automatically when building, no need to manually change
var SystemName = "New API"
var ServerAddress = "http://localhost:3000"
var OutProxyUrl = ""
var Footer = ""
var Logo = ""
var TopUpLink = ""

View File

@@ -8,7 +8,6 @@ import (
"golang.org/x/image/webp"
"image"
"io"
"net/http"
"strings"
)
@@ -32,7 +31,7 @@ func DecodeBase64ImageData(base64String string) (image.Config, string, string, e
}
func IsImageUrl(url string) (bool, error) {
resp, err := http.Head(url)
resp, err := ProxiedHttpHead(url, OutProxyUrl)
if err != nil {
return false, err
}
@@ -48,7 +47,7 @@ func GetImageFromUrl(url string) (mimeType string, data string, err error) {
if !isImage {
return
}
resp, err := http.Get(url)
resp, err := ProxiedHttpGet(url, OutProxyUrl)
if err != nil {
return
}
@@ -64,7 +63,7 @@ func GetImageFromUrl(url string) (mimeType string, data string, err error) {
}
func DecodeUrlImageData(imageUrl string) (image.Config, string, error) {
response, err := http.Get(imageUrl)
response, err := ProxiedHttpGet(imageUrl, OutProxyUrl)
if err != nil {
SysLog(fmt.Sprintf("fail to get image from url: %s", err.Error()))
return image.Config{}, "", err

View File

@@ -1,13 +1,18 @@
package common
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"golang.org/x/net/proxy"
"html/template"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
@@ -267,3 +272,56 @@ func StrToMap(str string) map[string]interface{} {
}
return m
}
func GetProxiedHttpClient(proxyUrl string) (*http.Client, error) {
if "" == proxyUrl {
return &http.Client{}, nil
}
u, err := url.Parse(proxyUrl)
if err != nil {
return nil, err
}
if strings.HasPrefix(proxyUrl, "http") {
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(u),
},
}, nil
} else if strings.HasPrefix(proxyUrl, "socks") {
dialer, err := proxy.FromURL(u, proxy.Direct)
if err != nil {
return nil, err
}
return &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.(proxy.ContextDialer).DialContext(ctx, network, addr)
},
},
}, nil
}
return nil, errors.New("unsupported proxy type")
}
func ProxiedHttpGet(url, proxyUrl string) (*http.Response, error) {
client, err := GetProxiedHttpClient(proxyUrl)
if err != nil {
return nil, err
}
return client.Get(url)
}
func ProxiedHttpHead(url, proxyUrl string) (*http.Response, error) {
client, err := GetProxiedHttpClient(proxyUrl)
if err != nil {
return nil, err
}
return client.Head(url)
}