Skip to content

Commit

Permalink
Мелкие правки
Browse files Browse the repository at this point in the history
  • Loading branch information
JoCat committed Jul 11, 2021
1 parent 36fb964 commit 53affea
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
32 changes: 25 additions & 7 deletions src/api/webserver/WebRequestManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IncomingMessage, ServerResponse } from "http"

import { AbstractRequest } from "./requests/AbstractRequest"
import { ProfilesRequest } from "./requests/authlib/accountsServer/ProfilesRequest"
import { PrivelegesRequest } from "./requests/authlib/servicesServer/PrivelegesRequest"
import { HasJoinedRequest } from "./requests/authlib/sessionServer/HasJoinedRequest"
import { JoinRequest } from "./requests/authlib/sessionServer/JoinRequest"
import { ProfileRequest } from "./requests/authlib/sessionServer/ProfileRequest"
import { ProfilesRequest } from "./requests/authlib/accountsHost/ProfilesRequest"
import { PrivelegesRequest } from "./requests/authlib/servicesHost/PrivelegesRequest"
import { HasJoinedRequest } from "./requests/authlib/sessionHost/HasJoinedRequest"
import { JoinRequest } from "./requests/authlib/sessionHost/JoinRequest"
import { ProfileRequest } from "./requests/authlib/sessionHost/ProfileRequest"

export class WebRequestManager {
private requests: AbstractRequest[] = []
Expand All @@ -25,8 +25,26 @@ export class WebRequestManager {
getRequest(req: IncomingMessage, res: ServerResponse): void {
res.setHeader("Content-Type", "application/json; charset=utf-8")

const request = this.requests.find((e) => e.method === req.method && e.url.test(req.url))
if (request === undefined) return res.writeHead(404).end("Not found!")
const request = this.requests.find((e) => e.url.test(req.url))
// нижние 2 обработчика корректны для api.mojang.com и authserver.mojang.com
if (request === undefined)
return res
.writeHead(404)
.end(
AbstractRequest.returnError(
"Not Found",
"The server has not found anything matching the request URI"
)
)
if (request.method !== req.method)
return res
.writeHead(405)
.end(
AbstractRequest.returnError(
"Method Not Allowed",
"The method specified in the request is not allowed for the resource identified by the request URI"
)
)
request.emit(req, res)
}
}
6 changes: 5 additions & 1 deletion src/api/webserver/requests/AbstractRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export abstract class AbstractRequest {
return query.toString().length === 0
}

protected returnError(error: string, errorMessage?: string): string {
public returnError(error: string, errorMessage?: string): string {
return JsonHelper.toJSON({ error, errorMessage })
}
// КХЪ Похоже что нужно рефакторить некоторе говно
public static returnError(error: string, errorMessage?: string): string {
return JsonHelper.toJSON({ error, errorMessage })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@ export class ProfilesRequest extends AbstractRequest {
let data: any = await this.getPostData(req)
res.statusCode = 400

if (data.length === 0) {
res.write(this.returnError("Bad Request"))
return res.end()
try {
data = JsonHelper.fromJSON(data)
} catch (error) {
return res.end(this.returnError("BadRequestException"))
}

data = JsonHelper.fromJSON(data)

if ("object" !== typeof data || !Array.isArray(data) || data.length === 0) {
return res.end()
}
if (!Array.isArray(data) || data.length === 0) return res.end(this.returnError("BadRequestException"))

if (data.length >= 10) {
res.write(
if (data.length > 10)
return res.end(
this.returnError("IllegalArgumentException", "Not more that 10 profile name per call is allowed.")
)
return res.end()
}

let users
try {
Expand All @@ -39,7 +34,6 @@ export class ProfilesRequest extends AbstractRequest {
}

res.statusCode = 200
res.write(JsonHelper.toJSON(users))
res.end()
res.end(JsonHelper.toJSON(users))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ export class PrivelegesRequest extends AbstractRequest {

async emit(req: IncomingMessage, res: ServerResponse): Promise<void> {
const accessToken = req.headers.authorization
res.statusCode = 400

if ("string" !== typeof accessToken || accessToken.length === 0) {
res.statusCode = 400
return res.end()
}

let user
try {
user = await App.AuthManager.getAuthProvider().privileges(accessToken.slice(7))
} catch (error) {
res.statusCode = 400
return res.end()
}

res.write(
res.statusCode = 200
res.end(
JsonHelper.toJSON({
privileges: {
onlineChat: {
Expand All @@ -43,6 +43,5 @@ export class PrivelegesRequest extends AbstractRequest {
},
})
)
res.end()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export class JoinRequest extends AbstractRequest {
let data: any = await this.getPostData(req)
res.statusCode = 400

if (data.length === 0) {
res.write(this.returnError("Bad Request"))
return res.end()
}
if (data.length === 0) return res.end(this.returnError("Bad Request"))

data = JsonHelper.fromJSON(data)

Expand All @@ -26,8 +23,7 @@ export class JoinRequest extends AbstractRequest {
this.isInvalidValue(data.selectedProfile) ||
this.isInvalidValue(data.serverId)
) {
res.write(this.returnError("Bad Request"))
return res.end()
return res.end(this.returnError("Bad Request"))
}

try {
Expand All @@ -37,10 +33,9 @@ export class JoinRequest extends AbstractRequest {
data.serverId
)
} catch (error) {
res.write(
return res.end(
this.returnError("ForbiddenOperationException", "Invalid credentials. Invalid username or password.")
)
return res.end()
}

res.statusCode = 204
Expand Down
2 changes: 1 addition & 1 deletion src/auth/authProviders/AcceptAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class AcceptAuthProvider extends AbstractAuthProvider {
onlineChat: true,
multiplayerServer: true,
multiplayerRealms: true,
telemetry: true,
telemetry: false,
}
}

Expand Down

0 comments on commit 53affea

Please sign in to comment.