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 / pin-project-lite.rs
Created April 6, 2025 15:14
pin-project-lite usage
// pin-project-lite = { version = "0.2", default-features = false }
#[cfg(test)]
mod tests {
use std::future::Future;
#[tokio::test]
async fn test_ipstack_tcp_stream() {
let fut = foo();
let ret = Map::new(fut, |i| i * 2).await;
@ssrlive
ssrlive / executor.rs
Last active February 28, 2025 03:54
async learning
//
// This is a simple implementation of a futures executor and spawner.
// ref: https://www.bilibili.com/video/BV1Ki4y1C7gj/?vd_source=1658ea36cae9cc99e7417f13c4b86b2f&p=4&spm_id_from=333.788.videopod.episodes
//
// [dependencies]
// futures = "0.3"
//
use futures::{
future::{BoxFuture, FutureExt},
@ssrlive
ssrlive / learn-async.rs
Last active February 27, 2025 04:41
learn async for rust
//
// cargo test mytest -- --nocapture
//
#[cfg(test)]
mod tests {
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
@ssrlive
ssrlive / digit_overflow.rs
Last active February 18, 2025 05:05
Rust digit_overflow
#[test]
fn digit_overflow() {
assert_eq!(u16::MAX.wrapping_add(9), 8);
assert_eq!(u16::MAX.saturating_add(9), u16::MAX);
assert_eq!(u16::MAX.checked_add(9), None);
assert_eq!(u16::MAX.overflowing_add(9), (8, true));
}
#[test]
fn digit_overflow_mul() {
@ssrlive
ssrlive / sockaddr_union.rs
Created November 27, 2024 03:46
sockaddr_union
#[cfg(unix)]
#[repr(C)]
#[derive(Clone, Copy)]
pub union sockaddr_union {
pub addr_stor: libc::sockaddr_storage,
pub addr6: libc::sockaddr_in6,
pub addr4: libc::sockaddr_in,
pub addr: libc::sockaddr,
}
/*
Navicat MySQL Data Transfer
Source Server : 本地
Source Server Type : MySQL
Source Server Version : 50726
Source Host : localhost:3306
Source Schema : grade
Target Server Type : MySQL
@ssrlive
ssrlive / proxy.rs
Created July 14, 2024 04:30
macOS settings for proxy
//
// https://github.com/ShadowsocksR-Live/ssrMac/blob/master/ssr_mac_sysconf/main.m
//
use core_foundation::{
array::CFArray,
base::{TCFType, ToVoid},
dictionary::{CFDictionary, CFMutableDictionary},
number::CFNumber,
propertylist::CFPropertyList,
string::{CFString, CFStringRef},
@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);