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_external_ip().into());
}
debug!("Own addresses: {:?}", self.own_addresses);
Ok(())
}

View File

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

View File

@ -272,9 +272,6 @@ fn main() {
Command::WsProxy => {
wsproxy::run_proxy();
}
Command::WsClient { url } => {
wsproxy::run_client(url);
}
}
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 {
fn listen(addr: &str) -> Result<Self, io::Error>;
@ -59,7 +64,9 @@ impl Socket for UdpSocket {
}
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> {

View File

@ -1,5 +1,5 @@
use super::{
net::{mapped_addr, Socket},
net::{mapped_addr, get_ip, Socket},
poll::{WaitImpl, WaitResult},
port_forwarding::PortForwarding,
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 udpsocket = UdpSocket::bind("[::]:0")?;
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);
addr.set_ip(get_ip());
write_addr(addr, &mut msg)?;
io_error!(websocket.write_message(Message::Binary(msg)), "Failed to write to ws connection: {}")?;
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];
for evt in poll {
match evt {
WaitResult::Socket => {
info!("WS -> UDP");
let msg = io_error!(websocket.read_message(), "Failed to read message on websocket {}: {}", peer)?;
info!("MSG: {}", msg);
match msg {
Message::Binary(data) => {
let dst = read_addr(Cursor::new(&data))?;
@ -73,7 +72,6 @@ fn serve_proxy_connection(stream: TcpStream) -> Result<(), io::Error> {
}
}
WaitResult::Device => {
info!("UDP -> WS");
let (size, addr) = udpsocket.recv_from(&mut buffer)?;
let mut data = Vec::with_capacity(18 + size);
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)?;
}
WaitResult::Timeout => {
info!("Sending ping");
io_error!(websocket.write_message(Message::Ping(vec![])), "Failed to send ping: {}")?;
}
WaitResult::Error(err) => return Err(err)
@ -163,12 +160,4 @@ impl Socket for ProxyConnection {
fn create_port_forwarding(&self) -> Option<PortForwarding> {
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();
}
}