1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
// Copyright (C) 2022 Andrew Ayer
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name(s) of the above copyright
// holders shall not be used in advertising or otherwise to promote the
// sale, use or other dealings in this Software without prior written
// authorization.
package listener // import "src.agwa.name/go-listener"
import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"
"src.agwa.name/go-listener/proxy"
"src.agwa.name/go-listener/unix"
)
func init() {
RegisterListenerType("fd", openFDListener)
RegisterListenerType("fdname", openFDNameListener)
RegisterListenerType("tcp", openTCPListener)
RegisterListenerType("unix", openUnixListener)
RegisterListenerType("proxy", openProxyListener)
}
func openFDListener(params map[string]interface{}, arg string) (net.Listener, error) {
var fdString string
if arg != "" {
fdString = arg
} else if param, ok := params["fd"].(json.Number); ok {
fdString = string(param)
} else if param, ok := params["fd"].(string); ok {
fdString = param
} else {
return nil, errors.New("file descriptor not specified for FD listener")
}
fd, err := strconv.ParseUint(fdString, 10, 64)
if err != nil {
return nil, fmt.Errorf("'%s' is a malformed file descriptor", fdString)
}
file := os.NewFile(uintptr(fd), fdString)
defer file.Close()
return net.FileListener(file)
}
func openFDNameListener(params map[string]interface{}, arg string) (net.Listener, error) {
var name string
if arg != "" {
name = arg
} else if param, ok := params["name"].(string); ok {
name = param
} else {
return nil, errors.New("name not specified for fdname listener")
}
if listenPidStr := os.Getenv("LISTEN_PID"); listenPidStr == "" {
return nil, errors.New("cannot create fdname listener because $LISTEN_PID is not set")
} else if listenPid, err := strconv.Atoi(listenPidStr); err != nil {
return nil, errors.New("cannot create fdname listener because $LISTEN_PID does not contain an integer")
} else if ourPid := os.Getpid(); listenPid != ourPid {
return nil, fmt.Errorf("cannot create fdname listener because $LISTEN_PID (%d) does not match our PID (%d)", listenPid, ourPid)
}
for i, ithname := range strings.Split(os.Getenv("LISTEN_FDNAMES"), ":") {
if ithname == name {
file := os.NewFile(uintptr(3+i), name)
defer file.Close()
return net.FileListener(file)
}
}
return nil, fmt.Errorf("fdname: %q not found in $LISTEN_FDNAMES", name)
}
func openTCPListener(params map[string]interface{}, arg string) (net.Listener, error) {
var ipString string
var portString string
var err error
if arg != "" {
if strings.Contains(arg, ":") {
ipString, portString, err = net.SplitHostPort(arg)
if err != nil {
return nil, fmt.Errorf("TCP listener has invalid argument: %w", err)
}
} else {
portString = arg
}
} else if param, ok := params["address"].(string); ok {
ipString = param
} else if param, ok := params["port"].(json.Number); ok {
portString = string(param)
} else if param, ok := params["port"].(string); ok {
portString = param
}
network := "tcp"
address := new(net.TCPAddr)
if ipString != "" {
address.IP = net.ParseIP(ipString)
if address.IP == nil {
return nil, errors.New("TCP listener has invalid IP address")
}
// Explicitly specify the IP protocol, to ensure that 0.0.0.0
// and :: work as expected (listen only on IPv4 or IPv6 interfaces)
if address.IP.To4() == nil {
network = "tcp6"
} else {
network = "tcp4"
}
}
address.Port, err = strconv.Atoi(portString)
if err != nil {
return nil, fmt.Errorf("TCP listener has invalid port: %w", err)
}
return net.ListenTCP(network, address)
}
func openUnixListener(params map[string]interface{}, arg string) (net.Listener, error) {
var path string
if arg != "" {
path = arg
} else if value, ok := params["path"].(string); ok {
path = value
} else {
return nil, errors.New("path not specified for UNIX listener")
}
return unix.Listen(path, 0666)
}
func openProxyListener(params map[string]interface{}, arg string) (net.Listener, error) {
var inner net.Listener
var err error
if arg != "" {
inner, err = Open(arg)
} else if spec, ok := params["listener"].(map[string]interface{}); ok {
inner, err = OpenJSON(spec)
} else {
return nil, errors.New("inner socket not specified for proxy listener")
}
if err != nil {
return nil, err
}
return proxy.NewListener(inner), nil
}
|