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 / 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 September 26, 2024 00:16
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
@ssrlive
ssrlive / iocp.rs
Last active April 4, 2024 09:24
CreateIoCompletionPort
//
// [dependencies]
// windows = { version = "0.51", features = [
// "Win32_System_SystemInformation",
// "Win32_Networking_WinSock",
// "Win32_System_Memory",
// "Win32_Foundation",
// "Win32_System_IO",
// "Win32_System_Threading",
// "Win32_Security",
@ssrlive
ssrlive / iocp.c
Created September 19, 2023 15:35
CreateIoCompletionPort
#include <WINSOCK2.H>
#include <stdio.h>
#define PORT 5150
#define MSGSIZE 1024
#pragma comment(lib, "ws2_32.lib")
typedef enum {
RECV_POSTED
} OPERATION_TYPE;
use smoltcp::wire::{IpProtocol, Ipv4Packet, Ipv6Packet};
fn extract_udp_packet(packet: &[u8]) -> Result<UdpPacket, Box<dyn std::error::Error>> {
if let Ok(ipv4_packet) = Ipv4Packet::new_checked(packet) {
let src_addr = ipv4_packet.src_addr();
let dst_addr = ipv4_packet.dst_addr();
let protocol = ipv4_packet.next_header();
let payload = ipv4_packet.payload();
if protocol == IpProtocol::Udp {
let udp_packet = smoltcp::wire::UdpPacket::new_checked(payload)?;