summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 1367c609df89d5b3bf95e8be4c20bc800eef85aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use std::{
    error::Error,
    sync::{ Arc, atomic::{ self, AtomicUsize }},
    net::SocketAddr,
    convert::Infallible,
    collections::HashMap,
};

mod minesweeper;
mod tls_stuff;
use minesweeper::*;
use tls_stuff::*;

use hyper::{ Method, StatusCode, Body, Request, Response, Server };
use hyper::server::conn::{ AddrStream, AddrIncoming };
use hyper::service::{make_service_fn, service_fn};
use tokio::sync::{
    RwLock,
    mpsc,
};

type HtmlResult = Result<Response<Body>, Response<Body>>;
use futures_channel::mpsc::{unbounded, UnboundedSender};
use futures_util::{future, pin_mut, stream::TryStreamExt, StreamExt, sink::SinkExt};

use tokio::fs;
use hyper_tungstenite::{ tungstenite::protocol::Message, HyperWebsocket };
use tokio_rustls::{ rustls, Accept, server::TlsStream };

type Tx = UnboundedSender<Message>;
type MovReqTx = mpsc::UnboundedSender<MetaMove>;
type PeerMap = Arc<RwLock<HashMap<SocketAddr, (Tx, usize, String, (usize, usize))>>>;
type PeerInfo = (PeerMap, Arc::<AtomicUsize>);

#[derive(Debug)]
enum MetaMove {
    Move(Move,SocketAddr),
    Dump,
    Reset,
}

const PAGE_RELPATH: &str = "./page.html";
const FONT_FILE_FUCKIT: &[u8] = include_bytes!("./VT323-Regular.ttf");

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let sequential_id = Arc::new(AtomicUsize::new(0));
    let peers = PeerMap::new(RwLock::new(HashMap::new()));
    let peer_info: PeerInfo = (peers.clone(), sequential_id.clone());
    let addr = SocketAddr::from(([0, 0, 0, 0], 31235));

    // Build TLS configuration.
    let tls_cfg = {
        // Load public certificate.
        let certs = load_certs("cert.pem")?;
        // Load private key.
        let key = load_private_key("cert.rsa")?;
        // Do not use client certificate authentication.
        let mut cfg = rustls::ServerConfig::builder()
            .with_safe_defaults()
            .with_no_client_auth()
            .with_single_cert(certs, key)
            .map_err(|e| error(format!("{}", e)))?;
        // Configure ALPN to accept HTTP/2, HTTP/1.1 in that order.
        cfg.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
        Arc::new(cfg)
    };

    // Create a TCP listener via tokio.
    let incoming = AddrIncoming::bind(&addr)?;

    let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
    let game_t = tokio::spawn(gameloop(cmd_rx, peers.clone()));

    let serv = make_service_fn(|socket: &tls_stuff::TlsStream| {
        let addr = {
            if let State::Streaming(ref s) = &socket.state {
                s.get_ref().0.remote_addr()
            } else {
                std::net::SocketAddr::new(std::net::Ipv4Addr::new(0,0,0,0).into(),0)
            }
        };
        let peer_info = peer_info.clone();
        let cmd_tx = cmd_tx.clone();
        async move {
            Ok::<_, Infallible>(service_fn(move |req: Request<Body>| {
                let peer_info = peer_info.clone();
                let cmd_tx = cmd_tx.clone();
                async move {
                    Ok::<_,Infallible>(match handle_req(req, peer_info.clone(), cmd_tx.clone(), addr).await {
                        Ok(r) => r,
                        Err(r) => r,
                    })
                }
            }))
        }
    });

    // Run the future, keep going until an error occurs.
    println!("Starting to serve on https://{}.", addr);
    let server = Server::builder(TlsAcceptor::new(tls_cfg, incoming))
        .serve(serv)
        .with_graceful_shutdown(shutdown_signal());
    if let Err(e) = server.await {
        eprint!("server error: {}", e);
    }
    Ok(())
}

