From 4239c121acc507ec87937d2a93d35ed9f4d8a8d6 Mon Sep 17 00:00:00 2001 From: nick Date: Thu, 16 Nov 2023 17:54:03 +0900 Subject: [PATCH] feat: use proxy for specified location if defined in adapter --- api/src/proxy/dto/proxy.ts | 4 ++-- api/src/proxy/proxy.service.spec.ts | 11 +++++++++++ cli/src/proxy.ts | 11 +++++++---- cli/src/utils.ts | 5 +++++ fetcher/src/job/job.types.ts | 1 + fetcher/src/job/job.utils.ts | 11 ++++++++++- 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/api/src/proxy/dto/proxy.ts b/api/src/proxy/dto/proxy.ts index 257fad0c8..3f0d5ff6d 100644 --- a/api/src/proxy/dto/proxy.ts +++ b/api/src/proxy/dto/proxy.ts @@ -1,4 +1,4 @@ -import { ApiProperty } from '@nestjs/swagger' +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger' export class ProxyDto { @ApiProperty() @@ -11,5 +11,5 @@ export class ProxyDto { port: number @ApiProperty() - location: string + location?: string } diff --git a/api/src/proxy/proxy.service.spec.ts b/api/src/proxy/proxy.service.spec.ts index 482579bce..e6ef84c2d 100644 --- a/api/src/proxy/proxy.service.spec.ts +++ b/api/src/proxy/proxy.service.spec.ts @@ -38,6 +38,17 @@ describe('ProxyService', () => { expect(proxyObj.port).toBe(proxyData.port) expect(proxyObj.location).toBe(proxyData.location) + const proxyDataWithoutLocation = { + protocol: 'http', + host: '127.0.0.2', + port: 80 + } + + const proxyObjWithoutLocation = await proxy.create(proxyDataWithoutLocation) + expect(proxyObjWithoutLocation.protocol).toBe(proxyData.protocol) + expect(proxyObjWithoutLocation.host).toBe(proxyData.host) + expect(proxyObjWithoutLocation.port).toBe(proxyData.port) + // The same proxy cannot be defined twice await expect(async () => { await proxy.create(proxyData) diff --git a/cli/src/proxy.ts b/cli/src/proxy.ts index 2d0cd797d..61517bfc2 100644 --- a/cli/src/proxy.ts +++ b/cli/src/proxy.ts @@ -1,6 +1,6 @@ import axios from 'axios' import { command, subcommands, option, string as cmdstring, number } from 'cmd-ts' -import { idOption, buildUrl, isOraklNetworkApiHealthy } from './utils' +import { idOption, buildUrl, isOraklNetworkApiHealthy, proxyOptionalOption } from './utils' import { ORAKL_NETWORK_API_URL } from './settings' const PROXY_ENDPOINT = buildUrl(ORAKL_NETWORK_API_URL, 'proxy') @@ -30,7 +30,8 @@ export function proxySub() { port: option({ type: number, long: 'port' - }) + }), + location: proxyOptionalOption }, handler: insertHandler() }) @@ -70,16 +71,18 @@ export function insertHandler() { async function wrapper({ protocol, host, - port + port, + location }: { protocol: string host: string port: number + location?: string }) { if (!(await isOraklNetworkApiHealthy())) return try { - const response = (await axios.post(PROXY_ENDPOINT, { protocol, host, port }))?.data + const response = (await axios.post(PROXY_ENDPOINT, { protocol, host, port, location }))?.data console.dir(response, { depth: null }) } catch (e) { console.error('Proxy was not inserted. Reason:') diff --git a/cli/src/utils.ts b/cli/src/utils.ts index 02a027057..61d40f669 100644 --- a/cli/src/utils.ts +++ b/cli/src/utils.ts @@ -18,6 +18,11 @@ export const fetcherTypeOptionalOption = option({ long: 'fetcherType' }) +export const proxyOptionalOption = option({ + type: optional(cmdstring), + long: 'location' +}) + export const idOption = option({ type: cmdnumber, long: 'id' diff --git a/fetcher/src/job/job.types.ts b/fetcher/src/job/job.types.ts index cf4a717b3..7293e41f5 100644 --- a/fetcher/src/job/job.types.ts +++ b/fetcher/src/job/job.types.ts @@ -78,4 +78,5 @@ export interface IProxy { protocol: string | undefined host: string | undefined port: number | undefined + location?: string | undefined } diff --git a/fetcher/src/job/job.utils.ts b/fetcher/src/job/job.utils.ts index 3cfb7d189..6939a1b9d 100644 --- a/fetcher/src/job/job.utils.ts +++ b/fetcher/src/job/job.utils.ts @@ -180,7 +180,16 @@ export function extractFeeds( const feeds = adapter.feeds.map((f) => { let proxy: IProxy try { - proxy = proxySelector(f.definition.url) + if (!f.location) { + proxy = proxySelector(f.definition.url) + } else { + const availableProxies = proxies.filter((item) => item.location === f.location) + if (availableProxies.length == 0) { + throw `no proxies available for location:${f.location}` + } + const randomIndex = Math.floor(Math.random() * availableProxies.length) + proxy = availableProxies[randomIndex] + } } catch (e) { logger.error('Assigning proxy has failed') logger.error(e)