logranger/listener.go
Winni Neessen ea1ac393bc
Add SPDX license identifiers and update copyright year
All source code files as well as README.md, .gitignore, and various configuration files have been updated with an SPDX license identifier, changing license from CC0-1.0 to MIT. The copyright year has been updated to 2023, and the copyright holder's email address has been corrected from 'winni@neessen.dev' to 'wn@neessen.dev'. An MIT license text file has been additionally added.
2023-12-15 16:35:41 +01:00

83 lines
2.2 KiB
Go

// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT
package logranger
import (
"crypto/tls"
"fmt"
"net"
"strings"
)
// ListenerType is an enumeration wrapper for the different listener types
type ListenerType uint
const (
ListenerUnix ListenerType = iota
ListenerTCP
ListenerTLS
)
func NewListener(c *Config) (net.Listener, error) {
var l net.Listener
var lerr error
switch c.Listener.Type {
case ListenerUnix:
rua, err := net.ResolveUnixAddr("unix", c.Listener.ListenerUnix.Path)
if err != nil {
return nil, fmt.Errorf("failed to resolve UNIX listener socket: %w", err)
}
l, lerr = net.Listen("unix", rua.String())
case ListenerTCP:
la := net.JoinHostPort(c.Listener.ListenerTCP.Addr, fmt.Sprintf("%d", c.Listener.ListenerTCP.Port))
l, lerr = net.Listen("tcp", la)
case ListenerTLS:
if c.Listener.ListenerTLS.CertPath == "" || c.Listener.ListenerTLS.KeyPath == "" {
return nil, ErrCertConfigEmpty
}
ce, err := tls.LoadX509KeyPair(c.Listener.ListenerTLS.CertPath, c.Listener.ListenerTLS.KeyPath)
if err != nil {
return nil, fmt.Errorf("failed to load X509 certificate: %w", err)
}
la := net.JoinHostPort(c.Listener.ListenerTCP.Addr, fmt.Sprintf("%d", c.Listener.ListenerTCP.Port))
lc := &tls.Config{Certificates: []tls.Certificate{ce}}
l, lerr = tls.Listen("tcp", la, lc)
default:
return nil, fmt.Errorf("failed to initialize listener: unknown listener type in config")
}
if lerr != nil {
return nil, fmt.Errorf("failed to initalize listener: %w", lerr)
}
return l, nil
}
// UnmarshalString satisfies the fig.StringUnmarshaler interface for the ListenerType type
func (l *ListenerType) UnmarshalString(v string) error {
switch strings.ToLower(v) {
case "unix":
*l = ListenerUnix
case "tcp":
*l = ListenerTCP
case "tls":
*l = ListenerTLS
default:
return fmt.Errorf("unknown listener type: %s", v)
}
return nil
}
// String satisfies the fmt.Stringer interface for the ListenerType type
func (l ListenerType) String() string {
switch l {
case ListenerUnix:
return "UNIX listener"
case ListenerTCP:
return "TCP listener"
case ListenerTLS:
return "TLS listener"
default:
return "Unknown listener type"
}
}