Skip to content

Instantly share code, notes, and snippets.

@imba-tjd
imba-tjd / udphp.py
Created October 19, 2024 11:56
udp hole punching
# 当两台电脑都有公网IP,却无法访问时,可能是被防火墙拦截了。inbound filter只允许内部往外部建立连接。
# 想到普通家宽,建立连接后,双方是可以互发消息的,证明网络是通的。而UDP是无连接的,怎么做到收回复呢?
# 其实防火墙的原理是检测发出去的五元组,遇到对应的回复就放行传入。
# 从实践上来说,只要双方互发消息,就可以了。这就是“打洞”
import socket, time, sys, threading
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.bind(('::', int(sys.argv[1])))
target = (sys.argv[2], int(sys.argv[3]))
@imba-tjd
imba-tjd / server.c
Last active November 3, 2024 14:00
Windows httpapi server example.
// https://learn.microsoft.com/zh-cn/windows/win32/Http
// gcc server.c -Os -Wall -lhttpapi -o server
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <http.h>
#include <windows.h>
#include <signal.h>
@imba-tjd
imba-tjd / AutoRemove-NetFirewallRule.ps1
Created March 3, 2023 04:22
Auto remove invalid Windows Firewall rules
# needs Admin privilege
# TODO: Add -confirm
$rules = Get-NetFirewallRule
foreach ($rule in $rules) {
if ($rule.Name.contains("Query User")) {
$filepath = $rule.Name.Substring($rule.Name.Indexof('}')+1)
if (-not (Test-Path $filepath)) {
echo "Removing $filepath"
@imba-tjd
imba-tjd / Readme.md
Last active February 14, 2023 01:50
Call UWP / WinRT API from Win32 without installing Windows SDK
@imba-tjd
imba-tjd / xargsu2a.cs
Last active July 8, 2023 13:54
A program helps pwsh to read from utf8 output.
// This program helps pwsh to read from external program output from utf8 encoding.
// Compile: bflat build xargsu2a.cs -Ot --no-debug-info --no-globalization --no-reflection --no-stacktrace-data
// License: MIT
using System;
using System.Diagnostics;
using System.Text;
if (args.Length == 0)
{
// https://github.com/abejfehr/URLDecode
// Compile: gcc -Ofast -mtune=native -D__USE_MINGW_ANSI_STDIO -o urld.exe
// When decoding UTF8-encoded data, work with [xargsu2a](https://gist.github.com/imba-tjd/0a5a41df029babc6c814efe5d9296593)
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char *urlDecode(const char *str) {
int d = 0;
@imba-tjd
imba-tjd / realpath.go
Last active February 2, 2023 03:21
realpath.exe for windows
// Git for Windows or git-bash contains a realpath.exe which is required by git code.editor
// However I'm using Git distribution from VS, which doesn't have it.
// Copying from Git for Windows isn't a good choice because it needs msys-2.0.dll
// This program offers similar function, which is enough for me.
// It won't resolve '~'. It works when path contains whitespaces.
package main
import (
"fmt"
// KP Backuper:帮助解决卡琳典狱长在非最低难度下强制覆盖自动保存的特性。
// 放到与nw.exe同一目录下。运行后每隔一分钟会检测存档有没有发生变化,如果变了,就备份到www/save_bak目录下。
// 并发安全性:读取时会加读锁,因此不会读到一半游戏自动保存导致读取内容错误,副作用是此次游戏的自动保存会不生效(静默失败);但本游戏保存很频繁,因此无影响。对于游戏保存到一半时本程序触发读,本程序会过一秒钟重试。
// 编译:C:\tools\bflat-7.0.1-windows-x64\bflat.exe build KPBackuper.cs -Os --no-globalization --no-reflection --no-stacktrace-data && upx --lzma KPBackuper.exe
using System;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
[DllImport("user32")]
@imba-tjd
imba-tjd / ngenize.cs
Last active February 22, 2023 12:31
A wrapper for ngen.exe
// LICENSE: MIT
using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
class Ngenize
{
const string NgenX86Path = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe";
const string NgenX64Path = @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe";
@imba-tjd
imba-tjd / check.c
Created January 11, 2023 10:45
SQLite Device Characteristics Check
/* SQLite Device Characteristics Check
Goal:
Tell you whether -DSQLITE_ENABLE_ATOMIC_WRITE has benefits on your filesystem.
However I don't konw much about sqlite so the logic may not be right.
Usage:
Download sqlite3 source code.
gcc check.c sqlite3.c -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_ATOMIC_WRITE -DSQLITE_OMIT_LOAD_EXTENSION