mirror of
https://github.com/matrixgpt/matrix-chatgpt-bot.git
synced 2024-09-27 19:45:38 +00:00
Remove KEYV_BOT_ENCRYPTION
This commit is contained in:
parent
f5997a9a4b
commit
89ec060bc1
@ -7,7 +7,6 @@ export const {
|
|||||||
DATA_PATH,
|
DATA_PATH,
|
||||||
KEYV_BACKEND,
|
KEYV_BACKEND,
|
||||||
KEYV_URL,
|
KEYV_URL,
|
||||||
KEYV_BOT_ENCRYPTION,
|
|
||||||
KEYV_BOT_STORAGE,
|
KEYV_BOT_STORAGE,
|
||||||
/** Matrix Bot Settings */
|
/** Matrix Bot Settings */
|
||||||
MATRIX_HOMESERVER_URL,
|
MATRIX_HOMESERVER_URL,
|
||||||
@ -35,7 +34,6 @@ export const {
|
|||||||
DATA_PATH: { schema: z.string().default("./storage"), description: "Set to /storage/ if using docker, ./storage if running without" },
|
DATA_PATH: { schema: z.string().default("./storage"), description: "Set to /storage/ if using docker, ./storage if running without" },
|
||||||
KEYV_BACKEND: { schema: z.enum(["file", "other"]).default("file"),description: "Set the Keyv backend to 'file' or 'other' if other set KEYV_URL" },
|
KEYV_BACKEND: { schema: z.enum(["file", "other"]).default("file"),description: "Set the Keyv backend to 'file' or 'other' if other set KEYV_URL" },
|
||||||
KEYV_URL: { schema: z.string().default(""), description: "Set Keyv backend for storage, in-memory if blank, ignored if KEYV_BACKEND set to `file`"},
|
KEYV_URL: { schema: z.string().default(""), description: "Set Keyv backend for storage, in-memory if blank, ignored if KEYV_BACKEND set to `file`"},
|
||||||
KEYV_BOT_ENCRYPTION: { schema: z.boolean().default(false), description: "Set to true to use a Keyv backend to store bot encryption keys. Uses a file if false."},
|
|
||||||
KEYV_BOT_STORAGE: { schema: z.boolean().default(false), description: "Set to true to use a Keyv backend to store bot data. Uses a file if false."},
|
KEYV_BOT_STORAGE: { schema: z.boolean().default(false), description: "Set to true to use a Keyv backend to store bot data. Uses a file if false."},
|
||||||
/** Matrix Bot Settings */
|
/** Matrix Bot Settings */
|
||||||
MATRIX_HOMESERVER_URL: { schema: z.string().default("https://matrix.org"), description: "Set matrix homeserver with 'https://' prefix" },
|
MATRIX_HOMESERVER_URL: { schema: z.string().default("https://matrix.org"), description: "Set matrix homeserver with 'https://' prefix" },
|
||||||
|
13
src/index.ts
13
src/index.ts
@ -8,9 +8,9 @@ import {
|
|||||||
} from "matrix-bot-sdk";
|
} from "matrix-bot-sdk";
|
||||||
|
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { DATA_PATH, KEYV_URL, OPENAI_API_KEY, MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, MATRIX_AUTOJOIN, MATRIX_BOT_PASSWORD, MATRIX_BOT_USERNAME, MATRIX_ENCRYPTION, MATRIX_THREADS, CHATGPT_CONTEXT, CHATGPT_MODEL, KEYV_BOT_ENCRYPTION, KEYV_BOT_STORAGE, KEYV_BACKEND } from './env.js'
|
import { DATA_PATH, KEYV_URL, OPENAI_API_KEY, MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, MATRIX_AUTOJOIN, MATRIX_BOT_PASSWORD, MATRIX_BOT_USERNAME, MATRIX_ENCRYPTION, MATRIX_THREADS, CHATGPT_CONTEXT, CHATGPT_MODEL, KEYV_BOT_STORAGE, KEYV_BACKEND } from './env.js'
|
||||||
import CommandHandler from "./handlers.js"
|
import CommandHandler from "./handlers.js"
|
||||||
import { KeyvCryptoStorageProvider, KeyvStorageProvider } from './storage.js'
|
import { KeyvStorageProvider } from './storage.js'
|
||||||
import { parseMatrixUsernamePretty } from './utils.js';
|
import { parseMatrixUsernamePretty } from './utils.js';
|
||||||
|
|
||||||
LogService.setLogger(new RichConsoleLogger());
|
LogService.setLogger(new RichConsoleLogger());
|
||||||
@ -36,14 +36,7 @@ if (KEYV_BOT_STORAGE) {
|
|||||||
storage = new SimpleFsStorageProvider(path.join(DATA_PATH, "bot.json")); // /storage/bot.json
|
storage = new SimpleFsStorageProvider(path.join(DATA_PATH, "bot.json")); // /storage/bot.json
|
||||||
}
|
}
|
||||||
|
|
||||||
let cryptoStore: ICryptoStorageProvider;
|
const cryptoStore: ICryptoStorageProvider = new RustSdkCryptoStorageProvider(path.join(DATA_PATH, "encrypted")); // /storage/encrypted
|
||||||
if (MATRIX_ENCRYPTION) {
|
|
||||||
if (KEYV_BOT_ENCRYPTION) {
|
|
||||||
cryptoStore = new KeyvCryptoStorageProvider('chatgpt-bot-encryption');
|
|
||||||
} else {
|
|
||||||
cryptoStore = new RustSdkCryptoStorageProvider(path.join(DATA_PATH, "encrypted")); // /storage/encrypted
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
if (!MATRIX_ACCESS_TOKEN){
|
if (!MATRIX_ACCESS_TOKEN){
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import Keyv from 'keyv'
|
import Keyv from 'keyv'
|
||||||
import { KeyvFile } from 'keyv-file';
|
import { KeyvFile } from 'keyv-file';
|
||||||
import * as sha512 from "hash.js/lib/hash/sha/512.js";
|
import * as sha512 from "hash.js/lib/hash/sha/512.js";
|
||||||
import * as sha256 from "hash.js/lib/hash/sha/256.js";
|
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { IAppserviceCryptoStorageProvider, IAppserviceStorageProvider, ICryptoRoomInformation, ICryptoStorageProvider, IFilterInfo, IStorageProvider } from "matrix-bot-sdk";
|
import { IAppserviceStorageProvider, IFilterInfo, IStorageProvider } from "matrix-bot-sdk";
|
||||||
import { DATA_PATH, KEYV_BACKEND, KEYV_URL } from './env.js';
|
import { DATA_PATH, KEYV_BACKEND, KEYV_URL } from './env.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,63 +128,3 @@ class NamespacedKeyvProvider implements IStorageProvider {
|
|||||||
return this.parent.storeValue(`${this.prefix}_kv_${key}`, value);
|
return this.parent.storeValue(`${this.prefix}_kv_${key}`, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A crypto storage provider for the default rust-sdk store (sled, file-based).
|
|
||||||
* @category Storage providers
|
|
||||||
*/
|
|
||||||
export class KeyvCryptoStorageProvider implements ICryptoStorageProvider {
|
|
||||||
private db: Keyv;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new rust-sdk storage provider.
|
|
||||||
* @param {string} namespace The *directory* to persist database details to.
|
|
||||||
*/
|
|
||||||
public constructor(public readonly namespace: string) {
|
|
||||||
if (KEYV_BACKEND === 'file'){
|
|
||||||
this.db = new Keyv({store: new KeyvFile({ filename: path.join(DATA_PATH, `${namespace}.json`),})})
|
|
||||||
} else {
|
|
||||||
this.db = new Keyv(KEYV_URL, { namespace: namespace });
|
|
||||||
}
|
|
||||||
this.db.set('deviceId', null)
|
|
||||||
this.db.set('rooms', {})
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getDeviceId(): Promise<string> {
|
|
||||||
return this.db.get('deviceId');
|
|
||||||
}
|
|
||||||
|
|
||||||
public async setDeviceId(deviceId: string): Promise<void> {
|
|
||||||
this.db.set('deviceId', deviceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getRoom(roomId: string): Promise<ICryptoRoomInformation> {
|
|
||||||
const key = sha512().update(roomId).digest('hex');
|
|
||||||
return this.db.get(`rooms.${key}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async storeRoom(roomId: string, config: ICryptoRoomInformation): Promise<void> {
|
|
||||||
const key = sha512().update(roomId).digest('hex');
|
|
||||||
this.db.set(`rooms.${key}`, config);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An appservice crypto storage provider for the default rust-sdk store (sled, file-based).
|
|
||||||
* @category Storage providers
|
|
||||||
*/
|
|
||||||
export class KeyvAppserviceCryptoStorageProvider extends KeyvCryptoStorageProvider implements IAppserviceCryptoStorageProvider {
|
|
||||||
/**
|
|
||||||
* Creates a new rust-sdk storage provider.
|
|
||||||
* @param {string} baseNamespace The *directory* to persist database details to.
|
|
||||||
*/
|
|
||||||
public constructor(private baseNamespace: string) {
|
|
||||||
super(baseNamespace + "_default");
|
|
||||||
}
|
|
||||||
|
|
||||||
public storageForUser(userId: string): ICryptoStorageProvider {
|
|
||||||
// sha256 because sha512 is a bit big for some operating systems
|
|
||||||
const key = sha256().update(userId).digest('hex');
|
|
||||||
return new KeyvCryptoStorageProvider(this.baseNamespace + "_" + key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user