-
Notifications
You must be signed in to change notification settings - Fork 70
/
config.rs
173 lines (148 loc) · 5.03 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::sync::Arc;
use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use http::StatusCode;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use tracing::warn;
use crate::{
app::{
api::AppState,
dispatcher,
dns::ThreadSafeDNSResolver,
inbound::manager::{Ports, ThreadSafeInboundManager},
},
config::{def, internal::config::BindAddress},
GlobalState,
};
#[derive(Clone)]
struct ConfigState {
inbound_manager: ThreadSafeInboundManager,
dispatcher: Arc<dispatcher::Dispatcher>,
global_state: Arc<Mutex<GlobalState>>,
dns_resolver: ThreadSafeDNSResolver,
}
pub fn routes(
inbound_manager: ThreadSafeInboundManager,
dispatcher: Arc<dispatcher::Dispatcher>,
global_state: Arc<Mutex<GlobalState>>,
dns_resolver: ThreadSafeDNSResolver,
) -> Router<Arc<AppState>> {
Router::new()
.route(
"/",
get(get_configs).put(update_configs).patch(patch_configs),
)
.with_state(ConfigState {
inbound_manager,
dispatcher,
global_state,
dns_resolver,
})
}
async fn get_configs(State(state): State<ConfigState>) -> impl IntoResponse {
let inbound_manager = state.inbound_manager.lock().await;
let run_mode = state.dispatcher.get_mode().await;
let global_state = state.global_state.lock().await;
let dns_resolver = state.dns_resolver;
let ports = inbound_manager.get_ports();
axum::response::Json(ConfigRequest {
port: ports.port,
socks_port: ports.socks_port,
redir_port: ports.redir_port,
tproxy_port: ports.tproxy_port,
mixed_port: ports.mixed_port,
bind_address: Some(inbound_manager.get_bind_address().to_string()),
mode: Some(run_mode),
log_level: Some(global_state.log_level),
ipv6: Some(dns_resolver.ipv6()),
allow_lan: Some(match inbound_manager.get_bind_address() {
BindAddress::Any => true,
BindAddress::One(one) => match one {
crate::proxy::utils::Interface::IpAddr(ip) => !ip.is_loopback(),
crate::proxy::utils::Interface::Name(iface) => iface != "lo",
},
}),
})
}
async fn update_configs() -> impl IntoResponse {
(
StatusCode::NOT_IMPLEMENTED,
axum::response::Json("don't do this please"),
)
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct ConfigRequest {
port: Option<u16>,
socks_port: Option<u16>,
redir_port: Option<u16>,
tproxy_port: Option<u16>,
mixed_port: Option<u16>,
bind_address: Option<String>,
mode: Option<def::RunMode>,
log_level: Option<def::LogLevel>,
ipv6: Option<bool>,
allow_lan: Option<bool>,
}
impl ConfigRequest {
fn rebuild_listeners(&self) -> bool {
self.port.is_some()
|| self.socks_port.is_some()
|| self.redir_port.is_some()
|| self.tproxy_port.is_some()
|| self.mixed_port.is_some()
|| self.bind_address.is_some()
}
}
async fn patch_configs(
State(state): State<ConfigState>,
Json(payload): Json<ConfigRequest>,
) -> impl IntoResponse {
if payload.allow_lan.is_some() {
warn!("setting allow_lan doesn't do anything. please set bind_address to a LAN address instead.");
}
let mut inbound_manager = state.inbound_manager.lock().await;
if let Some(bind_address) = payload.bind_address.clone() {
match bind_address.parse::<BindAddress>() {
Ok(bind_address) => {
inbound_manager.set_bind_address(bind_address);
}
Err(_) => {
return (
StatusCode::BAD_REQUEST,
format!("invalid bind address: {}", bind_address),
)
.into_response();
}
}
}
let mut global_state = state.global_state.lock().await;
if payload.rebuild_listeners() {
// TODO: maybe buggy
let current_ports = inbound_manager.get_ports();
let ports = Ports {
port: payload.port.or(current_ports.port),
socks_port: payload.socks_port.or(current_ports.socks_port),
redir_port: payload.redir_port.or(current_ports.redir_port),
tproxy_port: payload.tproxy_port.or(current_ports.tproxy_port),
mixed_port: payload.mixed_port.or(current_ports.mixed_port),
};
inbound_manager.rebuild_listeners(ports);
global_state
.inbound_listener_handle
.take()
.map(|h| h.abort());
let r = inbound_manager.get_runner().unwrap();
global_state.inbound_listener_handle = Some(tokio::spawn(r));
}
if let Some(mode) = payload.mode {
state.dispatcher.set_mode(mode).await;
}
if let Some(log_level) = payload.log_level {
global_state.log_level = log_level;
}
if let Some(ipv6) = payload.ipv6 {
state.dns_resolver.set_ipv6(ipv6);
}
StatusCode::ACCEPTED.into_response()
}