Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: listen on unix domain sockets #935

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

FallingSnow
Copy link

@FallingSnow FallingSnow commented Oct 14, 2022

Add support for listening on unix domain sockets

Synopsis

Initial conversations converged on using the --port argument but after attempting to implement this, --interface seemed more promising based on the current code structure. Also, a unix socket is more similar to an interface than a port so this made sense logically as well.

Interface arguments are now parsed into an Interface enum consisting of either a Interface::Address (IP address) or Interface::Path (Unix socket path). If an interface argument starts with a . or / it is considered a Interface::Path.

Currently the code does not clean up the socket file it creates. I'm not really sure what the desired behavior should be. Perhaps if miniserve creates the socket file, it should also delete it on shutdown. But then again what if another, different process is listening on the socket file... This does not cause any issues with starting miniserve up again since miniserve will use an existing socket file.

You can listen on both unix sockets and tcp sockets at the same time when given more than one --interface flag.

Tests

As for test code there is only one added test to ensure we don't overwrite non-socket files. I tried to get a GET test working but it keeps throwing an error about serve path even though it is provided. The non working test is commented out in tests/bind.rs.

01:11:46 [ERROR] Failed to resolve path to be served
01:11:46 [ERROR] caused by: No such file or directory (os error 2)

A test for IPv6 binding is failing but seems to work in manual testing. Not sure what is going on there.

Testing

An easy way to test this is to use the following commands:

$ cargo run -- -i /tmp/test.socket ./ -v
$ curl -X GET --unix-socket /tmp/test.socket http://localhost

You can also test in your browser using socat as a TCP->UNIX proxy:

$ socat TCP-LISTEN:8080,fork /tmp/test.socket

Todo

  • Add more tests

Let me know if you have any questions or changes you'd like.

@svenstaro
Copy link
Owner

Hey, is this still WIP? Would you like to add some tests and rebase and then I can take a proper look at this?

@FallingSnow
Copy link
Author

Wasn't looking forward to fixing the tests. Let me try to finish this up by today/tomorrow.

@FallingSnow FallingSnow changed the title WIP: Feature: listen on unix domain sockets Feature: listen on unix domain sockets Nov 12, 2022
@FallingSnow
Copy link
Author

Ready for review.

I moved the tests so that the working directory is set to the tmpdir of each test. This required tls files to be copied into a test's tmpdir so it could access them. This was mainly done so each test could create a socket file in their own directory.

If you'd like any changes please let me know.

@FallingSnow
Copy link
Author

Not sure why checks are failing. Same command succeeds on my machine. cargo fmt --all -- --check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants