Query conversion rates service over clearnet or Tor with mempool User-Agent
This commit is contained in:
		
							parent
							
								
									8a996cedb4
								
							
						
					
					
						commit
						b1dde4d8b1
					
				@ -2,6 +2,8 @@ import logger from '../logger';
 | 
				
			|||||||
import axios from 'axios';
 | 
					import axios from 'axios';
 | 
				
			||||||
import { IConversionRates } from '../mempool.interfaces';
 | 
					import { IConversionRates } from '../mempool.interfaces';
 | 
				
			||||||
import config from '../config';
 | 
					import config from '../config';
 | 
				
			||||||
 | 
					import backendInfo from './backend-info';
 | 
				
			||||||
 | 
					import { SocksProxyAgent } from 'socks-proxy-agent';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class FiatConversion {
 | 
					class FiatConversion {
 | 
				
			||||||
  private conversionRates: IConversionRates = {
 | 
					  private conversionRates: IConversionRates = {
 | 
				
			||||||
@ -17,6 +19,9 @@ class FiatConversion {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  public startService() {
 | 
					  public startService() {
 | 
				
			||||||
    logger.info('Starting currency rates service');
 | 
					    logger.info('Starting currency rates service');
 | 
				
			||||||
 | 
					    if (config.SOCKS5PROXY.ENABLED) {
 | 
				
			||||||
 | 
					      logger.info('Currency rates service will be queried over the Tor network');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    setInterval(this.updateCurrency.bind(this), 1000 * config.MEMPOOL.PRICE_FEED_UPDATE_INTERVAL);
 | 
					    setInterval(this.updateCurrency.bind(this), 1000 * config.MEMPOOL.PRICE_FEED_UPDATE_INTERVAL);
 | 
				
			||||||
    this.updateCurrency();
 | 
					    this.updateCurrency();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -26,8 +31,28 @@ class FiatConversion {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private async updateCurrency(): Promise<void> {
 | 
					  private async updateCurrency(): Promise<void> {
 | 
				
			||||||
 | 
					    const headers = { 'User-Agent': `mempool/v${backendInfo.getBackendInfo().version}` };
 | 
				
			||||||
 | 
					    let response;
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const response = await axios.get('https://price.bisq.wiz.biz/getAllMarketPrices', { timeout: 10000 });
 | 
					      let fiatConversionUrl = 'https://price.bisq.wiz.biz/getAllMarketPrices';
 | 
				
			||||||
 | 
					      if (config.SOCKS5PROXY.ENABLED) {
 | 
				
			||||||
 | 
					        let socksOptions: any = {
 | 
				
			||||||
 | 
					          agentOptions: {
 | 
				
			||||||
 | 
					            keepAlive: true,
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					          host: config.SOCKS5PROXY.HOST,
 | 
				
			||||||
 | 
					          port: config.SOCKS5PROXY.PORT
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        if (config.SOCKS5PROXY.USERNAME && config.SOCKS5PROXY.PASSWORD) {
 | 
				
			||||||
 | 
					          socksOptions.username = config.SOCKS5PROXY.USERNAME;
 | 
				
			||||||
 | 
					          socksOptions.password = config.SOCKS5PROXY.PASSWORD;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        const agent = new SocksProxyAgent(socksOptions);
 | 
				
			||||||
 | 
					        fiatConversionUrl = 'http://wizpriceje6q5tdrxkyiazsgu7irquiqjy2dptezqhrtu7l2qelqktid.onion/getAllMarketPrices';
 | 
				
			||||||
 | 
					        response = await axios.get(fiatConversionUrl, { httpAgent: agent, headers: headers, timeout: 30000 });
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        response = await axios.get(fiatConversionUrl, { headers: headers, timeout: 10000 });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      const usd = response.data.data.find((item: any) => item.currencyCode === 'USD');
 | 
					      const usd = response.data.data.find((item: any) => item.currencyCode === 'USD');
 | 
				
			||||||
      this.conversionRates = {
 | 
					      this.conversionRates = {
 | 
				
			||||||
        'USD': usd.price,
 | 
					        'USD': usd.price,
 | 
				
			||||||
 | 
				
			|||||||
@ -51,7 +51,7 @@ interface IConfig {
 | 
				
			|||||||
    ENABLED: boolean;
 | 
					    ENABLED: boolean;
 | 
				
			||||||
    HOST: string;
 | 
					    HOST: string;
 | 
				
			||||||
    PORT: number;
 | 
					    PORT: number;
 | 
				
			||||||
    MIN_PRIORITY: 'emerg' | 'alert' | 'crit' | 'err' |'warn' | 'notice' | 'info' | 'debug';
 | 
					    MIN_PRIORITY: 'emerg' | 'alert' | 'crit' | 'err' | 'warn' | 'notice' | 'info' | 'debug';
 | 
				
			||||||
    FACILITY: string;
 | 
					    FACILITY: string;
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
  STATISTICS: {
 | 
					  STATISTICS: {
 | 
				
			||||||
@ -62,6 +62,13 @@ interface IConfig {
 | 
				
			|||||||
    ENABLED: boolean;
 | 
					    ENABLED: boolean;
 | 
				
			||||||
    DATA_PATH: string;
 | 
					    DATA_PATH: string;
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					  SOCKS5PROXY: {
 | 
				
			||||||
 | 
					    ENABLED: boolean;
 | 
				
			||||||
 | 
					    HOST: string;
 | 
				
			||||||
 | 
					    PORT: number;
 | 
				
			||||||
 | 
					    USERNAME: string;
 | 
				
			||||||
 | 
					    PASSWORD: string;
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const defaults: IConfig = {
 | 
					const defaults: IConfig = {
 | 
				
			||||||
@ -128,6 +135,13 @@ const defaults: IConfig = {
 | 
				
			|||||||
    'ENABLED': false,
 | 
					    'ENABLED': false,
 | 
				
			||||||
    'DATA_PATH': '/bisq/statsnode-data/btc_mainnet/db'
 | 
					    'DATA_PATH': '/bisq/statsnode-data/btc_mainnet/db'
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					  'SOCKS5PROXY': {
 | 
				
			||||||
 | 
					    'ENABLED': false,
 | 
				
			||||||
 | 
					    'HOST': '127.0.0.1',
 | 
				
			||||||
 | 
					    'PORT': 9050,
 | 
				
			||||||
 | 
					    'USERNAME': '',
 | 
				
			||||||
 | 
					    'PASSWORD': ''
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Config implements IConfig {
 | 
					class Config implements IConfig {
 | 
				
			||||||
@ -140,6 +154,7 @@ class Config implements IConfig {
 | 
				
			|||||||
  SYSLOG: IConfig['SYSLOG'];
 | 
					  SYSLOG: IConfig['SYSLOG'];
 | 
				
			||||||
  STATISTICS: IConfig['STATISTICS'];
 | 
					  STATISTICS: IConfig['STATISTICS'];
 | 
				
			||||||
  BISQ: IConfig['BISQ'];
 | 
					  BISQ: IConfig['BISQ'];
 | 
				
			||||||
 | 
					  SOCKS5PROXY: IConfig['SOCKS5PROXY'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor() {
 | 
					  constructor() {
 | 
				
			||||||
    const configs = this.merge(configFile, defaults);
 | 
					    const configs = this.merge(configFile, defaults);
 | 
				
			||||||
@ -152,6 +167,7 @@ class Config implements IConfig {
 | 
				
			|||||||
    this.SYSLOG = configs.SYSLOG;
 | 
					    this.SYSLOG = configs.SYSLOG;
 | 
				
			||||||
    this.STATISTICS = configs.STATISTICS;
 | 
					    this.STATISTICS = configs.STATISTICS;
 | 
				
			||||||
    this.BISQ = configs.BISQ;
 | 
					    this.BISQ = configs.BISQ;
 | 
				
			||||||
 | 
					    this.SOCKS5PROXY = configs.SOCKS5PROXY;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  merge = (...objects: object[]): IConfig => {
 | 
					  merge = (...objects: object[]): IConfig => {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user