Skip to content

Instantly share code, notes, and snippets.

Avatar
👁️
What you look for is what you find.

Alfon alfonmga

👁️
What you look for is what you find.
View GitHub Profile
@alfonmga
alfonmga / down.sh
Created February 21, 2023 20:08
Deploy an Ubuntu 22.04 LTS Desktop on Google Cloud Platform with RDP ready.
View down.sh
#!/bin/sh
set -e
# Zone
ZONE=europe-west8-a
echo "Destroying cloud Ubuntu Desktop..."
gcloud compute instances delete ubuntu-desktop --zone=$ZONE --quiet
@alfonmga
alfonmga / main.c
Last active January 23, 2023 16:14
/proc/loadavg manipulation by monkey-patching `loadavg_proc_show` function https://github.com/torvalds/linux/blob/master/fs/proc/loadavg.c
View main.c
#include <linux/seq_file.h>
KHOOK_EXT(int, loadavg_proc_show, struct seq_file *, void *v);
static int khook_loadavg_proc_show(struct seq_file *m, void *v)
{
unsigned int random_number;
unsigned char rands[sizeof(unsigned int)];
get_random_bytes(rands, sizeof(unsigned int));
random_number = *(unsigned int*)rands;
random_number = (random_number % 6) + 14;
seq_printf(m, "0.%d 0.16 0.11 1/127 10420\n", random_number);
@alfonmga
alfonmga / logrus2telegram.go
Last active August 17, 2022 14:29
Logrus -> Telegram
View logrus2telegram.go
package logrus2telegram
import (
"fmt"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
"errors"
@alfonmga
alfonmga / ginlogrus.go
Created August 16, 2022 17:11
Gin -> Logrus
View ginlogrus.go
package ginlogrus
import (
"fmt"
"math"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
@alfonmga
alfonmga / gormlogrus.go
Created August 16, 2022 17:10
Gorm -> Logrus
View gormlogrus.go
package gormlogrus
import (
"context"
"errors"
"time"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
@alfonmga
alfonmga / gcs_reverse_proxy.go
Last active August 12, 2022 16:53
Google Cloud Storage reverse proxy code extracted from https://github.com/daichirata/gcsproxy
View gcs_reverse_proxy.go
package main
import (
"context"
"flag"
"io"
"log"
"net/http"
"strconv"
"time"
@alfonmga
alfonmga / validator_interceptor.go
Created August 7, 2022 21:33
Buf connect-go protoc-gen-validate interceptor by @akshayjshah
View validator_interceptor.go
var validatorInterceptor = connect.UnaryInterceptorFunc(
func(next connect.UnaryFunc) connect.UnaryFunc {
return connect.UnaryFunc(func(ctx context.Context, request connect.AnyRequest) (connect.AnyResponse, error) {
validator, ok := request.Any().(interface{ ValidateAll() error })
if !ok {
// Handle this however you'd like; maybe return an error with
// CodeInternal if all your types should support validation?
return next(ctx, request)
}
if err := validator.ValidateAll(); err != nil {
@alfonmga
alfonmga / ffmpeg-format-to-mimetype.js
Created June 23, 2022 15:38 — forked from DusanBrejka/ffmpeg-format-to-mimetype.js
FFMPEG - map of formats to default mime types
View ffmpeg-format-to-mimetype.js
// INCOMPLETE
// This command will give you list of available FFMPEG formats and their default Mime types
// ffmpeg -formats -hide_banner | tail -n +5 | cut -c5- | cut -d' ' -f1 | xargs -i{} ffmpeg -hide_banner -h demuxer={} | pcregrep -o2 -o4 -M '(Muxer (\w+) )|(Mime type:( .*).)'
// And then parse the output with regex to JSON format in JavaScript for example:
// str.match(/(.*)\n (.*)/gm).map(m => `"${m.replace(/\n /, '": "')}"`).join(',\n');
// Combine the output with MDN - Common MIME types
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
// And with IANA:
@alfonmga
alfonmga / useModals.tsx
Created June 21, 2022 17:03 — forked from statico/useModals.tsx
Chakra UI await-able alert, confirm, and prompt modal dialogs
View useModals.tsx
/*
* Usage:
* const { alert, confirm, prompt } = useModals()
* alert("Hey!") // awaitable too
* if (await confirm("Are you sure?")) ...
* const result = await prompt("Enter a URL", "http://")
*/
import React, {
createContext,
@alfonmga
alfonmga / promisified-grpc-client.ts
Created May 24, 2022 15:37 — forked from smnbbrv/promisified-grpc-client.ts
Promisify @grpc-js service client with typescript
View promisified-grpc-client.ts
import { Client, ServiceError, Metadata, CallOptions, ClientUnaryCall } from '@grpc/grpc-js';
import { Message } from 'google-protobuf';
type OriginalCall<T, U> = (request: T, metadata: Metadata, options: Partial<CallOptions>, callback: (error: ServiceError, res: U) => void) => ClientUnaryCall;
type PromisifiedCall<T, U> = ((request: T, metadata?: Metadata, options?: Partial<CallOptions>) => Promise<U>);
export type Promisified<C> = { $: C; } & {
[prop in Exclude<keyof C, keyof Client>]: (C[prop] extends OriginalCall<infer T, infer U> ? PromisifiedCall<T, U> : never);
}