-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8568ce5
commit 74ab963
Showing
28 changed files
with
974 additions
and
936 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"printWidth": 110, | ||
"singleQuote": true, | ||
"semi": true, | ||
"useTabs": true | ||
"printWidth": 140, | ||
"singleQuote": true, | ||
"semi": true, | ||
"useTabs": false, | ||
"tabWidth": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
{ | ||
"recommendations": [ | ||
"ms-azuretools.vscode-azurefunctions" | ||
] | ||
} | ||
"recommendations": ["ms-azuretools.vscode-azurefunctions"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { LogLevel } from "./utils/logger"; | ||
import { LogLevel } from './utils/logger'; | ||
|
||
export const config = { | ||
localEnvPath: '.dev.vars', | ||
renew: false, | ||
logLevel: 'info' as LogLevel, | ||
telegramWebhookPath: '/api/telegramBot', | ||
} | ||
localEnvPath: '.dev.vars', | ||
renew: false, | ||
logLevel: 'info' as LogLevel, | ||
telegramWebhookPath: '/api/telegramBot', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,67 @@ | ||
|
||
import fs from 'fs'; | ||
import { $ } from 'bun'; | ||
|
||
export interface SecretManagerOptions { | ||
localEnvPath: string; | ||
renew: boolean; | ||
localEnvPath: string; | ||
renew: boolean; | ||
} | ||
|
||
export class SecretManager { | ||
constructor(public options: SecretManagerOptions) { | ||
console.log('SecretManager created'); | ||
} | ||
|
||
async start(): Promise<string> { | ||
console.log('Starting secret generation'); | ||
const secret = this.getSecret(); | ||
if (!this.options.renew) { | ||
console.log('Renew option not set, skipping secret upload'); | ||
return secret; | ||
} | ||
this.saveSecret(secret, this.options.localEnvPath); | ||
await this.uploadSecret(secret); | ||
return secret; | ||
} | ||
constructor(public options: SecretManagerOptions) { | ||
console.log('SecretManager created'); | ||
} | ||
|
||
/** | ||
* Randomly generates a secret string of a given length. | ||
* @param length - Length of the secret string | ||
* @returns A randomly generated secret string | ||
*/ | ||
getSecret(length: number = 100): string { | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
let result = ''; | ||
async start(): Promise<string> { | ||
console.log('Starting secret generation'); | ||
const secret = this.getSecret(); | ||
if (!this.options.renew) { | ||
console.log('Renew option not set, skipping secret upload'); | ||
return secret; | ||
} | ||
this.saveSecret(secret, this.options.localEnvPath); | ||
await this.uploadSecret(secret); | ||
return secret; | ||
} | ||
|
||
for (let i = 0; i < length; i++) { | ||
const randomIndex = Math.floor(Math.random() * characters.length); | ||
result += characters[randomIndex]; | ||
} | ||
/** | ||
* Randomly generates a secret string of a given length. | ||
* @param length - Length of the secret string | ||
* @returns A randomly generated secret string | ||
*/ | ||
getSecret(length: number = 100): string { | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
let result = ''; | ||
|
||
return result; | ||
} | ||
for (let i = 0; i < length; i++) { | ||
const randomIndex = Math.floor(Math.random() * characters.length); | ||
result += characters[randomIndex]; | ||
} | ||
|
||
async saveSecret(secret: string, targetPath: string): Promise<void> { | ||
const localEnvExists = fs.existsSync(targetPath); | ||
if (!localEnvExists) { | ||
await fs.promises.writeFile(targetPath, ''); | ||
} | ||
// Renew the secret, by replacing the old one | ||
let localEnv = fs.readFileSync(targetPath, 'utf-8'); | ||
if(localEnv.includes('WEBHOOK_SECRET=')) { | ||
localEnv = localEnv.replace(/WEBHOOK_SECRET=.*/, `WEBHOOK_SECRET=${secret}`); | ||
} else { | ||
localEnv += `\nWEBHOOK_SECRET=${secret}`; | ||
} | ||
await fs.promises.writeFile(targetPath, localEnv); | ||
} | ||
return result; | ||
} | ||
|
||
async uploadSecret(secret: string): Promise<void> { | ||
// Upload the secret to Cloudflare Workers Secrets | ||
console.log('Uploading secret to Cloudflare Workers Secrets'); | ||
const tmpFile = `.dev.tmp.vars`; | ||
await this.saveSecret(secret, tmpFile); | ||
await $`npx wrangler secret bulk ${tmpFile}`; | ||
// Clean up the temporary file | ||
fs.unlinkSync(tmpFile); | ||
async saveSecret(secret: string, targetPath: string): Promise<void> { | ||
const localEnvExists = fs.existsSync(targetPath); | ||
if (!localEnvExists) { | ||
await fs.promises.writeFile(targetPath, ''); | ||
} | ||
// Renew the secret, by replacing the old one | ||
let localEnv = fs.readFileSync(targetPath, 'utf-8'); | ||
if (localEnv.includes('WEBHOOK_SECRET=')) { | ||
localEnv = localEnv.replace(/WEBHOOK_SECRET=.*/, `WEBHOOK_SECRET=${secret}`); | ||
} else { | ||
localEnv += `\nWEBHOOK_SECRET=${secret}`; | ||
} | ||
await fs.promises.writeFile(targetPath, localEnv); | ||
} | ||
|
||
} | ||
async uploadSecret(secret: string): Promise<void> { | ||
// Upload the secret to Cloudflare Workers Secrets | ||
console.log('Uploading secret to Cloudflare Workers Secrets'); | ||
const tmpFile = `.dev.tmp.vars`; | ||
await this.saveSecret(secret, tmpFile); | ||
await $`npx wrangler secret bulk ${tmpFile}`; | ||
// Clean up the temporary file | ||
fs.unlinkSync(tmpFile); | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import { ConsoleLogger, Logger } from "../utils/logger"; | ||
import { ConsoleLogger, Logger } from '../utils/logger'; | ||
|
||
export interface TelegramBotClientOptions { | ||
logger?: Logger; | ||
token: string; | ||
logger?: Logger; | ||
token: string; | ||
} | ||
|
||
export class TelegramBotClient { | ||
private logger: Logger; | ||
constructor(private readonly options: TelegramBotClientOptions) { | ||
this.logger = options.logger ?? new ConsoleLogger(); | ||
} | ||
private logger: Logger; | ||
constructor(private readonly options: TelegramBotClientOptions) { | ||
this.logger = options.logger ?? new ConsoleLogger(); | ||
} | ||
|
||
async setWebhook(url: string) { | ||
const targetUrl = `https://api.telegram.org/bot${this.options.token}/setWebhook?url=${url}`; | ||
this.logger.info(`Setting webhook to ${targetUrl.replace(this.options.token, '**BOT_TOKEN**')}`); | ||
await fetch(targetUrl); | ||
} | ||
async setWebhook(url: string) { | ||
const targetUrl = `https://api.telegram.org/bot${this.options.token}/setWebhook?url=${url}`; | ||
this.logger.info(`Setting webhook to ${targetUrl.replace(this.options.token, '**BOT_TOKEN**')}`); | ||
await fetch(targetUrl); | ||
} | ||
} |
Oops, something went wrong.