Skip to content

Commit

Permalink
Merge pull request #131 from shuymn/set-ua
Browse files Browse the repository at this point in the history
Change to set UserAgent as needed
  • Loading branch information
shuymn authored Jul 30, 2023
2 parents 6b1aa0e + 8e8d9ac commit b435df5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions backend/src/activitypub/actors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Handle, handleToMastodonUrl, handleToPleromaUrl, RemoteHandle } from 'w
import { generateMastodonId } from 'wildebeest/backend/src/utils/id'
import { generateUserKey } from 'wildebeest/backend/src/utils/key-ops'
import { defaultImages } from 'wildebeest/config/accounts'
import { UA } from 'wildebeest/config/ua'

const isTesting = typeof jest !== 'undefined'

Expand Down Expand Up @@ -57,6 +58,7 @@ export interface Person extends Actor {
export async function fetchActor(url: string | URL): Promise<Remote<Actor>> {
const headers = {
accept: 'application/activity+json',
'User-Agent': UA,
}
const res = await fetch(url, { headers })
if (!res.ok) {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/activitypub/objects/collection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ApObject } from 'wildebeest/backend/src/activitypub/objects'
import { UA } from 'wildebeest/config/ua'

export interface Collection<T> extends ApObject {
totalItems: number
Expand All @@ -17,6 +18,7 @@ export interface OrderedCollectionPage<T> extends ApObject {

const headers = {
accept: 'application/activity+json',
'User-Agent': UA,
}

export async function getMetadata<T>(url: URL): Promise<OrderedCollection<T>> {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/activitypub/objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { MastodonId } from 'wildebeest/backend/src/types'
import { isUUID } from 'wildebeest/backend/src/utils'
import { generateMastodonId } from 'wildebeest/backend/src/utils/id'
import { Intersect, RequiredProps, SingleOrArray } from 'wildebeest/backend/src/utils/type'
import { UA } from 'wildebeest/config/ua'

export const originalActorIdSymbol = Symbol()
export const originalObjectIdSymbol = Symbol()
Expand Down Expand Up @@ -152,6 +153,7 @@ export async function createObject<T extends ApObject>(
export async function get<T>(url: URL): Promise<T> {
const headers = {
accept: 'application/activity+json',
'User-Agent': UA,
}
const res = await fetch(url, { headers })
if (!res.ok) {
Expand Down
11 changes: 9 additions & 2 deletions backend/src/utils/httpsigjs/verifier.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UA } from 'wildebeest/config/ua'

import { importPublicKey, str2ab } from '../key-ops'
import { ParsedSignature } from './parser'

Expand All @@ -21,10 +23,15 @@ export async function verifySignature(parsedSignature: ParsedSignature, key: Cry
export async function fetchKey(parsedSignature: ParsedSignature): Promise<CryptoKey | null> {
const url = parsedSignature.keyId
const res = await fetch(url, {
headers: { Accept: 'application/activity+json' },
headers: {
Accept: 'application/activity+json',
'User-Agent': UA,
},
})
if (!res.ok) {
console.warn(`failed to fetch keys from "${url}", returned ${res.status}.`)
if (res.status !== 410) {
console.warn(`failed to fetch keys from "${url}", returned ${res.status}.`)
}
return null
}

Expand Down
4 changes: 3 additions & 1 deletion config/ua.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { WILDEBEEST_VERSION, MASTODON_API_VERSION } from 'wildebeest/config/versions'
import { MASTODON_API_VERSION, WILDEBEEST_VERSION } from 'wildebeest/config/versions'

export function getFederationUA(domain: string): string {
return `Wildebeest/${WILDEBEEST_VERSION} (Mastodon/${MASTODON_API_VERSION}; +${domain})`
}

export const UA = `Wildebeest/${WILDEBEEST_VERSION} (Mastodon/${MASTODON_API_VERSION})`

0 comments on commit b435df5

Please sign in to comment.