durnitisp/src/stun.rs

158 lines
5.3 KiB
Rust
Raw Normal View History

2020-12-24 13:24:55 -05:00
// Copyright 2020 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2020-12-24 15:48:50 -05:00
use gflags;
use prometheus::{CounterVec, IntGaugeVec};
2020-12-24 13:24:55 -05:00
use std::convert::From;
use std::io;
2020-12-24 15:48:50 -05:00
use std::net::{SocketAddr, UdpSocket};
2020-12-24 13:24:55 -05:00
use std::time::SystemTime;
2022-07-14 21:13:05 -04:00
use tracing::{debug, error, info, instrument};
2020-12-24 13:24:55 -05:00
gflags::define! {
/// Read timeout for the stun server udp receive
--stunRecvTimeoutSecs: u64 = 5
}
gflags::define! {
2020-12-24 23:28:30 -05:00
/// Delay between lookup attempts in seconds
2020-12-24 13:24:55 -05:00
--delaySecs: u64 = 60
}
const STUN_PAYLOAD: [u8; 20] = [
0, 1, // Binding request
0, 0, // Message length
0x21, 0x12, 0xa4, 0x42, // magic
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
];
enum ConnectError {
Timeout(SystemTime),
Err(io::Error),
Incomplete,
}
impl From<io::Error> for ConnectError {
fn from(e: io::Error) -> ConnectError {
if let io::ErrorKind::TimedOut = e.kind() {
return ConnectError::Timeout(SystemTime::now());
} else {
return ConnectError::Err(e);
}
}
}
fn attempt_stun_connect(addr: SocketAddr) -> Result<SystemTime, ConnectError> {
// We let the OS choose the port by specifying 0
let local_socket = UdpSocket::bind("0.0.0.0:0")?;
local_socket.connect(addr)?;
local_socket.set_read_timeout(Some(std::time::Duration::from_secs(
STUNRECVTIMEOUTSECS.flag,
)))?;
let _sent = local_socket.send(&STUN_PAYLOAD)?;
// TODO what if we didn't send the whole packet?
let mut buf = [0 as u8; 1024];
let rcvd = local_socket.recv(&mut buf)?;
if rcvd == 0 {
return Err(ConnectError::Incomplete);
}
Ok(SystemTime::now())
}
2022-07-14 21:13:05 -04:00
#[instrument(
name = "STUN",
fields(domain=domain_name, socket=%s),
skip(stun_counter_vec_copy, stun_latency_vec_copy, stun_success_vec_copy)
)]
2020-12-24 13:24:55 -05:00
pub fn start_listen_thread(
domain_name: &str,
s: SocketAddr,
stun_counter_vec_copy: CounterVec,
stun_latency_vec_copy: IntGaugeVec,
stun_success_vec_copy: IntGaugeVec,
) {
2022-07-14 21:13:05 -04:00
debug!("starting thread");
2020-12-24 13:24:55 -05:00
loop {
let now = SystemTime::now();
2022-07-14 21:13:05 -04:00
info!("Attempting to connect");
2020-12-24 13:24:55 -05:00
match attempt_stun_connect(s) {
Ok(finish_time) => {
2022-07-14 21:13:05 -04:00
info!(
timeout = false,
success = true,
millis = finish_time.duration_since(now).unwrap().as_millis(),
conn_type = "Stun connection",
);
2020-12-24 13:24:55 -05:00
stun_counter_vec_copy
.with(&prometheus::labels! {"result" => "ok", "domain" => domain_name})
.inc();
stun_latency_vec_copy
.with(&prometheus::labels! {"domain" => domain_name})
// Technically this could be lossy but we'll chance it anyway.
.set(finish_time.duration_since(now).unwrap().as_millis() as i64);
stun_success_vec_copy
.with(&prometheus::labels! {"domain" => domain_name})
.set(1);
}
Err(ConnectError::Timeout(finish_time)) => {
info!(
2022-07-14 21:13:05 -04:00
timeout = true,
success = false,
millis = finish_time.duration_since(now).unwrap().as_millis(),
conn_type = "Stun connection",
2020-12-24 13:24:55 -05:00
);
stun_counter_vec_copy
.with(&prometheus::labels! {"result" => "timeout", "domain" => domain_name})
.inc();
stun_success_vec_copy
.with(&prometheus::labels! {"domain" => domain_name})
.set(0);
}
Err(ConnectError::Err(e)) => {
2022-07-14 21:13:05 -04:00
error!(
timeout=true, success=false, err = ?e,
conn_type="Stun connection",
);
2020-12-24 13:24:55 -05:00
stun_counter_vec_copy
.with(&prometheus::labels! {"result" => "err", "domain" => domain_name})
.inc();
stun_success_vec_copy
.with(&prometheus::labels! {"domain" => domain_name})
.set(0);
}
Err(ConnectError::Incomplete) => {
2022-07-14 21:13:05 -04:00
error!(
timeout = true,
success = false,
err = "Incomplete",
conn_type = "Stun connection",
);
2020-12-24 13:24:55 -05:00
stun_counter_vec_copy
.with(&prometheus::labels! {"result" => "incomplete", "domain" => domain_name})
.inc();
stun_success_vec_copy
.with(&prometheus::labels! {"domain" => domain_name})
.set(0);
}
}
// Then we wait for some period of time.
std::thread::sleep(std::time::Duration::from_secs(DELAYSECS.flag))
}
}
pub fn delay_secs() -> u64 {
DELAYSECS.flag
}