vmnet-proxy/dialer.go

42 lines
857 B
Go
Raw Normal View History

2024-11-15 04:45:55 +00:00
package main
import (
"fmt"
"net"
"net/netip"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
func (s *Server) Dial(network, address string) (net.Conn, error) {
addrPort, err := netip.ParseAddrPort(address)
if err != nil {
return nil, err
}
remote := tcpip.FullAddress{
Addr: tcpip.AddrFromSlice(addrPort.Addr().AsSlice()),
Port: addrPort.Port(),
}
var networkProtocol tcpip.NetworkProtocolNumber
if addrPort.Addr().Is4() {
networkProtocol = header.IPv4ProtocolNumber
} else {
networkProtocol = header.IPv6ProtocolNumber
}
if network == "tcp" {
return gonet.DialTCP(s.stack, remote, networkProtocol)
}
if network == "udp" {
return gonet.DialUDP(s.stack, nil, &remote, networkProtocol)
}
return nil, fmt.Errorf("unsupported network: %s", network)
}