Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent User-Agent for all requests #448

Closed
Closed
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
11 changes: 11 additions & 0 deletions src/back/Axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { UserAgent } from '@shared/Util';
import * as axiosImport from 'axios';
const axios = axiosImport.default;

const axiosInstance = axios.create({
headers: {
'User-Agent': UserAgent
}
});

export default axiosInstance;
6 changes: 3 additions & 3 deletions src/back/curate/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CURATIONS_FOLDER_WORKING } from '@shared/constants';
import { getContentFolderByKey } from '@shared/curate/util';
import { GamePropSuggestions } from '@shared/interfaces';
import { LangContainer } from '@shared/lang';
import axios from 'axios';
import axiosInstance from '@back/Axios';
import { AddAppCuration, CurationFpfssInfo, CurationState, CurationWarnings, LoadedCuration } from 'flashpoint-launcher';
import * as fs from 'fs-extra';
import * as http from 'http';
Expand Down Expand Up @@ -298,7 +298,7 @@ export async function makeCurationFromGame(state: BackState, gameId: string, ski
const destPath = path.join(curPath, 'logo.png');
const url = new URL(logoRelPath, state.preferences.onDemandBaseUrl);
const writer = fs.createWriteStream(destPath);
await axios.get(url.href, {
await axiosInstance.get(url.href, {
responseType: 'stream',
}).then((response) => {
return new Promise<void>((resolve, reject) => {
Expand All @@ -321,7 +321,7 @@ export async function makeCurationFromGame(state: BackState, gameId: string, ski
const destPath = path.join(curPath, 'ss.png');
const url = new URL(screenshotRelPath, state.preferences.onDemandBaseUrl);
const writer = fs.createWriteStream(destPath);
await axios.get(url.href, {
await axiosInstance.get(url.href, {
responseType: 'stream',
}).then((response) => {
return new Promise<void>((resolve, reject) => {
Expand Down
4 changes: 2 additions & 2 deletions src/back/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
CURATIONS_FOLDER_TEMP,
CURATIONS_FOLDER_WORKING, CURATION_META_FILENAMES
} from '@shared/constants';
import axios from 'axios';
import axiosInstance from './Axios';
import { FlashpointArchive, enableDebug, loggerSusbcribe } from '@fparchive/flashpoint-archive';
import { Tail } from 'tail';
import { ConfigFile } from './ConfigFile';
Expand Down Expand Up @@ -1423,7 +1423,7 @@ async function updateFileServerDownloadQueue() {
url += '?type=jpg';
}
// Use arraybuffer since it's small memory footprint anyway
await axios.get(url, { responseType: 'arraybuffer' })
await axiosInstance.get(url, { responseType: 'arraybuffer' })
.then(async (res) => {
// Save response to image file
const imageData = res.data;
Expand Down
15 changes: 6 additions & 9 deletions src/back/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { TaskProgress } from '@shared/utils/TaskProgress';
import { chunkArray, getGameDataFilename, newGame } from '@shared/utils/misc';
import { sanitizeFilename } from '@shared/utils/sanitizeFilename';
import { throttle } from '@shared/utils/throttle';
import * as axiosImport from 'axios';
import axiosInstance from './Axios';
import * as child_process from 'child_process';
import { execSync } from 'child_process';
import {
Expand Down Expand Up @@ -124,9 +124,6 @@ import {
import { uuid } from './util/uuid';
import { FPFSS_INFO_FILENAME } from '@shared/curate/fpfss';
import { createSearchFilter } from '@back/util/search';
import { FpfssUser } from '@shared/back/types';

const axios = axiosImport.default;

/**
* Register all request callbacks to the socket server.
Expand Down Expand Up @@ -211,7 +208,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v

// Fetch update feed in background
if (state.preferences.updateFeedUrl) {
axios.get(state.preferences.updateFeedUrl, { timeout: 3000 })
axiosInstance.get(state.preferences.updateFeedUrl, { timeout: 3000 })
.then((res) => {
state.socketServer.broadcast(BackOut.UPDATE_FEED, res.data);
})
Expand All @@ -227,7 +224,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
const gotdPath = path.join(state.config.flashpointPath, 'Data', 'gotd.json');
const gotdDownload = new Promise((resolve, reject) => {
const thumbnailWriter = fs.createWriteStream(gotdPath);
axios.get(gotdUrl, { responseType: 'stream' })
axiosInstance.get(gotdUrl, { responseType: 'stream' })
.then((res) => {
res.data.pipe(thumbnailWriter);
thumbnailWriter.on('close', resolve);
Expand Down Expand Up @@ -383,7 +380,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
const updatesReady = state.componentStatuses.filter(c => c.id === 'core-launcher' && c.state === ComponentState.NEEDS_UPDATE).length > 0;
const version = state.version;
const versionUrl = `${source.baseUrl}/api/min-launcher`;
const res = await axios.get(versionUrl)
const res = await axiosInstance.get(versionUrl)
.catch((err) => { throw `Failed to find minimum launcher version requirement from metadata server.\n${err}`; });
if (compareSemVerVersions(version, res.data['min-version'] || '9999999999999') < 0) {
if (!updatesReady) {
Expand Down Expand Up @@ -1840,7 +1837,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
// Upload to log server
const postUrl = url.resolve(state.config.logsBaseUrl, 'logdata');
// Server responds with log id e.g ABC123
const res = await axios.post(postUrl, { entries: entries });
const res = await axiosInstance.post(postUrl, { entries: entries });
const id = res.data;
// Form into GET URL
let getUrl: string | undefined;
Expand Down Expand Up @@ -1942,7 +1939,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
state.socketServer.register(BackIn.DOWNLOAD_PLAYLIST, async (event, url) => {
// Attempt download
console.log(url);
const res = await axios.get(url);
const res = await axiosInstance.get(url);
if (res.status == 200) {
// Load as json
const json = res.data;
Expand Down
14 changes: 7 additions & 7 deletions src/back/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GameRedirect, RemoteCategory, RemoteDeletedGamesRes, RemoteGamesRes, RemotePlatform, RemoteTag } from '@fparchive/flashpoint-archive';
import axios from 'axios';
import axiosInstance from './Axios';
import { GameMetadataSource } from 'flashpoint-launcher';
import { camelCase, transform } from 'lodash';
import { fpDatabase } from '.';
Expand All @@ -8,7 +8,7 @@ export async function syncTags(source: GameMetadataSource): Promise<Date> {
const tagsUrl = `${source.baseUrl}/api/tags?after=${source.tags.latestUpdateTime}`;
const nextLatestDate = source.tags.latestUpdateTime;

const res = await axios.get(tagsUrl)
const res = await axiosInstance.get(tagsUrl)
.catch((err) => {
throw 'Failed to search tags';
});
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function syncTags(source: GameMetadataSource): Promise<Date> {
export async function syncPlatforms(source: GameMetadataSource): Promise<Date> {
const platformsUrl = `${source.baseUrl}/api/platforms?after=${source.tags.latestUpdateTime}`;

const res = await axios.get(platformsUrl)
const res = await axiosInstance.get(platformsUrl)
.catch((err) => {
throw 'Failed to search platforms';
});
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function syncGames(source: GameMetadataSource, dataPacksFolder: str
while (true) {
const reqUrl = `${gamesUrl}?after=${source.games.latestUpdateTime}&before=${capUpdateTime.toISOString()}&broad=true&afterId=${nextId}`;
console.log(reqUrl);
const res = await axios.get(reqUrl)
const res = await axiosInstance.get(reqUrl)
.catch((err) => {
throw 'Failed to search games';
});
Expand Down Expand Up @@ -120,7 +120,7 @@ export async function syncGames(source: GameMetadataSource, dataPacksFolder: str

// -- Deleted Games -- //
const reqUrl = `${deletedUrl}`;
const res = await axios.get(reqUrl)
const res = await axiosInstance.get(reqUrl)
.catch((err) => {
throw 'Failed to search deleted games';
});
Expand All @@ -134,7 +134,7 @@ export async function syncGames(source: GameMetadataSource, dataPacksFolder: str

export async function syncRedirects(source: GameMetadataSource): Promise<void> {
const gamesUrl = `${source.baseUrl}/api/game-redirects`;
const res = await axios.get(gamesUrl)
const res = await axiosInstance.get(gamesUrl)
.catch((err) => {
throw 'Failed to search game redirects';
});
Expand All @@ -155,7 +155,7 @@ export async function getMetaUpdateInfo(source: GameMetadataSource, accurate?: b
}
const countUrl = `${source.baseUrl}/api/games/updates?after=${fromScratch ? '1970-01-01' : d.toISOString()}`;
try {
const res = await axios.get(countUrl);
const res = await axiosInstance.get(countUrl);
return res.data.total;
} catch (err) {
log.error('Launcher', 'Error fetching update info for ' + countUrl + ' - ' + err);
Expand Down
7 changes: 6 additions & 1 deletion src/main/Main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as remoteMain from '@electron/remote/main';
import { InitRendererChannel, InitRendererData } from '@shared/IPC';
import { createErrorProxy } from '@shared/Util';
import { createErrorProxy, UserAgent } from '@shared/Util';
import { SocketClient } from '@shared/back/SocketClient';
import { BackIn, BackInitArgs, BackOut } from '@shared/back/types';
import { AppConfigData } from '@shared/config/interfaces';
Expand Down Expand Up @@ -311,6 +311,11 @@ export function main(init: Init): void {
state.socket.send(BackIn.SET_LOCALE, app.getLocale().toLowerCase());
state._sentLocaleCode = true;
}
// Set user agent for all requests
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
details.requestHeaders['User-Agent'] = UserAgent;
callback({ cancel: false, requestHeaders: details.requestHeaders });
});
// Reject all permission requests since we don't need any permissions.
session.defaultSession.setPermissionRequestHandler(
(webContents, permission, callback) => callback(false)
Expand Down
6 changes: 6 additions & 0 deletions src/shared/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DownloadDetails } from './back/types';
import { AppConfigData } from './config/interfaces';
import { parseVariableString } from './utils/VariableString';
import { throttle } from './utils/throttle';
import { VERSION } from './version';

const axios = axiosImport.default;

Expand Down Expand Up @@ -418,6 +419,9 @@ export function tagSort(tagA: Tag, tagB: Tag): number {

export async function downloadFile(url: string, filePath: string, abortSignal?: AbortSignal, onProgress?: (percent: number) => void, onDetails?: (details: DownloadDetails) => void, options?: axiosImport.AxiosRequestConfig): Promise<number> {
try {
options = options || {};
options.headers = options.headers || {};
options.headers['User-Agent'] = UserAgent;
const res = await axios.get(url, {
...options,
responseType: 'stream',
Expand Down Expand Up @@ -658,3 +662,5 @@ export function mapLocalToFpfssGame(game: Game): FpfssGame {
};
return fg;
}

export const UserAgent = `Flashpoint Launcher/${VERSION}`;