Verified Commit 9bd31736 authored by Maxime FRIESS's avatar Maxime FRIESS 💙
Browse files

[*] Switched to Seb.ts, added Dockerfile

parent a6ce3d7b
.env
node_modules
build
\ No newline at end of file
"@amicale-core:registry" "https://git.unistra.org/api/v4/packages/npm/"
\ No newline at end of file
# Use node lts
FROM node:lts
# Set working directory
WORKDIR /seb-gateway/
# Install dependencies
COPY package.json ./
COPY yarn.lock ./
RUN yarn install
# Build
COPY . ./
RUN yarn build
# Run on port 4000
EXPOSE 4000
# Uptimerobot check on 4001
EXPOSE 4001
CMD yarn start
......@@ -4,9 +4,10 @@
"main": "index.ts",
"repository": "https://git.unistra.fr/amicale-core/seb-gateway",
"author": "M4x1m3 <M4x1me@protonmail.com>",
"license": "AGPLv3",
"license": "AGPL-3.0",
"private": false,
"dependencies": {
"@amicale-core/seb-ts": "^1.0.1",
"axios": "^0.24.0",
"dotenv": "^10.0.0",
"typescript": "^4.4.4",
......
......@@ -17,12 +17,13 @@
* along with Seb-Gateway. If not, see <https://www.gnu.org/licenses/>.
*/
import { GetProfileRequest, REST } from '@amicale-core/seb-ts';
import { IncomingMessage } from 'http';
import { Socket } from 'net';
import { WebSocket, WebSocketServer } from 'ws';
import API from './API';
import Client from './Client';
import SebAPI from './SebAPI';
import HardConfig from './config/HardConfig';
import Loggers, { Logger } from './utils/Logger';
/**
......@@ -82,28 +83,27 @@ class _Gateway {
throw new Error();
}
let profile: any;
let rest: REST;
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
const token = req.headers.authorization.split(' ')[1];
profile = await SebAPI.profile({
mode: 'bearer',
token: token
});
rest = new REST(HardConfig.getSebURL(), token);
} else {
/*
let cookies = req.headers.cookie;
profile = await SebAPI.profile({
mode: 'cookie',
cookies: cookies
});
}
if (profile.good) {
let intents: string[] = this.getEffectiveIntents(intents_str.split(",") ?? [], profile?.data?.data?.permissions ?? []);
return new Client(intents);
} else {
*/
// TODO: Add this way of ligging in to Seb.TS
throw new Error();
}
const profile_req = new GetProfileRequest(rest);
const profile = await profile_req.make();
let intents: string[] = this.getEffectiveIntents(intents_str.split(",") ?? [], profile.permissions ?? []);
return new Client(intents);
}
private async upgrade(req: IncomingMessage, socket: Socket, head: Buffer): Promise<void> {
......@@ -114,6 +114,7 @@ class _Gateway {
this.ws?.emit('connection', ws, req, client);
}).bind(this));
} catch (e: any) {
console.error(e);
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
}
......
/**
* Copyright © 2021 Maxime Friess <M4x1me@pm.me>
*
* This file is part of Seb-Gateway.
*
* Seb-Gateway is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Seb-Gateway is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Seb-Gateway. If not, see <https://www.gnu.org/licenses/>.
*/
import axios, { AxiosRequestHeaders, Method } from "axios";
import HardConfig from "./config/HardConfig";
import Loggers, { Logger } from "./utils/Logger";
interface Authorization {
mode: 'cookie' | 'bearer';
cookies?: string;
token?: string;
}
class _SebAPI {
private logger: Logger;
constructor() {
this.logger = Loggers.getLogger("SebAPI");
}
private async request(method: Method, path: string, auth: Authorization): Promise<any> {
this.logger.info(method + " " + path);
try {
const headers: AxiosRequestHeaders = auth.mode === 'bearer' ? { 'Authorization': 'Bearer ' + auth.token ?? "" } : { 'Cookie': auth.cookies ?? "" };
let res = await axios.request({ method: method, headers: headers, url: HardConfig.getSebURL().replace(/\/$/, '') + path });
return {
good: true,
error: '',
status: res.status,
data: res.data
};
} catch (error: any) {
if (error?.response?.data?.message) {
let message = "";
if (error?.response?.data?.errors !== undefined) {
for (let ms in error.response.data.errors) {
for (let m of error.response.data.errors[ms]) {
message += m + "\n";
}
}
} else {
message = error?.response?.data?.message;
}
if (error.response.status !== 422)
this.logger.error(`Error with API Request:\n ${method} ${path}\n`, error);
return {
good: false,
error: message,
status: error.response.status,
data: {}
}
}
this.logger.error(`Error with API Request:\n ${method} ${path}\n`, error);
return {
good: false,
error: error?.message,
status: -1,
data: {}
}
}
}
async profile(auth: Authorization): Promise<any> {
return await this.request('GET', '/api/profile/me', auth);
}
}
const SebAPI = new _SebAPI();
export default SebAPI;
......@@ -2,6 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"pretty": true,
"sourceMap": true,
"strict": true,
......
......@@ -2,6 +2,17 @@
# yarn lockfile v1
"@amicale-core/seb-ts@^1.0.1":
version "1.0.1"
resolved "https://git.unistra.fr/api/v4/projects/23485/packages/npm/@amicale-core/seb-ts/-/@amicale-core/seb-ts-1.0.1.tgz#12035c77d54615740b34ebc726e0000b2b036729"
integrity sha1-EgNcd9VGFXQLNOvHJuAACysDZyk=
dependencies:
"@types/ws" "^8.2.0"
cookie "^0.4.1"
node-fetch-commonjs "^3.0.3"
set-cookie-parser "^2.4.8"
ws "^8.2.3"
"@babel/code-frame@^7.0.0":
version "7.16.0"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.16.0.tgz#0dfc80309beec8411e65e706461c408b0bb9b431"
......@@ -336,6 +347,11 @@ configstore@^5.0.1:
write-file-atomic "^3.0.0"
xdg-basedir "^4.0.0"
cookie@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.1.tgz#afd713fe26ebd21ba95ceb61f9a8116e50a537d1"
integrity sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==
create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
......@@ -453,6 +469,13 @@ esprima@^4.0.0:
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
fetch-blob@^3.1.2:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.1.3.tgz#a7dca4855e39d3e3c5a1da62d4ee335c37d26012"
integrity sha512-ax1Y5I9w+9+JiM+wdHkhBoxew+zG4AJ2SvAD1v1szpddUIiPERVGBxrMcB2ZqW0Y3PP8bOWYv2zqQq1Jp2kqUQ==
dependencies:
web-streams-polyfill "^3.0.3"
figures@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962"
......@@ -491,6 +514,13 @@ follow-redirects@^1.14.4:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.5.tgz#f09a5848981d3c772b5392309778523f8d85c381"
integrity sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==
formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"
fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
......@@ -898,6 +928,14 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
node-fetch-commonjs@^3.0.3:
version "3.1.1"
resolved "https://registry.yarnpkg.com/node-fetch-commonjs/-/node-fetch-commonjs-3.1.1.tgz#e36db76b9fe13bdd44a5bafabc29c61d0f01b4e1"
integrity sha512-TgkdVJdiEaauzWwB9NoD4TvHZFtG6KKEffvotWf9WNIyoRZHsCFjGfb3bhkIXrMt3YFgFi8ZApbwWoe1h3XTpA==
dependencies:
formdata-polyfill "^4.0.10"
web-streams-polyfill "^3.1.1"
nodemon@^2.0.13:
version "2.0.14"
resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-2.0.14.tgz#287c7a2f6cd8a18b07e94cd776ecb6a82e4ba439"
......@@ -1147,6 +1185,11 @@ semver@^7.3.4:
dependencies:
lru-cache "^6.0.0"
set-cookie-parser@^2.4.8:
version "2.4.8"
resolved "https://registry.yarnpkg.com/set-cookie-parser/-/set-cookie-parser-2.4.8.tgz#d0da0ed388bc8f24e706a391f9c9e252a13c58b2"
integrity sha512-edRH8mBKEWNVIVMKejNnuJxleqYE/ZSdcT8/Nem9/mmosx12pctd80s2Oy00KNZzrogMZS5mauK2/ymL1bvlvg==
signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.5"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.5.tgz#9e3e8cc0c75a99472b44321033a7702e7738252f"
......@@ -1379,6 +1422,11 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"
web-streams-polyfill@^3.0.3, web-streams-polyfill@^3.1.1:
version "3.2.0"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.0.tgz#a6b74026b38e4885869fb5c589e90b95ccfc7965"
integrity sha512-EqPmREeOzttaLRm5HS7io98goBgZ7IVz79aDvqjD0kYXLtFZTc0T/U6wHTPKyIjb+MdN7DFIIX6hgdBEpWmfPA==
widest-line@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/widest-line/-/widest-line-3.1.0.tgz#8292333bbf66cb45ff0de1603b136b7ae1496eca"
......
Supports Markdown
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