Skip to content

Commit 3078bbe

Browse files
authored
Merge pull request #39 from wollomatic/develop
1.5.2
2 parents ebad441 + 716f164 commit 3078bbe

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# syntax=docker/dockerfile:1
2-
FROM --platform=$BUILDPLATFORM golang:1.22.7-alpine3.20 AS build
2+
FROM --platform=$BUILDPLATFORM golang:1.23.2-alpine3.20 AS build
33
WORKDIR /application
44
COPY . ./
55
ARG TARGETOS

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ socket-proxy can be configured via command line parameters or via environment va
186186
| `-stoponwatchdog` | `SP_STOPONWATCHDOG` | (not set/false) | If set, socket-proxy will be stopped if the watchdog detects that the unix socket is not available. |
187187
| `-watchdoginterval` | `SP_WATCHDOGINTERVAL` | `0` | Check for socket availabibity every x seconds (disable checks, if not set or value is 0) |
188188
| `-proxysocketendpoint` | `SP_PROXYSOCKETENDPOINT` | (not set) | Proxy to the given unix socket instead of a TCP port |
189-
| `-proxysocketendpointfilemode` | `SP_PROXYSOCKETENDPOINTFILEMODE` | `0400` | Explicitly set the file mode for the filtered unix socket endpoint (only useful with `-proxysocketendpoint`) |
189+
| `-proxysocketendpointfilemode` | `SP_PROXYSOCKETENDPOINTFILEMODE` | `0600` | Explicitly set the file mode for the filtered unix socket endpoint (only useful with `-proxysocketendpoint`) |
190190

191191
### Changelog
192192

@@ -200,6 +200,8 @@ socket-proxy can be configured via command line parameters or via environment va
200200

201201
1.4 - allow configuration from env variables
202202

203+
1.5 - allow unix socket as proxied/filtered endpoint
204+
203205
## License
204206

205207
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

cmd/socket-proxy/handlehttprequest.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import (
88
"strings"
99
)
1010

11-
// handleHttpRequest checks if the request is allowed and sends it to the proxy.
11+
// handleHTTPRequest checks if the request is allowed and sends it to the proxy.
1212
// Otherwise, it returns a "405 Method Not Allowed" or a "403 Forbidden" error.
1313
// In case of an error, it returns a 500 Internal Server Error.
14-
func handleHttpRequest(w http.ResponseWriter, r *http.Request) {
15-
14+
func handleHTTPRequest(w http.ResponseWriter, r *http.Request) {
1615
if cfg.ProxySocketEndpoint == "" { // do not perform this check if we proxy to a unix socket
1716
allowedIP, err := isAllowedClient(r.RemoteAddr)
1817
if err != nil {

cmd/socket-proxy/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/wollomatic/socket-proxy/internal/config"
87
"log/slog"
98
"net"
109
"net/http"
@@ -15,10 +14,12 @@ import (
1514
"runtime"
1615
"syscall"
1716
"time"
17+
18+
"github.com/wollomatic/socket-proxy/internal/config"
1819
)
1920

2021
const (
21-
programUrl = "github.com/wollomatic/socket-proxy"
22+
programURL = "github.com/wollomatic/socket-proxy"
2223
logAddSource = false // set to true to log the source position (file and line) of the log message
2324
)
2425

@@ -55,7 +56,7 @@ func main() {
5556
slog.SetDefault(logger)
5657

5758
// print configuration
58-
slog.Info("starting socket-proxy", "version", version, "os", runtime.GOOS, "arch", runtime.GOARCH, "runtime", runtime.Version(), "URL", programUrl)
59+
slog.Info("starting socket-proxy", "version", version, "os", runtime.GOOS, "arch", runtime.GOARCH, "runtime", runtime.Version(), "URL", programURL)
5960
if cfg.ProxySocketEndpoint == "" {
6061
slog.Info("configuration info", "socketpath", cfg.SocketPath, "listenaddress", cfg.ListenAddress, "loglevel", cfg.LogLevel, "logjson", cfg.LogJSON, "allowfrom", cfg.AllowFrom, "shutdowngracetime", cfg.ShutdownGraceTime)
6162
} else {
@@ -90,8 +91,8 @@ func main() {
9091
}
9192

9293
// define the reverse proxy
93-
socketUrlDummy, _ := url.Parse("http://localhost") // dummy URL - we use the unix socket
94-
socketProxy = httputil.NewSingleHostReverseProxy(socketUrlDummy)
94+
socketURLDummy, _ := url.Parse("http://localhost") // dummy URL - we use the unix socket
95+
socketProxy = httputil.NewSingleHostReverseProxy(socketURLDummy)
9596
socketProxy.Transport = &http.Transport{
9697
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
9798
return net.Dial("unix", cfg.SocketPath)
@@ -125,7 +126,7 @@ func main() {
125126
}
126127

127128
srv := &http.Server{ // #nosec G112 -- intentionally do not time out the client
128-
Handler: http.HandlerFunc(handleHttpRequest), // #nosec G112
129+
Handler: http.HandlerFunc(handleHTTPRequest), // #nosec G112
129130
} // #nosec G112
130131

131132
// start the server in a goroutine
@@ -148,7 +149,6 @@ func main() {
148149
if cfg.AllowHealthcheck {
149150
go healthCheckServer(cfg.SocketPath)
150151
slog.Debug("healthcheck ready")
151-
152152
}
153153

154154
// Wait for stop signal

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525
defaultWatchdogInterval = uint(0) // watchdog interval in seconds (0 to disable)
2626
defaultStopOnWatchdog = false // set to true to stop the program when the socket gets unavailable (otherwise log only)
2727
defaultProxySocketEndpoint = "" // empty string means no socket listener, but regular TCP listener
28-
defaultProxySocketEndpointFileMode = uint(0400) // set the file mode of the unix socket endpoint
28+
defaultProxySocketEndpointFileMode = uint(0o400) // set the file mode of the unix socket endpoint
2929
)
3030

3131
type Config struct {
@@ -180,13 +180,13 @@ func InitConfig() (*Config, error) {
180180
if rx.regexStringFromParam != "" {
181181
r, err := regexp.Compile("^" + rx.regexStringFromParam + "$")
182182
if err != nil {
183-
return nil, fmt.Errorf("invalid regex \"%s\" for method %s in command line parameter: %s", rx.regexStringFromParam, rx.method, err)
183+
return nil, fmt.Errorf("invalid regex \"%s\" for method %s in command line parameter: %w", rx.regexStringFromParam, rx.method, err)
184184
}
185185
cfg.AllowedRequests[rx.method] = r
186186
} else if rx.regexStringFromEnv != "" {
187187
r, err := regexp.Compile("^" + rx.regexStringFromEnv + "$")
188188
if err != nil {
189-
return nil, fmt.Errorf("invalid regex \"%s\" for method %s in env variable: %s", rx.regexStringFromParam, rx.method, err)
189+
return nil, fmt.Errorf("invalid regex \"%s\" for method %s in env variable: %w", rx.regexStringFromParam, rx.method, err)
190190
}
191191
cfg.AllowedRequests[rx.method] = r
192192
}

0 commit comments

Comments
 (0)