Commit dc799bee authored by nanamicat's avatar nanamicat

config

parent 64c6814d
Pipeline #41945 passed with stages
in 1 minute and 16 seconds
......@@ -23,7 +23,9 @@ pub struct ConfigRouter {
pub dst_port: u16,
#[serde(deserialize_with = "deserialize_domain")]
pub family: Domain,
#[serde(default)]
pub mark: u32,
#[serde(default)]
pub endpoint: String,
#[serde(deserialize_with = "deserialize_secret")]
pub local_secret: [u8; SECRET_LENGTH],
......
use anyhow::{Result, bail, ensure};
use anyhow::{bail, ensure, Result};
use socket2::{Domain, Protocol, SockAddr, SockFilter, Socket, Type};
use std::net::Shutdown;
use std::sync::Arc;
......@@ -14,10 +14,10 @@ use std::{
use tun::Device;
use crate::config::{ConfigRouter, Schema};
use crossbeam::epoch::{Atomic, pin};
use crossbeam::epoch::{pin, Atomic};
use libc::{
BPF_ABS, BPF_B, BPF_IND, BPF_JEQ, BPF_JMP, BPF_K, BPF_LD, BPF_LDX, BPF_MSH, BPF_RET, BPF_W, MSG_FASTOPEN, SO_ATTACH_REUSEPORT_CBPF, SOL_SOCKET, setsockopt,
sock_filter, sock_fprog, socklen_t,
setsockopt, sock_filter, sock_fprog, socklen_t, BPF_ABS, BPF_B, BPF_IND, BPF_JEQ, BPF_JMP, BPF_K, BPF_LD, BPF_LDX, BPF_MSH, BPF_RET, BPF_W,
MSG_FASTOPEN, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF,
};
pub const SECRET_LENGTH: usize = 32;
......@@ -70,13 +70,17 @@ impl Router {
match config.schema {
Schema::IP => {
let result = Socket::new(config.family, Type::RAW, Some(Protocol::from(config.proto as i32)))?;
if config.mark != 0 {
result.set_mark(config.mark)?;
}
Self::attach_filter_ip(config, &result)?;
Ok(result)
}
Schema::UDP => {
let result = Socket::new(config.family, Type::DGRAM, Some(Protocol::UDP))?;
if config.mark != 0 {
result.set_mark(config.mark)?;
}
if config.src_port != 0 {
result.set_reuse_port(true)?;
let addr = Self::bind_addr(config);
......@@ -105,7 +109,9 @@ impl Router {
// 创建 socket 和 获取 endpoint 失败会 panic,连接失败会 error
let result = Socket::new(self.config.family, Type::STREAM, Some(Protocol::TCP)).unwrap();
result.set_tcp_nodelay(true).unwrap();
if self.config.mark != 0 {
result.set_mark(self.config.mark).unwrap();
}
if self.config.src_port != 0 {
result.set_reuse_address(true).unwrap();
let addr = Self::bind_addr(&self.config);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment