Working example

This commit is contained in:
Dennis Schwerdel 2021-02-03 22:03:42 +01:00
parent 124f7cbff9
commit e3fa631ed9
5 changed files with 14 additions and 24 deletions

View File

@ -221,6 +221,7 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
self.own_addresses.push(pfw.get_internal_ip().into()); self.own_addresses.push(pfw.get_internal_ip().into());
self.own_addresses.push(pfw.get_external_ip().into()); self.own_addresses.push(pfw.get_external_ip().into());
} }
debug!("Own addresses: {:?}", self.own_addresses);
Ok(()) Ok(())
} }

View File

@ -446,10 +446,6 @@ pub enum Command {
WsProxy, WsProxy,
WsClient {
url: String,
},
#[structopt(alias = "migrate")] #[structopt(alias = "migrate")]
MigrateConfig { MigrateConfig {
/// Config file /// Config file

View File

@ -272,9 +272,6 @@ fn main() {
Command::WsProxy => { Command::WsProxy => {
wsproxy::run_proxy(); wsproxy::run_proxy();
} }
Command::WsClient { url } => {
wsproxy::run_client(url);
}
} }
return; return;
} }

View File

@ -20,6 +20,11 @@ pub fn mapped_addr(addr: SocketAddr) -> SocketAddr {
} }
} }
pub fn get_ip() -> IpAddr {
let s = UdpSocket::bind("[::]:0").unwrap();
s.connect("8.8.8.8:0").unwrap();
s.local_addr().unwrap().ip()
}
pub trait Socket: AsRawFd + Sized { pub trait Socket: AsRawFd + Sized {
fn listen(addr: &str) -> Result<Self, io::Error>; fn listen(addr: &str) -> Result<Self, io::Error>;
@ -59,7 +64,9 @@ impl Socket for UdpSocket {
} }
fn address(&self) -> Result<SocketAddr, io::Error> { fn address(&self) -> Result<SocketAddr, io::Error> {
self.local_addr() let mut addr = self.local_addr()?;
addr.set_ip(get_ip());
Ok(addr)
} }
fn create_port_forwarding(&self) -> Option<PortForwarding> { fn create_port_forwarding(&self) -> Option<PortForwarding> {

View File

@ -1,5 +1,5 @@
use super::{ use super::{
net::{mapped_addr, Socket}, net::{mapped_addr, get_ip, Socket},
poll::{WaitImpl, WaitResult}, poll::{WaitImpl, WaitResult},
port_forwarding::PortForwarding, port_forwarding::PortForwarding,
util::MsgBuffer util::MsgBuffer
@ -50,19 +50,18 @@ fn serve_proxy_connection(stream: TcpStream) -> Result<(), io::Error> {
let mut websocket = io_error!(accept(stream), "Failed to initialize websocket with {}: {}", peer)?; let mut websocket = io_error!(accept(stream), "Failed to initialize websocket with {}: {}", peer)?;
let udpsocket = UdpSocket::bind("[::]:0")?; let udpsocket = UdpSocket::bind("[::]:0")?;
let mut msg = Vec::with_capacity(18); let mut msg = Vec::with_capacity(18);
let addr = udpsocket.local_addr()?; let mut addr = udpsocket.local_addr()?;
info!("Listening on {} for peer {}", addr, peer); info!("Listening on {} for peer {}", addr, peer);
addr.set_ip(get_ip());
write_addr(addr, &mut msg)?; write_addr(addr, &mut msg)?;
io_error!(websocket.write_message(Message::Binary(msg)), "Failed to write to ws connection: {}")?; io_error!(websocket.write_message(Message::Binary(msg)), "Failed to write to ws connection: {}")?;
let websocketfd = websocket.get_ref().as_raw_fd(); let websocketfd = websocket.get_ref().as_raw_fd();
let poll = WaitImpl::new(websocketfd, udpsocket.as_raw_fd(), 60)?; let poll = WaitImpl::new(websocketfd, udpsocket.as_raw_fd(), 60*1000)?;
let mut buffer = [0; 65535]; let mut buffer = [0; 65535];
for evt in poll { for evt in poll {
match evt { match evt {
WaitResult::Socket => { WaitResult::Socket => {
info!("WS -> UDP");
let msg = io_error!(websocket.read_message(), "Failed to read message on websocket {}: {}", peer)?; let msg = io_error!(websocket.read_message(), "Failed to read message on websocket {}: {}", peer)?;
info!("MSG: {}", msg);
match msg { match msg {
Message::Binary(data) => { Message::Binary(data) => {
let dst = read_addr(Cursor::new(&data))?; let dst = read_addr(Cursor::new(&data))?;
@ -73,7 +72,6 @@ fn serve_proxy_connection(stream: TcpStream) -> Result<(), io::Error> {
} }
} }
WaitResult::Device => { WaitResult::Device => {
info!("UDP -> WS");
let (size, addr) = udpsocket.recv_from(&mut buffer)?; let (size, addr) = udpsocket.recv_from(&mut buffer)?;
let mut data = Vec::with_capacity(18 + size); let mut data = Vec::with_capacity(18 + size);
write_addr(addr, &mut data)?; write_addr(addr, &mut data)?;
@ -81,7 +79,6 @@ fn serve_proxy_connection(stream: TcpStream) -> Result<(), io::Error> {
io_error!(websocket.write_message(Message::Binary(data)), "Failed to write to {}: {}", peer)?; io_error!(websocket.write_message(Message::Binary(data)), "Failed to write to {}: {}", peer)?;
} }
WaitResult::Timeout => { WaitResult::Timeout => {
info!("Sending ping");
io_error!(websocket.write_message(Message::Ping(vec![])), "Failed to send ping: {}")?; io_error!(websocket.write_message(Message::Ping(vec![])), "Failed to send ping: {}")?;
} }
WaitResult::Error(err) => return Err(err) WaitResult::Error(err) => return Err(err)
@ -164,11 +161,3 @@ impl Socket for ProxyConnection {
None None
} }
} }
pub fn run_client(url: String) {
let (mut socket, _) = connect(Url::parse(&url).unwrap()).unwrap();
socket.write_message(Message::Text("test".to_string())).unwrap();
let msg = socket.read_message().unwrap();
info!("msg: {}", msg);
socket.close(None).unwrap();
}