Skip to content

Commit

Permalink
feat: use proxy for specified location if defined in adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Nov 16, 2023
1 parent 9ce5818 commit 4239c12
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions api/src/proxy/dto/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '@nestjs/swagger'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'

Check warning on line 1 in api/src/proxy/dto/proxy.ts

View workflow job for this annotation

GitHub Actions / core-build

'ApiPropertyOptional' is defined but never used

export class ProxyDto {
@ApiProperty()
Expand All @@ -11,5 +11,5 @@ export class ProxyDto {
port: number

@ApiProperty()
location: string
location?: string
}
11 changes: 11 additions & 0 deletions api/src/proxy/proxy.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions cli/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -30,7 +30,8 @@ export function proxySub() {
port: option({
type: number,
long: 'port'
})
}),
location: proxyOptionalOption
},
handler: insertHandler()
})
Expand Down Expand Up @@ -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:')
Expand Down
5 changes: 5 additions & 0 deletions cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions fetcher/src/job/job.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ export interface IProxy {
protocol: string | undefined
host: string | undefined
port: number | undefined
location?: string | undefined
}
11 changes: 10 additions & 1 deletion fetcher/src/job/job.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4239c12

Please sign in to comment.