Skip to content

Commit

Permalink
implements udp conn
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jul 15, 2024
1 parent a56634f commit deb2038
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
85 changes: 84 additions & 1 deletion udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"errors"
"net"
"time"
)

var (
errBadHeader = errors.New("bad header")
errBadHeader = errors.New("bad header")
errUnsupportedMethod = errors.New("unsupported method")
)

type UDPConn struct {
Expand Down Expand Up @@ -89,3 +91,84 @@ func (c *UDPConn) Write(b []byte) (int, error) {
func (c *UDPConn) RemoteAddr() net.Addr {
return c.defaultTarget
}

// SetReadBuffer implements the net.UDPConn SetReadBuffer method.
func (c *UDPConn) SetReadBuffer(bytes int) error {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return errUnsupportedMethod
}
return udpConn.SetReadBuffer(bytes)
}

// SetWriteBuffer implements the net.UDPConn SetWriteBuffer method.
func (c *UDPConn) SetWriteBuffer(bytes int) error {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return errUnsupportedMethod
}
return udpConn.SetWriteBuffer(bytes)
}

// SetDeadline implements the Conn SetDeadline method.
func (c *UDPConn) SetDeadline(t time.Time) error {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return errUnsupportedMethod
}
return udpConn.SetDeadline(t)
}

// SetReadDeadline implements the Conn SetReadDeadline method.
func (c *UDPConn) SetReadDeadline(t time.Time) error {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return errUnsupportedMethod
}
return udpConn.SetReadDeadline(t)
}

// SetWriteDeadline implements the Conn SetWriteDeadline method.
func (c *UDPConn) SetWriteDeadline(t time.Time) error {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return errUnsupportedMethod
}
return udpConn.SetWriteDeadline(t)
}

// ReadFromUDP implements the net.UDPConn ReadFromUDP method.
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, nil, errUnsupportedMethod
}
return udpConn.ReadFromUDP(b)
}

// ReadMsgUDP implements the net.UDPConn ReadMsgUDP method.
func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, 0, 0, nil, errUnsupportedMethod
}
return udpConn.ReadMsgUDP(b, oob)
}

// WriteToUDP implements the net.UDPConn WriteToUDP method.
func (c *UDPConn) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, errUnsupportedMethod
}
return udpConn.WriteToUDP(b, addr)
}

// WriteMsgUDP implements the net.UDPConn WriteMsgUDP method.
func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, 0, errUnsupportedMethod
}
return udpConn.WriteMsgUDP(b, oob, addr)
}
45 changes: 45 additions & 0 deletions udp_netip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build go1.18
// +build go1.18

package socks5

import (
"net"
"net/netip"
)

// ReadFromUDPAddrPort implements the net.UDPConn ReadFromUDPAddrPort method.
func (c *UDPConn) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, addr, errUnsupportedMethod
}
return udpConn.ReadFromUDPAddrPort(b)
}

// ReadMsgUDPAddrPort implements the net.UDPConn ReadMsgUDPAddrPort method.
func (c *UDPConn) ReadMsgUDPAddrPort(b, oob []byte) (n, oobn, flags int, addr netip.AddrPort, err error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, 0, 0, addr, errUnsupportedMethod
}
return udpConn.ReadMsgUDPAddrPort(b, oob)
}

// WriteToUDPAddrPort implements the net.UDPConn WriteToUDPAddrPort method.
func (c *UDPConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, errUnsupportedMethod
}
return udpConn.WriteToUDPAddrPort(b, addr)
}

// WriteMsgUDPAddrPort implements the net.UDPConn WriteMsgUDPAddrPort method.
func (c *UDPConn) WriteMsgUDPAddrPort(b, oob []byte, addr netip.AddrPort) (n, oobn int, err error) {
udpConn, ok := c.PacketConn.(*net.UDPConn)
if !ok {
return 0, 0, errUnsupportedMethod
}
return udpConn.WriteMsgUDPAddrPort(b, oob, addr)
}

0 comments on commit deb2038

Please sign in to comment.