Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show tunnel connections #74

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/agent_cli/src/autorun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ use std::{
};

use playit_agent_core::{
api::api::*,
network::address_lookup::{AddressLookup, AddressValue},
tunnel_runner::TunnelRunner,
utils::now_milli,
api::api::*, match_ip::MatchIp, network::address_lookup::{AddressLookup, AddressValue}, tunnel_runner::TunnelRunner, utils::now_milli
};
use playit_agent_core::api::api::AgentType;

use crate::{API_BASE, CliError, match_ip::MatchIp, playit_secret::PlayitSecret, ui::UI};
use crate::{API_BASE, CliError, playit_secret::PlayitSecret, ui::UI};

pub async fn autorun(ui: &mut UI, mut secret: PlayitSecret) -> Result<(), CliError> {
let secret_code = secret
Expand Down Expand Up @@ -56,6 +53,7 @@ pub async fn autorun(ui: &mut UI, mut secret: PlayitSecret) -> Result<(), CliErr
};

let signal = runner.keep_running();
let (tcp_clients, udp_clients) = (runner.tcp_clients(), runner.udp_clients());
let runner = tokio::spawn(runner.run());

ui.write_screen("tunnel running").await;
Expand Down Expand Up @@ -144,17 +142,27 @@ pub async fn autorun(ui: &mut UI, mut secret: PlayitSecret) -> Result<(), CliErr
_ => format!("{}:{}", addr, tunnel.port.from),
};

let dst = format!("{}:{}", tunnel.local_ip, tunnel.local_port);

let dst = SocketAddr::new(tunnel.local_ip, tunnel.local_port);

let connection_count = match tunnel.proto {
PortType::Both => {
let tcp = tcp_clients.active_clients().client_count_by_agent_tunnel(&tunnel).await;
let udp = udp_clients.client_count_by_agent_tunnel(&tunnel).await;
tcp + udp
},
PortType::Tcp => tcp_clients.active_clients().client_count_by_agent_tunnel(&tunnel).await,
PortType::Udp => udp_clients.client_count_by_agent_tunnel(&tunnel).await,
};

