volter 0.0.178 → 0.0.180
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +141 -0
- package/CHANGELOG.md +118 -0
- package/CODE_OF_CONDUCT.md +89 -0
- package/CONTRIBUTING.md +38 -0
- package/Cargo.toml +10 -0
- package/SECURITY.md +63 -0
- package/SUPPORT.md +91 -0
- package/package.json +32 -15
- package/public/cover.png +0 -0
- package/src/crypto.ts +157 -0
- package/src/error.ts +51 -0
- package/src/index.ts +4 -0
- package/src/main.c +1 -0
- package/src/main.rs +172 -0
- package/src/network.ts +53 -0
- package/src/sessions.ts +287 -0
- package/src/std.ts +82 -0
- package/src/utils.rs +249 -0
- package/src/utils.ts +120 -0
- package/tsconfig.json +34 -0
- package/dist/crypto.js +0 -1
- package/dist/error.js +0 -40
- package/dist/index.js +0 -69
- package/dist/network.js +0 -1
- package/dist/sessions.js +0 -69
- package/dist/std.js +0 -1
- package/dist/utils.js +0 -41
- package/types/crypto.d.ts +0 -19
- package/types/error.d.ts +0 -32
- package/types/index.d.ts +0 -4
- package/types/network.d.ts +0 -46
- package/types/sessions.d.ts +0 -76
- package/types/std.d.ts +0 -46
- package/types/utils.d.ts +0 -39
package/src/crypto.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export function hash(password: string, hmac?: string): string {
|
|
2
|
+
const hasher = new Bun.CryptoHasher("sha256", hmac)
|
|
3
|
+
return hasher.update(password).digest("hex")
|
|
4
|
+
}
|
|
5
|
+
export async function hash_async(password: string): Promise<string> {
|
|
6
|
+
const encoded = new TextEncoder().encode(password)
|
|
7
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", encoded)
|
|
8
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
|
9
|
+
return hashArray.map(b => b.toString(16).padStart(2, "0")).join("")
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CipherText {
|
|
13
|
+
text: string
|
|
14
|
+
iv: Uint8Array<ArrayBuffer>
|
|
15
|
+
buffer: ArrayBuffer
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function cryptoKey(raw?: CryptoKey | Uint8Array<ArrayBuffer> | string): Promise<CryptoKey> {
|
|
19
|
+
if (raw) {
|
|
20
|
+
if (raw instanceof CryptoKey) return raw
|
|
21
|
+
if (raw instanceof Uint8Array) {
|
|
22
|
+
if (raw.length === 32) {
|
|
23
|
+
return await crypto.subtle.importKey("raw", raw, { name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"])
|
|
24
|
+
}
|
|
25
|
+
return await crypto.subtle.importKey("raw", raw, { name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"])
|
|
26
|
+
}
|
|
27
|
+
if (raw.length === 64) {
|
|
28
|
+
return await crypto.subtle.importKey(
|
|
29
|
+
"raw",
|
|
30
|
+
Uint8Array.fromHex(raw),
|
|
31
|
+
{
|
|
32
|
+
name: "AES-GCM",
|
|
33
|
+
length: 256,
|
|
34
|
+
},
|
|
35
|
+
true,
|
|
36
|
+
["encrypt", "decrypt"],
|
|
37
|
+
)
|
|
38
|
+
} else {
|
|
39
|
+
return await crypto.subtle.importKey(
|
|
40
|
+
"raw",
|
|
41
|
+
Uint8Array.fromHex(hash(raw)),
|
|
42
|
+
{
|
|
43
|
+
name: "AES-GCM",
|
|
44
|
+
length: 256,
|
|
45
|
+
},
|
|
46
|
+
true,
|
|
47
|
+
["encrypt", "decrypt"],
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
return await crypto.subtle.generateKey(
|
|
52
|
+
{
|
|
53
|
+
name: "AES-GCM",
|
|
54
|
+
length: 256,
|
|
55
|
+
},
|
|
56
|
+
true,
|
|
57
|
+
["encrypt", "decrypt"],
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export async function exportKey(key: CryptoKey, format: "raw" | "pkcs8" | "spki") {
|
|
62
|
+
const buffer = await crypto.subtle.exportKey(format, key)
|
|
63
|
+
return new Uint8Array(buffer).toBase64()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function generateECDSAKeyPair(): Promise<CryptoKeyPair> {
|
|
67
|
+
return await crypto.subtle.generateKey(
|
|
68
|
+
{
|
|
69
|
+
name: "ECDSA",
|
|
70
|
+
namedCurve: "P-256", // SECG curve
|
|
71
|
+
},
|
|
72
|
+
true,
|
|
73
|
+
["sign", "verify"],
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function exportJWK(key: CryptoKey): Promise<JsonWebKey> {
|
|
78
|
+
return await crypto.subtle.exportKey("jwk", key)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function importJWK(jwk: JsonWebKey, usage: KeyUsage[]): Promise<CryptoKey> {
|
|
82
|
+
return await crypto.subtle.importKey("jwk", jwk, { name: "ECDSA", namedCurve: "P-256" }, true, usage)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function sign(message: string, key: CryptoKey): Promise<Uint8Array<ArrayBuffer>> {
|
|
86
|
+
const data = new TextEncoder().encode(message)
|
|
87
|
+
|
|
88
|
+
const signature = await crypto.subtle.sign(
|
|
89
|
+
{
|
|
90
|
+
name: "ECDSA",
|
|
91
|
+
hash: { name: "SHA-256" },
|
|
92
|
+
},
|
|
93
|
+
key,
|
|
94
|
+
data,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
return new Uint8Array(signature)
|
|
98
|
+
}
|
|
99
|
+
export async function verifySign(message: string, signature: Uint8Array<ArrayBuffer>, key: CryptoKey): Promise<boolean> {
|
|
100
|
+
const data = new TextEncoder().encode(message)
|
|
101
|
+
|
|
102
|
+
return await crypto.subtle.verify(
|
|
103
|
+
{
|
|
104
|
+
name: "ECDSA",
|
|
105
|
+
hash: { name: "SHA-256" },
|
|
106
|
+
},
|
|
107
|
+
key,
|
|
108
|
+
signature,
|
|
109
|
+
data,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function cipher(text: string, key: CryptoKey | string): Promise<CipherText> {
|
|
114
|
+
const encoder = new TextEncoder()
|
|
115
|
+
|
|
116
|
+
const data = encoder.encode(text)
|
|
117
|
+
const iv = crypto.getRandomValues(new Uint8Array(12))
|
|
118
|
+
const buffer = await crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, await cryptoKey(key), data)
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
text: new Uint8Array(buffer).toHex(),
|
|
122
|
+
iv: iv,
|
|
123
|
+
buffer: buffer,
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export async function decipher(cipher: CipherText, key: CryptoKey | string) {
|
|
127
|
+
const decoder = new TextDecoder()
|
|
128
|
+
|
|
129
|
+
const buffer = await crypto.subtle.decrypt({ name: "AES-GCM", iv: cipher.iv }, await cryptoKey(key), Uint8Array.fromHex(cipher.text))
|
|
130
|
+
|
|
131
|
+
const data = decoder.decode(buffer)
|
|
132
|
+
|
|
133
|
+
return data
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export async function keypair(): Promise<CryptoKeyPair> {
|
|
137
|
+
return await crypto.subtle.generateKey(
|
|
138
|
+
{
|
|
139
|
+
name: "RSA-OAEP",
|
|
140
|
+
modulusLength: 2048,
|
|
141
|
+
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // 65537
|
|
142
|
+
hash: { name: "SHA-256" },
|
|
143
|
+
},
|
|
144
|
+
true,
|
|
145
|
+
["encrypt", "decrypt"],
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
export async function encrypt(text: string, key: CryptoKey): Promise<Uint8Array<ArrayBuffer>> {
|
|
149
|
+
const data = new TextEncoder().encode(text)
|
|
150
|
+
const buffer = await crypto.subtle.encrypt({ name: "RSA-OAEP" }, key, data)
|
|
151
|
+
return new Uint8Array(buffer)
|
|
152
|
+
}
|
|
153
|
+
export async function decrypt(text: Uint8Array<ArrayBuffer>, key: CryptoKey): Promise<string> {
|
|
154
|
+
const buffer = await crypto.subtle.decrypt({ name: "RSA-OAEP" }, key, text)
|
|
155
|
+
const data = new TextDecoder().decode(buffer)
|
|
156
|
+
return data
|
|
157
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export enum ErrorCodes {
|
|
2
|
+
// Server
|
|
3
|
+
INTERNAL_SERVER_ERROR,
|
|
4
|
+
SERVICE_UNAVAILABLE,
|
|
5
|
+
|
|
6
|
+
// Client
|
|
7
|
+
VALIDATION_FAILED,
|
|
8
|
+
TOO_MANY_REQUESTS,
|
|
9
|
+
|
|
10
|
+
// Resource
|
|
11
|
+
ALREADY_EXISTS,
|
|
12
|
+
RESOURCE_NOT_FOUND,
|
|
13
|
+
|
|
14
|
+
// A&A
|
|
15
|
+
AUTHORIZATION_REQUIRED,
|
|
16
|
+
AUTHENTICATION_FAILED,
|
|
17
|
+
INVALID_CREDENTIALS,
|
|
18
|
+
INVALID_TOKEN,
|
|
19
|
+
EXPIRED_TOKEN,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ServerErrorOptions {
|
|
23
|
+
cause?: unknown
|
|
24
|
+
path?: string[]
|
|
25
|
+
code?: ErrorCodes
|
|
26
|
+
}
|
|
27
|
+
export class ServerError extends Error {
|
|
28
|
+
constructor(message: string, options?: ServerErrorOptions) {
|
|
29
|
+
super(message, {
|
|
30
|
+
cause: options?.cause,
|
|
31
|
+
})
|
|
32
|
+
this.message = message
|
|
33
|
+
this.cause = options?.cause
|
|
34
|
+
this.path = options?.path
|
|
35
|
+
this.code = options?.code ?? ErrorCodes.INTERNAL_SERVER_ERROR
|
|
36
|
+
this.occurred = new Date()
|
|
37
|
+
}
|
|
38
|
+
name = "ServerError"
|
|
39
|
+
message: string
|
|
40
|
+
cause?: unknown
|
|
41
|
+
path?: string[]
|
|
42
|
+
code: ErrorCodes
|
|
43
|
+
occurred: Date
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class UniqueError extends ServerError {
|
|
47
|
+
name = "UniqueError"
|
|
48
|
+
code: ErrorCodes = ErrorCodes.ALREADY_EXISTS
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { ZodError as ValidationError } from "zod"
|
package/src/index.ts
ADDED
package/src/main.c
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#include "stdio.h"
|
package/src/main.rs
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
mod utils;
|
|
2
|
+
|
|
3
|
+
use crate::utils::{DateTimeExt, Socket, SocketAddrEnv};
|
|
4
|
+
use chrono::Utc;
|
|
5
|
+
use colored::Colorize;
|
|
6
|
+
use std::{
|
|
7
|
+
net::SocketAddr,
|
|
8
|
+
sync::{
|
|
9
|
+
Arc,
|
|
10
|
+
atomic::{AtomicUsize, Ordering},
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
use tokio::{
|
|
14
|
+
io::{AsyncReadExt, AsyncWriteExt, copy_bidirectional},
|
|
15
|
+
net::{TcpListener, TcpStream, UdpSocket, windows::named_pipe::ClientOptions},
|
|
16
|
+
spawn,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
#[allow(dead_code)]
|
|
20
|
+
async fn process_tcp(mut stream: TcpStream) -> tokio::io::Result<()> {
|
|
21
|
+
// info!("{}", StreamEvent::new(StreamEventLevel::Open, addr));
|
|
22
|
+
let mut client = TcpStream::connect("127.0.0.1:843").await?;
|
|
23
|
+
copy_bidirectional(&mut stream, &mut client).await?;
|
|
24
|
+
// info!("{}", StreamEvent::new(StreamEventLevel::Close, addr));
|
|
25
|
+
Ok(())
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
#[allow(dead_code)]
|
|
29
|
+
async fn process_udp(mut stream: TcpStream, target: &String) -> tokio::io::Result<()> {
|
|
30
|
+
stream.set_nodelay(true)?;
|
|
31
|
+
let datagram = UdpSocket::bind("0.0.0.0:0").await?;
|
|
32
|
+
datagram.connect(target).await?;
|
|
33
|
+
|
|
34
|
+
let mut buf = [0u8; 8192];
|
|
35
|
+
|
|
36
|
+
let n = stream.read(&mut buf).await?;
|
|
37
|
+
datagram.send(&buf[..n]).await?;
|
|
38
|
+
|
|
39
|
+
let n = datagram.recv(&mut buf).await?;
|
|
40
|
+
stream.write_all(&buf[..n]).await?;
|
|
41
|
+
Ok(())
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async fn process_raw(mut stream: TcpStream) -> tokio::io::Result<()> {
|
|
45
|
+
stream.set_nodelay(true)?;
|
|
46
|
+
stream
|
|
47
|
+
.write_all("HTTP/1.1 200 OK\r\n\r\nHello, world!".as_bytes())
|
|
48
|
+
.await?;
|
|
49
|
+
stream.shutdown().await?;
|
|
50
|
+
Ok(())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#[allow(dead_code)]
|
|
54
|
+
async fn process_pipe(mut stream: TcpStream) -> tokio::io::Result<()> {
|
|
55
|
+
stream.set_nodelay(true)?;
|
|
56
|
+
// info!("{}", StreamEvent::new(StreamEventLevel::Open, addr));
|
|
57
|
+
let mut pipe = ClientOptions::new().open("//./pipe/socket")?;
|
|
58
|
+
copy_bidirectional(&mut stream, &mut pipe).await?;
|
|
59
|
+
|
|
60
|
+
/*
|
|
61
|
+
let mut buf = [0; 1024];
|
|
62
|
+
|
|
63
|
+
let n = stream.read(&mut buf).await?;
|
|
64
|
+
pipe.write_all(&buf[..n]).await?;
|
|
65
|
+
// info!("{}", StreamEvent::new(StreamEventLevel::Data, addr));
|
|
66
|
+
|
|
67
|
+
let n = pipe.read(&mut buf).await?;
|
|
68
|
+
stream.write_all(&buf[..n]).await?;
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
// info!("{}", StreamEvent::new(StreamEventLevel::Close, addr));
|
|
72
|
+
Ok(())
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#[allow(dead_code)]
|
|
76
|
+
async fn process_udp_select(mut stream: TcpStream) -> tokio::io::Result<()> {
|
|
77
|
+
let datagram = UdpSocket::bind("0.0.0.0:0").await?;
|
|
78
|
+
datagram.connect("127.0.0.1:843").await?;
|
|
79
|
+
|
|
80
|
+
let mut req = [0u8; 8192];
|
|
81
|
+
let mut res = [0u8; 8192];
|
|
82
|
+
|
|
83
|
+
tokio::select! {
|
|
84
|
+
// TCP -> UDP (Request)
|
|
85
|
+
n = stream.read(&mut req) => {
|
|
86
|
+
let n = n?;
|
|
87
|
+
if n == 0 { () }
|
|
88
|
+
datagram.send(&req[..n]).await?;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// UDP -> TCP (Response)
|
|
92
|
+
n = datagram.recv(&mut res) => {
|
|
93
|
+
let n = n?;
|
|
94
|
+
// Note: UDP 0-byte packets are valid, so we just check for errors
|
|
95
|
+
stream.write_all(&res[..n]).await?;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
Ok(())
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#[tokio::main]
|
|
102
|
+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
103
|
+
let address = SocketAddr::env();
|
|
104
|
+
let listener = TcpListener::bind(address).await?;
|
|
105
|
+
info!("Listening to {}...", address);
|
|
106
|
+
|
|
107
|
+
let targets = vec!["127.0.0.1:3001".to_string(), "127.0.0.1:3002".to_string()];
|
|
108
|
+
let targets = Arc::new(targets);
|
|
109
|
+
let current_index = Arc::new(AtomicUsize::new(0));
|
|
110
|
+
|
|
111
|
+
let connection = Socket::RDMA;
|
|
112
|
+
|
|
113
|
+
match connection {
|
|
114
|
+
Socket::TCP => loop {
|
|
115
|
+
match listener.accept().await {
|
|
116
|
+
Ok((stream, _addr)) => {
|
|
117
|
+
spawn(async move {
|
|
118
|
+
if let Err(e) = process_tcp(stream).await {
|
|
119
|
+
error!("{:?}", e)
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
Err(e) => error!("{:?}", e),
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
Socket::UDP => loop {
|
|
127
|
+
match listener.accept().await {
|
|
128
|
+
Ok((stream, _addr)) => {
|
|
129
|
+
let targets = Arc::clone(&targets);
|
|
130
|
+
let index_ptr = Arc::clone(¤t_index);
|
|
131
|
+
spawn(async move {
|
|
132
|
+
let idx = index_ptr.fetch_add(1, Ordering::Relaxed) % targets.len();
|
|
133
|
+
if let Err(e) = process_udp(stream, &targets[idx]).await {
|
|
134
|
+
error!("{:?}", e)
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
Err(e) => error!("{:?}", e),
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
Socket::UDS => loop {
|
|
142
|
+
match listener.accept().await {
|
|
143
|
+
Ok((_stream, _addr)) => {}
|
|
144
|
+
Err(e) => error!("{:?}", e),
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
Socket::PIPE => loop {
|
|
148
|
+
match listener.accept().await {
|
|
149
|
+
Ok((stream, _addr)) => {
|
|
150
|
+
spawn(async move {
|
|
151
|
+
if let Err(e) = process_pipe(stream).await {
|
|
152
|
+
error!("{:?}", e)
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
Err(e) => error!("{:?}", e),
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
Socket::RDMA => loop {
|
|
160
|
+
match listener.accept().await {
|
|
161
|
+
Ok((stream, _addr)) => {
|
|
162
|
+
spawn(async move {
|
|
163
|
+
if let Err(e) = process_raw(stream).await {
|
|
164
|
+
error!("{:?}", e)
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
Err(e) => error!("{:?}", e),
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
}
|
package/src/network.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ServerError } from "./error"
|
|
2
|
+
|
|
3
|
+
export interface APIResponse {
|
|
4
|
+
data?: unknown
|
|
5
|
+
error?: ServerError[]
|
|
6
|
+
success?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type ID = number
|
|
10
|
+
|
|
11
|
+
export enum Method {
|
|
12
|
+
GET,
|
|
13
|
+
POST,
|
|
14
|
+
PUT,
|
|
15
|
+
DELETE,
|
|
16
|
+
PATCH,
|
|
17
|
+
HEAD,
|
|
18
|
+
OPTIONS,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export enum Event {
|
|
22
|
+
MESSAGE,
|
|
23
|
+
OPEN,
|
|
24
|
+
CLOSE,
|
|
25
|
+
DRAIN,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface LogHTTP {
|
|
29
|
+
id: ID
|
|
30
|
+
method: Method // what?
|
|
31
|
+
url: URL // where?
|
|
32
|
+
status: number // why?
|
|
33
|
+
ip: string // who?
|
|
34
|
+
user_agent?: string
|
|
35
|
+
size: number // how?
|
|
36
|
+
created: Date // when?
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface LogWS {
|
|
40
|
+
id: ID
|
|
41
|
+
event: Event // what?
|
|
42
|
+
url: URL // where?
|
|
43
|
+
ip: string // who?
|
|
44
|
+
user_agent?: string
|
|
45
|
+
size: number // how?
|
|
46
|
+
created: Date // when?
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface SocketConnection {
|
|
50
|
+
id: ID
|
|
51
|
+
ip: string
|
|
52
|
+
created: Date
|
|
53
|
+
}
|