mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
proxy: added vsock support (#295)
This commit is contained in:
1 parent
ed7ee6bcd4
commit
730e9c765e
8 files changed
+407
-8
No files matched your search
@@ -0,0 +1,27 @@
|
||||
package vsock
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
proxy.RegisterDialer("vsock", NewVSockDialer)
|
||||
}
|
||||
|
||||
// NewVSockDialer returns a vm socket dialer.
|
||||
func NewVSockDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {
|
||||
return NewVSock(s, d, nil)
|
||||
}
|
||||
|
||||
// Dial connects to the address addr on the network net via the proxy.
|
||||
// NOTE: must be the first dialer in a chain
|
||||
func (s *vsock) Dial(network, addr string) (net.Conn, error) {
|
||||
return Dial(s.cid, s.port)
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address via the proxy.
|
||||
func (s *vsock) DialUDP(network, addr string) (net.PacketConn, error) {
|
||||
return nil, proxy.ErrNotSupported
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package vsock
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/glider/pkg/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
proxy.RegisterServer("vsock", NewVSockServer)
|
||||
}
|
||||
|
||||
// NewVSockServer returns a vm socket server.
|
||||
func NewVSockServer(s string, p proxy.Proxy) (proxy.Server, error) {
|
||||
schemes := strings.SplitN(s, ",", 2)
|
||||
vsock, err := NewVSock(schemes[0], nil, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(schemes) > 1 {
|
||||
vsock.server, err = proxy.ServerFromURL(schemes[1], p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if vsock.cid == 0 {
|
||||
cid, err := ContextID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vsock.cid = cid
|
||||
}
|
||||
|
||||
return vsock, nil
|
||||
}
|
||||
|
||||
// ListenAndServe serves requests.
|
||||
func (s *vsock) ListenAndServe() {
|
||||
l, err := Listen(s.cid, s.port)
|
||||
if err != nil {
|
||||
log.Fatalf("[vsock] failed to listen: %v", err)
|
||||
return
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
log.F("[vsock] Listening on %s", l.Addr())
|
||||
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
log.F("[vsock] failed to accept: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
go s.Serve(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Serve serves requests.
|
||||
func (s *vsock) Serve(c net.Conn) {
|
||||
if s.server != nil {
|
||||
s.server.Serve(c)
|
||||
return
|
||||
}
|
||||
|
||||
defer c.Close()
|
||||
|
||||
rc, dialer, err := s.proxy.Dial("tcp", "")
|
||||
if err != nil {
|
||||
log.F("[vsock] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), s.addr, dialer.Addr(), err)
|
||||
s.proxy.Record(dialer, false)
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
log.F("[vsock] %s <-> %s", c.RemoteAddr(), dialer.Addr())
|
||||
|
||||
if err = proxy.Relay(c, rc); err != nil {
|
||||
log.F("[vsock] %s <-> %s, relay error: %v", c.RemoteAddr(), dialer.Addr(), err)
|
||||
// record remote conn failure only
|
||||
if !strings.Contains(err.Error(), s.addr) {
|
||||
s.proxy.Record(dialer, false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package vsock
|
||||
|
||||
// Source code from:
|
||||
// https://github.com/linuxkit/virtsock/tree/master/pkg/vsock
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Addr represents the address of a vsock end point.
|
||||
type Addr struct {
|
||||
CID uint32
|
||||
Port uint32
|
||||
}
|
||||
|
||||
// Network returns the network type for a Addr
|
||||
func (a Addr) Network() string {
|
||||
return "vsock"
|
||||
}
|
||||
|
||||
// String returns a string representation of a Addr
|
||||
func (a Addr) String() string {
|
||||
return fmt.Sprintf("%d:%d", a.CID, a.Port)
|
||||
}
|
||||
|
||||
// Conn is a vsock connection which supports half-close.
|
||||
type Conn interface {
|
||||
net.Conn
|
||||
CloseRead() error
|
||||
CloseWrite() error
|
||||
File() (*os.File, error)
|
||||
}
|
||||
|
||||
// SocketMode is a NOOP on Linux.
|
||||
func SocketMode(m string) {}
|
||||
|
||||
// Convert a generic unix.Sockaddr to a Addr.
|
||||
func sockaddrToVsock(sa unix.Sockaddr) *Addr {
|
||||
switch sa := sa.(type) {
|
||||
case *unix.SockaddrVM:
|
||||
return &Addr{CID: sa.CID, Port: sa.Port}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Closes fd, retrying EINTR
|
||||
func closeFD(fd int) error {
|
||||
for {
|
||||
if err := unix.Close(fd); err != nil {
|
||||
if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("failed to close() fd %d: %w", fd, err)
|
||||
}
|
||||
break
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dial connects to the CID.Port via virtio sockets.
|
||||
func Dial(cid, port uint32) (Conn, error) {
|
||||
fd, err := syscall.Socket(unix.AF_VSOCK, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create AF_VSOCK socket: %w", err)
|
||||
}
|
||||
sa := &unix.SockaddrVM{CID: cid, Port: port}
|
||||
// Retry connect in a loop if EINTR is encountered.
|
||||
for {
|
||||
if err := unix.Connect(fd, sa); err != nil {
|
||||
if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR {
|
||||
continue
|
||||
}
|
||||
// Trying not to leak fd here
|
||||
_ = closeFD(fd)
|
||||
return nil, fmt.Errorf("failed connect() to %d:%d: %w", cid, port, err)
|
||||
}
|
||||
break
|
||||
}
|
||||
return newVsockConn(uintptr(fd), nil, &Addr{cid, port}), nil
|
||||
}
|
||||
|
||||
// Listen returns a net.Listener which can accept connections on the given cid and port.
|
||||
func Listen(cid, port uint32) (net.Listener, error) {
|
||||
fd, err := syscall.Socket(unix.AF_VSOCK, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sa := &unix.SockaddrVM{CID: cid, Port: port}
|
||||
if err = unix.Bind(fd, sa); err != nil {
|
||||
return nil, fmt.Errorf("bind() to %d:%d failed: %w", cid, port, err)
|
||||
}
|
||||
|
||||
err = syscall.Listen(fd, syscall.SOMAXCONN)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listen() on %d:%d failed: %w", cid, port, err)
|
||||
}
|
||||
return &vsockListener{fd, Addr{cid, port}}, nil
|
||||
}
|
||||
|
||||
// ContextID retrieves the local context ID for this system.
|
||||
func ContextID() (uint32, error) {
|
||||
f, err := os.Open("/dev/vsock")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return unix.IoctlGetUint32(int(f.Fd()), unix.IOCTL_VM_SOCKETS_GET_LOCAL_CID)
|
||||
}
|
||||
|
||||
type vsockListener struct {
|
||||
fd int
|
||||
local Addr
|
||||
}
|
||||
|
||||
// Accept accepts an incoming call and returns the new connection.
|
||||
func (v *vsockListener) Accept() (net.Conn, error) {
|
||||
fd, sa, err := unix.Accept(v.fd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newVsockConn(uintptr(fd), &v.local, sockaddrToVsock(sa)), nil
|
||||
}
|
||||
|
||||
// Close closes the listening connection
|
||||
func (v *vsockListener) Close() error {
|
||||
// Note this won't cause the Accept to unblock.
|
||||
return unix.Close(v.fd)
|
||||
}
|
||||
|
||||
// Addr returns the address the Listener is listening on
|
||||
func (v *vsockListener) Addr() net.Addr {
|
||||
return v.local
|
||||
}
|
||||
|
||||
// a wrapper around FileConn which supports CloseRead and CloseWrite
|
||||
type vsockConn struct {
|
||||
vsock *os.File
|
||||
fd uintptr
|
||||
local *Addr
|
||||
remote *Addr
|
||||
}
|
||||
|
||||
func newVsockConn(fd uintptr, local, remote *Addr) *vsockConn {
|
||||
vsock := os.NewFile(fd, fmt.Sprintf("vsock:%d", fd))
|
||||
return &vsockConn{vsock: vsock, fd: fd, local: local, remote: remote}
|
||||
}
|
||||
|
||||
// LocalAddr returns the local address of a connection
|
||||
func (v *vsockConn) LocalAddr() net.Addr {
|
||||
return v.local
|
||||
}
|
||||
|
||||
// RemoteAddr returns the remote address of a connection
|
||||
func (v *vsockConn) RemoteAddr() net.Addr {
|
||||
return v.remote
|
||||
}
|
||||
|
||||
// Close closes the connection
|
||||
func (v *vsockConn) Close() error {
|
||||
return v.vsock.Close()
|
||||
}
|
||||
|
||||
// CloseRead shuts down the reading side of a vsock connection
|
||||
func (v *vsockConn) CloseRead() error {
|
||||
return syscall.Shutdown(int(v.fd), syscall.SHUT_RD)
|
||||
}
|
||||
|
||||
// CloseWrite shuts down the writing side of a vsock connection
|
||||
func (v *vsockConn) CloseWrite() error {
|
||||
return syscall.Shutdown(int(v.fd), syscall.SHUT_WR)
|
||||
}
|
||||
|
||||
// Read reads data from the connection
|
||||
func (v *vsockConn) Read(buf []byte) (int, error) {
|
||||
return v.vsock.Read(buf)
|
||||
}
|
||||
|
||||
// Write writes data over the connection
|
||||
func (v *vsockConn) Write(buf []byte) (int, error) {
|
||||
return v.vsock.Write(buf)
|
||||
}
|
||||
|
||||
// SetDeadline sets the read and write deadlines associated with the connection
|
||||
func (v *vsockConn) SetDeadline(t time.Time) error {
|
||||
return nil // FIXME
|
||||
}
|
||||
|
||||
// SetReadDeadline sets the deadline for future Read calls.
|
||||
func (v *vsockConn) SetReadDeadline(t time.Time) error {
|
||||
return nil // FIXME
|
||||
}
|
||||
|
||||
// SetWriteDeadline sets the deadline for future Write calls
|
||||
func (v *vsockConn) SetWriteDeadline(t time.Time) error {
|
||||
return nil // FIXME
|
||||
}
|
||||
|
||||
// File duplicates the underlying socket descriptor and returns it.
|
||||
func (v *vsockConn) File() (*os.File, error) {
|
||||
// This is equivalent to dup(2) but creates the new fd with CLOEXEC already set.
|
||||
r0, _, e1 := syscall.Syscall(syscall.SYS_FCNTL, uintptr(v.vsock.Fd()), syscall.F_DUPFD_CLOEXEC, 0)
|
||||
if e1 != 0 {
|
||||
return nil, os.NewSyscallError("fcntl", e1)
|
||||
}
|
||||
return os.NewFile(r0, v.vsock.Name()), nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package vsock
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/nadoo/glider/pkg/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
type vsock struct {
|
||||
dialer proxy.Dialer
|
||||
proxy proxy.Proxy
|
||||
server proxy.Server
|
||||
addr string
|
||||
cid, port uint32
|
||||
}
|
||||
|
||||
// NewVSock returns vm socket proxy.
|
||||
func NewVSock(s string, d proxy.Dialer, p proxy.Proxy) (*vsock, error) {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
log.F("[vsock] parse url err: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v := &vsock{dialer: d, proxy: p, addr: u.Host}
|
||||
if hostStr, portStr, _ := net.SplitHostPort(v.addr); portStr != "" {
|
||||
if hostStr != "" {
|
||||
host, err := strconv.ParseUint(hostStr, 10, 32)
|
||||
if err != nil {
|
||||
log.F("[vsock] parse cid err: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
v.cid = uint32(host)
|
||||
}
|
||||
|
||||
port, err := strconv.ParseUint(portStr, 10, 32)
|
||||
if err != nil {
|
||||
log.F("[vsock] parse port err: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
v.port = uint32(port)
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// Addr returns forwarder's address.
|
||||
func (s *vsock) Addr() string {
|
||||
if s.addr == "" {
|
||||
return s.dialer.Addr()
|
||||
}
|
||||
return s.addr
|
||||
}
|
||||
|
||||
func init() {
|
||||
proxy.AddUsage("vsock", `
|
||||
VM socket scheme:
|
||||
vsock://[contextID]:port
|
||||
`)
|
||||
}
|
||||
Reference in new issue
Block a user