From a07c921fee596c8f81b79bb35637c11807742a8d Mon Sep 17 00:00:00 2001 From: Richard Bradfield Date: Sat, 27 Jun 2020 14:13:36 +0100 Subject: [PATCH] Default to dual stack and add BIND arg Change the default bind address to be `[::]` and allow the user to specify the socket to bind on. This is distinct from the HOST/PORT pair already configurable as phd might be running behind a proxy where links need to be generated with a different destination to the bound socket. --- doc/phd.1 | 10 ++++++++-- doc/phd.1.md | 3 +++ src/main.rs | 16 +++++++++++++++- src/server.rs | 9 ++++----- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/doc/phd.1 b/doc/phd.1 index 7960f96..ab28ca5 100644 --- a/doc/phd.1 +++ b/doc/phd.1 @@ -1,10 +1,11 @@ -.\" Generated by scdoc 1.10.0 +.\" Generated by scdoc 1.11.0 +.\" Complete documentation for this program is not available as a GNU info page .ie \n(.g .ds Aq \(aq .el .ds Aq ' .nh .ad l .\" Begin generated content: -.TH "PHD" "1" "2020-05-09" +.TH "PHD" "1" "2020-06-27" .P .SH NAME .P @@ -41,6 +42,11 @@ of your Gopher site. Rather than start as a server, render the \fISELECTOR\fR of the site using the options provided and print the raw response to \fBSTDOUT\fR. .P .RE +\fB-b\fR \fIADDRESS\fR, \fB--bind\fR \fIADDRESS\fR +.RS 4 +Set the socket address to bind to, e.g. \fB127.0.0.1:7070\fR +.P +.RE \fB-p\fR \fIPORT\fR, \fB--port\fR \fIPORT\fR .RS 4 Set the \fIPORT\fR to use when generating Gopher links. diff --git a/doc/phd.1.md b/doc/phd.1.md index 8e8a365..69c36e9 100644 --- a/doc/phd.1.md +++ b/doc/phd.1.md @@ -31,6 +31,9 @@ of your Gopher site. Rather than start as a server, render the _SELECTOR_ of the site using the options provided and print the raw response to *STDOUT*. +*-b* _ADDRESS_, *--bind* _ADDRESS_ + Set the socket address to bind to, e.g. *127.0.0.1:7070* + *-p* _PORT_, *--port* _PORT_ Set the _PORT_ to use when generating Gopher links. diff --git a/src/main.rs b/src/main.rs index 8967776..7f165ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use phd; use std::process; +use std::net::SocketAddr; +const DEFAULT_BIND: &str = "[::]:7070"; const DEFAULT_HOST: &str = "127.0.0.1"; const DEFAULT_PORT: u16 = 7070; @@ -8,6 +10,7 @@ fn main() { let args = std::env::args().skip(1).collect::>(); let mut args = args.iter(); let mut root = "."; + let mut bind: SocketAddr = DEFAULT_BIND.parse().unwrap(); let mut host = DEFAULT_HOST; let mut port = DEFAULT_PORT; let mut render = ""; @@ -22,6 +25,17 @@ fn main() { render = "/"; } } + "--bind" | "-b" | "-bind" => { + if let Some(b) = args.next() { + bind = b + .parse() + .map_err(|_| { + eprintln!("bad socket bind: {}", b); + process::exit(1) + }) + .unwrap(); + } + } "--port" | "-p" | "-port" => { if let Some(p) = args.next() { port = p @@ -63,7 +77,7 @@ fn main() { }; } - if let Err(e) = phd::server::start(host, port, root) { + if let Err(e) = phd::server::start(bind, host, port, root) { eprintln!("{}", e); } } diff --git a/src/server.rs b/src/server.rs index 06e5190..c6a59f7 100644 --- a/src/server.rs +++ b/src/server.rs @@ -5,7 +5,7 @@ use std::{ cmp::Ordering, fs::{self, DirEntry}, io::{self, prelude::*, BufReader, Read, Write}, - net::{TcpListener, TcpStream}, + net::{SocketAddr, TcpListener, TcpStream}, os::unix::fs::PermissionsExt, path::Path, process::Command, @@ -50,9 +50,8 @@ macro_rules! info { } /// Starts a Gopher server at the specified host, port, and root directory. -pub fn start(host: &str, port: u16, root: &str) -> Result<()> { - let addr = format!("{}:{}", "0.0.0.0", port); - let listener = TcpListener::bind(&addr)?; +pub fn start(bind: SocketAddr, host: &str, port: u16, root: &str) -> Result<()> { + let listener = TcpListener::bind(&bind)?; let full_root_path = fs::canonicalize(&root)?.to_string_lossy().to_string(); let pool = ThreadPool::new(MAX_WORKERS); @@ -61,7 +60,7 @@ pub fn start(host: &str, port: u16, root: &str) -> Result<()> { color::Yellow, color::Reset, color::Yellow, - addr, + bind, color::Reset, color::Blue, full_root_path,