Skip to content

Instantly share code, notes, and snippets.

View donchev7's full-sized avatar

Bobby Donchev donchev7

View GitHub Profile
@donchev7
donchev7 / totp.ts
Created May 6, 2024 19:19
Totp implementation for cloudflare workers
declare global {
interface SubtleCrypto {
timingSafeEqual(a: ArrayBuffer, b: ArrayBuffer): boolean // cloudflare workers has a timingSafeEqual method
}
}
export const generateTOTP = async (secret: string): Promise<string> => {
const time = Math.floor(Date.now() / 1000) // get the current time in seconds
const timeWindow = 15 * 60 // 15-minute time window
const timeStep = Math.floor(time / timeWindow) // calculate the time step
@donchev7
donchev7 / index.ts
Created February 6, 2024 07:49
Branded types in typescript
export type Branded<T, K extends string = 'BRANDED_TYPE'> = T & { __opaque__?: K };
type Brand = {
User: Branded<string, 'User'>,
Product: Branded<string, 'Product'>,
}
function getUser(userId: Brand['User']) {
// get user
}
@donchev7
donchev7 / config_parser.rs
Created December 7, 2023 09:34
Fluentbit config parser
use std::fs::File;
use std::io::{self, prelude::*, BufReader};
#[derive(Clone)]
pub struct Property {
key: String,
value: String,
}
impl Property {
@donchev7
donchev7 / configure.sh
Last active May 22, 2023 18:22
ubunutu
#!/bin/bash
set -e
sudo rm -rf /var/lib/apt/lists/*
apt-get update
apt-cache gencaches
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
@donchev7
donchev7 / az.sh
Created January 4, 2023 11:21
azure cli in docker
alias az="docker run -it -u $UID:$GID --entrypoint='' -v $(pwd):/build --workdir='/build' -v ${HOME}/.azure:/.azure:rw -v ${HOME}/.azure-devops:/.azure-devops:rw mcr.microsoft.com/azure-cli az"
@donchev7
donchev7 / main.go
Created September 4, 2022 14:47
APIs with multiple parameters of the same type are hard to use correctly.
type Source string
func (src Source) CopyTo(dest string) error {
return CopyFile(dest, string(src))
}
func main() {
var from Source = "presentation.md"
from.CopyTo("/tmp/backup")
}
@donchev7
donchev7 / secrets.go
Last active August 23, 2022 16:31
Prevent secrets ending up in logs
package main
import "fmt"
type secret string
func (s secret) String() string {
return "*****"
}
@donchev7
donchev7 / retry.go
Created August 14, 2022 15:40
Go retry function
type Action func(context.Context) (string, error)
func Retry(action Action, retries int, delay time.Duration) Action {
return func(ctx context.Context) (string, error) {
for r := 0; ; r++ {
response, err := action(ctx)
if err == nil || r >= retries {
// Return when there is no error or the maximum amount
// of retries is reached.
return response, err
@donchev7
donchev7 / enum.go
Last active April 30, 2022 10:54
Doing enums in go
package main
type role uint
const (
Unknown role = iota
Guest
Member
Moderator
@donchev7
donchev7 / configure.sh
Last active April 28, 2022 14:18
agent setup
#!/bin/bash
set -e
sudo rm -rf /var/lib/apt/lists/*
apt-get update
apt-cache gencaches
apt-get install -y \
apt-transport-https \