v2fly/transport/internet/system_listener.go

105 lines
3.2 KiB
Go
Raw Normal View History

2018-09-10 11:23:27 +00:00
package internet
import (
"context"
2020-10-29 07:30:38 +00:00
"runtime"
2018-09-15 19:35:32 +00:00
"syscall"
2018-09-10 11:23:27 +00:00
2020-10-29 07:30:38 +00:00
"github.com/pires/go-proxyproto"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/session"
2018-09-10 11:23:27 +00:00
)
2021-05-19 21:28:52 +00:00
var effectiveListener = DefaultListener{}
2018-09-10 11:23:27 +00:00
2018-12-03 16:41:24 +00:00
type controller func(network, address string, fd uintptr) error
2018-09-10 11:23:27 +00:00
2018-12-03 16:41:24 +00:00
type DefaultListener struct {
2020-06-24 04:57:03 +00:00
controllers []controller
2018-12-03 16:41:24 +00:00
}
2018-09-10 11:23:27 +00:00
2020-06-24 04:57:03 +00:00
func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []controller) func(network, address string, c syscall.RawConn) error {
2018-12-03 16:41:24 +00:00
return func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if sockopt != nil {
2018-09-15 19:35:32 +00:00
if err := applyInboundSocketOptions(network, fd, sockopt); err != nil {
newError("failed to apply socket options to incoming connection").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
2018-12-03 16:41:24 +00:00
}
setReusePort(fd) // nolint: staticcheck
2020-06-24 04:57:03 +00:00
for _, controller := range controllers {
2018-12-03 16:41:24 +00:00
if err := controller(network, address, fd); err != nil {
newError("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
}
})
}
}
func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
var lc net.ListenConfig
2020-10-29 07:30:38 +00:00
var l net.Listener
var err error
var network, address string
switch addr := addr.(type) {
case *net.TCPAddr:
network = addr.Network()
address = addr.String()
lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
case *net.UnixAddr:
lc.Control = nil
network = addr.Network()
address = addr.Name
if (runtime.GOOS == "linux" || runtime.GOOS == "android") && address[0] == '@' {
2020-10-29 07:30:38 +00:00
// linux abstract unix domain socket is lockfree
if len(address) > 1 && address[1] == '@' {
// but may need padding to work with haproxy
fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path))
copy(fullAddr, address[1:])
address = string(fullAddr)
}
} else {
// normal unix domain socket needs lock
locker := &FileLocker{
path: address + ".lock",
}
err := locker.Acquire()
if err != nil {
return nil, err
}
ctx = context.WithValue(ctx, address, locker) // nolint: golint,staticcheck
2020-10-29 07:30:38 +00:00
}
}
2018-12-03 16:41:24 +00:00
2020-10-29 07:30:38 +00:00
l, err = lc.Listen(ctx, network, address)
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
2018-09-15 19:35:32 +00:00
}
2018-12-03 16:41:24 +00:00
func (dl *DefaultListener) ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
2018-09-15 19:35:32 +00:00
var lc net.ListenConfig
lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
2018-09-10 11:23:27 +00:00
2018-09-15 19:35:32 +00:00
return lc.ListenPacket(ctx, addr.Network(), addr.String())
2018-09-10 11:23:27 +00:00
}
2018-12-03 16:41:24 +00:00
// RegisterListenerController adds a controller to the effective system listener.
// The controller can be used to operate on file descriptors before they are put into use.
2018-12-03 22:39:21 +00:00
//
// v2ray:api:beta
2018-12-03 16:41:24 +00:00
func RegisterListenerController(controller func(network, address string, fd uintptr) error) error {
if controller == nil {
return newError("nil listener controller")
}
2020-06-24 04:57:03 +00:00
effectiveListener.controllers = append(effectiveListener.controllers, controller)
2018-12-03 16:41:24 +00:00
return nil
}