1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00

add more checks

This commit is contained in:
Misaki Kasumi 2024-09-03 01:07:42 +08:00 committed by Xiaokang Wang (Shelikhoo)
parent 4efdc8016b
commit 9bee648078
3 changed files with 33 additions and 7 deletions

View File

@ -8,6 +8,6 @@ import (
"github.com/v2fly/v2ray-core/v5/common/net"
)
func activate_socket(address string) (net.Listener, error) {
func activateSocket(address string) (net.Listener, error) {
return nil, fmt.Errorf("socket activation is not supported on this platform")
}

View File

@ -4,19 +4,45 @@
package internet
import (
"fmt"
"os"
"path"
"strconv"
"syscall"
"github.com/v2fly/v2ray-core/v5/common/net"
)
func activate_socket(address string) (net.Listener, error) {
fd, err := strconv.Atoi(address[8:])
func activateSocket(address string) (net.Listener, error) {
fd, err := strconv.Atoi(path.Base(address))
if err != nil {
return nil, err
}
// Ignore the fail of SetNonblock: it's merely an optimization so that Go can poll this fd.
_ = syscall.SetNonblock(fd, true)
return net.FileListener(os.NewFile(uintptr(fd), address))
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)
}
file := os.NewFile(uintptr(fd), address)
defer file.Close()
return net.FileListener(file)
}

View File

@ -97,7 +97,7 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
}
} else if strings.HasPrefix(address, "/dev/fd/") {
// socket activation
l, err = activate_socket(address)
l, err = activateSocket(address)
if err != nil {
return nil, err
}