2023-04-20 16:31:35 -04:00
|
|
|
// Package anyhttp has helpers to serve http from unix sockets and systemd socket activated fds
|
|
|
|
package anyhttp
|
|
|
|
|
|
|
|
import (
|
2024-12-17 13:21:37 -05:00
|
|
|
"context"
|
2023-04-20 16:31:35 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2024-12-17 12:07:42 -05:00
|
|
|
"net/url"
|
2023-04-20 16:31:35 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-04-30 23:37:46 -04:00
|
|
|
"sync"
|
2023-04-20 16:31:35 -04:00
|
|
|
"syscall"
|
2024-12-17 12:07:42 -05:00
|
|
|
"time"
|
2024-12-17 13:21:37 -05:00
|
|
|
|
|
|
|
"go.balki.me/anyhttp/idle"
|
2023-04-20 16:31:35 -04:00
|
|
|
)
|
|
|
|
|
2023-09-11 20:28:04 -04:00
|
|
|
// AddressType of the address passed
|
2023-09-08 19:10:36 -04:00
|
|
|
type AddressType string
|
|
|
|
|
|
|
|
var (
|
2023-09-11 20:28:04 -04:00
|
|
|
// UnixSocket - address is a unix socket, e.g. unix//run/foo.sock
|
2023-09-08 19:10:36 -04:00
|
|
|
UnixSocket AddressType = "UnixSocket"
|
2023-09-11 20:28:04 -04:00
|
|
|
// SystemdFD - address is a systemd fd, e.g. sysd/fdname/myapp.socket
|
|
|
|
SystemdFD AddressType = "SystemdFD"
|
|
|
|
// TCP - address is a TCP address, e.g. :1234
|
|
|
|
TCP AddressType = "TCP"
|
|
|
|
// Unknown - address is not recognized
|
|
|
|
Unknown AddressType = "Unknown"
|
2023-09-08 19:10:36 -04:00
|
|
|
)
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// UnixSocketConfig has the configuration for Unix socket
|
2023-04-20 16:31:35 -04:00
|
|
|
type UnixSocketConfig struct {
|
2023-04-20 17:56:44 -04:00
|
|
|
|
|
|
|
// Absolute or relative path of socket, e.g. /run/app.sock
|
|
|
|
SocketPath string
|
|
|
|
|
|
|
|
// Socket file permission
|
|
|
|
SocketMode fs.FileMode
|
|
|
|
|
|
|
|
// Whether to delete existing socket before creating new one
|
2023-04-20 16:31:35 -04:00
|
|
|
RemoveExisting bool
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// DefaultUnixSocketConfig has defaults for UnixSocketConfig
|
2023-04-20 16:31:35 -04:00
|
|
|
var DefaultUnixSocketConfig = UnixSocketConfig{
|
|
|
|
SocketMode: 0666,
|
|
|
|
RemoveExisting: true,
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// NewUnixSocketConfig creates a UnixSocketConfig with the default values and the socketPath passed
|
2023-04-20 16:31:35 -04:00
|
|
|
func NewUnixSocketConfig(socketPath string) UnixSocketConfig {
|
|
|
|
usc := DefaultUnixSocketConfig
|
|
|
|
usc.SocketPath = socketPath
|
|
|
|
return usc
|
|
|
|
}
|
|
|
|
|
2023-04-30 23:37:46 -04:00
|
|
|
type sysdEnvData struct {
|
|
|
|
pid int
|
|
|
|
fdNames []string
|
|
|
|
fdNamesStr string
|
|
|
|
numFds int
|
|
|
|
}
|
|
|
|
|
|
|
|
var sysdEnvParser = struct {
|
|
|
|
sysdOnce sync.Once
|
|
|
|
data sysdEnvData
|
|
|
|
err error
|
|
|
|
}{}
|
|
|
|
|
|
|
|
func parse() (sysdEnvData, error) {
|
|
|
|
p := &sysdEnvParser
|
|
|
|
p.sysdOnce.Do(func() {
|
|
|
|
p.data.pid, p.err = strconv.Atoi(os.Getenv("LISTEN_PID"))
|
|
|
|
if p.err != nil {
|
|
|
|
p.err = fmt.Errorf("invalid LISTEN_PID, err: %w", p.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.data.numFds, p.err = strconv.Atoi(os.Getenv("LISTEN_FDS"))
|
|
|
|
if p.err != nil {
|
|
|
|
p.err = fmt.Errorf("invalid LISTEN_FDS, err: %w", p.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.data.fdNamesStr = os.Getenv("LISTEN_FDNAMES")
|
|
|
|
p.data.fdNames = strings.Split(p.data.fdNamesStr, ":")
|
|
|
|
|
|
|
|
})
|
|
|
|
return p.data, p.err
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// SysdConfig has the configuration for the socket activated fd
|
2023-04-20 16:31:35 -04:00
|
|
|
type SysdConfig struct {
|
2023-04-20 17:56:44 -04:00
|
|
|
// Integer value starting at 0. Either index or name is required
|
|
|
|
FDIndex *int
|
|
|
|
// Name configured via FileDescriptorName or the default socket file name. Either index or name is required
|
|
|
|
FDName *string
|
|
|
|
// Check process PID matches LISTEN_PID
|
2023-04-20 16:31:35 -04:00
|
|
|
CheckPID bool
|
2023-04-20 17:56:44 -04:00
|
|
|
// Unsets the LISTEN* environment variables, so they don't get passed to any child processes
|
2023-04-20 16:31:35 -04:00
|
|
|
UnsetEnv bool
|
2024-12-17 12:07:42 -05:00
|
|
|
// Shutdown http server if no requests received for below timeout
|
|
|
|
IdleTimeout *time.Duration
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// DefaultSysdConfig has the default values for SysdConfig
|
2023-04-20 16:31:35 -04:00
|
|
|
var DefaultSysdConfig = SysdConfig{
|
|
|
|
CheckPID: true,
|
2023-04-30 23:37:46 -04:00
|
|
|
UnsetEnv: true,
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// NewSysDConfigWithFDIdx creates SysdConfig with defaults and fdIdx
|
2023-04-20 16:31:35 -04:00
|
|
|
func NewSysDConfigWithFDIdx(fdIdx int) SysdConfig {
|
|
|
|
sysc := DefaultSysdConfig
|
|
|
|
sysc.FDIndex = &fdIdx
|
|
|
|
return sysc
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// NewSysDConfigWithFDName creates SysdConfig with defaults and fdName
|
2023-04-20 16:31:35 -04:00
|
|
|
func NewSysDConfigWithFDName(fdName string) SysdConfig {
|
|
|
|
sysc := DefaultSysdConfig
|
|
|
|
sysc.FDName = &fdName
|
|
|
|
return sysc
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// GetListener returns the unix socket listener
|
2023-04-20 16:31:35 -04:00
|
|
|
func (u *UnixSocketConfig) GetListener() (net.Listener, error) {
|
|
|
|
|
|
|
|
if u.RemoveExisting {
|
|
|
|
if err := os.Remove(u.SocketPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.Listen("unix", u.SocketPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Chmod(u.SocketPath, u.SocketMode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// StartFD is the starting file descriptor number
|
2023-04-20 16:31:35 -04:00
|
|
|
const StartFD = 3
|
|
|
|
|
|
|
|
func makeFdListener(fd int, name string) (net.Listener, error) {
|
|
|
|
fdFile := os.NewFile(uintptr(fd), name)
|
|
|
|
l, err := net.FileListener(fdFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
syscall.CloseOnExec(fd)
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// GetListener returns the FileListener created with socketed activated fd
|
2023-04-20 16:31:35 -04:00
|
|
|
func (s *SysdConfig) GetListener() (net.Listener, error) {
|
|
|
|
|
|
|
|
if s.UnsetEnv {
|
|
|
|
defer UnsetSystemdListenVars()
|
|
|
|
}
|
|
|
|
|
2023-04-30 23:37:46 -04:00
|
|
|
envData, err := parse()
|
2023-04-20 16:31:35 -04:00
|
|
|
if err != nil {
|
2023-04-30 23:37:46 -04:00
|
|
|
return nil, err
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
|
2023-04-30 23:37:46 -04:00
|
|
|
if s.CheckPID {
|
|
|
|
if envData.pid != os.Getpid() {
|
|
|
|
return nil, fmt.Errorf("unexpected PID, current:%v, LISTEN_PID: %v", os.Getpid(), envData.pid)
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 16:31:35 -04:00
|
|
|
|
|
|
|
if s.FDIndex != nil {
|
|
|
|
idx := *s.FDIndex
|
2023-04-30 23:37:46 -04:00
|
|
|
if idx < 0 || idx >= envData.numFds {
|
|
|
|
return nil, fmt.Errorf("invalid fd index, expected between 0 and %v, got: %v", envData.numFds, idx)
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
fd := StartFD + idx
|
2023-04-30 23:37:46 -04:00
|
|
|
if idx < len(envData.fdNames) {
|
|
|
|
return makeFdListener(fd, envData.fdNames[idx])
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
2023-04-20 17:56:44 -04:00
|
|
|
return makeFdListener(fd, fmt.Sprintf("sysdfd_%d", fd))
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.FDName != nil {
|
2023-04-30 23:37:46 -04:00
|
|
|
for idx, name := range envData.fdNames {
|
2023-04-20 16:31:35 -04:00
|
|
|
if name == *s.FDName {
|
|
|
|
fd := StartFD + idx
|
|
|
|
return makeFdListener(fd, name)
|
|
|
|
}
|
|
|
|
}
|
2023-04-30 23:37:46 -04:00
|
|
|
return nil, fmt.Errorf("fdName not found: %q, LISTEN_FDNAMES:%q", *s.FDName, envData.fdNamesStr)
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 15:29:17 -04:00
|
|
|
return nil, errors.New("neither FDIndex nor FDName set")
|
|
|
|
}
|
|
|
|
|
2024-12-17 13:21:37 -05:00
|
|
|
// Serve creates and serve a http server.
|
|
|
|
func Serve(addr string, h http.Handler) (addrType AddressType, srv *http.Server, idler idle.Idler, done <-chan error, err error) {
|
|
|
|
addrType, usc, sysc, err := ParseAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
|
2024-12-17 13:21:37 -05:00
|
|
|
listener, err := func() (net.Listener, error) {
|
|
|
|
if usc != nil {
|
|
|
|
return usc.GetListener()
|
|
|
|
} else if sysc != nil {
|
|
|
|
return sysc.GetListener()
|
2023-09-08 19:10:36 -04:00
|
|
|
} else {
|
2024-12-17 13:21:37 -05:00
|
|
|
if addr == "" {
|
|
|
|
addr = ":http"
|
|
|
|
}
|
|
|
|
return net.Listen("tcp", addr)
|
2023-09-08 19:10:36 -04:00
|
|
|
}
|
2024-12-17 13:21:37 -05:00
|
|
|
}()
|
2023-09-08 19:10:36 -04:00
|
|
|
if err != nil {
|
2024-12-17 13:21:37 -05:00
|
|
|
return
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
2024-12-17 13:21:37 -05:00
|
|
|
errChan := make(chan error)
|
|
|
|
done = errChan
|
|
|
|
if addrType == SystemdFD && sysc.IdleTimeout != nil {
|
|
|
|
idler = idle.CreateIdler(*sysc.IdleTimeout)
|
|
|
|
srv = &http.Server{Handler: idle.WrapIdlerHandler(idler, h)}
|
|
|
|
waitErrChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
waitErrChan <- srv.Serve(listener)
|
|
|
|
close(waitErrChan)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case err := <-waitErrChan:
|
|
|
|
errChan <- err
|
|
|
|
case <-idler.Chan():
|
|
|
|
errChan <- srv.Shutdown(context.TODO())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
srv = &http.Server{Handler: h}
|
|
|
|
go func() {
|
|
|
|
errChan <- srv.Serve(listener)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return
|
2023-09-08 19:10:36 -04:00
|
|
|
}
|
2023-04-20 16:31:35 -04:00
|
|
|
|
2023-09-08 19:10:36 -04:00
|
|
|
// ListenAndServe is the drop-in replacement for `http.ListenAndServe`.
|
|
|
|
// Supports unix and systemd sockets in addition
|
|
|
|
func ListenAndServe(addr string, h http.Handler) error {
|
2024-12-17 13:21:37 -05:00
|
|
|
_, _, _, done, err := Serve(addr, h)
|
2023-09-11 20:28:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return <-done
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
|
|
|
|
2023-04-20 17:56:44 -04:00
|
|
|
// UnsetSystemdListenVars unsets the LISTEN* environment variables so they are not passed to any child processes
|
2023-04-20 16:31:35 -04:00
|
|
|
func UnsetSystemdListenVars() {
|
2023-04-30 23:37:46 -04:00
|
|
|
_ = os.Unsetenv("LISTEN_PID")
|
|
|
|
_ = os.Unsetenv("LISTEN_FDS")
|
|
|
|
_ = os.Unsetenv("LISTEN_FDNAMES")
|
2023-04-20 16:31:35 -04:00
|
|
|
}
|
2024-12-17 12:07:42 -05:00
|
|
|
|
|
|
|
func ParseAddress(addr string) (addrType AddressType, usc *UnixSocketConfig, sysc *SysdConfig, err error) {
|
|
|
|
// addrType = Unknown
|
|
|
|
usc = nil
|
|
|
|
sysc = nil
|
|
|
|
err = nil
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return TCP, nil, nil, nil
|
|
|
|
}
|
|
|
|
if u.Path == "unix" {
|
|
|
|
duc := DefaultUnixSocketConfig
|
|
|
|
usc = &duc
|
|
|
|
addrType = UnixSocket
|
|
|
|
for key, val := range u.Query() {
|
|
|
|
if key == "path" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("unix socket address error. Multiple path found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
usc.SocketPath = val[0]
|
|
|
|
} else if key == "mode" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("unix socket address error. Multiple mode found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, serr := fmt.Sscanf(val[0], "%o", &usc.SocketMode); serr != nil {
|
|
|
|
err = fmt.Errorf("unix socket address error. Bad mode: %v, err: %w", val, serr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if key == "remove_existing" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("unix socket address error. Multiple remove_existing found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if removeExisting, berr := strconv.ParseBool(val[0]); berr != nil {
|
|
|
|
err = fmt.Errorf("unix socket address error. Bad remove_existing: %v, err: %w", val, berr)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
usc.RemoveExisting = removeExisting
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("unix socket address error. Bad option; key: %v, val: %v", key, val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usc.SocketPath == "" {
|
|
|
|
err = fmt.Errorf("unix socket address error. Missing path; addr: %v", addr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if u.Path == "sysd" {
|
|
|
|
dsc := DefaultSysdConfig
|
|
|
|
sysc = &dsc
|
|
|
|
addrType = SystemdFD
|
|
|
|
for key, val := range u.Query() {
|
|
|
|
if key == "name" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Multiple name found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sysc.FDName = &val[0]
|
|
|
|
} else if key == "idx" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Multiple idx found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if idx, ierr := strconv.Atoi(val[0]); ierr != nil {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Bad idx: %v, err: %w", val, ierr)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
sysc.FDIndex = &idx
|
|
|
|
}
|
|
|
|
} else if key == "check_pid" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Multiple check_pid found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if checkPID, berr := strconv.ParseBool(val[0]); berr != nil {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Bad check_pid: %v, err: %w", val, berr)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
sysc.CheckPID = checkPID
|
|
|
|
}
|
|
|
|
} else if key == "unset_env" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Multiple unset_env found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if unsetEnv, berr := strconv.ParseBool(val[0]); berr != nil {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Bad unset_env: %v, err: %w", val, berr)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
sysc.UnsetEnv = unsetEnv
|
|
|
|
}
|
|
|
|
} else if key == "idle_timeout" {
|
|
|
|
if len(val) != 1 {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Multiple idle_timeout found: %v", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if timeout, terr := time.ParseDuration(val[0]); terr != nil {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Bad idle_timeout: %v, err: %w", val, terr)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
sysc.IdleTimeout = &timeout
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Bad option; key: %v, val: %v", key, val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sysc.FDIndex == nil) == (sysc.FDName == nil) {
|
|
|
|
err = fmt.Errorf("systemd socket fd address error. Exactly only one of name and idx has to be set. name: %v, idx: %v", sysc.FDName, sysc.FDIndex)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Just assume as TCP address
|
|
|
|
return TCP, nil, nil, nil
|
|
|
|
}
|