// If a move is made, broadcast new board, else just send current board
async fn gameloop(mut move_rx: mpsc::UnboundedReceiver<MetaMove>, peers: PeerMap) {
    let mut game = Game::new(Board::new(75,35), (75*35)/8);
    let mut latest_player_name = None;
    while let Some(req) = move_rx.recv().await {
        let done = game.phase == Phase::Die || game.phase == Phase::Win;
        match req {
            MetaMove::Move(m, o) => if !done {
                game = game.act(m);
                if game.phase == Phase::Win || game.phase == Phase::Die {
                    game.board = game.board.grade();
                }
                latest_player_name = peers.read().await.get(&o).map(|(_,_,n,_)| n.clone());
            },
            MetaMove::Dump => (),
            MetaMove::Reset => { game = Game::new(Board::new(75,35), (75*35)/8); },
        }
        let mut reply = vec![Message::binary(game.board.render())];
        let lpname = latest_player_name.as_ref().map(|s| s.as_str()).unwrap_or("unknown player");
        match game.phase {
            Phase::Win => { reply.push(Message::text(format!("win {lpname}"))); },
            Phase::Die => { reply.push(Message::text(format!("lose {lpname}"))); },
            _ => (),
        }
        {
            let peers = peers.read().await;
            for (addr, (tx, _, _, _)) in peers.iter() {
                for r in reply.iter() {
                    if let Err(e) = tx.unbounded_send(r.clone()) {
                        println!("couldn't send game update {r} to {addr}: {e}");
                    }
                }
            }
        }
    }
}

async fn peer_connection(peer_info: PeerInfo, cmd_tx: MovReqTx, socket: HyperWebsocket, addr: SocketAddr) {
    let socket = socket.await.unwrap(); // FIXME error handling
    println!("Incoming TCP connection from: {}", addr);

    let peer_map = peer_info.0;
    let peer_seqid = peer_info.1.fetch_add(1, atomic::Ordering::AcqRel);
    let mut peer_name = "unknown".to_string();

    let (tx, rx) = unbounded();

    let (outgoing, mut incoming) = socket.split();

    let process_incoming = async {
        while let Ok(cmd) = incoming.try_next().await {
            if let Some(cmd) = cmd {
                if cmd.is_close() {
                    println!("closing \"{peer_name}\"@{addr}");
                    let mut peers = peer_map.write().await;
                    if let Some(_) = peers.get(&addr) {
                        peers.remove(&addr);
                    }
                    for (paddr, (tx, _, pname, _)) in peers.iter() {
                        if let Err(e) = tx.unbounded_send(Message::text("logoff {peer_seqid} {peer_name}")) {
                            println!("couldn't deliver logoff info to \"{pname}\"@{paddr}: {e}");
                        }
                    }
                    break;
                }
                // if it ain't text we can't handle it
                if !cmd.is_text() { continue; }
                let cmd = cmd.to_text().unwrap();

                let mut fields = cmd.split(" ");
                let parse_pos = |mut fields: std::str::Split<&str>| -> Option<(usize, usize)> {
                    let x = fields.next().and_then(|xstr| xstr.parse::<usize>().ok());
                    let y = fields.next().and_then(|ystr| ystr.parse::<usize>().ok());
                    x.zip(y)
                };
                if let Some(cmd_name) = fields.next() {
                    match cmd_name {
                        "pos" => {
                            match parse_pos(fields) {
                                Some(pos) => {
                                    let (name, id) = {
                                        let mut peers = peer_map.write().await;
                                        let mut entry = peers.get_mut(&addr).unwrap();
                                        entry.3 = pos.clone();
                                        (entry.2.clone(), entry.1)
                                    };
                                    let sanitized_name = name.replace(" ", "&nbsp").to_string();
                                    {
                                        let peers = peer_map.read().await;
                                        for peer_tx in peers.iter().filter(|(s, _)| **s != addr).map(|(_,(peer_tx,_,_,_))| peer_tx) {
                                            let r = peer_tx.unbounded_send(Message::text(format!("pos {id} {sanitized_name} {} {}", pos.0, pos.1)));
                                            if let Err(e) = r {
                                                println!("error sending pos update: {e}");
                                            }
                                        }
                                    }
                                },
                                None => {
                                    println!("bad position update from \"{peer_name}@{addr}\"");
                                },
                            }
                        },
                        "reveal" => {
                            match parse_pos(fields) {
                                Some(pos) => {
                                    println!("{cmd} from \"{peer_name}\"@{addr}");
                                    cmd_tx.send(MetaMove::Move(Move { t: MoveType::Reveal, pos }, addr)).unwrap();
                                },
                                None => {
                                    println!("bad reveal from \"{peer_name}\"@{addr}");
                                }
                            }
                        },
                        "flag" => {
                            match parse_pos(fields) {
                                Some(pos) => {
                                    println!("{cmd} from \"{peer_name}\"@{addr}");
                                    cmd_tx.send(MetaMove::Move(Move { t: MoveType::ToggleFlag, pos }, addr)).unwrap();
                                },
                                None => {
                                    println!("bad flag from \"{peer_name}\"@{addr}");
                                }
                            }
                        },
                        "reset" => {
                            println!("{cmd} from \"{peer_name}\"@{addr}");
                            if let Err(e) = cmd_tx.send(MetaMove::Reset) {
                                println!("couldn't send game dump to \"{peer_name}\"@{addr}: {e}");
                            }
                        },
                        "register" => {
                            let name = fields.collect::<Vec<&str>>().join(&" ");
                            if name.is_empty() {
                                peer_name = "anon".to_string();
                            } else {
                                peer_name = name;
                            }
                            {   // new scope cuz paranoid bout deadlocks
                                peer_map.write().await.insert(addr, (tx.clone(), peer_seqid, peer_name.clone(), (0,0)));
                            }
                            tx.unbounded_send(Message::text(format!("id {}", peer_seqid))).unwrap();
                            if let Err(e) = cmd_tx.send(MetaMove::Dump) {
                                println!("couldn't send game dump to \"{peer_name}\"@{addr}: {e}");
                            }
                        },
                        e => println!("unknown command {e:?} from {peer_name}@{addr}, \"{cmd}\""),
                    }
                }
            }
        }
    };
    let send_to_browser = rx.map(Ok).forward(outgoing);

    pin_mut!(process_incoming, send_to_browser);
    future::select(process_incoming, send_to_browser).await;

    println!("{} disconnected", &addr);
    peer_map.write().await.remove(&addr);
}

