2019-10-15 16:39:51 +03:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2019-10-15 16:39:51 +03:00
|
|
|
// This code is heavily inspired by the archived gofacebook/gracenet/net.go handler
|
|
|
|
|
2021-08-24 19:47:09 +03:00
|
|
|
//go:build !windows
|
|
|
|
|
2019-10-15 16:39:51 +03:00
|
|
|
package graceful
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-05-16 01:20:30 +03:00
|
|
|
"time"
|
2019-10-15 16:39:51 +03:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-11-24 05:11:24 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-11 23:05:34 +03:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-10-15 16:39:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-05-16 01:20:30 +03:00
|
|
|
listenFDsEnv = "LISTEN_FDS"
|
|
|
|
startFD = 3
|
|
|
|
unlinkFDsEnv = "GITEA_UNLINK_FDS"
|
|
|
|
|
|
|
|
notifySocketEnv = "NOTIFY_SOCKET"
|
|
|
|
watchdogTimeoutEnv = "WATCHDOG_USEC"
|
2019-10-15 16:39:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// In order to keep the working directory the same as when we started we record
|
|
|
|
// it at startup.
|
|
|
|
var originalWD, _ = os.Getwd()
|
|
|
|
|
|
|
|
var (
|
|
|
|
once = sync.Once{}
|
|
|
|
mutex = sync.Mutex{}
|
|
|
|
|
2022-08-14 00:31:33 +03:00
|
|
|
providedListenersToUnlink = []bool{}
|
|
|
|
activeListenersToUnlink = []bool{}
|
|
|
|
providedListeners = []net.Listener{}
|
|
|
|
activeListeners = []net.Listener{}
|
2023-05-16 01:20:30 +03:00
|
|
|
|
|
|
|
notifySocketAddr string
|
|
|
|
watchdogTimeout time.Duration
|
2019-10-15 16:39:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func getProvidedFDs() (savedErr error) {
|
|
|
|
// Only inherit the provided FDS once but we will save the error so that repeated calls to this function will return the same error
|
|
|
|
once.Do(func() {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-05-16 01:20:30 +03:00
|
|
|
// now handle some additional systemd provided things
|
|
|
|
notifySocketAddr = os.Getenv(notifySocketEnv)
|
|
|
|
if notifySocketAddr != "" {
|
|
|
|
log.Debug("Systemd Notify Socket provided: %s", notifySocketAddr)
|
|
|
|
savedErr = os.Unsetenv(notifySocketEnv)
|
|
|
|
if savedErr != nil {
|
|
|
|
log.Warn("Unable to Unset the NOTIFY_SOCKET environment variable: %v", savedErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// FIXME: We don't handle WATCHDOG_PID
|
|
|
|
timeoutStr := os.Getenv(watchdogTimeoutEnv)
|
|
|
|
if timeoutStr != "" {
|
|
|
|
savedErr = os.Unsetenv(watchdogTimeoutEnv)
|
|
|
|
if savedErr != nil {
|
|
|
|
log.Warn("Unable to Unset the WATCHDOG_USEC environment variable: %v", savedErr)
|
|
|
|
return
|
|
|
|
}
|
2019-10-15 16:39:51 +03:00
|
|
|
|
2023-05-16 01:20:30 +03:00
|
|
|
s, err := strconv.ParseInt(timeoutStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to parse the provided WATCHDOG_USEC: %v", err)
|
|
|
|
savedErr = fmt.Errorf("unable to parse the provided WATCHDOG_USEC: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s <= 0 {
|
|
|
|
log.Error("Unable to parse the provided WATCHDOG_USEC: %s should be a positive number", timeoutStr)
|
|
|
|
savedErr = fmt.Errorf("unable to parse the provided WATCHDOG_USEC: %s should be a positive number", timeoutStr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
watchdogTimeout = time.Duration(s) * time.Microsecond
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Trace("No Systemd Notify Socket provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
numFDs := os.Getenv(listenFDsEnv)
|
2019-10-15 16:39:51 +03:00
|
|
|
if numFDs == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err := strconv.Atoi(numFDs)
|
|
|
|
if err != nil {
|
2023-05-16 01:20:30 +03:00
|
|
|
savedErr = fmt.Errorf("%s is not a number: %s. Err: %w", listenFDsEnv, numFDs, err)
|
2019-10-15 16:39:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-16 01:20:30 +03:00
|
|
|
fdsToUnlinkStr := strings.Split(os.Getenv(unlinkFDsEnv), ",")
|
2022-08-14 00:31:33 +03:00
|
|
|
providedListenersToUnlink = make([]bool, n)
|
|
|
|
for _, fdStr := range fdsToUnlinkStr {
|
|
|
|
i, err := strconv.Atoi(fdStr)
|
|
|
|
if err != nil || i < 0 || i >= n {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
providedListenersToUnlink[i] = true
|
|
|
|
}
|
|
|
|
|
2019-10-15 16:39:51 +03:00
|
|
|
for i := startFD; i < n+startFD; i++ {
|
|
|
|
file := os.NewFile(uintptr(i), fmt.Sprintf("listener_FD%d", i))
|
|
|
|
|
|
|
|
l, err := net.FileListener(file)
|
|
|
|
if err == nil {
|
|
|
|
// Close the inherited file if it's a listener
|
|
|
|
if err = file.Close(); err != nil {
|
2023-05-16 01:20:30 +03:00
|
|
|
savedErr = fmt.Errorf("error closing provided socket fd %d: %w", i, err)
|
2019-10-15 16:39:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
providedListeners = append(providedListeners, l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If needed we can handle packetconns here.
|
2022-10-24 22:29:17 +03:00
|
|
|
savedErr = fmt.Errorf("Error getting provided socket fd %d: %w", i, err)
|
2019-10-15 16:39:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return savedErr
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:14:51 +03:00
|
|
|
// closeProvidedListeners closes all unused provided listeners.
|
|
|
|
func closeProvidedListeners() {
|
2019-10-15 16:39:51 +03:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
for _, l := range providedListeners {
|
|
|
|
err := l.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error in closing unused provided listener: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
providedListeners = []net.Listener{}
|
|
|
|
}
|
|
|
|
|
2023-11-15 17:02:46 +03:00
|
|
|
// DefaultGetListener obtains a listener for the stream-oriented local network address:
|
|
|
|
// "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
|
Allow the use of alternative net.Listener implementations by downstreams (#25855)
This is a simple PR which moves the `GetListener` function to a
`DefaultGetListener` function, and changes `GetListener` to be a
variable which by default points to the `DefaultGetListener` function.
This allows people who may exist quasi-downstream of Gitea to create
alternate "GetListener" functions, with identical signatures, which
return different implementations of the `net.Listener` interface. This
approach is expressly intended to be non-invasive and have the least
possible impact on the gitea codebase. A previous version of this idea
was rejected before: https://github.com/go-gitea/gitea/issues/15544 but
because of issues like: https://github.com/go-gitea/gitea/issues/22335 I
**really** think that recommending people configure proxies by hand is
exactly the wrong way to do things(This is why there is a Tor Browser.).
This tiny change lets me put proper hidden service configuration into
single `i2p.go` file which lives in `modules/graceful/` and which never
has to be checked in to your codebase or affect your dependencies or
bloat your project in any way, it can live on a branch in my fork and
I'll fast-forward every release and never the twain shall meet.
The main use-case for this is to listen on Peer-to-Peer networks and
Hidden Services directly without error-prone and cumbersome
port-forwarding configuration. For instance, I might implement an
"I2PGetListener" as follows:
```Go
// adapted from i2p.go which is unchecked-in in my modules/graceful/ directory
import "github.com/eyedeekay/onramp"
var garlic = &onramp.Garlic{}
func I2PGetListener(network, address string) (net.Listener, error) {
// Add a deferral to say that we've tried to grab a listener
defer GetManager().InformCleanup()
switch network {
case "tcp", "tcp4", "tcp6", "i2p", "i2pt":
return garlic.Listen()
case "unix", "unixpacket":
// I2P isn't really a replacement for the stuff you use Unix sockets for and it's also not an anonymity risk, so treat them normally
unixAddr, err := net.ResolveUnixAddr(network, address)
if err != nil {
return nil, err
}
return GetListenerUnix(network, unixAddr)
default:
return nil, net.UnknownNetworkError(network)
}
}
```
I could then substitute that GetListener function and be 50% of the way
to having a fully-functioning gitea-over-hidden-services instance
without any additional configuration(The other 50% doesn't require any
code-changes on gitea's part).
There are 2 advantages here, one being convenience, first this turns
hidden services into a zero-configuration option for self-hosting gitea,
and second safety, these Go libraries are passing around
hidden-service-only versions of the net.Addr struct, they're using
hidden-service-only versions of the sockets, which are both expressly
designed to never require access to any information outside the hidden
service network, manipulating the application so it reveals information
about the host becomes much more difficult, and some attacks become
nearly impossible. It also opens up TLS-over-Hidden Services support
which is niche right now, of course, but in a future where gitea
instances federate if hidden services want to be part of the federation
they're probably going to need TLS certificates. They don't need to be
painful to set up.
This doesn't fix an open issue, but it might affect:
- https://github.com/go-gitea/gitea/issues/22335 - my `i2p.go` file
actually has a mod that fixes this but it requires adding a handful of
new dependencies to gitea and isn't compatible with the normal way you
guys recommend using a proxy so I don't think it's ready to send to you
as a PR, but if I can find a non-invasive way to fix it I will.
- https://github.com/go-gitea/gitea/issues/18240
I hereby agree to the Code of Conduct published here:
https://github.com/go-gitea/gitea/blob/8b89563bf1031089a218e6d05dc61047281b35ee/CODE_OF_CONDUCT.md
I have read and understood the recommendations published here:
https://github.com/go-gitea/gitea/blob/8b89563bf1031089a218e6d05dc61047281b35ee/CONTRIBUTING.md
Thank you for your consideration.
---------
Co-authored-by: eyedeekay <idk@mulder>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2023-07-24 10:18:17 +03:00
|
|
|
func DefaultGetListener(network, address string) (net.Listener, error) {
|
2019-10-15 16:39:51 +03:00
|
|
|
// Add a deferral to say that we've tried to grab a listener
|
2019-12-15 12:51:28 +03:00
|
|
|
defer GetManager().InformCleanup()
|
2019-10-15 16:39:51 +03:00
|
|
|
switch network {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
tcpAddr, err := net.ResolveTCPAddr(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return GetListenerTCP(network, tcpAddr)
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
unixAddr, err := net.ResolveUnixAddr(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return GetListenerUnix(network, unixAddr)
|
|
|
|
default:
|
|
|
|
return nil, net.UnknownNetworkError(network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetListenerTCP announces on the local network address. The network must be:
|
|
|
|
// "tcp", "tcp4" or "tcp6". It returns a provided net.Listener for the
|
|
|
|
// matching network and address, or creates a new one using net.ListenTCP.
|
|
|
|
func GetListenerTCP(network string, address *net.TCPAddr) (*net.TCPListener, error) {
|
|
|
|
if err := getProvidedFDs(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
// look for a provided listener
|
|
|
|
for i, l := range providedListeners {
|
|
|
|
if isSameAddr(l.Addr(), address) {
|
|
|
|
providedListeners = append(providedListeners[:i], providedListeners[i+1:]...)
|
2022-08-14 00:31:33 +03:00
|
|
|
needsUnlink := providedListenersToUnlink[i]
|
|
|
|
providedListenersToUnlink = append(providedListenersToUnlink[:i], providedListenersToUnlink[i+1:]...)
|
2019-10-15 16:39:51 +03:00
|
|
|
|
|
|
|
activeListeners = append(activeListeners, l)
|
2022-08-14 00:31:33 +03:00
|
|
|
activeListenersToUnlink = append(activeListenersToUnlink, needsUnlink)
|
2019-10-15 16:39:51 +03:00
|
|
|
return l.(*net.TCPListener), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no provided listener for this address -> make a fresh listener
|
|
|
|
l, err := net.ListenTCP(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
activeListeners = append(activeListeners, l)
|
2022-08-14 00:31:33 +03:00
|
|
|
activeListenersToUnlink = append(activeListenersToUnlink, false)
|
2019-10-15 16:39:51 +03:00
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetListenerUnix announces on the local network address. The network must be:
|
|
|
|
// "unix" or "unixpacket". It returns a provided net.Listener for the
|
|
|
|
// matching network and address, or creates a new one using net.ListenUnix.
|
|
|
|
func GetListenerUnix(network string, address *net.UnixAddr) (*net.UnixListener, error) {
|
|
|
|
if err := getProvidedFDs(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
// look for a provided listener
|
|
|
|
for i, l := range providedListeners {
|
|
|
|
if isSameAddr(l.Addr(), address) {
|
|
|
|
providedListeners = append(providedListeners[:i], providedListeners[i+1:]...)
|
2022-08-14 00:31:33 +03:00
|
|
|
needsUnlink := providedListenersToUnlink[i]
|
|
|
|
providedListenersToUnlink = append(providedListenersToUnlink[:i], providedListenersToUnlink[i+1:]...)
|
|
|
|
|
|
|
|
activeListenersToUnlink = append(activeListenersToUnlink, needsUnlink)
|
2019-10-15 16:39:51 +03:00
|
|
|
activeListeners = append(activeListeners, l)
|
2019-11-24 05:11:24 +03:00
|
|
|
unixListener := l.(*net.UnixListener)
|
2022-08-14 00:31:33 +03:00
|
|
|
if needsUnlink {
|
|
|
|
unixListener.SetUnlinkOnClose(true)
|
|
|
|
}
|
2019-11-24 05:11:24 +03:00
|
|
|
return unixListener, nil
|
2019-10-15 16:39:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a fresh listener
|
2020-08-11 23:05:34 +03:00
|
|
|
if err := util.Remove(address.Name); err != nil && !os.IsNotExist(err) {
|
2022-10-24 22:29:17 +03:00
|
|
|
return nil, fmt.Errorf("Failed to remove unix socket %s: %w", address.Name, err)
|
2019-11-24 05:11:24 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 16:39:51 +03:00
|
|
|
l, err := net.ListenUnix(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-24 05:11:24 +03:00
|
|
|
|
|
|
|
fileMode := os.FileMode(setting.UnixSocketPermission)
|
|
|
|
if err = os.Chmod(address.Name, fileMode); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return nil, fmt.Errorf("Failed to set permission of unix socket to %s: %w", fileMode.String(), err)
|
2019-11-24 05:11:24 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 16:39:51 +03:00
|
|
|
activeListeners = append(activeListeners, l)
|
2022-08-14 00:31:33 +03:00
|
|
|
activeListenersToUnlink = append(activeListenersToUnlink, true)
|
2019-10-15 16:39:51 +03:00
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSameAddr(a1, a2 net.Addr) bool {
|
|
|
|
// If the addresses are not on the same network fail.
|
|
|
|
if a1.Network() != a2.Network() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the two addresses have the same string representation they're equal
|
|
|
|
a1s := a1.String()
|
|
|
|
a2s := a2.String()
|
|
|
|
if a1s == a2s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// This allows for ipv6 vs ipv4 local addresses to compare as equal. This
|
|
|
|
// scenario is common when listening on localhost.
|
|
|
|
const ipv6prefix = "[::]"
|
|
|
|
a1s = strings.TrimPrefix(a1s, ipv6prefix)
|
|
|
|
a2s = strings.TrimPrefix(a2s, ipv6prefix)
|
|
|
|
const ipv4prefix = "0.0.0.0"
|
|
|
|
a1s = strings.TrimPrefix(a1s, ipv4prefix)
|
|
|
|
a2s = strings.TrimPrefix(a2s, ipv4prefix)
|
|
|
|
return a1s == a2s
|
|
|
|
}
|
|
|
|
|
|
|
|
func getActiveListeners() []net.Listener {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
listeners := make([]net.Listener, len(activeListeners))
|
|
|
|
copy(listeners, activeListeners)
|
|
|
|
return listeners
|
|
|
|
}
|
2022-08-14 00:31:33 +03:00
|
|
|
|
|
|
|
func getActiveListenersToUnlink() []bool {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
listenersToUnlink := make([]bool, len(activeListenersToUnlink))
|
|
|
|
copy(listenersToUnlink, activeListenersToUnlink)
|
|
|
|
return listenersToUnlink
|
|
|
|
}
|
2023-05-16 01:20:30 +03:00
|
|
|
|
|
|
|
func getNotifySocket() (*net.UnixConn, error) {
|
|
|
|
if err := getProvidedFDs(); err != nil {
|
|
|
|
// This error will be logged elsewhere
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if notifySocketAddr == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
socketAddr := &net.UnixAddr{
|
|
|
|
Name: notifySocketAddr,
|
|
|
|
Net: "unixgram",
|
|
|
|
}
|
|
|
|
|
|
|
|
notifySocket, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to dial NOTIFY_SOCKET %s: %v", socketAddr, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifySocket, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWatchdogTimeout() time.Duration {
|
|
|
|
if err := getProvidedFDs(); err != nil {
|
|
|
|
// This error will be logged elsewhere
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchdogTimeout
|
|
|
|
}
|