moved hash check and token download to main thread

This commit is contained in:
Shane Harris
2020-08-17 13:31:48 +01:00
committed by Your Name
parent ef163b2335
commit 7aab6ec072
6 changed files with 621 additions and 245 deletions
+63 -49
View File
@@ -65,57 +65,71 @@ export class ElectronService {
}
}
installFromToken(token) {
let options = {
url: 'https://api.sidequestvr.com/install-from-key',
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Origin: 'https://sidequestvr.com',
},
rejectUnauthorized: !isDevMode(),
json: { token: token },
};
// console.log(options);
this.appService.request(options, async (error, response, body) => {
// console.log(error, body);
if (!error && body.data && body.data.apps && body.data.apps.length) {
for (let i = 0; i < body.data.apps.length; i++) {
let app = body.data.apps[i];
if (Number(app.app_categories_id) === 1) {
let urls = app.urls.filter(l => l && ~['Github Release', 'APK', 'OBB', 'Mod'].indexOf(l.provider));
for (let i = 0; i < urls.length; i++) {
if (urls[i].provider === 'Mod') {
this.beatonService.downloadSong(urls[i].link_url, this.adbService);
} else {
const etx = urls[i].link_url
.split('?')[0]
.split('.')
.pop()
.toLowerCase();
switch (etx) {
case 'obb':
await this.adbService.installObb(urls[i].link_url, i + 1, urls.length, app.name);
break;
default:
await this.adbService.installAPK(
urls[i].link_url,
false,
false,
i + 1,
urls.length,
false,
app.name
);
break;
}
}
}
}
}
async installFromToken(token) {
let res = await this.adbService.adbCommand('installFromToken', { token });
res.forEach((l, i) => {
switch (l.type) {
case 'Mod':
this.beatonService.downloadSong(l.url, this.adbService);
break;
case 'APK':
this.adbService.installAPK(l.url, false, false, i + 1, res.length, false, l.name);
break;
case 'OBB':
this.adbService.installObb(l.url, i + 1, res.length, l.name);
break;
}
});
// let options = {
// url: 'https://api.sidequestvr.com/install-from-key',
// method: 'POST',
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json',
// Origin: 'https://sidequestvr.com',
// },
// rejectUnauthorized: !isDevMode(),
// json: { token: token },
// };
// // console.log(options);
// this.appService.request(options, async (error, response, body) => {
// // console.log(error, body);
// if (!error && body.data && body.data.apps && body.data.apps.length) {
// for (let i = 0; i < body.data.apps.length; i++) {
// let app = body.data.apps[i];
// if (Number(app.app_categories_id) === 1) {
// let urls = app.urls.filter(l => l && ~['Github Release', 'APK', 'OBB', 'Mod'].indexOf(l.provider));
// for (let i = 0; i < urls.length; i++) {
// if (urls[i].provider === 'Mod') {
// this.beatonService.downloadSong(urls[i].link_url, this.adbService);
// } else {
// const etx = urls[i].link_url
// .split('?')[0]
// .split('.')
// .pop()
// .toLowerCase();
// switch (etx) {
// case 'obb':
// await this.adbService.installObb(urls[i].link_url, i + 1, urls.length, app.name);
// break;
// default:
// await this.adbService.installAPK(
// urls[i].link_url,
// false,
// false,
// i + 1,
// urls.length,
// false,
// app.name
// );
// break;
// }
// }
// }
// }
// }
// }
// });
}
setupIPC() {
+47 -44
View File
@@ -2,6 +2,8 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AppService } from './app.service';
import { environment } from './../environments/environment';
import { ElectronService } from './electron.service';
import { AdbClientService } from './adb-client.service';
interface CheckSubmissionResponse {
found: boolean;
@@ -11,51 +13,52 @@ interface CheckSubmissionResponse {
providedIn: 'root',
})
export class SafeSideService {
constructor(private appService: AppService, private http: HttpClient) {}
constructor(private appService: AppService, private adbService: AdbClientService) {} // private http: HttpClient
async checkAPK(updateStatus: (string) => void, filePath: string): Promise<boolean> {
const hash = await this.computeFileHash(updateStatus, filePath);
const url = `${environment.safeSideApiUrl}/check-app-hash/${hash}`;
updateStatus('Checking APK against blacklist...');
const response = await this.http.get<CheckSubmissionResponse>(url).toPromise();
return response.found;
}
async computeFileHash(updateStatus: (string) => void, filePath: string): Promise<string> {
const fileSize = await this.computeFileSize(filePath);
return new Promise((resolve, reject) => {
let bytesRead: number = 0;
const fd = this.appService.fs.createReadStream(filePath);
const hash = this.appService.crypto.createHash('md5');
hash.setEncoding('hex');
fd.on('data', chunk => {
bytesRead += chunk.length;
const percentage = this.formattedPercentageComplete(bytesRead, fileSize);
updateStatus(`Computing APK hash (${percentage})...`);
});
fd.on('end', () => {
updateStatus(`Computing APK hash (100%)...`);
hash.end();
resolve(hash.read());
});
fd.on('error', error => {
reject(error);
});
fd.pipe(hash);
});
}
async computeFileSize(filePath: string): Promise<number> {
return new Promise((resolve, reject) => {
this.appService.fs.stat(filePath, (err, stats) => {
if (err) {
reject(err);
} else {
resolve(stats.size);
}
});
});
}
formattedPercentageComplete(amount: number, total: number): string {
const percentage = Math.round(Math.min(amount / total, 1) * 100);
return `${percentage}%`;
return this.adbService.adbCommand('hashCheck', { path: filePath }, updateStatus);
// const hash = await this.computeFileHash(updateStatus, filePath);
// const url = `${environment.safeSideApiUrl}/check-app-hash/${hash}`;
// updateStatus('Checking APK against blacklist...');
// const response = await this.http.get<CheckSubmissionResponse>(url).toPromise();
// return response.found;
}
// async computeFileHash(updateStatus: (string) => void, filePath: string): Promise<string> {
// const fileSize = await this.computeFileSize(filePath);
// return new Promise((resolve, reject) => {
// let bytesRead: number = 0;
// const fd = this.appService.fs.createReadStream(filePath);
// const hash = this.appService.crypto.createHash('md5');
// hash.setEncoding('hex');
// fd.on('data', chunk => {
// bytesRead += chunk.length;
// const percentage = this.formattedPercentageComplete(bytesRead, fileSize);
// updateStatus(`Computing APK hash (${percentage})...`);
// });
// fd.on('end', () => {
// updateStatus(`Computing APK hash (100%)...`);
// hash.end();
// resolve(hash.read());
// });
// fd.on('error', error => {
// reject(error);
// });
// fd.pipe(hash);
// });
// }
// async computeFileSize(filePath: string): Promise<number> {
// return new Promise((resolve, reject) => {
// this.appService.fs.stat(filePath, (err, stats) => {
// if (err) {
// reject(err);
// } else {
// resolve(stats.size);
// }
// });
// });
// }
// formattedPercentageComplete(amount: number, total: number): string {
// const percentage = Math.round(Math.min(amount / total, 1) * 100);
// return `${percentage}%`;
// }
}