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.
pull/1/head
Richard Bradfield 4 years ago
parent f3bfbbd9f5
commit a07c921fee
No known key found for this signature in database
GPG Key ID: 0A10CE15CBC4A23F

@ -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 .ie \n(.g .ds Aq \(aq
.el .ds Aq ' .el .ds Aq '
.nh .nh
.ad l .ad l
.\" Begin generated content: .\" Begin generated content:
.TH "PHD" "1" "2020-05-09" .TH "PHD" "1" "2020-06-27"
.P .P
.SH NAME .SH NAME
.P .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. 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 .P
.RE .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 \fB-p\fR \fIPORT\fR, \fB--port\fR \fIPORT\fR
.RS 4 .RS 4
Set the \fIPORT\fR to use when generating Gopher links. Set the \fIPORT\fR to use when generating Gopher links.

@ -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*. 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_ *-p* _PORT_, *--port* _PORT_
Set the _PORT_ to use when generating Gopher links. Set the _PORT_ to use when generating Gopher links.

@ -1,6 +1,8 @@
use phd; use phd;
use std::process; use std::process;
use std::net::SocketAddr;
const DEFAULT_BIND: &str = "[::]:7070";
const DEFAULT_HOST: &str = "127.0.0.1"; const DEFAULT_HOST: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 7070; const DEFAULT_PORT: u16 = 7070;
@ -8,6 +10,7 @@ fn main() {
let args = std::env::args().skip(1).collect::<Vec<_>>(); let args = std::env::args().skip(1).collect::<Vec<_>>();
let mut args = args.iter(); let mut args = args.iter();
let mut root = "."; let mut root = ".";
let mut bind: SocketAddr = DEFAULT_BIND.parse().unwrap();
let mut host = DEFAULT_HOST; let mut host = DEFAULT_HOST;
let mut port = DEFAULT_PORT; let mut port = DEFAULT_PORT;
let mut render = ""; let mut render = "";
@ -22,6 +25,17 @@ fn main() {
render = "/"; 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" => { "--port" | "-p" | "-port" => {
if let Some(p) = args.next() { if let Some(p) = args.next() {
port = p 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); eprintln!("{}", e);
} }
} }

@ -5,7 +5,7 @@ use std::{
cmp::Ordering, cmp::Ordering,
fs::{self, DirEntry}, fs::{self, DirEntry},
io::{self, prelude::*, BufReader, Read, Write}, io::{self, prelude::*, BufReader, Read, Write},
net::{TcpListener, TcpStream}, net::{SocketAddr, TcpListener, TcpStream},
os::unix::fs::PermissionsExt, os::unix::fs::PermissionsExt,
path::Path, path::Path,
process::Command, process::Command,
@ -50,9 +50,8 @@ macro_rules! info {
} }
/// Starts a Gopher server at the specified host, port, and root directory. /// Starts a Gopher server at the specified host, port, and root directory.
pub fn start(host: &str, port: u16, root: &str) -> Result<()> { pub fn start(bind: SocketAddr, host: &str, port: u16, root: &str) -> Result<()> {
let addr = format!("{}:{}", "0.0.0.0", port); let listener = TcpListener::bind(&bind)?;
let listener = TcpListener::bind(&addr)?;
let full_root_path = fs::canonicalize(&root)?.to_string_lossy().to_string(); let full_root_path = fs::canonicalize(&root)?.to_string_lossy().to_string();
let pool = ThreadPool::new(MAX_WORKERS); let pool = ThreadPool::new(MAX_WORKERS);
@ -61,7 +60,7 @@ pub fn start(host: &str, port: u16, root: &str) -> Result<()> {
color::Yellow, color::Yellow,
color::Reset, color::Reset,
color::Yellow, color::Yellow,
addr, bind,
color::Reset, color::Reset,
color::Blue, color::Blue,
full_root_path, full_root_path,

Loading…
Cancel
Save