WIP 1
This commit is contained in:
parent
c095fa7042
commit
43bfdaa1e3
186 changed files with 276 additions and 37155 deletions
|
|
@ -17,24 +17,24 @@
|
|||
"@emotion/react": "^11.11.4",
|
||||
"@emotion/server": "^11.11.0",
|
||||
"@emotion/styled": "^11.11.0",
|
||||
"@fontsource/playfair-display": "^5.0.21",
|
||||
"@fontsource/playfair-display": "^5.0.23",
|
||||
"@fontsource/poppins": "^5.0.12",
|
||||
"@fontsource/roboto": "^5.0.12",
|
||||
"@mui/icons-material": "^5",
|
||||
"@mui/lab": "^5.0.0-alpha.167",
|
||||
"@mui/lab": "^5.0.0-alpha.168",
|
||||
"@mui/material": "^5",
|
||||
"@mui/x-data-grid-pro": "^6.19.6",
|
||||
"@mui/x-date-pickers-pro": "^6.19.6",
|
||||
"@opensearch-project/opensearch": "^2.5.0",
|
||||
"@mui/x-date-pickers-pro": "^6.19.7",
|
||||
"@opensearch-project/opensearch": "^2.6.0",
|
||||
"cryptr": "^6.3.0",
|
||||
"date-fns": "^3.3.1",
|
||||
"date-fns": "^3.5.0",
|
||||
"http-proxy-middleware": "^2.0.6",
|
||||
"leafcutter-common": "*",
|
||||
"material-ui-popup-state": "^5.0.10",
|
||||
"next": "14.1.2",
|
||||
"next-auth": "^4.24.6",
|
||||
"next": "14.1.3",
|
||||
"next-auth": "^4.24.7",
|
||||
"next-http-proxy-middleware": "^1.2.6",
|
||||
"nodemailer": "^6.9.11",
|
||||
"nodemailer": "^6.9.12",
|
||||
"react": "18.2.0",
|
||||
"react-cookie": "^7.1.0",
|
||||
"react-cookie-consent": "^9.0.0",
|
||||
|
|
@ -49,18 +49,18 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.24.0",
|
||||
"@types/node": "^20.11.24",
|
||||
"@types/react": "18.2.63",
|
||||
"@types/node": "^20.11.28",
|
||||
"@types/react": "18.2.66",
|
||||
"@types/uuid": "^9.0.8",
|
||||
"babel-loader": "^9.1.3",
|
||||
"eslint": "^8.57.0",
|
||||
"eslint-config-airbnb": "^19.0.4",
|
||||
"eslint-config-next": "^14.1.2",
|
||||
"eslint-config-next": "^14.1.3",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-import": "^2.29.1",
|
||||
"eslint-plugin-jsx-a11y": "^6.8.0",
|
||||
"eslint-plugin-prettier": "^5.1.3",
|
||||
"eslint-plugin-react": "^7.34.0",
|
||||
"typescript": "5.3.3"
|
||||
"eslint-plugin-react": "^7.34.1",
|
||||
"typescript": "5.4.2"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,5 +6,10 @@ export const metadata: Metadata = {
|
|||
};
|
||||
|
||||
export default function Page() {
|
||||
return <Home />;
|
||||
return (
|
||||
<iframe src="/opensearch/app/dashboards?security_tenant=global#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f?embed=true&_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!f%2Cvalue%3A900000)%2Ctime%3A(from%3Anow-7d%2Cto%3Anow))&hide-filter-bar=true"
|
||||
height="1000"
|
||||
width="1200"
|
||||
></iframe>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
66
apps/link/app/api/proxy/[...path]/route.ts
Normal file
66
apps/link/app/api/proxy/[...path]/route.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import { createProxyMiddleware } from "http-proxy-middleware";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
/*
|
||||
|
||||
if (validDomains.includes(domain)) {
|
||||
res.headers.set("Access-Control-Allow-Origin", origin);
|
||||
res.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
res.headers.set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
const withAuthInfo =
|
||||
(handler: any) => async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session: any = await getToken({
|
||||
req,
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
let email = session?.email?.toLowerCase();
|
||||
|
||||
const requestSignature = req.query.signature;
|
||||
const url = new URL(req.headers.referer as string);
|
||||
const referrerSignature = url.searchParams.get("signature");
|
||||
|
||||
console.log({ requestSignature, referrerSignature });
|
||||
const isAppPath = !!req.url?.startsWith("/app");
|
||||
const isResourcePath = !!req.url?.match(/\/(api|app|bootstrap|3961|ui|translations|internal|login|node_modules)/);
|
||||
|
||||
if (requestSignature && isAppPath) {
|
||||
console.log("Has Signature");
|
||||
}
|
||||
|
||||
if (referrerSignature && isResourcePath) {
|
||||
console.log("Has Signature");
|
||||
}
|
||||
|
||||
if (!email) {
|
||||
return res.status(401).json({ error: "Not authorized" });
|
||||
}
|
||||
|
||||
req.headers["x-proxy-user"] = email;
|
||||
req.headers["x-proxy-roles"] = "leafcutter_user";
|
||||
const auth = `${email}:${process.env.OPENSEARCH_USER_PASSWORD}`;
|
||||
const buff = Buffer.from(auth);
|
||||
const base64data = buff.toString("base64");
|
||||
req.headers.Authorization = `Basic ${base64data}`;
|
||||
return handler(req, res);
|
||||
};
|
||||
|
||||
const proxy = createProxyMiddleware({
|
||||
target: process.env.OPENSEARCH_DASHBOARDS_URL,
|
||||
changeOrigin: true,
|
||||
xfwd: true,
|
||||
});
|
||||
|
||||
export default withAuthInfo(proxy);
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: false,
|
||||
externalResolver: true,
|
||||
},
|
||||
};
|
||||
|
|
@ -24,12 +24,37 @@ const rewriteURL = (request: NextRequestWithAuth, originBaseURL: string, destina
|
|||
const checkRewrites = async (request: NextRequestWithAuth) => {
|
||||
const linkBaseURL = process.env.LINK_URL ?? "http://localhost:3000";
|
||||
const zammadURL = process.env.ZAMMAD_URL ?? "http://zammad-nginx:8080";
|
||||
const opensearchURL = process.env.OPENSEARCH_URL ?? "http://macmini:5601";
|
||||
const metamigoURL = process.env.METAMIGO_URL ?? "http://metamigo-api:3000";
|
||||
const labelStudioURL = process.env.LABEL_STUDIO_URL ?? "http://label-studio:8080";
|
||||
const { token } = request.nextauth;
|
||||
const headers = { 'X-Forwarded-User': token?.email?.toLowerCase() };
|
||||
console.log ({ pathname: request.nextUrl.pathname});
|
||||
|
||||
if (request.nextUrl.pathname.startsWith('/metamigo')) {
|
||||
if (request.nextUrl.pathname.startsWith('/api/v1/configuration/account') ||
|
||||
request.nextUrl.pathname.startsWith('/api/v1/restapiinfo') ||
|
||||
request.nextUrl.pathname.startsWith('/api/v1/auth') ||
|
||||
request.nextUrl.pathname.startsWith('/api/core') ||
|
||||
request.nextUrl.pathname.startsWith('/api/dataconnections') ||
|
||||
request.nextUrl.pathname.startsWith('/api/v1/multitenancy') ||
|
||||
request.nextUrl.pathname.startsWith('/api/ism') ||
|
||||
request.nextUrl.pathname.startsWith('/node_modules') ||
|
||||
request.nextUrl.pathname.startsWith('/translations') || request.nextUrl.pathname.startsWith('/6867') || request.nextUrl.pathname.startsWith('/ui') || request.nextUrl.pathname.startsWith('/bootstrap')) {
|
||||
const headers = {
|
||||
'x-proxy-user': "admin",
|
||||
'x-proxy-roles': "all_access",
|
||||
// 'X-Forwarded-For': "link"
|
||||
};
|
||||
return rewriteURL(request, `${linkBaseURL}`, opensearchURL, headers);
|
||||
}
|
||||
else if (request.nextUrl.pathname.startsWith('/opensearch')) {
|
||||
const headers = {
|
||||
'x-proxy-user': "admin",
|
||||
'x-proxy-roles': "all_access",
|
||||
// 'X-Forwarded-For': "link"
|
||||
};
|
||||
return rewriteURL(request, `${linkBaseURL}/opensearch`, opensearchURL, headers);
|
||||
}else if (request.nextUrl.pathname.startsWith('/metamigo')) {
|
||||
return rewriteURL(request, `${linkBaseURL}/metamigo`, metamigoURL);
|
||||
} else if (request.nextUrl.pathname.startsWith('/label-studio')) {
|
||||
return rewriteURL(request, `${linkBaseURL}/label-studio`, labelStudioURL);
|
||||
|
|
@ -83,4 +108,3 @@ export const config = {
|
|||
'/((?!ws|wss|_next/static|_next/image|favicon.ico).*)',
|
||||
],
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -15,29 +15,29 @@
|
|||
"@emotion/react": "^11.11.4",
|
||||
"@emotion/server": "^11.11.0",
|
||||
"@emotion/styled": "^11.11.0",
|
||||
"@fontsource/playfair-display": "^5.0.21",
|
||||
"@fontsource/playfair-display": "^5.0.23",
|
||||
"@fontsource/poppins": "^5.0.12",
|
||||
"@fontsource/roboto": "^5.0.12",
|
||||
"@mui/icons-material": "^5",
|
||||
"@mui/lab": "^5.0.0-alpha.167",
|
||||
"@mui/lab": "^5.0.0-alpha.168",
|
||||
"@mui/material": "^5",
|
||||
"@mui/x-data-grid-pro": "^6.19.6",
|
||||
"@mui/x-date-pickers-pro": "^6.19.6",
|
||||
"@mui/x-date-pickers-pro": "^6.19.7",
|
||||
"cryptr": "^6.3.0",
|
||||
"date-fns": "^3.3.1",
|
||||
"date-fns": "^3.5.0",
|
||||
"graphql-request": "^6.1.0",
|
||||
"leafcutter-common": "*",
|
||||
"material-ui-popup-state": "^5.0.10",
|
||||
"mui-chips-input": "^2.1.4",
|
||||
"next": "14.1.2",
|
||||
"next-auth": "^4.24.6",
|
||||
"next": "14.1.3",
|
||||
"next-auth": "^4.24.7",
|
||||
"ra-data-graphql": "^4.16.12",
|
||||
"ra-i18n-polyglot": "^4.16.12",
|
||||
"ra-input-rich-text": "^4.16.12",
|
||||
"ra-input-rich-text": "^4.16.13",
|
||||
"ra-language-english": "^4.16.12",
|
||||
"ra-postgraphile": "^6.1.2",
|
||||
"react": "18.2.0",
|
||||
"react-admin": "^4.16.12",
|
||||
"react-admin": "^4.16.13",
|
||||
"react-cookie": "^7.1.0",
|
||||
"react-digit-input": "^2.1.0",
|
||||
"react-dom": "18.2.0",
|
||||
|
|
@ -52,18 +52,18 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.24.0",
|
||||
"@types/node": "^20.11.24",
|
||||
"@types/react": "18.2.63",
|
||||
"@types/node": "^20.11.28",
|
||||
"@types/react": "18.2.66",
|
||||
"@types/uuid": "^9.0.8",
|
||||
"babel-loader": "^9.1.3",
|
||||
"eslint": "^8.57.0",
|
||||
"eslint-config-airbnb": "^19.0.4",
|
||||
"eslint-config-next": "^14.1.2",
|
||||
"eslint-config-next": "^14.1.3",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-import": "^2.29.1",
|
||||
"eslint-plugin-jsx-a11y": "^6.8.0",
|
||||
"eslint-plugin-prettier": "^5.1.3",
|
||||
"eslint-plugin-react": "^7.34.0",
|
||||
"typescript": "5.3.3"
|
||||
"eslint-plugin-react": "^7.34.1",
|
||||
"typescript": "5.4.2"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
.git
|
||||
.idea
|
||||
**/node_modules
|
||||
!/node_modules
|
||||
**/build
|
||||
**/dist
|
||||
**/tmp
|
||||
**/.env*
|
||||
**/coverage
|
||||
**/.next
|
||||
**/amigo.*.json
|
||||
**/cypress/videos
|
||||
**/cypress/screenshots
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
require("eslint-config-link/patch/modern-module-resolution");
|
||||
module.exports = {
|
||||
extends: [
|
||||
"eslint-config-link/profile/node",
|
||||
"eslint-config-link/profile/typescript",
|
||||
"eslint-config-link/profile/jest",
|
||||
],
|
||||
parserOptions: { tsconfigRootDir: __dirname },
|
||||
rules: {
|
||||
"new-cap": "off"
|
||||
},
|
||||
};
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"presets": [
|
||||
"babel-preset-link"
|
||||
]
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"preset": "jest-config-link",
|
||||
"setupFiles": ["<rootDir>/src/setup.test.ts"]
|
||||
}
|
||||
|
|
@ -1,85 +0,0 @@
|
|||
{
|
||||
"name": "@digiresilience/metamigo-api",
|
||||
"version": "0.2.0",
|
||||
"type": "module",
|
||||
"main": "build/main/main.js",
|
||||
"author": "Abel Luck <abel@guardianproject.info>",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"@adiwajshing/keyed-db": "0.2.4",
|
||||
"@digiresilience/hapi-nextauth": "*",
|
||||
"@digiresilience/hapi-pg-promise": "*",
|
||||
"@digiresilience/metamigo-common": "*",
|
||||
"@digiresilience/metamigo-config": "*",
|
||||
"@digiresilience/metamigo-db": "*",
|
||||
"@digiresilience/montar": "*",
|
||||
"@digiresilience/node-signald": "*",
|
||||
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
|
||||
"@hapi/basic": "^7.0.2",
|
||||
"@hapi/boom": "^10.0.1",
|
||||
"@hapi/vision": "^7.0.3",
|
||||
"@hapi/wreck": "^18.0.1",
|
||||
"@hapipal/schmervice": "^3.0.0",
|
||||
"@hapipal/toys": "^4.0.0",
|
||||
"blipp": "^4.0.2",
|
||||
"camelcase-keys": "^9.1.3",
|
||||
"expiry-map": "^2.0.0",
|
||||
"fluent-ffmpeg": "^2.1.2",
|
||||
"graphile-migrate": "^1.4.1",
|
||||
"graphile-worker": "^0.13.0",
|
||||
"hapi-auth-bearer-token": "^8.0.0",
|
||||
"hapi-auth-jwt2": "^10.5.1",
|
||||
"hapi-swagger": "^17.2.1",
|
||||
"joi": "^17.12.2",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"jwks-rsa": "^3.1.0",
|
||||
"long": "^5.2.3",
|
||||
"p-memoize": "^7.1.1",
|
||||
"pg": "^8.11.3",
|
||||
"pg-monitor": "^2.0.0",
|
||||
"pg-promise": "^11.5.4",
|
||||
"postgraphile": "4.12.3",
|
||||
"postgraphile-plugin-connection-filter": "^2.3.0",
|
||||
"remeda": "^1.46.2",
|
||||
"twilio": "^4.23.0",
|
||||
"typeorm": "^0.3.20",
|
||||
"@whiskeysockets/baileys": "^6.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/long": "^4.0.2",
|
||||
"@types/node": "*",
|
||||
"babel-preset-link": "*",
|
||||
"camelcase-keys": "^9.1.3",
|
||||
"eslint-config-link": "*",
|
||||
"jest-config-link": "*",
|
||||
"nodemon": "^3.1.0",
|
||||
"pg-monitor": "^2.0.0",
|
||||
"pino-pretty": "^10.3.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsc-watch": "^6.0.4",
|
||||
"tsconfig-link": "*",
|
||||
"typedoc": "^0.25.11",
|
||||
"typescript": "^5.3.3"
|
||||
},
|
||||
"nodemonConfig": {
|
||||
"ignore": [
|
||||
"docs/*"
|
||||
],
|
||||
"ext": "ts,json,js"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"test": "JEST_CIRCUS=1 jest --coverage --forceExit --detectOpenHandles --reporters=default --reporters=jest-junit",
|
||||
"fmt": "prettier \"src/**/*.ts\" --write",
|
||||
"lint": "eslint src --ext .ts",
|
||||
"lint-fmt": "prettier \"src/**/*.ts\" --list-different",
|
||||
"fix:lint": "eslint src --ext .ts --fix",
|
||||
"cli": "NODE_ENV=development nodemon --unhandled-rejections=strict build/main/cli/index.js",
|
||||
"serve": "NODE_ENV=development npm run cli server",
|
||||
"serve:prod": "NODE_ENV=production npm run cli server",
|
||||
"worker": "NODE_ENV=development npm run cli worker",
|
||||
"worker:prod": "NODE_ENV=production npm run cli worker",
|
||||
"watch:build": "tsc -p tsconfig.json -w",
|
||||
"dev": "tsc-watch --build --noClear --onSuccess \"node ./build/main/main.js\""
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
import type * as Hapi from "@hapi/hapi";
|
||||
import Joi from "joi";
|
||||
import type { IAppConfig } from "../config.js";
|
||||
import * as Services from "./services/index.js";
|
||||
import * as Routes from "./routes/index.js";
|
||||
import * as Plugins from "./plugins/index.js";
|
||||
|
||||
const AppPlugin = {
|
||||
name: "App",
|
||||
async register(
|
||||
server: Hapi.Server,
|
||||
options: { config: IAppConfig },
|
||||
): Promise<void> {
|
||||
// declare our **run-time** plugin dependencies
|
||||
// these are runtime only deps, not registration time
|
||||
// ref: https://hapipal.com/best-practices/handling-plugin-dependencies
|
||||
server.dependency(["config", "hapi-pino"]);
|
||||
|
||||
server.validator(Joi as any);
|
||||
await Plugins.register(server, options.config);
|
||||
await Services.register(server);
|
||||
await Routes.register(server);
|
||||
},
|
||||
};
|
||||
|
||||
export default AppPlugin;
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
import type * as Hapi from "@hapi/hapi";
|
||||
import AuthBearer from "hapi-auth-bearer-token";
|
||||
import { IAppConfig } from "@digiresilience/metamigo-config";
|
||||
import { IMetamigoRepositories } from "@digiresilience/metamigo-common";
|
||||
|
||||
export const registerAuthBearer = async (
|
||||
server: Hapi.Server,
|
||||
config: IAppConfig
|
||||
): Promise<void> => {
|
||||
await server.register(AuthBearer);
|
||||
|
||||
server.auth.strategy("session-id-bearer-token", "bearer-access-token", {
|
||||
allowQueryToken: false,
|
||||
validate: async (
|
||||
request: Hapi.Request,
|
||||
token: string,
|
||||
h: Hapi.ResponseToolkit
|
||||
) => {
|
||||
const repos = request.db() as IMetamigoRepositories;
|
||||
const session = await repos.sessions.findBy({ sessionToken: token });
|
||||
const isValid = !!session;
|
||||
if (!isValid) return { isValid, credentials: {} };
|
||||
const user = await repos.users.findById({ id: session.userId });
|
||||
const credentials = { sessionToken: token, user };
|
||||
return { isValid, credentials };
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
@ -1,125 +0,0 @@
|
|||
import * as Boom from "@hapi/boom";
|
||||
import * as Hoek from "@hapi/hoek";
|
||||
import * as Hapi from "@hapi/hapi";
|
||||
import { promisify } from "node:util";
|
||||
import jwt from "jsonwebtoken";
|
||||
import jwksClient, { hapiJwt2KeyAsync } from "jwks-rsa";
|
||||
import type { IAppConfig } from "../../config";
|
||||
|
||||
const CF_JWT_HEADER_NAME = "cf-access-jwt-assertion";
|
||||
const CF_JWT_ALGOS = ["RS256"];
|
||||
|
||||
type VerifyFn = (token: string) => Promise<void>;
|
||||
|
||||
const verifyToken = (settings) => {
|
||||
const { audience, issuer } = settings;
|
||||
const client = jwksClient({
|
||||
jwksUri: `${issuer}/cdn-cgi/access/certs`,
|
||||
});
|
||||
|
||||
return async (token: string) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const getKey = (header: any, callback: any) => {
|
||||
client.getSigningKey(header.kid, (err, key) => {
|
||||
if (err)
|
||||
throw Boom.serverUnavailable(
|
||||
"failed to fetch cloudflare access jwks"
|
||||
);
|
||||
callback(undefined, key?.getPublicKey());
|
||||
});
|
||||
};
|
||||
|
||||
const opts = {
|
||||
algorithms: CF_JWT_ALGOS,
|
||||
audience,
|
||||
issuer,
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return (promisify(jwt.verify) as any)(token, getKey, opts);
|
||||
};
|
||||
};
|
||||
|
||||
const handleCfJwt =
|
||||
(verify: VerifyFn) =>
|
||||
async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
|
||||
const token = request.headers[CF_JWT_HEADER_NAME];
|
||||
if (token) {
|
||||
try {
|
||||
await verify(token);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return Boom.unauthorized("invalid cloudflare access token");
|
||||
}
|
||||
}
|
||||
|
||||
return h.continue;
|
||||
};
|
||||
|
||||
const defaultOpts = {
|
||||
issuer: undefined,
|
||||
audience: undefined,
|
||||
strategyName: "clouflareaccess",
|
||||
validate: undefined,
|
||||
};
|
||||
|
||||
const cfJwtRegister = async (
|
||||
server: Hapi.Server,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
options: any
|
||||
): Promise<void> => {
|
||||
server.dependency(["hapi-auth-jwt2"]);
|
||||
const settings = Hoek.applyToDefaults(defaultOpts, options);
|
||||
const verify = verifyToken(settings);
|
||||
|
||||
const { validate, strategyName, audience, issuer } = settings;
|
||||
server.ext("onPreAuth", handleCfJwt(verify));
|
||||
|
||||
if (!strategyName) {
|
||||
throw new Error("Missing strategyName for cloudflare-jwt hapi plugin!");
|
||||
}
|
||||
|
||||
server.auth.strategy(strategyName, "jwt", {
|
||||
key: hapiJwt2KeyAsync({
|
||||
jwksUri: `${issuer}/cdn-cgi/access/certs`,
|
||||
}),
|
||||
cookieKey: false,
|
||||
urlKey: false,
|
||||
headerKey: CF_JWT_HEADER_NAME,
|
||||
validate,
|
||||
verifyOptions: {
|
||||
audience,
|
||||
issuer,
|
||||
algorithms: ["RS256"],
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const registerCloudflareAccessJwt = async (
|
||||
server: Hapi.Server,
|
||||
config: IAppConfig
|
||||
): Promise<void> => {
|
||||
const { audience, domain } = config.cfaccess;
|
||||
// only enable this plugin if cloudflare access config is configured
|
||||
if (audience && domain) {
|
||||
server.log(["auth"], "cloudflare access authorization enabled");
|
||||
await server.register({
|
||||
plugin: {
|
||||
name: "cloudflare-jwt",
|
||||
version: "0.0.1",
|
||||
register: cfJwtRegister,
|
||||
},
|
||||
options: {
|
||||
issuer: `https://${domain}`,
|
||||
audience,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
validate(decoded: any, _request: any) {
|
||||
const { email, name } = decoded;
|
||||
return {
|
||||
isValid: true,
|
||||
credentials: { user: { email, name } },
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
import type * as Hapi from "@hapi/hapi";
|
||||
import NextAuthPlugin from "@digiresilience/hapi-nextauth";
|
||||
import { NextAuthAdapter } from "@digiresilience/metamigo-common";
|
||||
import { IAppConfig } from "@digiresilience/metamigo-config";
|
||||
|
||||
export const registerNextAuth = async (
|
||||
server: Hapi.Server,
|
||||
config: IAppConfig
|
||||
): Promise<void> => {
|
||||
const nextAuthAdapterFactory: any = (request: Hapi.Request) =>
|
||||
new NextAuthAdapter(request.db());
|
||||
|
||||
await server.register({
|
||||
plugin: NextAuthPlugin,
|
||||
options: {
|
||||
nextAuthAdapterFactory,
|
||||
sharedSecret: config.nextAuth.secret,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
import type * as Hapi from "@hapi/hapi";
|
||||
import { IAppConfig } from "@digiresilience/metamigo-config";
|
||||
import { postgraphile, HttpRequestHandler } from "postgraphile";
|
||||
import { getPostGraphileOptions } from "@digiresilience/metamigo-db";
|
||||
|
||||
export interface HapiPostgraphileOptions { }
|
||||
|
||||
const PostgraphilePlugin: Hapi.Plugin<HapiPostgraphileOptions> = {
|
||||
name: "postgraphilePlugin",
|
||||
version: "1.0.0",
|
||||
register: async function (server, options: HapiPostgraphileOptions) {
|
||||
const config = server.config();
|
||||
const postgraphileMiddleware: HttpRequestHandler = postgraphile(
|
||||
config.postgraphile.authConnection,
|
||||
"app_public",
|
||||
{
|
||||
...getPostGraphileOptions(),
|
||||
jwtSecret: "",
|
||||
pgSettings: async (req) => {
|
||||
const auth = (req as any).hapiAuth;
|
||||
if (auth.isAuthenticated && auth.credentials.user.userRole) {
|
||||
return {
|
||||
role: `app_${auth.credentials.user.userRole}`,
|
||||
"jwt.claims.session_id": auth.credentials.sessionToken,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
role: "app_anonymous",
|
||||
};
|
||||
}
|
||||
},
|
||||
} as any
|
||||
);
|
||||
|
||||
server.route({
|
||||
method: ["POST"],
|
||||
path: "/graphql",
|
||||
options: {
|
||||
auth: "session-id-bearer-token",
|
||||
payload: {
|
||||
parse: false, // this disables payload parsing
|
||||
output: "stream", // ensures the payload is a readable stream which postgraphile expects
|
||||
},
|
||||
},
|
||||
handler: (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const rawReq = request.raw.req as any;
|
||||
rawReq.hapiAuth = request.auth;
|
||||
postgraphileMiddleware(rawReq, request.raw.res, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
// PostGraphile responds directly to the request
|
||||
resolve(h.abandon);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export const registerPostgraphile = async (
|
||||
server: Hapi.Server,
|
||||
config: IAppConfig
|
||||
): Promise<void> => {
|
||||
await server.register({
|
||||
plugin: PostgraphilePlugin,
|
||||
options: {},
|
||||
});
|
||||
};
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
import type * as Hapi from "@hapi/hapi";
|
||||
import type { IInitOptions } from "pg-promise";
|
||||
import Schmervice from "@hapipal/schmervice";
|
||||
import { makePlugin } from "@digiresilience/hapi-pg-promise";
|
||||
|
||||
import type { IAppConfig } from "../../config";
|
||||
import { dbInitOptions, IRepositories } from "@digiresilience/metamigo-db";
|
||||
import { registerNextAuth } from "./hapi-nextauth.js";
|
||||
import { registerSwagger } from "./swagger.js";
|
||||
import { registerCloudflareAccessJwt } from "./cloudflare-jwt.js";
|
||||
import { registerAuthBearer } from "./auth-bearer.js";
|
||||
import pg from "pg-promise/typescript/pg-subset";
|
||||
|
||||
import { registerPostgraphile } from "./hapi-postgraphile.js";
|
||||
|
||||
export const register = async (
|
||||
server: Hapi.Server,
|
||||
config: IAppConfig
|
||||
): Promise<void> => {
|
||||
await server.register(Schmervice);
|
||||
|
||||
const pgpInit = dbInitOptions(config);
|
||||
const options = {
|
||||
// the only required parameter is the connection string
|
||||
connection: config.db.connection,
|
||||
// ... and the pg-promise initialization options
|
||||
pgpInit,
|
||||
};
|
||||
|
||||
await server.register([
|
||||
{
|
||||
plugin: makePlugin<IInitOptions<IRepositories, pg.IClient>>(),
|
||||
options,
|
||||
},
|
||||
]);
|
||||
|
||||
// await registerNextAuth(server, config);
|
||||
await registerSwagger(server);
|
||||
//await registerCloudflareAccessJwt(server, config);
|
||||
// await registerAuthBearer(server, config);
|
||||
await registerPostgraphile(server, config);
|
||||
};
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
import * as Inert from "@hapi/inert";
|
||||
import * as Vision from "@hapi/vision";
|
||||
import type * as Hapi from "@hapi/hapi";
|
||||
import * as HapiSwagger from "hapi-swagger";
|
||||
|
||||
export const registerSwagger = async (server: Hapi.Server): Promise<void> => {
|
||||
const swaggerOptions: HapiSwagger.RegisterOptions = {
|
||||
info: {
|
||||
title: "Metamigo API Docs",
|
||||
description: "part of CDR Link",
|
||||
version: "0.1",
|
||||
},
|
||||
// group sets of endpoints by tag
|
||||
tags: [
|
||||
{
|
||||
name: "users",
|
||||
description: "API for Users",
|
||||
},
|
||||
],
|
||||
documentationRouteTags: ["swagger"],
|
||||
documentationPath: "/api-docs",
|
||||
};
|
||||
|
||||
await server.register([
|
||||
{ plugin: Inert },
|
||||
{ plugin: Vision },
|
||||
{
|
||||
plugin: HapiSwagger,
|
||||
options: swaggerOptions,
|
||||
},
|
||||
]);
|
||||
};
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
import * as Metamigo from "@digiresilience/metamigo-common";
|
||||
import Toys from "@hapipal/toys";
|
||||
|
||||
export const withDefaults = Toys.withRouteDefaults({
|
||||
options: {
|
||||
cors: true,
|
||||
auth: "session-id-bearer-token",
|
||||
validate: {
|
||||
failAction: Metamigo.validatingFailAction,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const noAuth = Toys.withRouteDefaults({
|
||||
options: {
|
||||
cors: true,
|
||||
validate: {
|
||||
failAction: Metamigo.validatingFailAction,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
import isFunction from "lodash/isFunction.js";
|
||||
import type * as Hapi from "@hapi/hapi";
|
||||
import * as UserRoutes from "./users/index.js";
|
||||
import * as VoiceRoutes from "./voice/index.js";
|
||||
import * as WhatsappRoutes from "./whatsapp/index.js";
|
||||
import * as SignalRoutes from "./signal/index.js";
|
||||
|
||||
const loadRouteIndex = async (server, index) => {
|
||||
const routes = [];
|
||||
for (const exported in index) {
|
||||
if (Object.prototype.hasOwnProperty.call(index, exported)) {
|
||||
const route = index[exported];
|
||||
routes.push(route);
|
||||
}
|
||||
}
|
||||
|
||||
routes.forEach(async (route) => {
|
||||
if (isFunction(route)) server.route(await route(server));
|
||||
else server.route(route);
|
||||
});
|
||||
};
|
||||
|
||||
export const register = async (server: Hapi.Server): Promise<void> => {
|
||||
// Load your routes here.
|
||||
// routes are loaded from the list of exported vars
|
||||
// a route file should export routes directly or an async function that returns the routes.
|
||||
loadRouteIndex(server, UserRoutes);
|
||||
loadRouteIndex(server, VoiceRoutes);
|
||||
loadRouteIndex(server, WhatsappRoutes);
|
||||
loadRouteIndex(server, SignalRoutes);
|
||||
};
|
||||
|
|
@ -1,250 +0,0 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import Joi from "joi";
|
||||
import * as Helpers from "../helpers/index.js";
|
||||
import Boom from "@hapi/boom";
|
||||
|
||||
const getSignalService = (request) => request.services("app").signaldService;
|
||||
|
||||
export const GetAllSignalBotsRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/signal/bots",
|
||||
options: {
|
||||
description: "Get all bots",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const signalService = getSignalService(request);
|
||||
const bots = await signalService.findAll();
|
||||
|
||||
if (bots) {
|
||||
// with the pino logger the first arg is an object of data to log
|
||||
// the second arg is a message
|
||||
// all other args are formated args for the msg
|
||||
request.logger.info({ bots }, "Retrieved bot(s) at %s", new Date());
|
||||
|
||||
return { bots };
|
||||
}
|
||||
|
||||
return _h.response().code(204);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const GetBotsRoute = Helpers.noAuth({
|
||||
method: "get",
|
||||
path: "/api/signal/bots/{token}",
|
||||
options: {
|
||||
description: "Get one bot",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { token } = request.params;
|
||||
const signalService = getSignalService(request);
|
||||
|
||||
const bot = await signalService.findByToken(token);
|
||||
|
||||
if (bot) {
|
||||
// with the pino logger the first arg is an object of data to log
|
||||
// the second arg is a message
|
||||
// all other args are formated args for the msg
|
||||
request.logger.info({ bot }, "Retrieved bot(s) at %s", new Date());
|
||||
|
||||
return bot;
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
interface MessageRequest {
|
||||
phoneNumber: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export const SendBotRoute = Helpers.noAuth({
|
||||
method: "post",
|
||||
path: "/api/signal/bots/{token}/send",
|
||||
options: {
|
||||
description: "Send a message",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { token } = request.params;
|
||||
const { phoneNumber, message } = request.payload as MessageRequest;
|
||||
const signalService = getSignalService(request);
|
||||
|
||||
const bot = await signalService.findByToken(token);
|
||||
|
||||
if (bot) {
|
||||
request.logger.info({ bot }, "Sent a message at %s", new Date());
|
||||
|
||||
await signalService.send(bot, phoneNumber, message as string);
|
||||
return _h
|
||||
.response({
|
||||
result: {
|
||||
recipient: phoneNumber,
|
||||
timestamp: new Date().toISOString(),
|
||||
source: bot.phoneNumber,
|
||||
},
|
||||
})
|
||||
.code(200); // temp
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
interface ResetSessionRequest {
|
||||
phoneNumber: string;
|
||||
}
|
||||
|
||||
export const ResetSessionBotRoute = Helpers.noAuth({
|
||||
method: "post",
|
||||
path: "/api/signal/bots/{token}/resetSession",
|
||||
options: {
|
||||
description: "Reset a session with another user",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { token } = request.params;
|
||||
const { phoneNumber } = request.payload as ResetSessionRequest;
|
||||
const signalService = getSignalService(request);
|
||||
|
||||
const bot = await signalService.findByToken(token);
|
||||
|
||||
if (bot) {
|
||||
await signalService.resetSession(bot, phoneNumber);
|
||||
return _h
|
||||
.response({
|
||||
result: {
|
||||
recipient: phoneNumber,
|
||||
timestamp: new Date().toISOString(),
|
||||
source: bot.phoneNumber,
|
||||
},
|
||||
})
|
||||
.code(200); // temp
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const ReceiveBotRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/signal/bots/{token}/receive",
|
||||
options: {
|
||||
description: "Receive messages",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { token } = request.params;
|
||||
const signalService = getSignalService(request);
|
||||
|
||||
const bot = await signalService.findByToken(token);
|
||||
|
||||
if (bot) {
|
||||
request.logger.info({ bot }, "Received messages at %s", new Date());
|
||||
|
||||
return signalService.receive(bot);
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const RegisterBotRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/signal/bots/{id}/register",
|
||||
options: {
|
||||
description: "Register a bot",
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const signalService = getSignalService(request);
|
||||
const { code } = request.query;
|
||||
|
||||
const bot = await signalService.findById(id);
|
||||
if (!bot) throw Boom.notFound("Bot not found");
|
||||
|
||||
try {
|
||||
request.logger.info({ bot }, "Create bot at %s", new Date());
|
||||
await signalService.register(bot, code);
|
||||
return h.response(bot).code(200);
|
||||
} catch (error) {
|
||||
return h.response().code(error.code);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
interface BotRequest {
|
||||
phoneNumber: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export const CreateBotRoute = Helpers.withDefaults({
|
||||
method: "post",
|
||||
path: "/api/signal/bots",
|
||||
options: {
|
||||
description: "Register a bot",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { phoneNumber, description } = request.payload as BotRequest;
|
||||
const signalService = getSignalService(request);
|
||||
console.log("request.auth.credentials:", request.auth.credentials);
|
||||
|
||||
const bot = await signalService.create(
|
||||
phoneNumber,
|
||||
description,
|
||||
request.auth.credentials.email as string
|
||||
);
|
||||
if (bot) {
|
||||
request.logger.info({ bot }, "Create bot at %s", new Date());
|
||||
return bot;
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const RequestCodeRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/signal/bots/{id}/requestCode",
|
||||
options: {
|
||||
description: "Register a bot",
|
||||
validate: {
|
||||
params: Joi.object({
|
||||
id: Joi.string().uuid().required(),
|
||||
}),
|
||||
query: Joi.object({
|
||||
mode: Joi.string().valid("sms", "voice").required(),
|
||||
captcha: Joi.string(),
|
||||
}),
|
||||
},
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { mode, captcha } = request.query;
|
||||
const signalService = getSignalService(request);
|
||||
|
||||
const bot = await signalService.findById(id);
|
||||
|
||||
if (!bot) {
|
||||
throw Boom.notFound("Bot not found");
|
||||
}
|
||||
|
||||
try {
|
||||
if (mode === "sms") {
|
||||
await signalService.requestSMSVerification(bot, captcha);
|
||||
} else if (mode === "voice") {
|
||||
await signalService.requestVoiceVerification(bot, captcha);
|
||||
}
|
||||
|
||||
return h.response().code(200);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
if (error.name === "CaptchaRequiredException") {
|
||||
return h.response().code(402);
|
||||
}
|
||||
|
||||
if (error.code) {
|
||||
return h.response().code(error.code);
|
||||
}
|
||||
|
||||
return h.response().code(500);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
import Joi from "joi";
|
||||
import * as Hapi from "@hapi/hapi";
|
||||
import {
|
||||
UserRecord,
|
||||
crudRoutesFor,
|
||||
CrudControllerBase,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
import * as RouteHelpers from "../helpers/index.js";
|
||||
|
||||
class UserRecordController extends CrudControllerBase(UserRecord) {}
|
||||
|
||||
const validator = (): Record<string, Hapi.RouteOptionsValidate> => ({
|
||||
create: {
|
||||
payload: Joi.object({
|
||||
name: Joi.string().required(),
|
||||
email: Joi.string().email().required(),
|
||||
emailVerified: Joi.string().isoDate().required(),
|
||||
createdBy: Joi.string().required(),
|
||||
avatar: Joi.string()
|
||||
.uri({ scheme: ["http", "https"] })
|
||||
.optional(),
|
||||
userRole: Joi.string().optional(),
|
||||
isActive: Joi.boolean().optional(),
|
||||
}).label("UserCreate"),
|
||||
},
|
||||
updateById: {
|
||||
params: {
|
||||
userId: Joi.string().uuid().required(),
|
||||
},
|
||||
payload: Joi.object({
|
||||
name: Joi.string().optional(),
|
||||
email: Joi.string().email().optional(),
|
||||
emailVerified: Joi.string().isoDate().optional(),
|
||||
createdBy: Joi.boolean().optional(),
|
||||
avatar: Joi.string()
|
||||
.uri({ scheme: ["http", "https"] })
|
||||
.optional(),
|
||||
userRole: Joi.string().optional(),
|
||||
isActive: Joi.boolean().optional(),
|
||||
createdAt: Joi.string().isoDate().optional(),
|
||||
updatedAt: Joi.string().isoDate().optional(),
|
||||
}).label("UserUpdate"),
|
||||
},
|
||||
deleteById: {
|
||||
params: {
|
||||
userId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
getById: {
|
||||
params: {
|
||||
userId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const UserRoutes = RouteHelpers.withDefaults(
|
||||
crudRoutesFor(
|
||||
"user",
|
||||
"/api/users",
|
||||
new UserRecordController("users", "userId"),
|
||||
"userId",
|
||||
validator()
|
||||
)
|
||||
);
|
||||
|
|
@ -1,125 +0,0 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import Joi from "joi";
|
||||
import * as Boom from "@hapi/boom";
|
||||
import * as R from "remeda";
|
||||
import * as Helpers from "../helpers/index.js";
|
||||
import Twilio from "twilio";
|
||||
import {
|
||||
crudRoutesFor,
|
||||
CrudControllerBase,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
import { VoiceLineRecord, SavedVoiceLine } from "@digiresilience/metamigo-db";
|
||||
|
||||
const TwilioHandlers = {
|
||||
async freeNumbers(provider, request: Hapi.Request) {
|
||||
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
|
||||
const client = Twilio(apiKeySid, apiKeySecret, {
|
||||
accountSid,
|
||||
});
|
||||
const numbers = R.pipe(
|
||||
await client.incomingPhoneNumbers.list({ limit: 100 }),
|
||||
R.filter((n) => n.capabilities.voice),
|
||||
R.map(R.pick(["sid", "phoneNumber"]))
|
||||
);
|
||||
const numberSids: any = R.map(numbers, R.prop("sid"));
|
||||
const voiceLineRepo = request.db().voiceLines;
|
||||
const voiceLines: SavedVoiceLine[] =
|
||||
await voiceLineRepo.findAllByProviderLineSids(numberSids);
|
||||
const voiceLineSids = new Set(R.map(voiceLines, R.prop("providerLineSid")));
|
||||
|
||||
return R.pipe(
|
||||
numbers,
|
||||
R.reject((n) => voiceLineSids.has(n.sid as any)),
|
||||
R.map((n) => ({ id: n.sid, name: n.phoneNumber }))
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export const VoiceProviderRoutes = Helpers.withDefaults([
|
||||
{
|
||||
method: "GET",
|
||||
path: "/api/voice/providers/{providerId}/freeNumbers",
|
||||
options: {
|
||||
description:
|
||||
"get a list of the incoming numbers for a provider account that aren't assigned to a voice line",
|
||||
validate: {
|
||||
params: {
|
||||
providerId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { providerId } = request.params;
|
||||
const voiceProvidersRepo = request.db().voiceProviders;
|
||||
const provider = await voiceProvidersRepo.findById(providerId);
|
||||
if (!provider) return Boom.notFound();
|
||||
switch (provider.kind) {
|
||||
case "TWILIO": {
|
||||
return TwilioHandlers.freeNumbers(provider, request);
|
||||
}
|
||||
|
||||
default: {
|
||||
return Boom.badImplementation();
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
class VoiceLineRecordController extends CrudControllerBase(VoiceLineRecord) {}
|
||||
|
||||
const validator = (): Record<string, Hapi.RouteOptionsValidate> => ({
|
||||
create: {
|
||||
payload: Joi.object({
|
||||
providerType: Joi.string().required(),
|
||||
providerId: Joi.string().required(),
|
||||
number: Joi.string().required(),
|
||||
language: Joi.string().required(),
|
||||
voice: Joi.string().required(),
|
||||
promptText: Joi.string().optional(),
|
||||
promptRecording: Joi.binary()
|
||||
.encoding("base64")
|
||||
.max(50 * 1000 * 1000)
|
||||
.optional(),
|
||||
}).label("VoiceLineCreate"),
|
||||
},
|
||||
updateById: {
|
||||
params: {
|
||||
id: Joi.string().uuid().required(),
|
||||
},
|
||||
payload: Joi.object({
|
||||
providerType: Joi.string().optional(),
|
||||
providerId: Joi.string().optional(),
|
||||
number: Joi.string().optional(),
|
||||
language: Joi.string().optional(),
|
||||
voice: Joi.string().optional(),
|
||||
promptText: Joi.string().optional(),
|
||||
promptRecording: Joi.binary()
|
||||
.encoding("base64")
|
||||
.max(50 * 1000 * 1000)
|
||||
.optional(),
|
||||
}).label("VoiceLineUpdate"),
|
||||
},
|
||||
deleteById: {
|
||||
params: {
|
||||
id: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
getById: {
|
||||
params: {
|
||||
id: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const VoiceLineRoutes = Helpers.withDefaults(
|
||||
crudRoutesFor(
|
||||
"voice-line",
|
||||
"/api/voice/voice-line",
|
||||
new VoiceLineRecordController("voiceLines", "id"),
|
||||
"id",
|
||||
validator()
|
||||
)
|
||||
);
|
||||
|
||||
export * from "./twilio/index.js";
|
||||
|
|
@ -1,230 +0,0 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import Joi from "joi";
|
||||
import * as Boom from "@hapi/boom";
|
||||
import Twilio from "twilio";
|
||||
import { SavedVoiceProvider } from "@digiresilience/metamigo-db";
|
||||
import pMemoize from "p-memoize";
|
||||
import ExpiryMap from "expiry-map";
|
||||
import ms from "ms";
|
||||
import * as Helpers from "../../helpers/index.js";
|
||||
import workerUtils from "../../../../worker-utils.js";
|
||||
|
||||
const queueRecording = async (meta) =>
|
||||
workerUtils.addJob("twilio-recording", meta, { jobKey: meta.callSid });
|
||||
|
||||
const twilioClientFor = (provider: SavedVoiceProvider): Twilio.Twilio => {
|
||||
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
|
||||
if (!accountSid || !apiKeySid || !apiKeySecret)
|
||||
throw new Error(
|
||||
`twilio provider ${provider.name} does not have credentials`
|
||||
);
|
||||
|
||||
return Twilio(apiKeySid, apiKeySecret, {
|
||||
accountSid,
|
||||
});
|
||||
};
|
||||
|
||||
const _getOrCreateTTSTestApplication = async (
|
||||
url,
|
||||
name,
|
||||
client: Twilio.Twilio
|
||||
) => {
|
||||
const application = await client.applications.list({ friendlyName: name });
|
||||
|
||||
if (application[0] && application[0].voiceUrl === url) {
|
||||
return application[0];
|
||||
}
|
||||
|
||||
return client.applications.create({
|
||||
voiceMethod: "POST",
|
||||
voiceUrl: url,
|
||||
friendlyName: name,
|
||||
});
|
||||
};
|
||||
|
||||
const cache = new ExpiryMap(ms("1h"));
|
||||
const getOrCreateTTSTestApplication = pMemoize(_getOrCreateTTSTestApplication, {
|
||||
cache,
|
||||
});
|
||||
|
||||
export const TwilioRoutes = Helpers.noAuth([
|
||||
{
|
||||
method: "get",
|
||||
path: "/api/voice/twilio/prompt/{voiceLineId}",
|
||||
options: {
|
||||
description: "download the mp3 file to play as a prompt for the user",
|
||||
validate: {
|
||||
params: {
|
||||
voiceLineId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { voiceLineId } = request.params;
|
||||
const voiceLine = await request
|
||||
.db()
|
||||
.voiceLines.findById({ id: voiceLineId });
|
||||
|
||||
if (!voiceLine) return Boom.notFound();
|
||||
if (!voiceLine.audioPromptEnabled) return Boom.badRequest();
|
||||
|
||||
const mp3 = voiceLine.promptAudio["audio/mpeg"];
|
||||
if (!mp3) {
|
||||
return Boom.serverUnavailable();
|
||||
}
|
||||
|
||||
return h
|
||||
.response(Buffer.from(mp3, "base64"))
|
||||
.header("Content-Type", "audio/mpeg")
|
||||
.header("Content-Disposition", "attachment; filename=prompt.mp3");
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
method: "post",
|
||||
path: "/api/voice/twilio/record/{voiceLineId}",
|
||||
options: {
|
||||
description: "webhook for twilio to handle an incoming call",
|
||||
validate: {
|
||||
params: {
|
||||
voiceLineId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { voiceLineId } = request.params;
|
||||
const { To } = request.payload as { To: string; };
|
||||
const voiceLine = await request.db().voiceLines.findBy({ number: To });
|
||||
if (!voiceLine) return Boom.notFound();
|
||||
if (voiceLine.id !== voiceLineId) return Boom.badRequest();
|
||||
|
||||
const frontendUrl = request.server.config().frontend.url;
|
||||
const useTextPrompt = !voiceLine.audioPromptEnabled;
|
||||
|
||||
const twiml = new Twilio.twiml.VoiceResponse();
|
||||
if (useTextPrompt) {
|
||||
let prompt = voiceLine.promptText;
|
||||
if (!prompt || prompt.length === 0)
|
||||
prompt =
|
||||
"The grabadora text prompt is unconfigured. Please set a prompt in the administration screen.";
|
||||
twiml.say(
|
||||
{
|
||||
language: voiceLine.language as any,
|
||||
voice: voiceLine.voice as any,
|
||||
},
|
||||
prompt
|
||||
);
|
||||
} else {
|
||||
const promptUrl = `${frontendUrl}/api/v1/voice/twilio/prompt/${voiceLineId}`;
|
||||
twiml.play({ loop: 1 }, promptUrl);
|
||||
}
|
||||
|
||||
twiml.record({
|
||||
playBeep: true,
|
||||
finishOnKey: "1",
|
||||
recordingStatusCallback: `${frontendUrl}/api/v1/voice/twilio/recording-ready/${voiceLineId}`,
|
||||
});
|
||||
return twiml.toString();
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
method: "post",
|
||||
path: "/api/voice/twilio/recording-ready/{voiceLineId}",
|
||||
options: {
|
||||
description: "webhook for twilio to handle a recording",
|
||||
validate: {
|
||||
params: {
|
||||
voiceLineId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { voiceLineId } = request.params;
|
||||
const voiceLine = await request
|
||||
.db()
|
||||
.voiceLines.findById({ id: voiceLineId });
|
||||
if (!voiceLine) return Boom.notFound();
|
||||
|
||||
const { AccountSid, RecordingSid, CallSid } = request.payload as {
|
||||
AccountSid: string;
|
||||
RecordingSid: string;
|
||||
CallSid: string;
|
||||
};
|
||||
|
||||
await queueRecording({
|
||||
voiceLineId,
|
||||
accountSid: AccountSid,
|
||||
callSid: CallSid,
|
||||
recordingSid: RecordingSid,
|
||||
});
|
||||
return h.response().code(203);
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
method: "post",
|
||||
path: "/api/voice/twilio/text-to-speech/{providerId}",
|
||||
options: {
|
||||
description: "webook for twilio to test the twilio text-to-speech",
|
||||
validate: {
|
||||
params: {
|
||||
providerId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { language, voice, prompt } = request.payload as {
|
||||
language: any;
|
||||
voice: any;
|
||||
prompt: string;
|
||||
};
|
||||
const twiml = new Twilio.twiml.VoiceResponse();
|
||||
twiml.say({ language, voice }, prompt);
|
||||
return twiml.toString();
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
method: "get",
|
||||
path: "/api/voice/twilio/text-to-speech-token/{providerId}",
|
||||
options: {
|
||||
description:
|
||||
"generates a one time token to test the twilio text-to-speech",
|
||||
validate: {
|
||||
params: {
|
||||
providerId: Joi.string().uuid().required(),
|
||||
},
|
||||
},
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { providerId } = request.params as { providerId: string; };
|
||||
const provider: SavedVoiceProvider = await request
|
||||
.db()
|
||||
.voiceProviders.findById({ id: providerId });
|
||||
if (!provider) return Boom.notFound();
|
||||
|
||||
const frontendUrl = request.server.config().frontend.url;
|
||||
const url = `${frontendUrl}/api/v1/voice/twilio/text-to-speech/${providerId}`;
|
||||
const name = `Grabadora text-to-speech tester: ${providerId}`;
|
||||
const app = await getOrCreateTTSTestApplication(
|
||||
url,
|
||||
name,
|
||||
twilioClientFor(provider)
|
||||
);
|
||||
|
||||
const { accountSid, apiKeySecret, apiKeySid } = provider.credentials;
|
||||
const token = new Twilio.jwt.AccessToken(
|
||||
accountSid,
|
||||
apiKeySid,
|
||||
apiKeySecret,
|
||||
{ identity: "tts-test" }
|
||||
);
|
||||
|
||||
const grant = new Twilio.jwt.AccessToken.VoiceGrant({
|
||||
outgoingApplicationSid: app.sid,
|
||||
incomingAllow: true,
|
||||
});
|
||||
token.addGrant(grant);
|
||||
return h.response({
|
||||
token: token.toJwt(),
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
|
@ -1,215 +0,0 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import * as Helpers from "../helpers/index.js";
|
||||
import Boom from "@hapi/boom";
|
||||
|
||||
export const GetAllWhatsappBotsRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/whatsapp/bots",
|
||||
options: {
|
||||
description: "Get all bots",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { whatsappService } = request.services("app");
|
||||
|
||||
const bots = await whatsappService.findAll();
|
||||
|
||||
if (bots) {
|
||||
// with the pino logger the first arg is an object of data to log
|
||||
// the second arg is a message
|
||||
// all other args are formated args for the msg
|
||||
request.logger.info({ bots }, "Retrieved bot(s) at %s", new Date());
|
||||
|
||||
return { bots };
|
||||
}
|
||||
|
||||
return _h.response().code(204);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const GetBotsRoute = Helpers.noAuth({
|
||||
method: "get",
|
||||
path: "/api/whatsapp/bots/{token}",
|
||||
options: {
|
||||
description: "Get one bot",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { token } = request.params;
|
||||
const { whatsappService } = request.services("app");
|
||||
|
||||
const bot = await whatsappService.findByToken(token);
|
||||
|
||||
if (bot) {
|
||||
// with the pino logger the first arg is an object of data to log
|
||||
// the second arg is a message
|
||||
// all other args are formated args for the msg
|
||||
request.logger.info({ bot }, "Retrieved bot(s) at %s", new Date());
|
||||
|
||||
return bot;
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
interface MessageRequest {
|
||||
phoneNumber: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export const SendBotRoute = Helpers.noAuth({
|
||||
method: "post",
|
||||
path: "/api/whatsapp/bots/{token}/send",
|
||||
options: {
|
||||
description: "Send a message",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { token } = request.params;
|
||||
const { phoneNumber, message } = request.payload as MessageRequest;
|
||||
const { whatsappService } = request.services("app");
|
||||
|
||||
const bot = await whatsappService.findByToken(token);
|
||||
|
||||
if (bot) {
|
||||
request.logger.info({ bot }, "Sent a message at %s", new Date());
|
||||
|
||||
await whatsappService.send(bot, phoneNumber, message as string);
|
||||
return _h
|
||||
.response({
|
||||
result: {
|
||||
recipient: phoneNumber,
|
||||
timestamp: new Date().toISOString(),
|
||||
source: bot.phoneNumber,
|
||||
},
|
||||
})
|
||||
.code(200); // temp
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const ReceiveBotRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/whatsapp/bots/{token}/receive",
|
||||
options: {
|
||||
description: "Receive messages",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { token } = request.params;
|
||||
const { whatsappService } = request.services("app");
|
||||
|
||||
const bot = await whatsappService.findByToken(token);
|
||||
|
||||
if (bot) {
|
||||
request.logger.info({ bot }, "Received messages at %s", new Date());
|
||||
|
||||
// temp
|
||||
const date = new Date();
|
||||
const twoDaysAgo = new Date(date.getTime());
|
||||
twoDaysAgo.setDate(date.getDate() - 2);
|
||||
return whatsappService.receive(bot, twoDaysAgo);
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const RegisterBotRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/whatsapp/bots/{id}/register",
|
||||
options: {
|
||||
description: "Register a bot",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { whatsappService } = request.services("app");
|
||||
|
||||
const bot = await whatsappService.findById(id);
|
||||
|
||||
if (bot) {
|
||||
await whatsappService.register(bot, (error: string) => {
|
||||
if (error) {
|
||||
return _h.response(error).code(500);
|
||||
}
|
||||
|
||||
request.logger.info({ bot }, "Register bot at %s", new Date());
|
||||
return _h.response().code(200);
|
||||
});
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const UnverifyBotRoute = Helpers.withDefaults({
|
||||
method: "post",
|
||||
path: "/api/whatsapp/bots/{id}/unverify",
|
||||
options: {
|
||||
description: "Unverify bot",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { whatsappService } = request.services("app");
|
||||
|
||||
const bot = await whatsappService.findById(id);
|
||||
|
||||
if (bot) {
|
||||
return whatsappService.unverify(bot);
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const RefreshBotRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/whatsapp/bots/{id}/refresh",
|
||||
options: {
|
||||
description: "Refresh messages",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { whatsappService } = request.services("app");
|
||||
|
||||
const bot = await whatsappService.findById(id);
|
||||
|
||||
if (bot) {
|
||||
request.logger.info({ bot }, "Refreshed messages at %s", new Date());
|
||||
|
||||
// await whatsappService.refresh(bot);
|
||||
return;
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
interface BotRequest {
|
||||
phoneNumber: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export const CreateBotRoute = Helpers.withDefaults({
|
||||
method: "post",
|
||||
path: "/api/whatsapp/bots",
|
||||
options: {
|
||||
description: "Register a bot",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { phoneNumber, description } = request.payload as BotRequest;
|
||||
const { whatsappService } = request.services("app");
|
||||
console.log("request.auth.credentials:", request.auth.credentials);
|
||||
|
||||
const bot = await whatsappService.create(
|
||||
phoneNumber,
|
||||
description,
|
||||
request.auth.credentials.email as string
|
||||
);
|
||||
if (bot) {
|
||||
request.logger.info({ bot }, "Register bot at %s", new Date());
|
||||
return bot;
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
import type * as Hapi from "@hapi/hapi";
|
||||
import SettingsService from "./settings.js";
|
||||
import WhatsappService from "./whatsapp.js";
|
||||
import SignaldService from "./signald.js";
|
||||
|
||||
export const register = async (server: Hapi.Server): Promise<void> => {
|
||||
// register your services here
|
||||
// don't forget to add them to the AppServices interface in ../types/index.ts
|
||||
server.registerService(SettingsService);
|
||||
server.registerService(WhatsappService);
|
||||
server.registerService(SignaldService);
|
||||
};
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import * as Schmervice from "@hapipal/schmervice";
|
||||
import { settingInfo, SettingsService } from "@digiresilience/metamigo-db";
|
||||
|
||||
export const VoicemailPrompt = settingInfo<string>("voicemail-prompt");
|
||||
export const VoicemailMinLength = settingInfo<number>("voicemail-min-length");
|
||||
export const VoicemailUseTextPrompt = settingInfo<boolean>(
|
||||
"voicemail-use-text-prompt"
|
||||
);
|
||||
|
||||
export { ISettingsService } from "@digiresilience/metamigo-db";
|
||||
const service = (server: Hapi.Server): Schmervice.ServiceFunctionalInterface =>
|
||||
SettingsService(server.db().settings);
|
||||
|
||||
export default service;
|
||||
|
|
@ -1,231 +0,0 @@
|
|||
import { Server } from "@hapi/hapi";
|
||||
import { Service } from "@hapipal/schmervice";
|
||||
import { promises as fs } from "node:fs";
|
||||
import {
|
||||
SignaldAPI,
|
||||
SendResponsev1,
|
||||
IncomingMessagev1,
|
||||
ClientMessageWrapperv1,
|
||||
} from "@digiresilience/node-signald";
|
||||
import { SavedSignalBot as Bot } from "@digiresilience/metamigo-db";
|
||||
import workerUtils from "../../worker-utils.js";
|
||||
|
||||
export default class SignaldService extends Service {
|
||||
signald: SignaldAPI;
|
||||
subscriptions: Set<string>;
|
||||
|
||||
constructor(server: Server, options: never) {
|
||||
super(server, options);
|
||||
|
||||
if (this.server.config().signald.enabled) {
|
||||
this.signald = new SignaldAPI();
|
||||
this.signald.setLogger((level, msg, extra?) => {
|
||||
this.server.logger[level]({ extra }, msg);
|
||||
});
|
||||
this.subscriptions = new Set();
|
||||
}
|
||||
}
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
if (this.server.config().signald.enabled && this.signald) {
|
||||
this.setupListeners();
|
||||
this.connect();
|
||||
}
|
||||
}
|
||||
|
||||
async teardown(): Promise<void> {
|
||||
if (this.server.config().signald.enabled && this.signald)
|
||||
this.signald.disconnect();
|
||||
}
|
||||
|
||||
private connect() {
|
||||
const { enabled, socket } = this.server.config().signald;
|
||||
if (!enabled) return;
|
||||
this.signald.connectWithBackoff(socket);
|
||||
}
|
||||
|
||||
private async onConnected() {
|
||||
await this.subscribeAll();
|
||||
}
|
||||
|
||||
private setupListeners() {
|
||||
this.signald.on("transport_error", async (error) => {
|
||||
this.server.logger.info({ error }, "signald transport error");
|
||||
});
|
||||
this.signald.on("transport_connected", async () => {
|
||||
this.onConnected();
|
||||
});
|
||||
this.signald.on(
|
||||
"transport_received_payload",
|
||||
async (payload: ClientMessageWrapperv1) => {
|
||||
this.server.logger.debug({ payload }, "signald payload received");
|
||||
if (payload.type === "IncomingMessage") {
|
||||
this.receiveMessage(payload.data);
|
||||
}
|
||||
}
|
||||
);
|
||||
this.signald.on("transport_sent_payload", async (payload) => {
|
||||
this.server.logger.debug({ payload }, "signald payload sent");
|
||||
});
|
||||
}
|
||||
|
||||
private async subscribeAll() {
|
||||
const result = await this.signald.listAccounts();
|
||||
const accounts = result.accounts.map((account) => account.address.number);
|
||||
await Promise.all(
|
||||
accounts.map(async (account) => {
|
||||
await this.signald.subscribe(account);
|
||||
this.subscriptions.add(account);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private async unsubscribeAll() {
|
||||
await Promise.all(
|
||||
[...this.subscriptions].map(async (account) => {
|
||||
await this.signald.unsubscribe(account);
|
||||
this.subscriptions.delete(account);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async create(
|
||||
phoneNumber: string,
|
||||
description: string,
|
||||
email: string
|
||||
): Promise<Bot> {
|
||||
const db = this.server.db();
|
||||
const user = await db.users.findBy({ email });
|
||||
const row = await db.signalBots.insert({
|
||||
phoneNumber,
|
||||
description,
|
||||
userId: user.id,
|
||||
});
|
||||
return row;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Bot[]> {
|
||||
const db = this.server.db();
|
||||
return db.signalBots.findAll();
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Bot> {
|
||||
const db = this.server.db();
|
||||
return db.signalBots.findById({ id });
|
||||
}
|
||||
|
||||
async findByToken(token: string): Promise<Bot> {
|
||||
const db = this.server.db();
|
||||
return db.signalBots.findBy({ token });
|
||||
}
|
||||
|
||||
async register(bot: Bot, code: string): Promise<void> {
|
||||
const address = await this.signald.verify(bot.phoneNumber, code);
|
||||
this.server.db().signalBots.updateAuthInfo(bot, address.address.uuid);
|
||||
}
|
||||
|
||||
async send(
|
||||
bot: Bot,
|
||||
phoneNumber: string,
|
||||
message: string
|
||||
): Promise<SendResponsev1> {
|
||||
this.server.logger.debug(
|
||||
{ us: bot.phoneNumber, them: phoneNumber, message },
|
||||
"signald send"
|
||||
);
|
||||
return this.signald.send(
|
||||
bot.phoneNumber,
|
||||
{ number: phoneNumber },
|
||||
undefined,
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
async resetSession(bot: Bot, phoneNumber: string): Promise<SendResponsev1> {
|
||||
return this.signald.resetSession(bot.phoneNumber, {
|
||||
number: phoneNumber,
|
||||
});
|
||||
}
|
||||
|
||||
async requestVoiceVerification(bot: Bot, captcha?: string): Promise<void> {
|
||||
this.server.logger.debug(
|
||||
{ number: bot.phoneNumber, captcha },
|
||||
"requesting voice verification for"
|
||||
);
|
||||
|
||||
await this.signald.register(bot.phoneNumber, true, captcha);
|
||||
}
|
||||
|
||||
async requestSMSVerification(bot: Bot, captcha?: string): Promise<void> {
|
||||
this.server.logger.debug(
|
||||
{ number: bot.phoneNumber, captcha },
|
||||
"requesting sms verification for"
|
||||
);
|
||||
await this.signald.register(bot.phoneNumber, false, captcha);
|
||||
}
|
||||
|
||||
private async receiveMessage(message: IncomingMessagev1) {
|
||||
const { account } = message;
|
||||
if (!account) {
|
||||
this.server.logger.debug({ message }, "invalid message received");
|
||||
this.server.logger.error("invalid message received");
|
||||
}
|
||||
|
||||
const bot = await this.server
|
||||
.db()
|
||||
.signalBots.findBy({ phoneNumber: account });
|
||||
if (!bot) {
|
||||
this.server.logger.info("message received for unknown bot", {
|
||||
account,
|
||||
message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await this.queueMessage(bot, message);
|
||||
}
|
||||
|
||||
private async getAttachmentInfo(dataMessage: IncomingMessagev1) {
|
||||
if (dataMessage.attachments?.length > 0) {
|
||||
const attachmentInfo = dataMessage.attachments[0];
|
||||
const buffer = await fs.readFile(attachmentInfo.storedFilename);
|
||||
const attachment = buffer.toString("base64");
|
||||
const mimetype = attachmentInfo.contentType ?? "application/octet-stream";
|
||||
const filename = attachmentInfo.customFilename ?? "unknown-filename";
|
||||
|
||||
return { attachment, mimetype, filename };
|
||||
}
|
||||
|
||||
return { attachment: undefined, mimetype: undefined, filename: undefined };
|
||||
}
|
||||
|
||||
private async queueMessage(bot: Bot, message: IncomingMessagev1) {
|
||||
const { timestamp, account, data_message: dataMessage } = message;
|
||||
if (!dataMessage?.body && !dataMessage?.attachments) {
|
||||
this.server.logger.info({ message }, "message received with no content");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!timestamp || !account) {
|
||||
this.server.logger.debug({ message }, "invalid message received");
|
||||
}
|
||||
|
||||
const { attachment, mimetype, filename } = await this.getAttachmentInfo(
|
||||
dataMessage
|
||||
);
|
||||
|
||||
const receivedMessage = {
|
||||
message,
|
||||
botId: bot.id,
|
||||
botPhoneNumber: bot.phoneNumber,
|
||||
attachment,
|
||||
mimetype,
|
||||
filename,
|
||||
};
|
||||
|
||||
workerUtils.addJob("signald-message", receivedMessage, {
|
||||
jobKey: `signal-bot-${bot.id}-${timestamp}`,
|
||||
queueName: `signal-bot-${bot.id}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,297 +0,0 @@
|
|||
/* eslint-disable unicorn/no-abusive-eslint-disable */
|
||||
/* eslint-disable */
|
||||
import { Server } from "@hapi/hapi";
|
||||
import { Service } from "@hapipal/schmervice";
|
||||
import { SavedWhatsappBot as Bot } from "@digiresilience/metamigo-db";
|
||||
import makeWASocket, {
|
||||
DisconnectReason,
|
||||
proto,
|
||||
downloadContentFromMessage,
|
||||
MediaType,
|
||||
fetchLatestBaileysVersion,
|
||||
isJidBroadcast,
|
||||
isJidStatusBroadcast,
|
||||
useMultiFileAuthState,
|
||||
} from "@whiskeysockets/baileys";
|
||||
import fs from "fs";
|
||||
import workerUtils from "../../worker-utils.js";
|
||||
|
||||
export type AuthCompleteCallback = (error?: string) => void;
|
||||
|
||||
export default class WhatsappService extends Service {
|
||||
connections: { [key: string]: any; } = {};
|
||||
loginConnections: { [key: string]: any; } = {};
|
||||
|
||||
static browserDescription: [string, string, string] = [
|
||||
"Metamigo",
|
||||
"Chrome",
|
||||
"2.0",
|
||||
];
|
||||
|
||||
constructor(server: Server, options: never) {
|
||||
super(server, options);
|
||||
}
|
||||
|
||||
getAuthDirectory(bot: Bot): string {
|
||||
return `/baileys/${bot.id}`;
|
||||
}
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
this.updateConnections();
|
||||
}
|
||||
|
||||
async teardown(): Promise<void> {
|
||||
this.resetConnections();
|
||||
}
|
||||
|
||||
private async sleep(ms: number): Promise<void> {
|
||||
console.log(`pausing ${ms}`);
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
private async resetConnections() {
|
||||
for (const connection of Object.values(this.connections)) {
|
||||
try {
|
||||
connection.end(null);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
this.connections = {};
|
||||
}
|
||||
|
||||
private async createConnection(
|
||||
bot: Bot,
|
||||
server: Server,
|
||||
options: any,
|
||||
authCompleteCallback?: any
|
||||
) {
|
||||
const directory = this.getAuthDirectory(bot);
|
||||
const { state, saveCreds } = await useMultiFileAuthState(directory);
|
||||
const msgRetryCounterMap: any = {};
|
||||
const socket = makeWASocket({
|
||||
...options,
|
||||
auth: state,
|
||||
msgRetryCounterMap,
|
||||
shouldIgnoreJid: (jid) =>
|
||||
isJidBroadcast(jid) || isJidStatusBroadcast(jid),
|
||||
});
|
||||
let pause = 5000;
|
||||
|
||||
socket.ev.process(async (events) => {
|
||||
if (events["connection.update"]) {
|
||||
const update = events["connection.update"];
|
||||
const {
|
||||
connection: connectionState,
|
||||
lastDisconnect,
|
||||
qr,
|
||||
isNewLogin,
|
||||
} = update;
|
||||
if (qr) {
|
||||
console.log("got qr code");
|
||||
await this.server.db().whatsappBots.updateQR(bot, qr);
|
||||
} else if (isNewLogin) {
|
||||
console.log("got new login");
|
||||
await this.server.db().whatsappBots.updateVerified(bot, true);
|
||||
} else if (connectionState === "open") {
|
||||
console.log("opened connection");
|
||||
} else if (connectionState === "close") {
|
||||
console.log("connection closed due to ", lastDisconnect.error);
|
||||
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
|
||||
?.statusCode;
|
||||
|
||||
if (disconnectStatusCode === DisconnectReason.restartRequired) {
|
||||
console.log("reconnecting after got new login");
|
||||
const updatedBot = await this.findById(bot.id);
|
||||
await this.createConnection(updatedBot, server, options);
|
||||
authCompleteCallback?.();
|
||||
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
|
||||
console.log("reconnecting");
|
||||
await this.sleep(pause);
|
||||
pause *= 2;
|
||||
this.createConnection(bot, server, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (events["creds.update"]) {
|
||||
console.log("creds update");
|
||||
await saveCreds();
|
||||
}
|
||||
|
||||
if (events["messages.upsert"]) {
|
||||
console.log("messages upsert");
|
||||
const upsert = events["messages.upsert"];
|
||||
const { messages } = upsert;
|
||||
if (messages) {
|
||||
await this.queueUnreadMessages(bot, messages);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.connections[bot.id] = { socket, msgRetryCounterMap };
|
||||
}
|
||||
|
||||
private async updateConnections() {
|
||||
this.resetConnections();
|
||||
|
||||
const bots = await this.server.db().whatsappBots.findAll();
|
||||
for await (const bot of bots) {
|
||||
if (bot.isVerified) {
|
||||
const { version, isLatest } = await fetchLatestBaileysVersion();
|
||||
console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`);
|
||||
|
||||
await this.createConnection(bot, this.server, {
|
||||
browser: WhatsappService.browserDescription,
|
||||
printQRInTerminal: false,
|
||||
version,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async queueMessage(bot: Bot, webMessageInfo: proto.IWebMessageInfo) {
|
||||
const {
|
||||
key: { id, fromMe, remoteJid },
|
||||
message,
|
||||
messageTimestamp,
|
||||
} = webMessageInfo;
|
||||
if (!fromMe && message && remoteJid !== "status@broadcast") {
|
||||
const { audioMessage, documentMessage, imageMessage, videoMessage } =
|
||||
message;
|
||||
const isMediaMessage =
|
||||
audioMessage || documentMessage || imageMessage || videoMessage;
|
||||
|
||||
const messageContent = Object.values(message)[0];
|
||||
let messageType: MediaType;
|
||||
let attachment: string;
|
||||
let filename: string;
|
||||
let mimetype: string;
|
||||
if (isMediaMessage) {
|
||||
if (audioMessage) {
|
||||
messageType = "audio";
|
||||
filename = id + "." + audioMessage.mimetype.split("/").pop();
|
||||
mimetype = audioMessage.mimetype;
|
||||
} else if (documentMessage) {
|
||||
messageType = "document";
|
||||
filename = documentMessage.fileName;
|
||||
mimetype = documentMessage.mimetype;
|
||||
} else if (imageMessage) {
|
||||
messageType = "image";
|
||||
filename = id + "." + imageMessage.mimetype.split("/").pop();
|
||||
mimetype = imageMessage.mimetype;
|
||||
} else if (videoMessage) {
|
||||
messageType = "video";
|
||||
filename = id + "." + videoMessage.mimetype.split("/").pop();
|
||||
mimetype = videoMessage.mimetype;
|
||||
}
|
||||
|
||||
const stream = await downloadContentFromMessage(
|
||||
messageContent,
|
||||
messageType
|
||||
);
|
||||
let buffer = Buffer.from([]);
|
||||
for await (const chunk of stream) {
|
||||
buffer = Buffer.concat([buffer, chunk]);
|
||||
}
|
||||
attachment = buffer.toString("base64");
|
||||
}
|
||||
|
||||
if (messageContent || attachment) {
|
||||
const receivedMessage = {
|
||||
waMessageId: id,
|
||||
waMessage: JSON.stringify(webMessageInfo),
|
||||
waTimestamp: new Date((messageTimestamp as number) * 1000),
|
||||
attachment,
|
||||
filename,
|
||||
mimetype,
|
||||
whatsappBotId: bot.id,
|
||||
botPhoneNumber: bot.phoneNumber,
|
||||
};
|
||||
|
||||
workerUtils.addJob("whatsapp-message", receivedMessage, {
|
||||
jobKey: id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async queueUnreadMessages(
|
||||
bot: Bot,
|
||||
messages: proto.IWebMessageInfo[]
|
||||
) {
|
||||
for await (const message of messages) {
|
||||
await this.queueMessage(bot, message);
|
||||
}
|
||||
}
|
||||
|
||||
async create(
|
||||
phoneNumber: string,
|
||||
description: string,
|
||||
email: string
|
||||
): Promise<Bot> {
|
||||
const db = this.server.db();
|
||||
const user = await db.users.findBy({ email });
|
||||
const row = await db.whatsappBots.insert({
|
||||
phoneNumber,
|
||||
description,
|
||||
userId: user.id,
|
||||
});
|
||||
return row;
|
||||
}
|
||||
|
||||
async unverify(bot: Bot): Promise<Bot> {
|
||||
const directory = this.getAuthDirectory(bot);
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
return this.server.db().whatsappBots.updateVerified(bot, false);
|
||||
}
|
||||
|
||||
async remove(bot: Bot): Promise<number> {
|
||||
const directory = this.getAuthDirectory(bot);
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
return this.server.db().whatsappBots.remove(bot);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Bot[]> {
|
||||
return this.server.db().whatsappBots.findAll();
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Bot> {
|
||||
return this.server.db().whatsappBots.findById({ id });
|
||||
}
|
||||
|
||||
async findByToken(token: string): Promise<Bot> {
|
||||
return this.server.db().whatsappBots.findBy({ token });
|
||||
}
|
||||
|
||||
async register(bot: Bot, callback: AuthCompleteCallback): Promise<void> {
|
||||
const { version } = await fetchLatestBaileysVersion();
|
||||
await this.createConnection(bot, this.server, { version }, callback);
|
||||
}
|
||||
|
||||
async send(bot: Bot, phoneNumber: string, message: string): Promise<void> {
|
||||
const connection = this.connections[bot.id]?.socket;
|
||||
const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`;
|
||||
await connection.sendMessage(recipient, { text: message });
|
||||
}
|
||||
|
||||
async receiveSince(bot: Bot, lastReceivedDate: Date): Promise<void> {
|
||||
const connection = this.connections[bot.id]?.socket;
|
||||
const messages = await connection.messagesReceivedAfter(
|
||||
lastReceivedDate,
|
||||
false
|
||||
);
|
||||
for (const message of messages) {
|
||||
this.queueMessage(bot, message);
|
||||
}
|
||||
}
|
||||
|
||||
async receive(
|
||||
bot: Bot,
|
||||
_lastReceivedDate: Date
|
||||
): Promise<proto.IWebMessageInfo[]> {
|
||||
const connection = this.connections[bot.id]?.socket;
|
||||
const messages = await connection.loadAllUnreadMessages();
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
import type { IMain } from "pg-promise";
|
||||
import type { ISettingsService } from "../services/settings";
|
||||
import type WhatsappService from "../services/whatsapp";
|
||||
import type SignaldService from "../services/signald";
|
||||
import type { IAppConfig } from "../../config";
|
||||
import type { AppDatabase } from "@digiresilience/metamigo-db";
|
||||
|
||||
// add your service interfaces here
|
||||
|
||||
// extend the hapi types with our services and config
|
||||
declare module "@hapi/hapi" {
|
||||
export interface Request {
|
||||
db(): AppDatabase;
|
||||
pgp: IMain;
|
||||
}
|
||||
export interface Server {
|
||||
config(): IAppConfig;
|
||||
db(): AppDatabase;
|
||||
pgp: IMain;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "@hapipal/schmervice" {
|
||||
interface AppServices {
|
||||
settingsService: ISettingsService;
|
||||
whatsappService: WhatsappService;
|
||||
signaldService: SignaldService;
|
||||
}
|
||||
|
||||
interface SchmerviceDecorator {
|
||||
(namespace: "app"): AppServices;
|
||||
}
|
||||
type ServiceFunctionalInterface = { name: string };
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
export {
|
||||
default,
|
||||
loadConfig,
|
||||
loadConfigRaw,
|
||||
IAppConfig,
|
||||
IAppConvict,
|
||||
} from "@digiresilience/metamigo-config";
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
export * from "./server/index.js";
|
||||
export * from "./logger.js";
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
import { defState } from "@digiresilience/montar";
|
||||
import { configureLogger } from "@digiresilience/metamigo-common";
|
||||
import config from "@digiresilience/metamigo-config";
|
||||
|
||||
export const logger = defState("apiLogger", {
|
||||
start: async () => configureLogger(config),
|
||||
});
|
||||
export default logger;
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
import { startWithout } from "@digiresilience/montar";
|
||||
import "./index.js";
|
||||
|
||||
async function runServer(): Promise<void> {
|
||||
await startWithout(["worker"]);
|
||||
}
|
||||
|
||||
runServer();
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
import * as Metamigo from "@digiresilience/metamigo-common";
|
||||
import { defState } from "@digiresilience/montar";
|
||||
import Manifest from "./manifest.js";
|
||||
import config, { IAppConfig } from "../config.js";
|
||||
|
||||
export const deployment = async (
|
||||
config: IAppConfig,
|
||||
start = false
|
||||
): Promise<Metamigo.Server> => {
|
||||
// Build the manifest, which describes all the plugins needed for our application server
|
||||
const manifest = await Manifest.build(config);
|
||||
|
||||
// Create the server and optionally start it
|
||||
const server = Metamigo.deployment(manifest, config, start);
|
||||
|
||||
return server;
|
||||
};
|
||||
|
||||
export const stopDeployment = async (server: Metamigo.Server): Promise<void> =>
|
||||
Metamigo.stopDeployment(server);
|
||||
|
||||
const server = defState("server", {
|
||||
start: () => deployment(config, true),
|
||||
stop: () => stopDeployment(server),
|
||||
});
|
||||
|
||||
export default server;
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
import * as Glue from "@hapi/glue";
|
||||
import * as Metamigo from "@digiresilience/metamigo-common";
|
||||
import * as Blipp from "blipp";
|
||||
import HapiBasic from "@hapi/basic";
|
||||
import AppPlugin from "../app/index.js";
|
||||
import type { IAppConfig } from "../config.js";
|
||||
|
||||
const build = async (config: IAppConfig): Promise<Glue.Manifest> => {
|
||||
const { port, address } = config.server;
|
||||
const metamigoPlugins = Metamigo.defaultPlugins(config);
|
||||
return {
|
||||
server: {
|
||||
port,
|
||||
address,
|
||||
debug: false, // We use pino not the built-in hapi logger
|
||||
routes: {
|
||||
validate: {
|
||||
failAction: Metamigo.validatingFailAction,
|
||||
},
|
||||
},
|
||||
},
|
||||
register: {
|
||||
plugins: [
|
||||
// Blipp prints the nicely formatted list of endpoints at app boot
|
||||
{ plugin: Blipp },
|
||||
|
||||
// load the metamigo base plugins
|
||||
...metamigoPlugins,
|
||||
|
||||
// basic authentication, required by hapi-nextauth
|
||||
{ plugin: HapiBasic },
|
||||
|
||||
// load our main app
|
||||
{
|
||||
plugin: AppPlugin,
|
||||
options: {
|
||||
config,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const Manifest = {
|
||||
build,
|
||||
};
|
||||
|
||||
export default Manifest;
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
import * as Worker from "graphile-worker";
|
||||
import { defState } from "@digiresilience/montar";
|
||||
import config from "./config.js";
|
||||
|
||||
const startWorkerUtils = async (): Promise<Worker.WorkerUtils> => {
|
||||
const workerUtils = await Worker.makeWorkerUtils({
|
||||
connectionString: config.worker.connection,
|
||||
});
|
||||
return workerUtils;
|
||||
};
|
||||
|
||||
const stopWorkerUtils = async (): Promise<void> => workerUtils.release();
|
||||
|
||||
const workerUtils = defState("apiWorkerUtils", {
|
||||
start: startWorkerUtils,
|
||||
stop: stopWorkerUtils,
|
||||
});
|
||||
|
||||
export default workerUtils;
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
{
|
||||
"extends": "tsconfig-link",
|
||||
"compilerOptions": {
|
||||
"outDir": "build/main",
|
||||
"rootDir": "src",
|
||||
"skipLibCheck": true,
|
||||
"types": ["jest", "node", "long"],
|
||||
"lib": ["es2020", "DOM"],
|
||||
"composite": true,
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/.*.ts"],
|
||||
"exclude": ["node_modules/**"],
|
||||
"references": [
|
||||
{"path": "../../packages/metamigo-common" },
|
||||
{"path": "../../packages/metamigo-config" },
|
||||
{"path": "../../packages/metamigo-db" },
|
||||
{"path": "../../packages/hapi-nextauth" },
|
||||
{"path": "../../packages/hapi-pg-promise" },
|
||||
{"path": "../../packages/node-signald" },
|
||||
{"path": "../../packages/montar" }
|
||||
]
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
require("eslint-config-link/patch/modern-module-resolution");
|
||||
module.exports = {
|
||||
extends: [
|
||||
"eslint-config-link/profile/node",
|
||||
"eslint-config-link/profile/typescript",
|
||||
"eslint-config-link/profile/jest",
|
||||
],
|
||||
parserOptions: { tsconfigRootDir: __dirname },
|
||||
rules: {
|
||||
"new-cap": "off"
|
||||
},
|
||||
};
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
FROM node:20 as base
|
||||
|
||||
FROM base AS builder
|
||||
ARG APP_DIR=/opt/metamigo-cli
|
||||
RUN mkdir -p ${APP_DIR}/
|
||||
RUN npm i -g turbo
|
||||
WORKDIR ${APP_DIR}
|
||||
COPY . .
|
||||
RUN turbo prune --scope=@digiresilience/metamigo-cli --docker
|
||||
|
||||
FROM base AS installer
|
||||
ARG APP_DIR=/opt/metamigo-cli
|
||||
WORKDIR ${APP_DIR}
|
||||
COPY .gitignore .gitignore
|
||||
COPY --from=builder ${APP_DIR}/out/json/ .
|
||||
COPY --from=builder ${APP_DIR}/out/package-lock.json ./package-lock.json
|
||||
RUN npm i
|
||||
|
||||
COPY --from=builder ${APP_DIR}/out/full/ .
|
||||
RUN npm i -g turbo
|
||||
RUN turbo run build --filter=metamigo-cli
|
||||
|
||||
FROM base AS runner
|
||||
ARG APP_DIR=/opt/metamigo-cli
|
||||
WORKDIR ${APP_DIR}/
|
||||
ARG BUILD_DATE
|
||||
ARG VERSION
|
||||
LABEL maintainer="Darren Clarke <darren@redaranj.com>"
|
||||
LABEL org.label-schema.build-date=$BUILD_DATE
|
||||
LABEL org.label-schema.version=$VERSION
|
||||
ENV APP_DIR ${APP_DIR}
|
||||
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
dumb-init
|
||||
RUN mkdir -p ${APP_DIR}
|
||||
RUN chown -R node ${APP_DIR}/
|
||||
|
||||
USER node
|
||||
WORKDIR ${APP_DIR}
|
||||
COPY --from=installer ${APP_DIR}/node_modules/ ./node_modules/
|
||||
COPY --from=installer ${APP_DIR}/packages/ ./packages/
|
||||
COPY --from=installer ${APP_DIR}/apps/metamigo-cli/ ./apps/metamigo-cli/
|
||||
COPY --from=installer ${APP_DIR}/apps/metamigo-api/ ./apps/metamigo-api/
|
||||
COPY --from=installer ${APP_DIR}/apps/metamigo-worker/ ./apps/metamigo-worker/
|
||||
COPY --from=installer ${APP_DIR}/package.json ./package.json
|
||||
USER root
|
||||
WORKDIR ${APP_DIR}/apps/metamigo-cli/
|
||||
RUN chmod +x docker-entrypoint.sh
|
||||
USER node
|
||||
EXPOSE 3000
|
||||
ENV PORT 3000
|
||||
ENV NODE_ENV production
|
||||
ENTRYPOINT ["/opt/metamigo-cli/apps/metamigo-cli/docker-entrypoint.sh"]
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"presets": ["babel-preset-link"]
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
|
||||
node ./build/main/index.js ${@}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
if [[ "$1" == "api" ]]; then
|
||||
echo "docker-entrypoint: starting api server"
|
||||
./cli db -- migrate
|
||||
exec dumb-init ./cli api
|
||||
elif [[ "$1" == "worker" ]]; then
|
||||
echo "docker-entrypoint: starting worker"
|
||||
exec dumb-init ./cli worker
|
||||
elif [[ "$1" == "cli" ]]; then
|
||||
echo "docker-entrypoint: starting cli"
|
||||
shift 1
|
||||
exec ./cli "$@"
|
||||
else
|
||||
echo "docker-entrypoint: missing argument, one of: api, worker, cli"
|
||||
exit 1
|
||||
fi
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"preset": "jest-config-link",
|
||||
"setupFiles": ["<rootDir>/src/setup.test.ts"]
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
{
|
||||
"name": "@digiresilience/metamigo-cli",
|
||||
"version": "0.2.0",
|
||||
"main": "build/main/index.js",
|
||||
"author": "Abel Luck <abel@guardianproject.info>",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"metamigo": "./build/main/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@digiresilience/montar": "*",
|
||||
"@digiresilience/metamigo-config": "*",
|
||||
"@digiresilience/metamigo-common": "*",
|
||||
"@digiresilience/metamigo-db": "*",
|
||||
"@digiresilience/metamigo-api": "*",
|
||||
"@digiresilience/metamigo-worker": "*",
|
||||
"commander": "^12.0.0",
|
||||
"graphile-migrate": "^1.4.1",
|
||||
"graphile-worker": "^0.13.0",
|
||||
"node-jose": "^2.2.0",
|
||||
"postgraphile": "4.13.0",
|
||||
"graphql": "15.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"pino-pretty": "^10.3.1",
|
||||
"nodemon": "^3.1.0",
|
||||
"tsconfig-link": "*",
|
||||
"eslint-config-link": "*",
|
||||
"jest-config-link": "*",
|
||||
"babel-preset-link": "*",
|
||||
"typescript": "^5.3.3"
|
||||
},
|
||||
"scripts": {
|
||||
"migrate": "NODE_ENV=development node --unhandled-rejections=strict build/main/index.js db -- migrate",
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"fix:lint": "eslint src --ext .ts --fix",
|
||||
"fmt": "prettier \"src/**/*.ts\" --write",
|
||||
"lint": "eslint src --ext .ts && prettier \"src/**/*.ts\" --list-different",
|
||||
"test": "echo no tests"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
import {
|
||||
generateConfig,
|
||||
printConfigOptions,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
import { IAppConfig, IAppConvict } from "@digiresilience/metamigo-config";
|
||||
import { loadConfigRaw } from "@digiresilience/metamigo-config";
|
||||
|
||||
export const genConf = async (): Promise<void> => {
|
||||
const c = (await loadConfigRaw()) as any;
|
||||
const generated = generateConfig(c) as any;
|
||||
console.log(generated);
|
||||
};
|
||||
|
||||
export const genSchema = async (): Promise<void> => {
|
||||
const c: any = await loadConfigRaw();
|
||||
console.log(c.getSchemaString());
|
||||
};
|
||||
|
||||
export const listConfig = async (): Promise<void> => {
|
||||
const c = (await loadConfigRaw()) as any;
|
||||
printConfigOptions(c);
|
||||
};
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import { Command } from "commander";
|
||||
import { startWithout } from "@digiresilience/montar";
|
||||
import { migrateWrapper } from "@digiresilience/metamigo-db";
|
||||
import { loadConfig } from "@digiresilience/metamigo-config";
|
||||
import { genConf, listConfig } from "./config.js";
|
||||
import { createTokenForTesting, generateJwks } from "./jwks.js";
|
||||
import { exportGraphqlSchema } from "./metamigo-postgraphile.js";
|
||||
import "@digiresilience/metamigo-api";
|
||||
import "@digiresilience/metamigo-worker";
|
||||
|
||||
const program = new Command();
|
||||
|
||||
export async function runServer(): Promise<void> {
|
||||
await startWithout(["worker"]);
|
||||
}
|
||||
|
||||
export async function runWorker(): Promise<void> {
|
||||
await startWithout(["server"]);
|
||||
}
|
||||
|
||||
program
|
||||
.command("config-generate")
|
||||
.description("Generate a sample JSON configuration file (to stdout)")
|
||||
.action(genConf);
|
||||
|
||||
program
|
||||
.command("config-help")
|
||||
.description("Prints the entire convict config ")
|
||||
.action(listConfig);
|
||||
|
||||
program
|
||||
.command("api")
|
||||
.description("Run the application api server")
|
||||
.action(runServer);
|
||||
|
||||
program
|
||||
.command("worker")
|
||||
.description("Run the worker to process jobs")
|
||||
.action(runWorker);
|
||||
|
||||
program
|
||||
.command("db <commands...>")
|
||||
.description("Run graphile-migrate commands with your app's config loaded.")
|
||||
.action(async (args) => {
|
||||
const config = await loadConfig();
|
||||
return migrateWrapper(args, config);
|
||||
});
|
||||
|
||||
program
|
||||
.command("gen-jwks")
|
||||
.description("Generate the JWKS")
|
||||
.action(generateJwks);
|
||||
|
||||
program
|
||||
.command("gen-testing-jwt")
|
||||
.description("Generate a JWT for the test suite")
|
||||
.action(createTokenForTesting);
|
||||
|
||||
program
|
||||
.command("export-graphql-schema")
|
||||
.description("Export the graphql schema")
|
||||
.action(exportGraphqlSchema);
|
||||
|
||||
program.parse(process.argv);
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
import jose from "node-jose";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
|
||||
const generateKeystore = async () => {
|
||||
const keystore = jose.JWK.createKeyStore();
|
||||
await keystore.generate("oct", 256, {
|
||||
alg: "A256GCM",
|
||||
use: "enc",
|
||||
});
|
||||
await keystore.generate("oct", 256, {
|
||||
alg: "HS512",
|
||||
use: "sig",
|
||||
});
|
||||
return keystore;
|
||||
};
|
||||
|
||||
const safeString = (input) =>
|
||||
Buffer.from(JSON.stringify(input)).toString("base64");
|
||||
|
||||
const stringify = (v) => JSON.stringify(v, undefined, 2);
|
||||
|
||||
const _generateJwks = async () => {
|
||||
const keystore = await generateKeystore();
|
||||
const encryption = keystore.all({ use: "enc" })[0].toJSON(true);
|
||||
const signing = keystore.all({ use: "sig" })[0].toJSON(true);
|
||||
|
||||
return {
|
||||
nextAuth: {
|
||||
signingKeyB64: safeString(signing),
|
||||
encryptionKeyB64: safeString(encryption),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const generateJwks = async (): Promise<void> => {
|
||||
console.log(stringify(await _generateJwks()));
|
||||
};
|
||||
|
||||
export const createTokenForTesting = async (): Promise<void> => {
|
||||
const keys = await _generateJwks();
|
||||
const signingKey = Buffer.from(
|
||||
JSON.parse(
|
||||
Buffer.from(keys.nextAuth.signingKeyB64, "base64").toString("utf-8")
|
||||
).k,
|
||||
"base64"
|
||||
);
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
iss: "Test Env",
|
||||
iat: 1606893960,
|
||||
aud: "metamigo",
|
||||
sub: "abel@guardianproject.info",
|
||||
name: "Abel Luck",
|
||||
email: "abel@guardianproject.info",
|
||||
userRole: "admin",
|
||||
},
|
||||
signingKey,
|
||||
{ expiresIn: "100y", algorithm: "HS512" }
|
||||
);
|
||||
console.log("CONFIG");
|
||||
console.log(stringify(keys));
|
||||
console.log();
|
||||
console.log("TOKEN");
|
||||
console.log(token);
|
||||
console.log();
|
||||
};
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
import { writeFileSync } from "node:fs";
|
||||
import {
|
||||
getIntrospectionQuery,
|
||||
GraphQLSchema,
|
||||
graphqlSync,
|
||||
lexicographicSortSchema,
|
||||
printSchema,
|
||||
} from "graphql";
|
||||
import { createPostGraphileSchema } from "postgraphile";
|
||||
import pg from "pg";
|
||||
import { loadConfig } from "@digiresilience/metamigo-config";
|
||||
import { getPostGraphileOptions } from "@digiresilience/metamigo-db";
|
||||
|
||||
const { Pool } = pg;
|
||||
|
||||
export const exportGraphqlSchema = async (): Promise<void> => {
|
||||
const config = await loadConfig();
|
||||
|
||||
const rootPgPool = new Pool({
|
||||
connectionString: config.db.connection,
|
||||
});
|
||||
const exportSchema = `../../data/schema.graphql`;
|
||||
const exportJson = `../../frontend/lib/graphql-schema.json`;
|
||||
try {
|
||||
const schema = (await createPostGraphileSchema(
|
||||
config.postgraphile.authConnection,
|
||||
"app_public",
|
||||
getPostGraphileOptions()
|
||||
)) as unknown as GraphQLSchema;
|
||||
const sorted = lexicographicSortSchema(schema);
|
||||
const json = graphqlSync({ schema, source: getIntrospectionQuery() });
|
||||
writeFileSync(exportSchema, printSchema(sorted));
|
||||
writeFileSync(exportJson, JSON.stringify(json));
|
||||
|
||||
console.log(`GraphQL schema exported to ${exportSchema}`);
|
||||
console.log(`GraphQL schema json exported to ${exportJson}`);
|
||||
} finally {
|
||||
rootPgPool.end();
|
||||
}
|
||||
};
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"extends": "tsconfig-link",
|
||||
"compilerOptions": {
|
||||
"incremental": true,
|
||||
"outDir": "build/main",
|
||||
"rootDir": "src",
|
||||
"baseUrl": "./",
|
||||
"skipLibCheck": true,
|
||||
"types": ["jest", "node"],
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"exclude": ["node_modules/**"]
|
||||
}
|
||||
|
|
@ -13,20 +13,20 @@
|
|||
"@emotion/react": "^11.11.4",
|
||||
"@emotion/server": "^11.11.0",
|
||||
"@emotion/styled": "^11.11.0",
|
||||
"@fontsource/playfair-display": "^5.0.21",
|
||||
"@fontsource/playfair-display": "^5.0.23",
|
||||
"@fontsource/poppins": "^5.0.12",
|
||||
"@fontsource/roboto": "^5.0.12",
|
||||
"@mui/icons-material": "^5",
|
||||
"@mui/lab": "^5.0.0-alpha.167",
|
||||
"@mui/lab": "^5.0.0-alpha.168",
|
||||
"@mui/material": "^5",
|
||||
"@mui/x-data-grid-pro": "^6.19.6",
|
||||
"@mui/x-date-pickers-pro": "^6.19.6",
|
||||
"date-fns": "^3.3.1",
|
||||
"@mui/x-date-pickers-pro": "^6.19.7",
|
||||
"date-fns": "^3.5.0",
|
||||
"leafcutter-common": "*",
|
||||
"material-ui-popup-state": "^5.0.10",
|
||||
"mui-chips-input": "^2.1.4",
|
||||
"next": "14.1.2",
|
||||
"next-auth": "^4.24.6",
|
||||
"next": "14.1.3",
|
||||
"next-auth": "^4.24.7",
|
||||
"react": "18.2.0",
|
||||
"react-cookie": "^7.1.0",
|
||||
"react-digit-input": "^2.1.0",
|
||||
|
|
@ -42,6 +42,6 @@
|
|||
"@types/react": "^18",
|
||||
"@types/react-dom": "^18",
|
||||
"eslint": "^8",
|
||||
"eslint-config-next": "14.1.2"
|
||||
"eslint-config-next": "14.1.3"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@
|
|||
"html-to-text": "^9.0.5",
|
||||
"node-fetch": "^3",
|
||||
"pg-promise": "^11.5.4",
|
||||
"remeda": "^1.46.2",
|
||||
"twilio": "^4.23.0"
|
||||
"remeda": "^1.50.1",
|
||||
"twilio": "^5.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.24.0",
|
||||
|
|
@ -31,8 +31,8 @@
|
|||
"pino-pretty": "^10.3.1",
|
||||
"prettier": "^3.2.5",
|
||||
"ts-node": "^10.9.2",
|
||||
"typedoc": "^0.25.11",
|
||||
"typescript": "^5.3.3"
|
||||
"typedoc": "^0.25.12",
|
||||
"typescript": "^5.4.2"
|
||||
},
|
||||
"nodemonConfig": {
|
||||
"ignore": [
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"extends": "tsconfig-link",
|
||||
"extends": "tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "build/main",
|
||||
"esModuleInterop": true,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue