@@ -45,6 +45,10 @@ export default class Binance {
4545 stream = 'wss://stream.binance.com:9443/ws/' ;
4646 combineStream = 'wss://stream.binance.com:9443/stream?streams=' ;
4747
48+ // proxy variables
49+ httpsProxy : string = undefined ;
50+ socksProxy : string = undefined ;
51+
4852 APIKEY : string = undefined ;
4953 APISECRET : string = undefined ;
5054 test = false ;
@@ -159,11 +163,8 @@ export default class Binance {
159163
160164 this . assignOptions ( opt , callback ) ;
161165 if ( this . Options . useServerTime ) {
162-
163166 const res = await this . publicRequest ( this . getSpotUrl ( ) + 'v3/time' ) ;
164- console . log ( res )
165167 this . info . timeOffset = res . serverTime - new Date ( ) . getTime ( ) ;
166-
167168 }
168169 return this ;
169170 }
@@ -193,6 +194,26 @@ export default class Binance {
193194 return a ? ( a ^ Math . random ( ) * 16 >> a / 4 ) . toString ( 16 ) : ( ( [ 1e7 ] as any ) + 1e3 + 4e3 + 8e5 ) . replace ( / [ 0 1 8 ] / g, this . uuid22 ) ;
194195 } ;
195196
197+ getHttpsProxy ( ) {
198+ if ( this . httpsProxy ) {
199+ return this . httpsProxy
200+ }
201+ if ( process . env . https_proxy ) {
202+ return process . env . https_proxy
203+ }
204+ return undefined
205+ }
206+
207+ getSocksProxy ( ) {
208+ if ( this . socksProxy ) {
209+ return this . socksProxy
210+ }
211+ if ( process . env . socks_proxy ) {
212+ return process . env . socks_proxy
213+ }
214+ return undefined
215+ }
216+
196217 // ------ Request Related Functions ------ //
197218
198219 /**
@@ -975,11 +996,11 @@ export default class Binance {
975996 * @return {WebSocket } - websocket reference
976997 */
977998 subscribe ( endpoint : string , callback : Callback , reconnect ?: Callback , opened_callback ?: Callback ) {
978- let httpsproxy = process . env . https_proxy || false ;
979- let socksproxy = process . env . socks_proxy || false ;
999+ let httpsproxy = this . getHttpsProxy ( ) ;
1000+ let socksproxy = this . getSocksProxy ( ) ;
9801001 let ws : any = undefined ;
9811002
982- if ( socksproxy !== false ) {
1003+ if ( socksproxy ) {
9831004 socksproxy = this . proxyReplacewithIp ( socksproxy ) ;
9841005 if ( this . Options . verbose ) this . Options . log ( 'using socks proxy server ' + socksproxy ) ;
9851006 let agent = new SocksProxyAgent ( {
@@ -988,7 +1009,7 @@ export default class Binance {
9881009 port : this . parseProxy ( socksproxy ) [ 2 ]
9891010 } ) ;
9901011 ws = new WebSocket ( this . stream + endpoint , { agent : agent } ) ;
991- } else if ( httpsproxy !== false ) {
1012+ } else if ( httpsproxy ) {
9921013 let config = url . parse ( httpsproxy ) ;
9931014 let agent = new HttpsProxyAgent ( config ) ;
9941015 if ( this . Options . verbose ) this . Options . log ( 'using proxy server ' + agent ) ;
@@ -1024,11 +1045,11 @@ export default class Binance {
10241045 * @return {WebSocket } - websocket reference
10251046 */
10261047 subscribeCombined ( streams : any , callback : Callback , reconnect ?: Callback , opened_callback ?: Callback ) {
1027- let httpsproxy = process . env . https_proxy || false ;
1028- let socksproxy = process . env . socks_proxy || false ;
1048+ let httpsproxy = this . getHttpsProxy ( ) ;
1049+ let socksproxy = this . getSocksProxy ( ) ;
10291050 const queryParams = streams . join ( '/' ) ;
10301051 let ws : any = undefined ;
1031- if ( socksproxy !== false ) {
1052+ if ( socksproxy ) {
10321053 socksproxy = this . proxyReplacewithIp ( socksproxy ) ;
10331054 if ( this . Options . verbose ) this . Options . log ( 'using socks proxy server ' + socksproxy ) ;
10341055 let agent = new SocksProxyAgent ( {
@@ -1037,7 +1058,7 @@ export default class Binance {
10371058 port : this . parseProxy ( socksproxy ) [ 2 ]
10381059 } ) ;
10391060 ws = new WebSocket ( this . combineStream + queryParams , { agent : agent } ) ;
1040- } else if ( httpsproxy !== false ) {
1061+ } else if ( httpsproxy ) {
10411062 if ( this . Options . verbose ) this . Options . log ( 'using proxy server ' + httpsproxy ) ;
10421063 let config = url . parse ( httpsproxy ) ;
10431064 let agent = new HttpsProxyAgent ( config ) ;
@@ -1172,11 +1193,11 @@ export default class Binance {
11721193 if ( ! params . reconnect ) params . reconnect = false ;
11731194 if ( ! params . openCallback ) params . openCallback = false ;
11741195 if ( ! params . id ) params . id = false ;
1175- let httpsproxy = process . env . https_proxy || false ;
1176- let socksproxy = process . env . socks_proxy || false ;
1196+ let httpsproxy = this . getHttpsProxy ( ) ;
1197+ let socksproxy = this . getSocksProxy ( ) ;
11771198 let ws : any = undefined ;
11781199
1179- if ( socksproxy !== false ) {
1200+ if ( socksproxy ) {
11801201 socksproxy = this . proxyReplacewithIp ( socksproxy ) ;
11811202 if ( this . Options . verbose ) this . Options . log ( `futuresSubscribeSingle: using socks proxy server: ${ socksproxy } ` ) ;
11821203 let agent = new SocksProxyAgent ( {
@@ -1185,7 +1206,7 @@ export default class Binance {
11851206 port : this . parseProxy ( socksproxy ) [ 2 ]
11861207 } ) ;
11871208 ws = new WebSocket ( ( this . Options . test ? this . fstreamSingleTest : this . fstreamSingle ) + endpoint , { agent } ) ;
1188- } else if ( httpsproxy !== false ) {
1209+ } else if ( httpsproxy ) {
11891210 let config = url . parse ( httpsproxy ) ;
11901211 let agent = new HttpsProxyAgent ( config ) ;
11911212 if ( this . Options . verbose ) this . Options . log ( `futuresSubscribeSingle: using proxy server: ${ agent } ` ) ;
@@ -1198,10 +1219,10 @@ export default class Binance {
11981219 ws . reconnect = this . Options . reconnect ;
11991220 ws . endpoint = endpoint ;
12001221 ws . isAlive = false ;
1201- ws . on ( 'open' , this . handleFuturesSocketOpen . bind ( ws , params . openCallback ) ) ;
1222+ ws . on ( 'open' , this . handleFuturesSocketOpen . bind ( this , params . openCallback ) ) ;
12021223 ws . on ( 'pong' , this . handleFuturesSocketHeartbeat ) ;
12031224 ws . on ( 'error' , this . handleFuturesSocketError ) ;
1204- ws . on ( 'close' , this . handleFuturesSocketClose . bind ( ws , params . reconnect ) ) ;
1225+ ws . on ( 'close' , this . handleFuturesSocketClose . bind ( this , params . reconnect ) ) ;
12051226 ws . on ( 'message' , data => {
12061227 try {
12071228 callback ( JSONbig . parse ( data ) ) ;
@@ -1225,11 +1246,11 @@ export default class Binance {
12251246 if ( ! params . reconnect ) params . reconnect = false ;
12261247 if ( ! params . openCallback ) params . openCallback = false ;
12271248 if ( ! params . id ) params . id = false ;
1228- let httpsproxy = process . env . https_proxy || false ;
1229- let socksproxy = process . env . socks_proxy || false ;
1249+ let httpsproxy = this . getHttpsProxy ( ) ;
1250+ let socksproxy = this . getSocksProxy ( ) ;
12301251 const queryParams = streams . join ( '/' ) ;
12311252 let ws : any = undefined ;
1232- if ( socksproxy !== false ) {
1253+ if ( socksproxy ) {
12331254 socksproxy = this . proxyReplacewithIp ( socksproxy ) ;
12341255 if ( this . Options . verbose ) this . Options . log ( `futuresSubscribe: using socks proxy server ${ socksproxy } ` ) ;
12351256 let agent = new SocksProxyAgent ( {
@@ -1238,7 +1259,7 @@ export default class Binance {
12381259 port : this . parseProxy ( socksproxy ) [ 2 ]
12391260 } ) ;
12401261 ws = new WebSocket ( ( this . Options . test ? this . fstreamTest : this . fstream ) + queryParams , { agent } ) ;
1241- } else if ( httpsproxy !== false ) {
1262+ } else if ( httpsproxy ) {
12421263 if ( this . Options . verbose ) this . Options . log ( `futuresSubscribe: using proxy server ${ httpsproxy } ` ) ;
12431264 let config = url . parse ( httpsproxy ) ;
12441265 let agent = new HttpsProxyAgent ( config ) ;
@@ -1253,10 +1274,10 @@ export default class Binance {
12531274 if ( this . Options . verbose ) {
12541275 this . Options . log ( `futuresSubscribe: Subscribed to [${ ws . endpoint } ] ${ queryParams } ` ) ;
12551276 }
1256- ws . on ( 'open' , this . handleFuturesSocketOpen . bind ( ws , params . openCallback ) ) ;
1277+ ws . on ( 'open' , this . handleFuturesSocketOpen . bind ( this , params . openCallback ) ) ;
12571278 ws . on ( 'pong' , this . handleFuturesSocketHeartbeat ) ;
12581279 ws . on ( 'error' , this . handleFuturesSocketError ) ;
1259- ws . on ( 'close' , this . handleFuturesSocketClose . bind ( ws , params . reconnect ) ) ;
1280+ ws . on ( 'close' , this . handleFuturesSocketClose . bind ( this , params . reconnect ) ) ;
12601281 ws . on ( 'message' , data => {
12611282 try {
12621283 callback ( JSON . parse ( data ) . data ) ;
@@ -1878,10 +1899,10 @@ export default class Binance {
18781899 if ( ! params . reconnect ) params . reconnect = false ;
18791900 if ( ! params . openCallback ) params . openCallback = false ;
18801901 if ( ! params . id ) params . id = false ;
1881- let httpsproxy = process . env . https_proxy || false ;
1882- let socksproxy = process . env . socks_proxy || false ;
1902+ let httpsproxy = this . getHttpsProxy ( ) ;
1903+ let socksproxy = this . getSocksProxy ( ) ;
18831904 let ws : any = undefined ;
1884- if ( socksproxy !== false ) {
1905+ if ( socksproxy ) {
18851906 socksproxy = this . proxyReplacewithIp ( socksproxy ) ;
18861907 if ( this . Options . verbose ) this . Options . log ( `deliverySubscribeSingle: using socks proxy server: ${ socksproxy } ` ) ;
18871908 let agent = new SocksProxyAgent ( {
@@ -1890,7 +1911,7 @@ export default class Binance {
18901911 port : this . parseProxy ( socksproxy ) [ 2 ]
18911912 } ) ;
18921913 ws = new WebSocket ( ( this . Options . test ? this . dstreamSingleTest : this . dstreamSingle ) + endpoint , { agent } ) ;
1893- } else if ( httpsproxy !== false ) {
1914+ } else if ( httpsproxy ) {
18941915 let config = url . parse ( httpsproxy ) ;
18951916 let agent = new HttpsProxyAgent ( config ) ;
18961917 if ( this . Options . verbose ) this . Options . log ( `deliverySubscribeSingle: using proxy server: ${ agent } ` ) ;
@@ -1930,11 +1951,11 @@ export default class Binance {
19301951 if ( ! params . reconnect ) params . reconnect = false ;
19311952 if ( ! params . openCallback ) params . openCallback = false ;
19321953 if ( ! params . id ) params . id = false ;
1933- let httpsproxy = process . env . https_proxy || false ;
1934- let socksproxy = process . env . socks_proxy || false ;
1954+ let httpsproxy = this . getHttpsProxy ( ) ;
1955+ let socksproxy = this . getSocksProxy ( ) ;
19351956 const queryParams = streams . join ( '/' ) ;
19361957 let ws : any = undefined ;
1937- if ( socksproxy !== false ) {
1958+ if ( socksproxy ) {
19381959 socksproxy = this . proxyReplacewithIp ( socksproxy ) ;
19391960 if ( this . Options . verbose ) this . Options . log ( `deliverySubscribe: using socks proxy server ${ socksproxy } ` ) ;
19401961 let agent = new SocksProxyAgent ( {
@@ -1943,7 +1964,7 @@ export default class Binance {
19431964 port : this . parseProxy ( socksproxy ) [ 2 ]
19441965 } ) ;
19451966 ws = new WebSocket ( ( this . Options . test ? this . dstreamTest : this . dstream ) + queryParams , { agent } ) ;
1946- } else if ( httpsproxy !== false ) {
1967+ } else if ( httpsproxy ) {
19471968 if ( this . Options . verbose ) this . Options . log ( `deliverySubscribe: using proxy server ${ httpsproxy } ` ) ;
19481969 let config = url . parse ( httpsproxy ) ;
19491970 let agent = new HttpsProxyAgent ( config ) ;
0 commit comments