Skip to content

Instantly share code, notes, and snippets.

@myl7
myl7 / .gitconfig
Created November 20, 2021 02:54
git with proxy for github.com
[http "https://github.com"]
proxy = http://127.0.0.1:10800
[https "https://github.com"]
proxy = http://127.0.0.1:10800
@myl7
myl7 / index.html
Created November 5, 2021 08:44
Experiment to decide which way is the best one to process large (such as > 1GB) binary data in JS
<input id="input" type="file" onchange="handle()" />
<script>
let a
const handle = () => {
let e = document.getElementById('input')
let f = e.files[0]
setInterval(() => {
// console.log(f)
// No mem usage increament
// At least in Chrome, Blob is automatically put on memory or disk
@myl7
myl7 / req.sh
Last active February 14, 2022 06:51
Zero-dependency HTTP request script from https://www.v2ex.com/t/811424
#!/bin/bash
set -euo pipefail
# From https://www.v2ex.com/t/811424
function __curl() {
read proto server path <<<$(echo ${1//// })
DOC=/${path// //}
HOST=${server//:*}
PORT=${server//*:}
@myl7
myl7 / Snowflake.cs
Created October 17, 2021 05:13
snow flake, distributed (basicly) time-sortable ID generator in C#. Sleeps to avoid seq exhaustion and time reversal.
using System;
using System.Threading;
namespace Myl7Snowflake
{
public class Snowflake
{
private readonly ushort _machineId;
public Snowflake(ushort machineId)
@myl7
myl7 / mysql.sql
Last active July 4, 2021 04:38
Reproduce the bug that inline foreign key in create table is ignored by MySQL: https://bugs.mysql.com/bug.php?id=17943 & https://stackoverflow.com/a/60103054/11691878
-- $ mysql --version
-- mysql Ver 8.0.24 for Linux on x86_64 (Source distribution)
create table a(id int primary key);
-- Query OK, 0 rows affected (0.03 sec)
create table b(id int primary key, fk int references a(id));
-- Query OK, 0 rows affected (0.05 sec)
insert into b values (1, 2);
@myl7
myl7 / filter.py
Created May 14, 2021 11:39
Filter goaccess json with IP ranges
import json
from netaddr import IPSet, IPAddress
with open('ipset.txt') as f:
ipset = IPSet(list(f))
with open('visitors.json') as f:
addrs = json.load(f)
@myl7
myl7 / analyze_rsyncd.py
Last active May 1, 2021 16:59
Analyze rsyncd log with multiprocessing and handle log file UTF-8 error. Based on https://gist.github.com/taoky/91c12185c2cd38f264fe2863a6b13c27 .
import sys
from collections import defaultdict
from multiprocessing import Pool
import json
import glob
try:
import yaml
dumper = yaml.dump
except ImportError:
@myl7
myl7 / fix.sh
Created February 10, 2021 12:53
Fix git submodule for public repo in AWS Amplify
#!/bin/bash
set -euo pipefail
# Fix git submodule for AWS Amplify build
git config --file=.gitmodules submodule.images.url <repo-http-url>
git config --file=.gitmodules submodule.images.branch <branch>
git submodule sync --recursive
git submodule update --init --remote --recursive
@myl7
myl7 / index.js
Created January 8, 2021 22:30
Cloudflare worker HTTP basic auth
addEventListener('fetch', e => {
e.respondWith(handleReq(e.request))
})
const handleReq = async (req) => {
if (!req.headers.has('Authorization')) {
return new Response(null, {
status: 401,
headers: {'WWW-Authenticate': 'Basic realm="mylmoe admin secret"'}
})