[add] 新增币安的代理

This commit is contained in:
lychiyu 2024-08-30 23:10:58 +08:00
parent 8f98cf650e
commit ae47d3d3b7
3 changed files with 23 additions and 6 deletions

View File

@ -42,6 +42,7 @@ type ExchangeSession struct {
Key string `json:"key,omitempty" yaml:"key,omitempty"`
Secret string `json:"secret,omitempty" yaml:"secret,omitempty"`
Passphrase string `json:"passphrase,omitempty" yaml:"passphrase,omitempty"`
Proxy string `json:"proxy,omitempty" yaml:"proxy,omitempty"`
SubAccount string `json:"subAccount,omitempty" yaml:"subAccount,omitempty"`
// Withdrawal is used for enabling withdrawal functions
@ -793,7 +794,7 @@ func (session *ExchangeSession) newBasicPrivateExchange(exchangeName types.Excha
var err error
var exMinimal types.ExchangeMinimal
if session.Key != "" && session.Secret != "" {
exMinimal, err = exchange2.New(exchangeName, session.Key, session.Secret, session.Passphrase)
exMinimal, err = exchange2.New(exchangeName, session.Key, session.Secret, session.Passphrase, session.Proxy)
} else {
exMinimal, err = exchange2.NewWithEnvVarPrefix(exchangeName, session.EnvVarPrefix)
}

View File

@ -3,6 +3,8 @@ package binance
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
@ -90,7 +92,7 @@ type Exchange struct {
var timeSetterOnce sync.Once
func New(key, secret string) *Exchange {
func New(key, secret, proxy string) *Exchange {
if util.IsPaperTrade() {
binance.UseTestnet = true
}
@ -109,6 +111,19 @@ func New(key, secret string) *Exchange {
client2 := binanceapi.NewClient(client.BaseURL)
futuresClient2 := binanceapi.NewFuturesRestClient(futuresClient.BaseURL)
if proxy != "" {
proxyURL, err := url.Parse(proxy)
if err == nil {
proxyTransport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client.HTTPClient.Transport = proxyTransport
futuresClient.HTTPClient.Transport = proxyTransport
client2.HttpClient.Transport = proxyTransport
futuresClient2.HttpClient.Transport = proxyTransport
}
}
ex := &Exchange{
key: key,
secret: secret,

View File

@ -15,7 +15,7 @@ import (
)
func NewPublic(exchangeName types.ExchangeName) (types.Exchange, error) {
exMinimal, err := New(exchangeName, "", "", "")
exMinimal, err := New(exchangeName, "", "", "", "")
if err != nil {
return nil, err
}
@ -27,11 +27,11 @@ func NewPublic(exchangeName types.ExchangeName) (types.Exchange, error) {
return nil, fmt.Errorf("exchange %T does not implement types.Exchange", exMinimal)
}
func New(n types.ExchangeName, key, secret, passphrase string) (types.ExchangeMinimal, error) {
func New(n types.ExchangeName, key, secret, passphrase, proxy string) (types.ExchangeMinimal, error) {
switch n {
case types.ExchangeBinance:
return binance.New(key, secret), nil
return binance.New(key, secret, proxy), nil
case types.ExchangeMax:
return max.New(key, secret), nil
@ -70,5 +70,6 @@ func NewWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.Exchange
}
passphrase := os.Getenv(varPrefix + "_API_PASSPHRASE")
return New(n, key, secret, passphrase)
proxy := os.Getenv(varPrefix + "_PROXY")
return New(n, key, secret, passphrase, proxy)
}