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 { ...@@ -23,7 +23,9 @@ pub struct ConfigRouter {
pub dst_port: u16, pub dst_port: u16,
#[serde(deserialize_with = "deserialize_domain")] #[serde(deserialize_with = "deserialize_domain")]
pub family: Domain, pub family: Domain,
#[serde(default)]
pub mark: u32, pub mark: u32,
#[serde(default)]
pub endpoint: String, pub endpoint: String,
#[serde(deserialize_with = "deserialize_secret")] #[serde(deserialize_with = "deserialize_secret")]
pub local_secret: [u8; SECRET_LENGTH], 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 socket2::{Domain, Protocol, SockAddr, SockFilter, Socket, Type};
use std::net::Shutdown; use std::net::Shutdown;
use std::sync::Arc; use std::sync::Arc;
...@@ -14,10 +14,10 @@ use std::{ ...@@ -14,10 +14,10 @@ use std::{
use tun::Device; use tun::Device;
use crate::config::{ConfigRouter, Schema}; use crate::config::{ConfigRouter, Schema};
use crossbeam::epoch::{Atomic, pin}; use crossbeam::epoch::{pin, Atomic};
use libc::{ 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, 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,
sock_filter, sock_fprog, socklen_t, MSG_FASTOPEN, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF,
}; };
pub const SECRET_LENGTH: usize = 32; pub const SECRET_LENGTH: usize = 32;
...@@ -70,13 +70,17 @@ impl Router { ...@@ -70,13 +70,17 @@ impl Router {
match config.schema { match config.schema {
Schema::IP => { Schema::IP => {
let result = Socket::new(config.family, Type::RAW, Some(Protocol::from(config.proto as i32)))?; let result = Socket::new(config.family, Type::RAW, Some(Protocol::from(config.proto as i32)))?;
result.set_mark(config.mark)?; if config.mark != 0 {
result.set_mark(config.mark)?;
}
Self::attach_filter_ip(config, &result)?; Self::attach_filter_ip(config, &result)?;
Ok(result) Ok(result)
} }
Schema::UDP => { Schema::UDP => {
let result = Socket::new(config.family, Type::DGRAM, Some(Protocol::UDP))?; let result = Socket::new(config.family, Type::DGRAM, Some(Protocol::UDP))?;
result.set_mark(config.mark)?; if config.mark != 0 {
result.set_mark(config.mark)?;
}
if config.src_port != 0 { if config.src_port != 0 {
result.set_reuse_port(true)?; result.set_reuse_port(true)?;
let addr = Self::bind_addr(config); let addr = Self::bind_addr(config);
...@@ -105,7 +109,9 @@ impl Router { ...@@ -105,7 +109,9 @@ impl Router {
// 创建 socket 和 获取 endpoint 失败会 panic,连接失败会 error // 创建 socket 和 获取 endpoint 失败会 panic,连接失败会 error
let result = Socket::new(self.config.family, Type::STREAM, Some(Protocol::TCP)).unwrap(); let result = Socket::new(self.config.family, Type::STREAM, Some(Protocol::TCP)).unwrap();
result.set_tcp_nodelay(true).unwrap(); result.set_tcp_nodelay(true).unwrap();
result.set_mark(self.config.mark).unwrap(); if self.config.mark != 0 {
result.set_mark(self.config.mark).unwrap();
}
if self.config.src_port != 0 { if self.config.src_port != 0 {
result.set_reuse_address(true).unwrap(); result.set_reuse_address(true).unwrap();
let addr = Self::bind_addr(&self.config); 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