Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for socket activation #3133

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/net/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
DialUDP = net.DialUDP
DialUnix = net.DialUnix
FileConn = net.FileConn
FileListener = net.FileListener
Listen = net.Listen
ListenTCP = net.ListenTCP
ListenUDP = net.ListenUDP
Expand Down
13 changes: 13 additions & 0 deletions transport/internet/socket_activation_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !unix
// +build !unix

package internet

import (
"fmt"
"github.com/v2fly/v2ray-core/v5/common/net"
)

func activateSocket(address string, f func(network, address string, fd uintptr)) (net.Listener, error) {
return nil, fmt.Errorf("socket activation is not supported on this platform")
}
63 changes: 63 additions & 0 deletions transport/internet/socket_activation_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build unix
// +build unix

package internet

import (
"fmt"
"os"
"path"
"strconv"
"syscall"

"github.com/v2fly/v2ray-core/v5/common/net"
)

func activateSocket(address string, f func(network, address string, fd uintptr)) (net.Listener, error) {
fd, err := strconv.Atoi(path.Base(address))
if err != nil {
return nil, err
}

err = syscall.SetNonblock(fd, true)
if err != nil {
return nil, err
}

acceptConn, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_ACCEPTCONN)
if err != nil {
return nil, err
}
if acceptConn == 0 {
return nil, fmt.Errorf("socket '%s' has not been marked to accept connections", address)
}

sockType, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
if err != nil {
return nil, err
}
if sockType != syscall.SOCK_STREAM {
// XXX: currently only stream socks are supported
return nil, fmt.Errorf("socket '%s' is not a stream socket", address)
}

ufd := uintptr(fd)

sa, err := syscall.Getsockname(fd)
if err != nil {
return nil, err
}
switch sa := sa.(type) {
case *syscall.SockaddrInet4:
addr := net.TCPAddr{IP: sa.Addr[:], Port: sa.Port, Zone: ""}
f("tcp4", addr.String(), ufd)
case *syscall.SockaddrInet6:
addr := net.TCPAddr{IP: sa.Addr[:], Port: sa.Port, Zone: strconv.Itoa(int(sa.ZoneId))}
f("tcp6", addr.String(), ufd)
}

file := os.NewFile(ufd, address)
defer file.Close()

return net.FileListener(file)
}
53 changes: 36 additions & 17 deletions transport/internet/system_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,35 @@ func (l *combinedListener) Close() error {
return l.Listener.Close()
}

func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []controller) func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if sockopt != nil {
if err := applyInboundSocketOptions(network, fd, sockopt); err != nil {
newError("failed to apply socket options to incoming connection").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
func getRawControlFunc(network, address string, ctx context.Context, sockopt *SocketConfig, controllers []controller) func(fd uintptr) {
return func(fd uintptr) {
if sockopt != nil {
if err := applyInboundSocketOptions(network, fd, sockopt); err != nil {
newError("failed to apply socket options to incoming connection").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
}

setReusePort(fd) // nolint: staticcheck
setReusePort(fd) // nolint: staticcheck

for _, controller := range controllers {
if err := controller(network, address, fd); err != nil {
newError("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
for _, controller := range controllers {
if err := controller(network, address, fd); err != nil {
newError("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
})
}
}
}

func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []controller) func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
return c.Control(getRawControlFunc(network, address, ctx, sockopt, controllers))
}
}

func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
var lc net.ListenConfig
var network, address string
var l net.Listener
var err error
// callback is called after the Listen function returns
// this is used to wrap the listener and do some post processing
callback := func(l net.Listener, err error) (net.Listener, error) {
Expand Down Expand Up @@ -93,6 +99,14 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
copy(fullAddr, address[1:])
address = string(fullAddr)
}
} else if strings.HasPrefix(address, "/dev/fd/") {
// socket activation
l, err = activateSocket(address, func(network, address string, fd uintptr) {
getRawControlFunc(network, address, ctx, sockopt, dl.controllers)(fd)
})
if err != nil {
return nil, err
}
} else {
// normal unix domain socket
var fileMode *os.FileMode
Expand Down Expand Up @@ -133,13 +147,18 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
}
}

l, err := lc.Listen(ctx, network, address)
l, err = callback(l, err)
if err == nil && sockopt != nil && sockopt.AcceptProxyProtocol {
if l == nil {
l, err = lc.Listen(ctx, network, address)
l, err = callback(l, err)
if err != nil {
return nil, err
}
}
if sockopt != nil && sockopt.AcceptProxyProtocol {
policyFunc := func(upstream net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil }
l = &proxyproto.Listener{Listener: l, Policy: policyFunc}
}
return l, err
return l, nil
}

func (dl *DefaultListener) ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
Expand Down
Loading