-
Notifications
You must be signed in to change notification settings - Fork 40
unix socket support #378
unix socket support #378
Changes from 4 commits
ae73ace
b055d27
d04a0a7
4c858fe
8d38785
2f2501e
c1dca54
06fcbb1
bf477c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ node_modules | |
| # Go workspaces | ||
| go.work | ||
| go.work.sum | ||
| .idea | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,7 @@ func ExpiredSessionError() *HandlerError { | |
| // An optional debugContext map can be provided. If it is present and sentry is configured, | ||
| // it is added as context to the sentry events generated for failed assertions. | ||
| func Assert(msg string, expr bool, debugContext ...map[string]interface{}) { | ||
| assert(msg, expr) | ||
| assertCustom(msg, expr) | ||
| if !expr { | ||
| sentry.WithScope(func(scope *sentry.Scope) { | ||
| if len(debugContext) > 0 { | ||
|
|
@@ -90,13 +90,13 @@ func Assert(msg string, expr bool, debugContext ...map[string]interface{}) { | |
| // AssertWithContext is a version of Assert that associates any sentry events with a | ||
| // request context. | ||
| func AssertWithContext(ctx context.Context, msg string, expr bool) { | ||
| assert(msg, expr) | ||
| assertCustom(msg, expr) | ||
| if !expr { | ||
| GetSentryHubFromContextOrDefault(ctx).CaptureException(fmt.Errorf("assertion failed: %s", msg)) | ||
| } | ||
| } | ||
|
|
||
| func assert(msg string, expr bool) { | ||
| func assertCustom(msg string, expr bool) { | ||
|
||
| if expr { | ||
| return | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package internal | ||
|
|
||
| import "strings" | ||
|
|
||
| type HomeServerUrl struct { | ||
| HttpOrUnixStr string | ||
| } | ||
|
|
||
| func (u HomeServerUrl) IsUnixSocket() bool { | ||
kegsay marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return strings.HasPrefix(u.HttpOrUnixStr, "/") | ||
| } | ||
|
|
||
| func (u HomeServerUrl) GetUnixSocket() string { | ||
| if u.IsUnixSocket() { | ||
| return u.HttpOrUnixStr | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func (u HomeServerUrl) GetBaseUrl() string { | ||
kegsay marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if u.IsUnixSocket() { | ||
| return "http://unix" | ||
| } | ||
| return u.HttpOrUnixStr | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestHomeServerUrl_IsUnixSocket_True(t *testing.T) { | ||
| assert.True(t, HomeServerUrl{"/path/to/socket"}.IsUnixSocket()) | ||
| } | ||
|
|
||
| func TestHomeServerUrl_IsUnixSocket_False(t *testing.T) { | ||
| assert.False(t, HomeServerUrl{"localhost:8080"}.IsUnixSocket()) | ||
| } | ||
|
|
||
| func TestHomeServerUrl_GetUnixSocket(t *testing.T) { | ||
| assert.Equal(t, "/path/to/socket", HomeServerUrl{"/path/to/socket"}.GetUnixSocket()) | ||
| } | ||
|
|
||
| func TestHomeServerUrl_GetUnixSocket_Http(t *testing.T) { | ||
| assert.Equal(t, "", HomeServerUrl{"localhost:8080"}.GetUnixSocket()) | ||
| } | ||
|
|
||
| func TestHomeServerUrl_GetBaseUrl_UnixSocket(t *testing.T) { | ||
| assert.Equal(t, "http://unix", HomeServerUrl{"/path/to/socket"}.GetBaseUrl()) | ||
| } | ||
|
|
||
| func TestHomeServerUrl_GetBaseUrl_Http(t *testing.T) { | ||
| assert.Equal(t, "localhost:8080", HomeServerUrl{"localhost:8080"}.GetBaseUrl()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,10 @@ import ( | |
| "context" | ||
| "embed" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "strings" | ||
|
|
@@ -216,12 +219,18 @@ func RunSyncV3Server(h http.Handler, bindAddr, destV2Server, tlsCert, tlsKey str | |
|
|
||
| // Block forever | ||
| var err error | ||
| if tlsCert != "" && tlsKey != "" { | ||
| logger.Info().Msgf("listening TLS on %s", bindAddr) | ||
| err = http.ListenAndServeTLS(bindAddr, tlsCert, tlsKey, srv) | ||
| if strings.HasPrefix(bindAddr, "/") { | ||
kegsay marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| logger.Info().Msgf("listening on unix socket %s", bindAddr) | ||
| listener := unixSocketListener(bindAddr) | ||
| err = http.Serve(listener, srv) | ||
| } else { | ||
| logger.Info().Msgf("listening on %s", bindAddr) | ||
| err = http.ListenAndServe(bindAddr, srv) | ||
| if tlsCert != "" && tlsKey != "" { | ||
| logger.Info().Msgf("listening TLS on %s", bindAddr) | ||
| err = http.ListenAndServeTLS(bindAddr, tlsCert, tlsKey, srv) | ||
| } else { | ||
| logger.Info().Msgf("listening on %s", bindAddr) | ||
| err = http.ListenAndServe(bindAddr, srv) | ||
| } | ||
| } | ||
| if err != nil { | ||
| sentry.CaptureException(err) | ||
|
|
@@ -230,6 +239,22 @@ func RunSyncV3Server(h http.Handler, bindAddr, destV2Server, tlsCert, tlsKey str | |
| } | ||
| } | ||
|
|
||
| func unixSocketListener(bindAddr string) net.Listener { | ||
| err := os.Remove(bindAddr) | ||
| if err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| logger.Fatal().Err(err).Msg("failed to remove existing unix socket") | ||
| } | ||
| listener, err := net.Listen("unix", bindAddr) | ||
| if err != nil { | ||
| logger.Fatal().Err(err).Msg("failed to serve unix socket") | ||
| } | ||
| err = os.Chmod(bindAddr, 0755) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document the respective r/w/x permissions you wish to give to this socket as a comment.
Comment on lines
+251
to
+252
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what is meant by "safe default" here. To connect to and use a unix socket as a client, all you need is the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was under impression that 755 will only allow the owner to use it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to unix(7), write permission is required to connect to a unix domain socket.
I'd suggest to make it at least user and group connectable, that is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First and foremost: Sorry for my original comment. Multiple /bit flips/ happened in my original comment, which I want to point out:
I just was very happy to see unix socket support implemented so soon, but then saw some unusual permission bits. Given I worked on unix socket permission bits as part of caddyserver/caddy#4741, I figured I should comment. Which then lead to that hastily, and more importantly, incorrect comment. Sorry for that. According to the current For more details on this, see caddyserver/caddy#4741 (comment).
I would suggest the same, but without |
||
| if err != nil { | ||
| logger.Fatal().Err(err).Msg("failed to set unix socket permissions") | ||
| } | ||
| return listener | ||
| } | ||
|
|
||
| type HandlerError struct { | ||
| StatusCode int | ||
| Err error | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't add an entire test package just for basic equality checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go mod tidyplease.