Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
basilgello committed Jul 19, 2024
1 parent 0783b42 commit 191b2ea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 40 deletions.
35 changes: 17 additions & 18 deletions cmd/yggstack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ type node struct {
socks5Listener net.Listener
}

type UDPConnSession struct {
conn *net.UDPConn
remoteAddr net.Addr
}

type UDPPacketConnSession struct {
conn *net.PacketConn
type UDPSession struct {
conn interface{}
remoteAddr net.Addr
}

Expand Down Expand Up @@ -396,22 +391,23 @@ func main() {
logger.Errorf("Failed to connect to %s: %s", mapping.Mapped, err)
continue
}
udpSession := &UDPPacketConnSession{
conn: &udpFwdConn,
udpSession := &UDPSession{
conn: udpFwdConn,
remoteAddr: remoteUdpAddr,
}
localUdpConnections.Store(remoteUdpAddrStr, udpSession)
go types.ReverseProxyUDPPacketConn(mtu, udpListenConn, remoteUdpAddr, udpFwdConn)
go types.ReverseProxyUDP(mtu, udpListenConn, remoteUdpAddr, *udpFwdConn)

Check failure on line 399 in cmd/yggstack/main.go

View workflow job for this annotation

GitHub Actions / Build Windows/Linux/MacOS/FreeBSD/Android

cannot use *udpFwdConn (variable of type gonet.UDPConn) as *net.Conn value in argument to types.ReverseProxyUDP: gonet.UDPConn does not implement *net.Conn (type *net.Conn is pointer to interface, not interface)
}

udpSession, ok := connVal.(*UDPPacketConnSession)
udpSession, ok := connVal.(*UDPSession)
if !ok {
continue
}

udpFwdConn := *udpSession.conn
udpFwdConnPtr := udpSession.conn.(*net.Conn)
udpFwdConn := *udpFwdConnPtr

_, err = udpFwdConn.WriteTo(udpBuffer[:bytesRead], mapping.Mapped)
_, err = udpFwdConn.Write(udpBuffer[:bytesRead])
if err != nil {
logger.Debugf("Cannot write from yggdrasil to udp listener: %q", err)
udpFwdConn.Close()
Expand Down Expand Up @@ -482,23 +478,26 @@ func main() {
logger.Errorf("Failed to connect to %s: %s", mapping.Mapped, err)
continue
}
udpSession := &UDPConnSession{
udpSession := &UDPSession{
conn: udpFwdConn,
remoteAddr: remoteUdpAddr,
}
remoteUdpConnections.Store(remoteUdpAddrStr, udpSession)
go types.ReverseProxyUDPConn(mtu, udpListenConn, remoteUdpAddr, *udpFwdConn)
go types.ReverseProxyUDP(mtu, udpListenConn, remoteUdpAddr, *udpFwdConn)

Check failure on line 486 in cmd/yggstack/main.go

View workflow job for this annotation

GitHub Actions / Build Windows/Linux/MacOS/FreeBSD/Android

cannot use *udpFwdConn (variable of type net.UDPConn) as *net.Conn value in argument to types.ReverseProxyUDP: net.UDPConn does not implement *net.Conn (type *net.Conn is pointer to interface, not interface)
}

udpSession, ok := connVal.(*UDPConnSession)
udpSession, ok := connVal.(*UDPSession)
if !ok {
continue
}

_, err = udpSession.conn.Write(udpBuffer[:bytesRead])
udpFwdConnPtr := udpSession.conn.(*net.Conn)
udpFwdConn := *udpFwdConnPtr

_, err = udpFwdConn.Write(udpBuffer[:bytesRead])
if err != nil {
logger.Debugf("Cannot write from yggdrasil to udp listener: %q", err)
udpSession.conn.Close()
udpFwdConn.Close()
remoteUdpConnections.Delete(remoteUdpAddrStr)
continue
}
Expand Down
6 changes: 3 additions & 3 deletions src/netstack/netstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func (s *YggdrasilNetstack) DialContext(ctx context.Context, network, address st
}
}

func (s *YggdrasilNetstack) DialTCP(addr *net.TCPAddr) (net.Conn, error) {
func (s *YggdrasilNetstack) DialTCP(addr *net.TCPAddr) (*gonet.TCPConn, error) {
fa, pn, _ := convertToFullAddr(addr.IP, addr.Port)
return gonet.DialTCP(s.stack, fa, pn)
}

func (s *YggdrasilNetstack) DialUDP(addr *net.UDPAddr) (net.PacketConn, error) {
func (s *YggdrasilNetstack) DialUDP(addr *net.UDPAddr) (*gonet.UDPConn, error) {
fa, pn, _ := convertToFullAddr(addr.IP, addr.Port)
return gonet.DialUDP(s.stack, nil, &fa, pn)
}
Expand All @@ -101,7 +101,7 @@ func (s *YggdrasilNetstack) ListenTCP(addr *net.TCPAddr) (net.Listener, error) {
return gonet.ListenTCP(s.stack, fa, pn)
}

func (s *YggdrasilNetstack) ListenUDP(addr *net.UDPAddr) (net.PacketConn, error) {
func (s *YggdrasilNetstack) ListenUDP(addr *net.UDPAddr) (*gonet.UDPConn, error) {
fa, pn, _ := convertToFullAddr(addr.IP, addr.Port)
return gonet.DialUDP(s.stack, &fa, nil, pn)
}
22 changes: 3 additions & 19 deletions src/types/udpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,11 @@ import (
"net"
)

func ReverseProxyUDPConn(mtu uint64, dst net.PacketConn, dstAddr net.Addr, src net.UDPConn) error {
func ReverseProxyUDP(mtu uint64, dst net.PacketConn, dstAddr net.Addr, src *net.Conn) error {
buf := make([]byte, mtu)
for {
n, err := src.Read(buf[:])
if err != nil {
return err
}
if n > 0 {
n, err = dst.WriteTo(buf[:n], dstAddr)
if err != nil {
return err
}
}
}
return nil
}

func ReverseProxyUDPPacketConn(mtu uint64, dst net.PacketConn, dstAddr net.Addr, src net.PacketConn) error {
buf := make([]byte, mtu)
for {
n, _, err := src.ReadFrom(buf[:])
srcConn := *src
n, err := srcConn.Read(buf[:])
if err != nil {
return err
}
Expand Down

0 comments on commit 191b2ea

Please sign in to comment.