Commit 72201d5f authored by chenhuaqing's avatar chenhuaqing

fixed configuration without token

parent 51f9f415
......@@ -1220,8 +1220,8 @@ impl Config {
nsvr.set_timeout(timeout);
}
if method.is_none() {
nsvr.set_token(config.token.expect("token"));
if let Some(token) = config.token {
nsvr.set_token(token);
nsvr.set_retry_limit(config.retry_limit.expect("retry_limit"));
nsvr.set_auth_timeout(config.auth_timeout.expect("auth_timeout"));
nsvr.set_beats_interval(config.beats_interval.expect("beats_interval"));
......
......@@ -503,6 +503,7 @@ impl DnsClient {
message = result;
message.set_id(request.id());
} else {
error!("acl_lookup error {:?}", r);
message.set_response_code(ResponseCode::ServFail);
}
}
......
......@@ -79,10 +79,12 @@ impl DnsClient {
connect_opts: &ConnectOpts,
flow_stat: Arc<FlowStat>,
) -> io::Result<DnsClient> {
let stream = ProxyClientStream::connect_with_opts_map(context, svr_cfg, ns, connect_opts, |s| {
let auth_context = context.auth_context().clone();
let mut stream = ProxyClientStream::connect_with_opts_map(context, svr_cfg, ns, connect_opts, |s| {
MonProxyStream::from_stream(s, flow_stat)
})
.await?;
stream.write_buf(&mut auth_context.get_token().as_bytes()).await?;
Ok(DnsClient::TcpRemote { stream })
}
......@@ -135,6 +137,7 @@ impl DnsClient {
DnsClient::UnixStream { ref mut stream } => stream_query(stream, msg).await,
DnsClient::TcpRemote { ref mut stream } => stream_query(stream, msg).await,
DnsClient::UdpRemote { ref mut socket, ref ns } => {
trace!("send dns query to remote server {}", ns);
let bytes = msg.to_vec()?;
socket.send(ns, &bytes).await?;
......
......@@ -117,13 +117,6 @@ pub async fn run(mut config: Config) -> io::Result<()> {
assert!(!config.local.is_empty(), "no valid local server configuration");
if let Some(svr_cfg) = config.server.first() {
context
.context_ref()
.auth_context()
.update_token(svr_cfg.token().expect("token"));
}
let context = Arc::new(context);
let vfut = FuturesUnordered::new();
......@@ -157,6 +150,9 @@ pub async fn run(mut config: Config) -> io::Result<()> {
if let Some(watched_config) = config.watched_config {
for server in &config.server {
if let Some(token) = server.token() {
context.context_ref().auth_context().update_token(token);
// Start monitor for token based server
if let Some(auth_stream) = authenticate_server(&server, context.clone()).await? {
vfut.push(
......@@ -175,6 +171,7 @@ pub async fn run(mut config: Config) -> io::Result<()> {
);
}
}
}
// Load balancer will check all servers' score before server's actual start.
// So we have to ensure all plugins have been started before that.
......@@ -379,7 +376,7 @@ async fn authenticate_server(
use tokio::time;
// connect to remote server
if svr_cfg.method().is_none() {
if svr_cfg.token().is_some() {
let context = svr_context.clone().context();
debug!("connect to {}", svr_cfg.addr());
let mut auth_stream = OutboundTcpStream::connect_server_with_opts(
......@@ -446,7 +443,7 @@ async fn create_auth_stream_monitor(
debug!("prepare send packet to remote server");
while retry_count > 0 {
let mut req_buf = BytesMut::with_capacity(1);
req_buf.put_i32(1);
req_buf.put_u8(1);
match time::timeout(auth_timeout, stream.write_buf(&mut req_buf.freeze())).await {
Ok(res) => {
if let Err(err) = res {
......
......@@ -160,7 +160,7 @@ impl Socks5TcpHandler {
}
};
if svr_cfg.method().is_none() {
if svr_cfg.token().is_some() {
let token = self.context.context_ref().auth_context().get_token();
trace!("stream send token {} to remote server", token);
remote.write_buf(&mut token.as_bytes()).await?;
......
......@@ -228,6 +228,12 @@ where
self.enc.poll_write_encrypted(cx, &mut self.stream, buf)
}
/// Attempt to write data to `stream`
#[inline]
pub fn poll_write(&mut self, cx: &mut task::Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
Pin::new(&mut self.stream).poll_write(cx, buf)
}
/// Polls `flush` on the underlying stream
#[inline]
pub fn poll_flush(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
......
......@@ -184,9 +184,9 @@ where
fn poll_write(self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &[u8]) -> Poll<Result<usize, io::Error>> {
let mut this = self.project();
if !*this.authed {
if this.context.auth_context().get_token_len() > 0 && !*this.authed {
*this.authed = true;
return this.stream.poll_write_encrypted(cx, buf);
return this.stream.poll_write(cx, buf);
}
loop {
......
......@@ -42,7 +42,9 @@ pub fn encrypt_payload(
) {
match method.category() {
CipherCategory::None => {
dst.reserve(addr.serialized_len() + payload.len());
let auth_ctx = context.auth_context();
dst.reserve(auth_ctx.get_token_len() + addr.serialized_len() + payload.len());
dst.put_slice(auth_ctx.get_token().as_bytes());
addr.write_to_buf(dst);
dst.put_slice(payload);
}
......
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