Skip to content

Instantly share code, notes, and snippets.

View ssrlive's full-sized avatar

ssrlive

  • telegram: realssrlive
  • ssrlivebox(at)gmail(dot)com
View GitHub Profile
@ssrlive
ssrlive / notify.rs
Created June 16, 2024 05:47
tokio::sync::Notify usage
use std::sync::Arc;
use tokio::sync::Notify;
#[tokio::main]
async fn main() {
let notify = Arc::new(Notify::new());
// 注册两个等候者
let notified1 = notify.notified();
let notified2 = notify.notified();
@ssrlive
ssrlive / DnsProxy.h
Last active May 2, 2024 16:51
DnsProxy class: DNS proxy for DoH, implement with Objective-C
//
// DnsProxy.h
//
#import <Foundation/Foundation.h>
/**
* DNS Proxy
*
* This class is responsible for setting up a DNS proxy server that listens on a local port and
@ssrlive
ssrlive / icmp.rs
Created April 23, 2024 11:44
ICMP demo code
fn handle_icmp_echo_request(u: &ipstack::stream::IpStackUnknownTransport) -> Result<()> {
use etherparse::{IcmpEchoHeader, Icmpv4Header, Icmpv6Header, IpNumber};
assert!(u.ip_protocol() == IpNumber::ICMP || u.ip_protocol() == IpNumber::IPV6_ICMP);
if u.src_addr().is_ipv4() {
let (icmp_header, req_payload) = Icmpv4Header::from_slice(u.payload()).map_err(|e| Error::from(e.to_string()))?;
if let etherparse::Icmpv4Type::EchoRequest(req) = icmp_header.icmp_type {
log::info!("#0 ICMPv4 echo");
let echo = IcmpEchoHeader { id: req.id, seq: req.seq };
let mut resp = Icmpv4Header::new(etherparse::Icmpv4Type::EchoReply(echo));
resp.update_checksum(req_payload);
@ssrlive
ssrlive / socketpair.c
Created March 28, 2024 05:44
socketpair usage
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
void child(int sock) {
char buf[1024];
while (1) {
ssize_t n = read(sock, buf, sizeof(buf));
if (n <= 0) {
@ssrlive
ssrlive / acl.rs
Last active March 8, 2024 04:35
Generate ACL file
use log::{info, LevelFilter};
use reqwest;
use serde_json::Value;
use std::fs::File;
use std::io::prelude::*;
use std::time::SystemTime;
use tokio;
// https://github.com/shadowsocks/shadowsocks-rust/blob/master/acl/genacl_proxy_gfw_bypass_china_ip.py
@ssrlive
ssrlive / raw_async.rs
Created February 13, 2024 13:51
Tiny async raw demo
use core::{
future::Future,
pin::Pin,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};
use std::collections::VecDeque;
fn dummy_raw_waker() -> RawWaker {
fn no_op(_: *const ()) {}
fn clone(_: *const ()) -> RawWaker {
@ssrlive
ssrlive / macos-utun.c
Created October 4, 2023 05:46
utun demo in macos
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sys_domain.h>
#include <sys/kern_control.h>
#include <net/if_utun.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
@ssrlive
ssrlive / macos-gateway.rs
Created October 2, 2023 16:08
macos gateway
use libc::{
c_int, c_long, c_uint, c_void, freeifaddrs, getifaddrs, ifaddrs, sockaddr, sockaddr_in,
sockaddr_in6, AF_INET, AF_INET6, IFF_BROADCAST, IFF_LOOPBACK, IFF_MULTICAST, IFF_RUNNING,
IFF_UP, RTF_GATEWAY,
};
use std::{
ffi::CStr,
io, mem,
net::{IpAddr, Ipv4Addr},
ptr,
@ssrlive
ssrlive / macos-route.md
Last active October 2, 2023 08:41
macOS 路由表 tips
# 为虚拟网络接口 utun0 配置 IP 地址 10.0.0.33 和子网掩码 255.255.255.0 网关地址 10.0.0.1
sudo ifconfig utun0 10.0.0.33 10.0.0.1 netmask 255.255.255.0

# 配置目标地址是网段 192.168.1.0/24 的流量走 10.0.0.1 网关
sudo route add 192.168.1.0/24 10.0.0.1

# 将 6.11.20.10 这个单一地址(因为掩码是 32)路由到 192.168.4.1 这个网关
sudo route add 6.11.20.10/32 192.168.4.1
@ssrlive
ssrlive / echo.rs
Last active February 9, 2024 08:37
Echo server TCP and UDP
//
// tokio = { version = "1.36", features = ["full"] }
//
use std::{env, error::Error};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, ToSocketAddrs, UdpSocket},
};
const TCP_TIMEOUT: u64 = 10 * 1000; // 10sec