Skip to content

Instantly share code, notes, and snippets.

View leiless's full-sized avatar
🎯
Focusing

Fishbone° leiless

🎯
Focusing
View GitHub Profile
@leiless
leiless / restore-etc-resolv-conf-on-wsl-2.md
Last active May 30, 2024 07:38
How to restore default `/etc/resolv.conf` on WSL 2
@leiless
leiless / ms-edge-tts-api.md
Created May 10, 2024 06:05 — forked from wilinz/ms-edge-tts-api.md
微软edge-tts-api(大声朗读协议)
import os
import sys
import zipfile
import shutil
def unzip_py_env(py_env_zip: str):
py_env_dir = py_env_zip.removesuffix('.zip')
need_update = not os.path.isdir(py_env_dir)
if not need_update:
@leiless
leiless / _setmode.rs
Last active April 10, 2024 06:48
Rust windows: Set stdin(fd=0) translation mode to binary mode.
#[cfg(target_os = "windows")]
extern {
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170
// https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa298581(v=vs.60)
fn _setmode(fd: std::os::raw::c_int, mode: std::os::raw::c_int) -> std::os::raw::c_int;
// https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/wwfcfxas(v=vs.100)
fn _get_errno(p_value: *mut std::os::raw::c_int) -> std::os::raw::c_int;
}
#[cfg(target_os = "windows")]
@leiless
leiless / aes256_gcm_cbc_pkcs7.rs
Created April 3, 2024 03:27
Rust: AES-256, CBC mode, PKCS#7 padding
use aes::cipher::block_padding::Pkcs7;
use aes::cipher::{KeyIvInit, BlockEncryptMut, BlockDecryptMut};
use rand::RngCore;
type Aes256CbcEnc = cbc::Encryptor<aes::Aes256>;
type Aes256CbcDec = cbc::Decryptor<aes::Aes256>;
const AES256_SECRET_SIZE: usize = 32;
const AES256_BLOCK_SIZE: usize = 16;
@leiless
leiless / aes_256_cbc_pkcs7.py
Created April 3, 2024 02:18
Python: AES-256, CBC mode, PKCS#7 padding
#!/usr/bin/env python3
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
# https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption/
# https://cryptography.io/en/latest/hazmat/primitives/padding/#cryptography.hazmat.primitives.padding.PKCS7
class AES256:
KEY_SIZE = 32
BLOCK_SIZE = 16
@leiless
leiless / pushd-popd.py
Last active February 22, 2024 06:18
Bring bash's pushd/popd to Python3
#!/usr/bin/env python3
import os
import sys
DIR_STACK = []
def pushd(path: str):
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
@leiless
leiless / lmdb_MDB_DUPSORT_write.rs
Created October 30, 2023 02:09
Test LMDB MDB_DUPSORT write with same key-value.
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}
@leiless
leiless / lmdb_INTEGER_KEY_cursor_get_bug.rs
Created October 12, 2023 07:17
LMDB INTEGER_KEY big key `mdb_cursor_get(MDB_SET_RANGE)` bug
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}