async fn handle_req(mut request: Request<Body>, peer_info: PeerInfo, cmd_tx: MovReqTx, addr: SocketAddr) -> HtmlResult {
    if hyper_tungstenite::is_upgrade_request(&request) {
        let (resp, wsocket) = hyper_tungstenite::upgrade(&mut request, None).expect("couldn't upgrade to websocket");
        tokio::spawn(async move {
            peer_connection(peer_info.clone(), cmd_tx.clone(), wsocket, addr).await;
        });
        return Ok(resp);
    }

    let page = fs::read_to_string(PAGE_RELPATH).await.unwrap();
    let mut uri_path = request.uri().path().split('/').skip(1);
    let actual_path = uri_path.next();

    match (request.method(), actual_path) {
        (&Method::GET, None | Some("")) => {
            Response::builder()
                .status(StatusCode::OK)
                .body(Body::from(page))
                .map_err(errpage)
        },
        (&Method::GET, Some("saddr")) => {
            Response::builder()
                .status(StatusCode::OK)
                .body(Body::from("placeholder"))
                .map_err(errpage)
        },
        (&Method::GET, Some("font.ttf")) => {
            Response::builder()
                .status(StatusCode::OK)
                .body(Body::from(FONT_FILE_FUCKIT))
                .map_err(errpage)
        },
        _ => {
            Response::builder()
                .status(StatusCode::METHOD_NOT_ALLOWED)
                .header("ALLOW", "GET, POST")
                .body(Body::empty()).map_err(errpage)
        }
    }
}

fn errpage<T: Error>(e: T) -> Response<Body> {
    Response::builder()
        .status(StatusCode::INTERNAL_SERVER_ERROR)
        .body(e.to_string().into())
        .unwrap()
}

fn error(err: String) -> std::io::Error {
    std::io::Error::new(std::io::ErrorKind::Other, err)
}

async fn shutdown_signal() {
    tokio::signal::ctrl_c()
        .await
        .expect("failed to install CTRL+C signal handler");
}