#![feature(trivial_bounds)]
#![feature(unboxed_closures)]
#![feature(fn_traits)]
mod en;
use crate::en::{En, De};
use std::io::Result;
use keybob::Key;
use tun_tap::{Iface, Mode};
use tun_tap::r#async::Async;
use std::process::Command;
use tokio_core::reactor::Handle;
use futures::prelude::*;
use futures::stream::{SplitSink, SplitStream};
use futures::sink::With;
use futures::stream::Map;
use std::result::Result as DualResult;
fn cmd(cmd: &str, args: &[&str]) {
let ecode = Command::new("ip")
.args(args)
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(ecode.success(), "Failed to execute {}", cmd);
}
pub struct Tun {
sink: SplitSink<Async>,
stream: SplitStream<Async>,
}
impl Tun {
pub fn new(handle: &Handle) -> Result<Tun>
{
let tun = Iface::new("vpn%d", Mode::Tun);
if tun.is_err() {
eprintln!("ERROR: Permission denied. Try running as superuser");
::std::process::exit(1);
}
let tun_ok = tun.unwrap();
cmd("ip", &["addr", "add", "dev", tun_ok.name(), "10.107.1.3/24"]);
cmd("ip", &["link", "set", "up", "dev", tun_ok.name()]);
let (sink, stream) = Async::new(tun_ok, handle)
.unwrap()
.split();
Ok(Tun {
sink: sink,
stream: stream,
})
}
pub fn encrypt(self, key: &Key) -> Result<EncryptedTun> {
let encryptor = En::new(&key);
let decryptor = De::new(&key);
Ok(EncryptedTun {
sink: self.sink.with(decryptor),
stream: self.stream.map(encryptor),
})
}
pub fn send(self, msg: Vec<u8>) -> Result<()> {
match self.sink.send(msg).wait() {
Ok(_res) => Ok(()),
Err(err) => Err(err)
}
}
pub fn recv(self, buf: &mut Vec<u8>) -> Result<()> {
match self.stream.take(1).wait().last() {
Some(res) => {
buf.extend(res.unwrap().as_slice());
Ok(())
},
None => Err(std::io::Error::new(std::io::ErrorKind::Other, "Sending failed"))
}
}
pub fn split(self) -> (SplitSink<Async>, SplitStream<Async>) {
(self.sink, self.stream)
}
}
impl Sink for Tun {
type SinkItem = Vec<u8>;
type SinkError = std::io::Error;
fn start_send(&mut self, item: Vec<u8>) -> DualResult<futures::AsyncSink<Vec<u8>>, std::io::Error> {
self.sink.start_send(item)
}
fn poll_complete(&mut self) -> DualResult<futures::Async<()>, std::io::Error> {
self.sink.poll_complete()
}
}
impl Stream for Tun {
type Item = Vec<u8>;
type Error = std::io::Error;
fn poll(&mut self) -> DualResult<futures::Async<Option<Vec<u8>>>, std::io::Error> {
self.stream.poll()
}
}
pub struct EncryptedTun {
sink: With<SplitSink<Async>, Vec<u8>, en::De, DualResult<Vec<u8>, std::io::Error>>,
stream: Map<SplitStream<Async>, en::En>,
}
impl EncryptedTun {
pub fn send(self, msg: Vec<u8>) -> Result<()> {
match self.sink.send(msg).wait() {
Ok(_res) => Ok(()),
Err(err) => Err(err)
}
}
pub fn recv(self, buf: &mut Vec<u8>) -> Result<()> {
match self.stream.take(1).wait().last() {
Some(res) => {
buf.extend(res.unwrap().as_slice());
Ok(())
},
None => Err(std::io::Error::new(std::io::ErrorKind::Other, "Sending failed"))
}
}
pub fn split(self) -> (With<SplitSink<Async>, Vec<u8>, en::De, DualResult<Vec<u8>, std::io::Error>>, Map<SplitStream<Async>, en::En>) {
(self.sink, self.stream)
}
}
impl Sink for EncryptedTun {
type SinkItem = Vec<u8>;
type SinkError = std::io::Error;
fn start_send(&mut self, item: Vec<u8>) -> DualResult<futures::AsyncSink<Vec<u8>>, std::io::Error> {
self.sink.start_send(item)
}
fn poll_complete(&mut self) -> DualResult<futures::Async<()>, std::io::Error> {
self.sink.poll_complete()
}
}
impl Stream for EncryptedTun {
type Item = Vec<u8>;
type Error = std::io::Error;
fn poll(&mut self) -> DualResult<futures::Async<Option<Vec<u8>>>, std::io::Error> {
self.stream.poll()
}
}