if let Some(disabled) = tunnel.disabled {
writeln!(msg, "{} => {} (disabled)", src, dst).unwrap();
if disabled == AgentTunnelDisabled::BySystem {
writeln!(msg, "\tsee: https://playit.gg/account/tunnels/{}", tunnel.id).unwrap();
}
} else if let Some(tunnel_type) = &tunnel.tunnel_type {
writeln!(msg, "{} => {} ({})", src, dst, tunnel_type).unwrap();
writeln!(msg, "{} => {} ({}, connections: {})", src, dst, tunnel_type, connection_count).unwrap();
} else {
writeln!(msg, "{} => {} (proto: {:?}, port count: {})", src, dst, tunnel.proto, tunnel.port.to - tunnel.port.from).unwrap();
writeln!(msg, "{} => {} (proto: {:?}, port count: {}, connections: {})", src, dst, tunnel.proto, tunnel.port.to - tunnel.port.from, connection_count).unwrap();
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/agent_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;
use std::time::Duration;

use clap::{arg, Command};
use playit_agent_core::match_ip::MatchIp;
use rand::Rng;
use uuid::Uuid;

Expand All @@ -20,7 +21,6 @@ use playit_agent_core::tunnel_runner::TunnelRunner;
use playit_agent_core::utils::now_milli;
use playit_secret::PlayitSecret;

use crate::match_ip::MatchIp;
use crate::signal_handle::get_signal_handle;
use crate::ui::{UI, UISettings};

Expand All @@ -29,7 +29,6 @@ pub const API_BASE: &'static str = "https://api.playit.gg";
pub mod util;
pub mod autorun;
pub mod playit_secret;
pub mod match_ip;
pub mod ui;
pub mod signal_handle;

Expand Down
14 changes: 14 additions & 0 deletions packages/agent_core/src/api/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::net::Ipv6Addr;
use super::ip_resource::{IpResource, PlayitRegion};

impl<C: PlayitHttpClient> PlayitApiClient<C> {
pub fn new(client: C) -> Self {
PlayitApiClient { client }
Expand Down Expand Up @@ -648,6 +651,17 @@ pub struct AgentTunnel {
pub disabled: Option<AgentTunnelDisabled>,
}

impl AgentTunnel {
pub fn to_tunnel_ip(&self) -> Ipv6Addr {
let ip_resource = IpResource {
ip_num: self.ip_num,
region: PlayitRegion::from_id(self.region_num).unwrap(),
};

ip_resource.to_tunnel_ip()
}
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct PortRange {
pub from: u16,
Expand Down
13 changes: 13 additions & 0 deletions packages/agent_core/src/api/ip_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,17 @@ impl PlayitRegion {
BigEndian::write_u64(&mut octs[8..], ip_number);
octs.into()
}

pub fn from_id(id: u16) -> Option<Self> {
match id {
0 => Some(PlayitRegion::Anycast),
1 => Some(PlayitRegion::Global),
2 => Some(PlayitRegion::NorthAmerica),
3 => Some(PlayitRegion::Europe),
4 => Some(PlayitRegion::Asia),
5 => Some(PlayitRegion::India),
6 => Some(PlayitRegion::SouthAmerica),
_ => None,
}
}
}
1 change: 1 addition & 0 deletions packages/agent_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod tunnel;
pub mod network;
pub mod utils;
pub mod tunnel_runner;
pub mod match_ip;

#[cfg(test)]
mod test {
Expand Down
File renamed without changes.
16 changes: 14 additions & 2 deletions packages/agent_core/src/network/tcp_clients.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap};
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::io::Error;
use std::net::SocketAddr;
Expand All @@ -11,8 +11,10 @@ use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::TcpStream;
use tokio::sync::RwLock;

use playit_agent_proto::control_feed::{NewClient};
use playit_agent_proto::control_feed::NewClient;

use crate::api::api::AgentTunnel;
use crate::match_ip::MatchIp;
use crate::tunnel::tcp_tunnel::TcpTunnel;

#[derive(Clone)]
Expand Down Expand Up @@ -52,6 +54,16 @@ impl ActiveClients {
let lock = self.active.read().await;
lock.values().map(|v| v.clone()).collect()
}

pub async fn client_count_by_agent_tunnel(&self, tunnel: &AgentTunnel) -> usize {
let tunnel_ip = tunnel.to_tunnel_ip();
let ip = MatchIp::new(tunnel_ip);

let lock = self.active.read().await;
lock.values().filter(|v| {
ip.matches(v.connect_addr.ip()) && tunnel.port.contains(v.connect_addr.port())
}).count()
}
}

impl Default for ActiveClients {
Expand Down
14 changes: 13 additions & 1 deletion packages/agent_core/src/network/udp_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use std::time::Duration;
use tokio::net::UdpSocket;
use tokio::sync::RwLock;

use crate::api::api::PortType;
use crate::api::api::{AgentTunnel, PortType};
use crate::match_ip::MatchIp;
use crate::network::address_lookup::AddressLookup;
use crate::network::lan_address::LanAddress;
use crate::tunnel::udp_proto::UdpFlow;
use crate::tunnel::udp_tunnel::UdpTunnel;
use crate::utils::now_sec;

#[derive(Clone)]
pub struct UdpClients<L: AddressLookup> {
udp_tunnel: UdpTunnel,
lookup: L,
Expand Down Expand Up @@ -42,6 +44,16 @@ impl<L: AddressLookup> UdpClients<L> where L::Value: Into<SocketAddr> {
clients_lock.len()
}

pub async fn client_count_by_agent_tunnel(&self, tunnel: &AgentTunnel) -> usize {
let tunnel_ip = tunnel.to_tunnel_ip();
let ip = MatchIp::new(tunnel_ip);

let lock = self.udp_clients.read().await;
lock.values().filter(|v| {
ip.matches(v.client_key.tunnel_addr.ip()) && tunnel.port.contains(v.client_key.tunnel_addr.port())
}).count()
}

pub async fn forward_packet(&self, flow: &UdpFlow, data: &[u8]) -> std::io::Result<usize> {
let flow_dst = flow.dst();

Expand Down
8 changes: 8 additions & 0 deletions packages/agent_core/src/tunnel_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ impl<L: AddressLookup + Sync + Send> TunnelRunner<L> where L::Value: Into<Socket
self.keep_running.clone()
}

pub fn tcp_clients(&self) -> TcpClients {
self.tcp_clients.clone()
}

pub fn udp_clients(&self) -> UdpClients<Arc<L>> {
self.udp_clients.clone()
}

pub async fn run(self) {
let mut tunnel = self.tunnel;
let udp = tunnel.udp_tunnel();
Expand Down