small changes

This commit is contained in:
jusax23 2023-03-05 12:42:51 +01:00
parent ed4b74362e
commit 8accb3fd2f
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
8 changed files with 42 additions and 25 deletions

View file

@ -144,7 +144,7 @@ export const addOTA = {
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
try { try {
await insert(signupOTA.token, signupOTA.expires, signupOTA.usesLeft) await insert(signupOTA.token, signupOTA.expires, signupOTA.usesLeft)
.add(data.token, data.expires, data. usesLeft) .add(data.token, data.expires, data.usesLeft)
.query(db); .query(db);
} catch (error) { } catch (error) {
await update(signupOTA) await update(signupOTA)

View file

@ -6,8 +6,22 @@ import { oConf } from "../../sys/config.js";
import { sha256, verify } from "../../sys/crypto.js"; import { sha256, verify } from "../../sys/crypto.js";
import { accounts, db, signupOTA as signupOTATable } from "../../sys/db.js"; import { accounts, db, signupOTA as signupOTATable } from "../../sys/db.js";
import { get64, uts } from "../../sys/tools.js"; import { get64, uts } from "../../sys/tools.js";
import { addTempToken, postClient } from "../post.js";
import { Act, Client, STATE } from "../user.js"; import { Act, Client, STATE } from "../user.js";
export const requestTempToken: Act = {
state: STATE.no,
right: 0,
data: {},
func: async (client: Client, data: any, aws: (code: string, data: any) => void): Promise<void> => {
if (!(client instanceof postClient)) return void aws("error", "mode");
let token = addTempToken(client);
aws("ok", {
token
});
}
};
export const signup: Act = { export const signup: Act = {
state: STATE.no, state: STATE.no,
right: 0, right: 0,
@ -128,7 +142,7 @@ export const remote1 = {
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
data.server = await outbagURLshort(data.server); data.server = await outbagURLshort(data.server);
try{ try {
var cert = await getRemote(data.server); var cert = await getRemote(data.server);
var tagAcert = data.name + "@" + data.server + "-" + data.key; var tagAcert = data.name + "@" + data.server + "-" + data.key;
if (!(await verify(tagAcert, data.sign, cert))) { if (!(await verify(tagAcert, data.sign, cert))) {
@ -143,9 +157,9 @@ export const remote1 = {
client.state = STATE.remoteP; client.state = STATE.remoteP;
client.remoteKey = data.key; client.remoteKey = data.key;
aws("ok", client.challenge); aws("ok", client.challenge);
}catch (e){ } catch (e) {
client.suspect(); client.suspect();
aws("error","signature"); aws("error", "signature");
} }
} }
}; };
@ -157,10 +171,10 @@ export const remote2 = {
sign: "string" sign: "string"
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if(await verify(client.challenge,data.sign,client.remoteKey)){ if (await verify(client.challenge, data.sign, client.remoteKey)) {
aws("ok", ""); aws("ok", "");
client.state = STATE.remote; client.state = STATE.remote;
}else{ } else {
client.suspect(); client.suspect();
aws("error", "signature"); aws("error", "signature");
} }

0
src/api/acts/server.ts Normal file
View file

View file

@ -28,11 +28,13 @@ export const addPostMethods = (server: express.Express) => {
let client: postClient | null = null; let client: postClient | null = null;
try { try {
let auth = authorization.parse(req.headers["authorization"] ?? ""); let auth = authorization.parse(req.headers["authorization"] ?? "");
if (auth.token != null && typeof auth.token == "string") { if (auth.scheme == "outbagServer") {
} else if (auth.token != null && typeof auth.token == "string") {
if (tempTokens[auth.token] != null) { if (tempTokens[auth.token] != null) {
client = tempTokens[auth.token]; client = tempTokens[auth.token];
} else { } else {
if(req.suspect)req.suspect(); if (req.suspect) req.suspect();
aws("error", "token"); aws("error", "token");
return; return;
} }
@ -50,7 +52,7 @@ export const addPostMethods = (server: express.Express) => {
.query(db); .query(db);
if (query.length == 0 || query[0].accountKey != sha256((query[0].accountKeySalt ?? '') + accountKey)) { if (query.length == 0 || query[0].accountKey != sha256((query[0].accountKeySalt ?? '') + accountKey)) {
if(req.suspect)req.suspect(); if (req.suspect) req.suspect();
aws("error", "auth"); aws("error", "auth");
return; return;
} }
@ -72,7 +74,7 @@ export const addPostMethods = (server: express.Express) => {
} }
} }
class postClient extends Client { export class postClient extends Client {
lastReq = uts(); lastReq = uts();
constructor(ip: string) { constructor(ip: string) {
super(ip); super(ip);

View file

@ -3,10 +3,11 @@ import { accounts, db, roomMembers, rooms } from "../sys/db.js";
import { addBruteforcePotantial } from "../sys/bruteforce.js"; import { addBruteforcePotantial } from "../sys/bruteforce.js";
export const STATE = { export const STATE = {
no: 0b0001, no: 0b00001,
remoteP: 0b0010, remoteP: 0b00010,
remote: 0b0100, remote: 0b00100,
client: 0b1000 client: 0b01000,
server: 0b10000,
}; };
export const MODE = { export const MODE = {

View file

@ -15,7 +15,7 @@ export const wsOnConnection = (socket: ws.WebSocket, req: http.IncomingMessage)
let clients: wsClient[] = []; let clients: wsClient[] = [];
class wsClient extends Client { export class wsClient extends Client {
socket: ws.WebSocket; socket: ws.WebSocket;
open = true; open = true;
activeRequests = 0; activeRequests = 0;

View file

@ -1,6 +1,6 @@
import { outbagURL } from "./outbagURL.js"; import { outbagURL, outbagURLshort } from "./outbagURL.js";
import { db, serverCerts } from "../sys/db.js" import { db, serverCerts } from "../sys/db.js"
import { eq, exists, insert, not, remove, select, update } from "dblang"; import { eq, insert, select, update } from "dblang";
import { error, log } from "../sys/log.js"; import { error, log } from "../sys/log.js";
import { uts } from "../sys/tools.js"; import { uts } from "../sys/tools.js";
import { getSettings, setSettings, SETTINGS } from "../sys/settings.js" import { getSettings, setSettings, SETTINGS } from "../sys/settings.js"
@ -27,7 +27,6 @@ export const startUpdateCerts = () => {
}, 100); }, 100);
} }
async function updateRemote(url: string, pKey: string = ""): Promise<boolean | string> { async function updateRemote(url: string, pKey: string = ""): Promise<boolean | string> {
var urlP = await outbagURL(url); var urlP = await outbagURL(url);
return new Promise((res, rej) => { return new Promise((res, rej) => {
@ -63,9 +62,9 @@ async function updateRemote(url: string, pKey: string = ""): Promise<boolean | s
return; return;
} }
res(false); res(false);
}) });
}) });
} };
export const getRemote = async (url: string) => { export const getRemote = async (url: string) => {
let query = await select([serverCerts.publicKey, serverCerts.expires], serverCerts) let query = await select([serverCerts.publicKey, serverCerts.expires], serverCerts)
@ -74,4 +73,4 @@ export const getRemote = async (url: string) => {
if (query.length == 0 || query[0][serverCerts.expires] < uts() - 60) if (query.length == 0 || query[0][serverCerts.expires] < uts() - 60)
return await updateRemote(url, query[0][serverCerts.publicKey]); return await updateRemote(url, query[0][serverCerts.publicKey]);
return query[0][serverCerts.publicKey]; return query[0][serverCerts.publicKey];
} };

View file

@ -72,7 +72,8 @@ serverCerts.addAttributes({
serverCertID: { type: INT, primaryKey: true, autoIncrement: true }, serverCertID: { type: INT, primaryKey: true, autoIncrement: true },
url: { type: TEXT }, url: { type: TEXT },
publicKey: { type: TEXT }, publicKey: { type: TEXT },
expires: { type: BIGINT } expires: { type: BIGINT },
token: { type: TEXT, notNull: false },
}); });
export const signupOTA = db.newTable("signupOTA"); export const signupOTA = db.newTable("signupOTA");