-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.ts
209 lines (182 loc) · 5.63 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import crypto from 'node:crypto';
import path from 'node:path';
import { Readable } from 'node:stream';
import fs from 'fs-extra';
import { createChannel, createClient, Metadata } from 'nice-grpc';
import { GetObjectCommand, PutObjectCommand, S3, S3Client } from '@aws-sdk/client-s3';
import { AccountClient, AccountDefinition } from '@pretendonetwork/grpc/account/account_service';
import { FriendsClient, FriendsDefinition } from '@pretendonetwork/grpc/friends/friends_service';
import { GetNEXDataResponse } from '@pretendonetwork/grpc/account/get_nex_data_rpc';
import { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc';
import { GetUserFriendPIDsResponse } from '@pretendonetwork/grpc/friends/get_user_friend_pids_rpc';
import { config, disabledFeatures } from '@/config-manager';
import { NodeJsClient } from '@smithy/types';
import { Response } from 'express';
import { LOG_ERROR } from '@/logger';
let s3: NodeJsClient<S3Client>;
if (!disabledFeatures.s3) {
s3 = new S3({
forcePathStyle: false,
endpoint: config.cdn.s3.endpoint,
region: config.cdn.s3.region,
credentials: {
accessKeyId: config.cdn.s3.key,
secretAccessKey: config.cdn.s3.secret
}
});
}
const gRPCAccountChannel = createChannel(`${config.grpc.account.address}:${config.grpc.account.port}`);
const gRPCAccountClient: AccountClient = createClient(AccountDefinition, gRPCAccountChannel);
const gRPCFriendsChannel = createChannel(`${config.grpc.friends.address}:${config.grpc.friends.port}`);
const gRPCFriendsClient: FriendsClient = createClient(FriendsDefinition, gRPCFriendsChannel);
const VALID_COUNTRIES = [
'GB', 'US', 'IT', 'NL', 'DE', 'CA', 'FR', 'HU', 'CR',
'AU', 'BR', 'RO', 'CL', 'MX', 'RU', 'ES', 'JP', 'CZ',
'PT', 'MT', 'AR', 'SE', 'PL', 'IE', 'BE', 'HT', 'NO',
'FI', 'GR', 'BO', 'AT', 'VE', 'PA', 'PE', 'GF', 'SA',
'CO', 'LT', 'NA', 'CH', 'CY', 'RS', 'KY', 'GP', 'DK',
'KR', 'LU', 'SV', 'VA', 'GT', 'SK', 'HR', 'ZA', 'DO',
'UY', 'LV', 'HN', 'JM', 'TR', 'IN', 'ER', 'AW', 'NZ',
'EC', 'TW', 'EE', 'CN', 'SI', 'AI', 'BG', 'NI', 'IS',
'MQ', 'BZ', 'BA', 'MY', 'AZ', 'ZW', 'AL', 'IM', 'VG',
'VI', 'BM', 'GY', 'SR', 'MS', 'TC', 'BB', 'TT'
];
const VALID_LANGUAGES = [
'en', 'it', 'de', 'fr', 'es', 'us', 'pt', 'ru', 'ja',
'nl', 'ko', 'zh', 'tw'
];
const VALID_FILE_TYPES = [
'Message', 'AppData'
];
const VALID_FILE_NOTIFY_CONDITIONS = [
'app', 'account'
];
export function fileErrCallback(response: Response) {
return (err: NodeJS.ErrnoException): void => {
if (err) {
if (err.code === 'ENOENT') {
response.sendStatus(404);
} else {
if (!response.headersSent) {
response.status(500).send('Server Error');
}
LOG_ERROR('Error in sending file: ' + err.message);
}
}
};
}
export function md5(input: crypto.BinaryLike): string {
return crypto.createHash('md5').update(input).digest('hex');
}
export function isValidCountryCode(countryCode: string): boolean {
return VALID_COUNTRIES.includes(countryCode);
}
export function isValidLanguage(language: string): boolean {
return VALID_LANGUAGES.includes(language);
}
export function isValidFileType(type: string): boolean {
return VALID_FILE_TYPES.includes(type);
}
export function isValidFileNotifyCondition(condition: string): boolean {
return VALID_FILE_NOTIFY_CONDITIONS.includes(condition);
}
export async function getUserDataByPID(pid: number): Promise<GetUserDataResponse | null> {
try {
return await gRPCAccountClient.getUserData({
pid: pid
}, {
metadata: Metadata({
'X-API-Key': config.grpc.account.api_key
})
});
} catch {
// TODO - Handle error
return null;
}
}
export async function getNEXDataByPID(pid: number): Promise<GetNEXDataResponse | null> {
try {
return await gRPCAccountClient.getNEXData({
pid: pid
}, {
metadata: Metadata({
'X-API-Key': config.grpc.account.api_key
})
});
} catch {
// TODO - Handle error
return null;
}
}
export async function getUserDataByToken(token: string): Promise<GetUserDataResponse | null> {
try {
return await gRPCAccountClient.exchangeTokenForUserData({
token: token
}, {
metadata: Metadata({
'X-API-Key': config.grpc.account.api_key
})
});
} catch (error) {
// TODO - Handle error
console.log(error);
return null;
}
}
export async function getFriends(pid: number): Promise<GetUserFriendPIDsResponse | null> {
try {
return await gRPCFriendsClient.getUserFriendPIDs({
pid: pid
}, {
metadata: Metadata({
'X-API-Key': config.grpc.friends.api_key
})
});
} catch {
// TODO - Handle error
return null;
}
}
export async function getCDNFileStream(key: string): Promise<Readable | null> {
try {
if (disabledFeatures.s3) {
return await getLocalCDNFile(key);
} else {
const response = await s3.send(new GetObjectCommand({
Key: key,
Bucket: config.cdn.s3.bucket
}));
if (!response.Body) {
return null;
}
return response.Body;
}
} catch (error) {
return null;
}
}
export async function getLocalCDNFile(key: string): Promise<fs.ReadStream | null> {
const filePath = path.join(config.cdn.disk_path, key);
if (await !fs.exists(filePath)) {
return null;
}
return fs.createReadStream(filePath);
}
export async function uploadCDNFile(key: string, data: Buffer): Promise<void> {
if (disabledFeatures.s3) {
await writeLocalCDNFile(key, data);
} else {
await s3.send(new PutObjectCommand({
Key: key,
Bucket: config.cdn.s3.bucket,
Body: data,
ACL: 'private'
}));
}
}
export async function writeLocalCDNFile(key: string, data: Buffer): Promise<void> {
const filePath = path.join(config.cdn.disk_path, key);
const folder = path.dirname(filePath);
await fs.ensureDir(folder);
await fs.writeFile(filePath, data);
}