Commit 269944bb authored by nanahira's avatar nanahira

Merge branch 'patch-u16secret'

parents ae590fcf e53ef745
Pipeline #42197 passed with stages
in 9 minutes and 37 seconds
......@@ -7,10 +7,10 @@ variables:
build:
stage: build
image: node:16
dependencies: []
tags:
- linux
image: node:16
script:
- npm ci
- npm run build
......
......@@ -4,7 +4,7 @@ import { Subscription, timer } from 'rxjs';
import { LoginService } from '../login.service';
import { map } from 'rxjs/internal/operators';
import { HttpClient } from '@angular/common/http';
import { MatchResponse } from '../ygopro.service';
import { MatchResponse, YGOProService } from '../ygopro.service';
const second = 1000;
const offset = new Date().getTimezoneOffset() * 60 * second;
......@@ -27,14 +27,16 @@ export class MatchDialogComponent implements OnInit, OnDestroy {
@Inject(MAT_DIALOG_DATA) public arena: string,
private dialogRef: MatDialogRef<MatchDialogComponent>,
private http: HttpClient,
private login: LoginService
private login: LoginService,
private ygopro: YGOProService,
) {}
ngOnInit() {
async ngOnInit() {
const u16Secret = await this.ygopro.getUserU16Secret();
this.matching = this.http
.post<MatchResponse>('https://sapi.moecube.com:444/ygopro/match', null, {
headers: {
Authorization: 'Basic ' + Buffer.from(this.login.user.username + ':' + this.login.user.external_id).toString('base64')
Authorization: 'Basic ' + Buffer.from(this.login.user.username + ':' + u16Secret).toString('base64')
},
params: { arena: this.arena, locale: 'zh-CN' }
})
......@@ -50,6 +52,8 @@ export class MatchDialogComponent implements OnInit, OnDestroy {
}
ngOnDestroy() {
if (this.matching) {
this.matching.unsubscribe();
}
}
}
......@@ -275,7 +275,32 @@ export class YGOProService {
}
}
create_room(room: Room, host_password: string) {
async getUserU16Secret() {
const bad = (msg: string) => {
alert(`获取用户密钥失败: ${msg},请尝试重新登录`);
throw new Error(msg);
}
const token = this.login.user.token;
if (!token) {
return bad('token not found');
}
try {
const res = await this.http.get<{
u16Secret: number
}>('https://sapi.moecube.com:444/accounts/authUser', {
headers: {
Authorization: `Bearer ${token}`
}
})
.toPromise();
return res.u16Secret;
} catch (e) {
return bad(e.message || 'unknown error');
}
}
async create_room(room: Room, host_password: string) {
const options_buffer = Buffer.alloc(6);
// 建主密码 https://docs.google.com/document/d/1rvrCGIONua2KeRaYNjKBLqyG9uybs9ZI-AmzZKNftOI/edit
options_buffer.writeUInt8(((room.private ? 2 : 1) << 4) | (room.options.duel_rule << 1) | (room.options.auto_death ? 0x1 : 0), 1);
......@@ -294,7 +319,8 @@ export class YGOProService {
}
options_buffer.writeUInt8(checksum & 0xff, 0);
const secret = (this.login.user.external_id % 65535) + 1;
const u16Secret = await this.getUserU16Secret();
const secret = (u16Secret % 65535) + 1;
for (let i = 0; i < options_buffer.length; i += 2) {
options_buffer.writeUInt16LE(options_buffer.readUInt16LE(i) ^ secret, i);
}
......@@ -312,7 +338,7 @@ export class YGOProService {
this.join(password, this.currentServer);
}
join_room(room: Room) {
async join_room(room: Room) {
const options_buffer = new Buffer(6);
options_buffer.writeUInt8(3 << 4, 1);
let checksum = 0;
......@@ -321,7 +347,8 @@ export class YGOProService {
}
options_buffer.writeUInt8(checksum & 0xff, 0);
const secret = (this.login.user.external_id % 65535) + 1;
const u16Secret = await this.getUserU16Secret();
const secret = (u16Secret % 65535) + 1;
for (let i = 0; i < options_buffer.length; i += 2) {
options_buffer.writeUInt16LE(options_buffer.readUInt16LE(i) ^ secret, i);
}
......@@ -331,7 +358,7 @@ export class YGOProService {
this.join(name, room.server!);
}
join_private(password: string) {
async join_private(password: string) {
const options_buffer = new Buffer(6);
options_buffer.writeUInt8(5 << 4, 1);
let checksum = 0;
......@@ -340,7 +367,8 @@ export class YGOProService {
}
options_buffer.writeUInt8(checksum & 0xff, 0);
const secret = (this.login.user.external_id % 65535) + 1;
const u16Secret = await this.getUserU16Secret();
const secret = (u16Secret % 65535) + 1;
for (let i = 0; i < options_buffer.length; i += 2) {
options_buffer.writeUInt16LE(options_buffer.readUInt16LE(i) ^ secret, i);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment