Skip to content

Instantly share code, notes, and snippets.

View wonderbeyond's full-sized avatar
🎯
Focusing

Wonder wonderbeyond

🎯
Focusing
View GitHub Profile
@wonderbeyond
wonderbeyond / set-apt-proxy.md
Last active April 8, 2024 05:08
[ubuntu][socks5][proxy] Set proxy for apt

Writing an apt proxy conf file /etc/apt/apt.conf.d/proxy.conf as below.

Acquire::http::Proxy "socks5h://127.0.0.1:1080";
Acquire::https::Proxy "socks5h://127.0.0.1:1080";
Acquire::socks::Proxy "socks5h://127.0.0.1:1080";

And the proxy settings will be applied the next time we run apt.

@wonderbeyond
wonderbeyond / index.md
Last active November 10, 2023 19:14
gfwlist + genpac + linux => system-wide auto proxy

genpac

$ pip install genpac
$ genpac \
    --pac-proxy="SOCKS5 172.16.1.117:1080" \
    --gfwlist-proxy="SOCKS5 172.16.1.117:1080" \
    --gfwlist-url="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt" \
    -o ~/autoproxy.pac
# we got: file:///home/wonder/autoproxy.pac
@wonderbeyond
wonderbeyond / pgcall.py
Last active August 24, 2023 08:33
Execute SQL on remote Postgres server.
#!/usr/bin/env python
import argparse
import urllib.parse
import textwrap
import psycopg2
import psycopg2.extras
def main():
@wonderbeyond
wonderbeyond / jsonb.py
Last active July 21, 2023 08:08
Live easily with jsonb under SQLAlchemy
from typing import Any
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import array as pg_array, JSONB
def jsonb_set(target, path: list[str], val: Any):
"""
An easy wrapper over sa.func.jsonb_set().
@wonderbeyond
wonderbeyond / tempfile.py
Created July 17, 2023 04:30
An alternative to tempfile.NamedTemporaryFile working with Windows NT.
import os
from contextlib import contextmanager
from tempfile import mkstemp
@contextmanager
def MakeTemporaryFile(suffix=None, prefix=None, dir=None):
"""An alternative to tempfile.NamedTemporaryFile working with Windows NT."""
try:
fd, name = mkstemp(suffix=suffix, prefix=prefix, dir=dir)
@wonderbeyond
wonderbeyond / min-max-ignore-none.py
Last active July 9, 2023 06:48
Safe alternative of min/max in Python (the builtin min/max is evil)
"""
Python's builtin min function is evil
>>> min(2, 1)
1
>>> min([2, 1])
1
>>> min([2])
2
>>> min([])
@wonderbeyond
wonderbeyond / sa-task-step-dep.py
Last active June 21, 2023 02:48
[SQLAlchemy] A Task-Step model that can describe steps' upstream and downstream relationships.
"""
A Task-Step model that can describe steps' upstream and downstream relationships.
Also refer to https://docs.sqlalchemy.org/en/20/orm/join_conditions.html#self-referential-many-to-many-relationship
"""
from typing import Annotated, List
import sqlalchemy as sa
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship, backref
from sqlalchemy.orm import MappedAsDataclass, DeclarativeBase
@wonderbeyond
wonderbeyond / sa-1.4-adb.py
Last active June 13, 2023 06:03
SQLAlchemy db (engine + session) wrapper
class AsyncSQLAlchemy:
def __init__(
self,
url: str,
engine_options: Optional[Dict[str, Any]] = None,
session_options: Optional[Dict[str, Any]] = None,
):
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
@wonderbeyond
wonderbeyond / viaproxy.sh
Last active June 10, 2023 10:04
viaproxy "socks5 127.0.0.1 1080" telnet some-host
#!/bin/bash
# Author: wonderbeyond@gmail.com
# Usage: viaproxy "socks5 127.0.0.1 1080" telnet some-host
command -v proxychains4 > /dev/null && PROXYCHAINS="proxychains4 -q" || PROXYCHAINS=proxychains
proxy="$1"
shift
temp_conf_file=$(mktemp /tmp/viaproxy-proxchains-conf-XXXX)