Skip to content

Commit

Permalink
Adds region/shard detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachrip committed Mar 14, 2023
1 parent 041b573 commit cce1af6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
84 changes: 70 additions & 14 deletions app/utils.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import axios from 'axios';
import axios, { isAxiosError } from 'axios';
import fsPromise from 'fs/promises';
import path from 'path';
import https from 'https';

import { v4 as uuidv4 } from 'uuid';

import type { Loadout, UserConfig } from '~/utils';
import { User } from 'server/userman';
import { generateRequestHeaders, User } from 'server/userman';
import { Regions, Shards } from 'types';

async function exists(filename: string) {
try {
Expand Down Expand Up @@ -123,6 +124,14 @@ const httpClient = axios.create({
}),
});

declare global {
var userShardRegionCache: Map<string, { region: Regions; shard: Shards }>;
}

global.userShardRegionCache =
global.userShardRegionCache ||
new Map<string, { region: Regions; shard: Shards }>();

export async function getUser() {
try {
const lockfile = await getLockfile();
Expand All @@ -149,23 +158,70 @@ export async function getUser() {
})
).data;

const region = (
await httpClient.get(
`https://127.0.0.1:${port}/riotclient/region-locale`,
{
headers: {
Authorization: `Basic ${Buffer.from(`riot:${password}`).toString(
'base64'
)}`,
},
const shardRegionMap = [
[Shards.NorthAmerica, Regions.NorthAmerica],
[Shards.NorthAmerica, Regions.LatinAmerica],
[Shards.NorthAmerica, Regions.Brazil],
[Shards.Europe, Regions.Europe],
[Shards.AsiaPacific, Regions.AsiaPacific],
[Shards.Korea, Regions.Korea],
] as const;

const res =
global.userShardRegionCache.get(tokens.subject) ||
(await (async function findRegionAndShard() {
for (const [shard, region] of shardRegionMap) {
try {
const response = await httpClient.get(
`https://glz-${region}-1.${shard}.a.pvp.net/parties/v1/players/${tokens.subject}`,
{
headers: generateRequestHeaders({
accessToken: tokens.accessToken,
entitlementsToken: tokens.token,
}),
}
);

if (!response.data.Subject) {
continue;
}

return {
region,
shard,
};
} catch (e) {
if (isAxiosError(e)) {
switch (e.response?.status) {
case 404: {
console.log('404, continuing');
break;
}
default: {
console.warn(
'Caught error trying to get user for region/shard detection',
e
);
break;
}
}
}
}
}
)
).data.region;
})());

if (!res) {
return null;
}

console.log('Found user region and shard', res);
userShardRegionCache.set(tokens.subject, res);

return new User({
accessToken: tokens.accessToken,
entitlementsToken: tokens.token,
region,
region: res.region,
shard: res.shard,
userId: tokens.subject,
});
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valpal",
"version": "0.0.1",
"version": "0.0.2",
"private": true,
"sideEffects": false,
"scripts": {
Expand Down
8 changes: 3 additions & 5 deletions server/userman.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import https from 'https';
import events from 'events';

import axios from 'axios';
import { getUserConfig, randomItem } from 'app/utils.server';
import {
EntitlementsByCategory,
EntitlementsByType,
Expand All @@ -12,7 +10,6 @@ import {
EquippedSpray,
ValorantLoadout,
Shards,
regionToShard,
PregamePlayer,
PregameMatch,
} from 'types';
Expand Down Expand Up @@ -50,7 +47,7 @@ function getPartyServiceUrl(region: Regions, shard: Shards) {
return `https://glz-${region}-1.${shard}.a.pvp.net`;
}

function generateRequestHeaders(
export function generateRequestHeaders(
args: {
accessToken: string;
entitlementsToken: string;
Expand Down Expand Up @@ -92,12 +89,13 @@ export class User {
entitlementsToken: string;
userId: string;
region: Regions;
shard: Shards;
}) {
this.accessToken = args.accessToken;
this.entitlementsToken = args.entitlementsToken;
this.userId = args.userId;
this.region = args.region;
this.shard = regionToShard[this.region];
this.shard = args.shard;
this.requestHeaders = generateRequestHeaders({
accessToken: this.accessToken,
entitlementsToken: this.entitlementsToken,
Expand Down

0 comments on commit cce1af6

Please sign in to comment.