RejectedSoftware Forums

Sign up

Can't listen to 80 and 443 ports

Hello. I'm trying to make a web server that can listen http requests on port 80 and htts on 443, but vibe throws an Exception:

vibe/http/server.d(188): Failed to listen for incoming HTTP connections on any of the supplied interfaces.
----------------
./tweb(pure @safe bool std.exception.enforce!(Exception, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x6b) [0x6e27c3]
./tweb(void vibe.http.server.listenHTTPPlain(vibe.http.server.HTTPServerSettings)+0x30c) [0x7a6cc8]
./tweb(void vibe.http.server.listenHTTP(vibe.http.server.HTTPServerSettings, void delegate(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse))+0x22c) [0x7a699c]
./tweb(void vibe.http.server.listenHTTP(vibe.http.server.HTTPServerSettings, vibe.http.server.HTTPServerRequestHandler)+0x2f) [0x73594b]
./tweb(void qw.server.Server.start()+0x1bd) [0x69dd1d]
./tweb(_Dmain+0x31) [0x69ce21]
./tweb(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x831c57]
./tweb(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x831baa]
./tweb(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x30) [0x831c10]
./tweb(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x831baa]
./tweb(_d_run_main+0x1dc) [0x831b24]
./tweb(main+0x25) [0x69db15]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f69aeed7ec5]
Error executing command run:
Program exited with code 1

Log:

[B0519F69:00000000 dbg] dnsresolve 127.0.0.1
[B0519F69:00000000 dbg] dnsresolve ret
[B0519F69:00000000 WRN] Failed to listen on 127.0.0.1:80
[B0519F69:00000000 dia] Main thread exiting

If I change ports to 8080 and 8088, for example, then server starts without any errors. I had apache2 installed, but I've removed it already, nothing listens to this ports:

Nmap scan report for localhost (127.0.0.1)
Host is up (0.000023s latency).
PORT   STATE  SERVICE
80/tcp closed http
443/tcp closed https

OS - Ubuntu, vibe 0.7.23 or 0.7.24-beta.1

Here is my code:

		auto router = new URLRouter;
		router.registerWebInterface(new WebInterface);
		router.get("*", serveStaticFiles("./public/"));

		HTTPServerSettings settings = new HTTPServerSettings;
		settings.sessionStore = new MemorySessionStore;
		
		settings.port = 80;
		settings.bindAddresses = ["127.0.0.1"];
		
		listenHTTP(settings, router);
		
		// ssl settings
		
		HTTPServerSettings sslsettings = settings.dup;
		
		sslsettings.port = 443;
		
		sslsettings.sslContext = createSSLContext(SSLContextKind.server);
		sslsettings.sslContext.useCertificateChainFile(Config.sslChainFile);
		sslsettings.sslContext.usePrivateKeyFile(Config.sslKeyFile);
	
		listenHTTP(sslsettings, router);

Re: Can't listen to 80 and 443 ports

Oh, sorry. I found the problem. It requires root privileges to use this ports, i didn't know that/

Re: Can't listen to 80 and 443 ports

On Mon, 15 Jun 2015 17:49:45 GMT, Oleh wrote:

Oh, sorry. I found the problem. It requires root privileges to use this ports, i didn't know that/

All port < 1024 do.
Note that Vibe.d allows you to lower the priviledge after binding: http://vibed.org/docs#privilege-lowering

Re: Can't listen to 80 and 443 ports

Alternatively, you can use this command as a post-build step to have the kernel allow your executable to bind to privileged ports without running as root. This might or might not fit your usage better.

setcap cap_net_bind_service=+ep <yourexecutable>