Simple Relay between a Unix socket and a TCP socket, and vice versa.

Simple TCP <-> Unix Relay

simpletcpunixrelay is a program which exposes a TCP endpoint as a Unix socket and vice versa.

Usecase

Diagram of interconnection

Let's say you are running your systemd services securely, and use PrivateNetwork=yes in your unit configuration. If you want other programs to connect and interact with your service, you best bet is to listen onto an Unix socket. This works well for program that supports it, PostgreSQL is an example.

However, some programs do not support listening and/or accepting connections over a UNIX socket. simpletcpunixrelay converts a TCP only program into a program which accepts connections over a UNIX socket.

WARNING: If you're looking for a program to securely relay a Unix socket over the internet, do NOT use simpletcpunixrelay, as it does NOT provide any encryption. For this use case, we recommend spiped, and recommend against simpletcpunixrelay.

Why not X?

There are multiple programs which could achieve the same result, but they are not the best tool for the job.

socat

One can use socat to relay from a Unix socket towards a TCP socket, and vice-versa.

However socat forks a new process for each connection, which leads to the C10k problem.

spiped

spiped is a recommended alternative for exposing a Unix socket over the internet. It is not impacted by the C10k problem.

However, it adds a layer of encryption, which impacts performance, and is unnecessary overhead for loopback traffic.

Configuration example

Use PrivateNetwork=yes for your service:

# /etc/systemd/system/foobar.service
[Unit]
Description=Foobar server

[Service]
User=foobar
Group=foobar

ExecStart=/usr/local/bin/foobard --address=127.0.0.1:8000

PrivateNetwork=yes
ProtectSystem=full
ProtectHome=yes
ProtectProc=invisible

[Install]
WantedBy=network.target

Then create a service to relay foobar.service to a Unix socket. You need to use JoinsNamespaceOf= in order to run in the same network namespace.

# /etc/systemd/system/foobar-unix.service
[Unit]
Description=Foobar unix socket
JoinsNamespaceOf=foobar.service
Requires=foobar.service

[Service]
User=foobar
Group=foobar

ExecStart=/usr/local/bin/simpletcpunixrelay ${RUNTIME_DIRECTORY}/socket 127.0.0.1:8000

RuntimeDirectory=foobar-unix
PrivateNetwork=yes
ProtectSystem=full
ProtectHome=yes
ProtectProc=invisible

You can also run it as a different user and/or group than foobar, in order to limit who can connect to it.

Optionally, if your service needs to be exposed as TCP. For example, in the case of minio, its client library doesn't support connecting over Unix socket. You can re-expose the service externally. For permission reasons, you need to run it as the same user as foobar-unix.service.

# /etc/systemd/system/foobar-external.service
[Unit]
Description=Foobar exposed to other programs
Requires=foobar-unix.service

[Service]
User=foobar
Group=foobar

ExecStart=/usr/local/bin/simpletcpunixrelay 127.0.0.1:8000 /var/run/foobar-unix/socket
Owner
Antoine Catton
PGP: 627E 0611 417E BADD EE9C 3A6B 8A28 54E3 A76E BE89. For more info look at here 🠖
Antoine Catton
Similar Resources

A TCP socket based chat server implemented using Go

Go Chat Server A better TCP socket chat server implemented using Go Connecting nc localhost 5000 Docker Build the container image docker build -t grub

Oct 16, 2021

Make TCP connection storm between server and client for benchmarking network stuff

Make TCP connection storm between server and client for benchmarking network stuff

Nov 14, 2021

TcpRoute , TCP 层的路由器。对于 TCP 连接自动从多个线路(电信、联通、移动)、多个域名解析结果中选择最优线路。

TcpRoute2 TcpRoute , TCP 层的路由器。对于 TCP 连接自动从多个线路(允许任意嵌套)、多个域名解析结果中选择最优线路。 TcpRoute 使用激进的选路策略,对 DNS 解析获得的多个IP同时尝试连接,同时使用多个线路进行连接,最终使用最快建立的连接。支持 TcpRoute

Dec 27, 2022

Multiplexer over TCP. Useful if target server only allows you to create limited tcp connections concurrently.

tcp-multiplexer Use it in front of target server and let your client programs connect it, if target server only allows you to create limited tcp conne

May 27, 2021

Tcp chat go - Create tcp chat in golang

TCP chat in GO libs Go net package and goroutines and channels tcp tcp or transm

Feb 5, 2022

Server and relay side infrastructure for RDA

BitterJohn Server and relay side infrastructure for RDA. Usage install sudo ./BitterJohn install -g systemctl enable --now BitterJohn upgrade sudo ./B

Dec 14, 2022

Message relay written in golang for PostgreSQL and Apache Kafka

Message Relay Message relay written in golang for PostgreSQL and Apache Kafka Requirements Docker and Docker Compose Local installation and using dock

Dec 19, 2021

gRelay is an open source project written in Go that provides the circuit break pattern with a relay idea behind.

gRelay is an open source project written in Go that provides the circuit break pattern with a relay idea behind.

gRELAY gRelay is an open source project written in Go that provides: Circuit Break ✔️ Circuit Break + Relay ✔️ Concurrecny Safe ✔️ Getting start Insta

Sep 30, 2022

A http-relay server/client written in golang to forward requests to a service behind a nat router from web

http-relay This repo is WIP http-relay is a server/client application written in go(lang) to forward http(s) requests to an application behind a nat r

Dec 16, 2021
oniongrok forwards ports on the local host to remote Onion addresses as Tor hidden services and vice-versa

oniongrok Onion addresses for anything. oniongrok forwards ports on the local host to remote Onion addresses as Tor hidden services and vice-versa. Wh

Jan 1, 2023
TCP output for beats to send events over TCP socket.

beats-tcp-output How To Use Clone this project to elastic/beats/libbeat/output/ Modify elastic/beats/libbeat/publisher/includes/includes.go : // add i

Aug 25, 2022
Proxy - Minimalistic TCP relay proxy.

Proxy Minimalistic TCP relay proxy. Installation ensure you have go >= 1.17 installed clone the repo cd proxy go install main.go Examples Listen on po

May 22, 2022
Simple forwarding a unix domain socket to a local port.

WaziApp Proxy WaziApp proxy is a simple http proxy that is intended to listen on the WaziApp unix socket /var/lib/waziapp/proxy.sock and forwards to a

Nov 18, 2021
Golang unix-socket wrapper

Sockunx Golang unix-socket wrapper Server Running server server, e := sockunx.NewServer("/path/to/your/socks.sock", 512) if e != nil { log.Fatal(e

Jan 17, 2022
Glue - Robust Go and Javascript Socket Library (Alternative to Socket.io)

Glue - Robust Go and Javascript Socket Library Glue is a real-time bidirectional socket library. It is a clean, robust and efficient alternative to so

Nov 25, 2022
GOWS is GoLang web-socket module Provides you with ease of handling web socket connections with a few lines

GOWS GOWS is GoLang web-socket module Provides you with ease of handling web socket connections with a few lines, it supports multi-connection on one

Apr 4, 2022
Plugs module to see different types of plug types needed in different countries, and a comparison tool between two countries plug socket types.

plugs Importing the module: go get github.com/matthewboyd/plugs "github.com/matthewboyd/plugs" How to use the module: There are two functions wi

Dec 28, 2021
Encode and Decode Message Length Indicators for TCP/IP socket based protocols

SimpleMLI A Message Length Indicator Encoder/Decoder Message Length Indicators (MLI) are commonly used in communications over raw TCP/IP sockets. This

Nov 24, 2022