Skip to content

Commit ee5fcad

Browse files
committed
wip
1 parent 1a6c8b7 commit ee5fcad

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/matches/match-relay/match-relay.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export class MatchRelayService {
3838

3939
const prime = path.shift();
4040

41+
console.info("prime", prime);
42+
4143
if (prime == null || prime == "" || prime == "index.html") {
4244
this.respondSimpleError(uri, response, 401, "Unauthorized");
4345
return;
@@ -64,6 +66,11 @@ export class MatchRelayService {
6466
this.stats.new_match_broadcasts++;
6567
} else {
6668
if (prime == "sync") {
69+
console.info({
70+
uri,
71+
param,
72+
path,
73+
});
6774
if (
6875
this.token_redirect &&
6976
this.match_broadcasts[this.token_redirect]
Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
1-
import { Injectable, NestMiddleware } from "@nestjs/common";
1+
import { Injectable, Logger, NestMiddleware } from "@nestjs/common";
22
import { Request, Response, NextFunction } from "express";
33
import * as express from "express";
4+
import { HasuraService } from "src/hasura/hasura.service";
5+
import { CacheService } from "src/cache/cache.service";
46

57
@Injectable()
68
export class RawBodyMiddleware implements NestMiddleware {
7-
use(req: Request, res: Response, next: NextFunction) {
9+
constructor(
10+
private readonly hasura: HasuraService,
11+
private readonly logger: Logger,
12+
private readonly cache: CacheService,
13+
) {}
14+
15+
async use(request: Request, response: Response, next: NextFunction) {
16+
try {
17+
const [matchId, apiPassword] = (
18+
request.headers["x-origin-auth"] as string
19+
)?.split(":");
20+
21+
const { matches_by_pk: match } = await this.hasura.query({
22+
matches_by_pk: {
23+
__args: {
24+
id: matchId,
25+
},
26+
password: true,
27+
},
28+
});
29+
30+
if (match?.password !== apiPassword) {
31+
this.logger.warn("invalid api password", {
32+
matchId,
33+
apiPassword,
34+
});
35+
return response.status(401).end();
36+
}
37+
} catch (error) {
38+
this.logger.warn("unable to fetch server", error.message);
39+
return response.status(401).end();
40+
}
41+
842
express.raw({
943
type: "*/*",
1044
limit: "50mb",
11-
verify: (req: any, res, buf) => {
12-
(req as any).rawBody = buf;
13-
req.body = buf;
45+
verify: (_request: any, _response: any, buf: Buffer) => {
46+
(_request as any).rawBody = buf;
47+
_request.body = buf;
1448
},
15-
})(req, res, next);
49+
})(request, response, next);
1650
}
1751
}

0 commit comments

Comments
 (0)