Skip to content

Commit da22380

Browse files
committed
add proxy support
1 parent 26e1faf commit da22380

1 file changed

Lines changed: 15 additions & 53 deletions

File tree

node-binance-api.js

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,18 @@ let api = function Binance() {
8585
}
8686

8787
const addProxy = opt => {
88-
let socksproxy = process.env.socks_proxy || false;
89-
if (socksproxy === false) return opt;
90-
socksproxy = proxyReplacewithIp(socksproxy);
91-
92-
if (Binance.options.verbose) Binance.options.log('using socks proxy server ' + socksproxy);
93-
94-
opt.agentClass = SocksProxyAgent;
95-
opt.agentOptions = {
96-
protocol: parseProxy(socksproxy)[0],
97-
host: parseProxy(socksproxy)[1],
98-
port: parseProxy(socksproxy)[2]
88+
let proxy = Binance.options.proxy
89+
? `http://${
90+
Binance.options.proxy.auth
91+
? Binance.options.proxy.auth.username +
92+
':' +
93+
Binance.options.proxy.auth.password +
94+
'@'
95+
: ''
96+
}${Binance.options.proxy.host}:${Binance.options.proxy.port}`
97+
: '';
98+
if (proxy) {
99+
opt.proxy = proxy;
99100
}
100101
return opt;
101102
}
@@ -1234,25 +1235,12 @@ let api = function Binance() {
12341235
* @return {undefined}
12351236
*/
12361237
avgPrice: function (symbol, callback = false) {
1237-
let socksproxy = process.env.socks_proxy || false;
1238-
12391238
let opt = {
12401239
url: base + 'v3/avgPrice?symbol=' + symbol,
12411240
timeout: Binance.options.recvWindow
12421241
};
12431242

1244-
if (socksproxy !== false) {
1245-
socksproxy = proxyReplacewithIp(socksproxy);
1246-
if (Binance.options.verbose) Binance.options.log('using socks proxy server ' + socksproxy);
1247-
opt.agentClass = SocksProxyAgent;
1248-
opt.agentOptions = {
1249-
protocol: parseProxy(socksproxy)[0],
1250-
host: parseProxy(socksproxy)[1],
1251-
port: parseProxy(socksproxy)[2]
1252-
}
1253-
}
1254-
1255-
request(opt, function (error, response, body) {
1243+
request(addProxy(opt), function (error, response, body) {
12561244
if (!callback) return;
12571245

12581246
if (error) return callback(error);
@@ -1273,25 +1261,12 @@ let api = function Binance() {
12731261
const params = typeof symbol === 'string' ? '?symbol=' + symbol : '';
12741262
if (typeof symbol === 'function') callback = symbol; // backwards compatibility
12751263

1276-
let socksproxy = process.env.socks_proxy || false;
1277-
12781264
let opt = {
12791265
url: base + 'v3/ticker/price' + params,
12801266
timeout: Binance.options.recvWindow
12811267
};
12821268

1283-
if (socksproxy !== false) {
1284-
socksproxy = proxyReplacewithIp(socksproxy);
1285-
if (Binance.options.verbose) Binance.options.log('using socks proxy server ' + socksproxy);
1286-
opt.agentClass = SocksProxyAgent;
1287-
opt.agentOptions = {
1288-
protocol: parseProxy(socksproxy)[0],
1289-
host: parseProxy(socksproxy)[1],
1290-
port: parseProxy(socksproxy)[2]
1291-
}
1292-
}
1293-
1294-
request(opt, function (error, response, body) {
1269+
request(addProxy(opt), function (error, response, body) {
12951270
if (!callback) return;
12961271

12971272
if (error) return callback(error);
@@ -1312,25 +1287,12 @@ let api = function Binance() {
13121287
const params = typeof symbol === 'string' ? '?symbol=' + symbol : '';
13131288
if (typeof symbol === 'function') callback = symbol; // backwards compatibility
13141289

1315-
let socksproxy = process.env.socks_proxy || false;
1316-
13171290
let opt = {
13181291
url: base + 'v3/ticker/bookTicker' + params,
13191292
timeout: Binance.options.recvWindow
13201293
};
13211294

1322-
if (socksproxy !== false) {
1323-
socksproxy = proxyReplacewithIp(socksproxy);
1324-
if (Binance.options.verbose) Binance.options.log('using socks proxy server ' + socksproxy);
1325-
opt.agentClass = SocksProxyAgent;
1326-
opt.agentOptions = {
1327-
protocol: parseProxy(socksproxy)[0],
1328-
host: parseProxy(socksproxy)[1],
1329-
port: parseProxy(socksproxy)[2]
1330-
}
1331-
}
1332-
1333-
request(opt, function (error, response, body) {
1295+
request(addProxy(opt), function (error, response, body) {
13341296
if (!callback) return;
13351297

13361298
if (error) return callback(error);

0 commit comments

Comments
 (0)