Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/booru/booru.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ describe('BooruService', () => {
expect(directUrl.searchParams.get('api_key')).toBe('pass_1')
})

it('should allow bypassing proxies with the NONE magic string', () => {
mockConfigService.get.mockImplementation((key: string) => {
if (key === 'BOORU_OUTBOUND_PROXY_CONFIG') {
return JSON.stringify({
'gelbooru.com': {
baseUrl: 'NONE'
}
})
}

return undefined
})

const queries = {
...baseQueries,
baseEndpoint: 'gelbooru.com',
auth_user: 'managed_1',
auth_pass: 'pass_1'
} as booruQueriesDTO

const directUrl = buildPostUrl(service.buildApiClass(mockParams, queries))

expect(directUrl.origin).toBe('https://gelbooru.com')
expect(directUrl.searchParams.get('tags')).toBe('diana')
expect(directUrl.searchParams.get('user_id')).toBe('managed_1')
expect(directUrl.searchParams.get('api_key')).toBe('pass_1')
})

it('should rotate through multiple configured provider proxies', () => {
mockConfigService.get.mockImplementation((key: string) => {
if (key === 'BOORU_OUTBOUND_PROXY_CONFIG') {
Expand All @@ -317,6 +345,9 @@ describe('BooruService', () => {
{
baseUrl: 'https://cors-proxy.refinedsoftware00.workers.dev/',
targetParam: 'q'
},
{
baseUrl: 'NONE'
}
]
})
Expand All @@ -335,10 +366,12 @@ describe('BooruService', () => {
const firstUrl = buildPostUrl(service.buildApiClass(mockParams, queries))
const secondUrl = buildPostUrl(service.buildApiClass(mockParams, queries))
const thirdUrl = buildPostUrl(service.buildApiClass(mockParams, queries))
const fourthUrl = buildPostUrl(service.buildApiClass(mockParams, queries))

expect(firstUrl.origin).toBe('https://cors-proxy2.rule34.workers.dev')
expect(secondUrl.origin).toBe('https://cors-proxy.refinedsoftware00.workers.dev')
expect(thirdUrl.origin).toBe('https://cors-proxy2.rule34.workers.dev')
expect(thirdUrl.origin).toBe('https://gelbooru.com')
expect(fourthUrl.origin).toBe('https://cors-proxy2.rule34.workers.dev')
})

it('should not rotate proxies while building an API that never fetches', () => {
Expand All @@ -353,6 +386,9 @@ describe('BooruService', () => {
{
baseUrl: 'https://cors-proxy.refinedsoftware00.workers.dev/',
targetParam: 'q'
},
{
baseUrl: 'NONE'
}
]
})
Expand Down
8 changes: 6 additions & 2 deletions src/booru/booru.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface BooruQueryIdentifierDefaults {
}

interface BooruOutboundProxyPolicy {
baseUrl: string
baseUrl: string | 'NONE'
targetParam: string
}

Expand Down Expand Up @@ -421,7 +421,7 @@ export class BooruService {
const upstreamUrl = originalMethod.apply(api, args) as URL
const proxyPolicy = this.getOutboundProxyPolicy(domain)

if (proxyPolicy === undefined) {
if (proxyPolicy === undefined || proxyPolicy.baseUrl === 'NONE') {
return upstreamUrl
}

Expand Down Expand Up @@ -523,6 +523,10 @@ export class BooruService {
const baseUrl = policy['baseUrl']
const targetParam = policy['targetParam'] ?? 'q'

if (baseUrl === 'NONE') {
return { baseUrl, targetParam: 'q' }
}

if (typeof baseUrl !== 'string' || !URL.canParse(baseUrl)) {
throw new Error(`Invalid BOORU_OUTBOUND_PROXY_CONFIG baseUrl for ${domain}`)
}
Expand Down