Skip to content

Commit

Permalink
dispatch udp response
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Sep 28, 2015
1 parent 05b8350 commit 544b99b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
12 changes: 6 additions & 6 deletions common/net/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func NewTCPPacket(dest Destination) *TCPPacket {
}
}

func NewUDPPacket(dest Destination, data []byte, id uint16) *UDPPacket {
func NewUDPPacket(dest Destination, data []byte, token uint16) *UDPPacket {
return &UDPPacket{
basePacket: basePacket{destination: dest},
data: data,
id: id,
token: token,
}
}

Expand All @@ -42,12 +42,12 @@ func (packet *TCPPacket) MoreChunks() bool {

type UDPPacket struct {
basePacket
data []byte
id uint16
data []byte
token uint16
}

func (packet *UDPPacket) ID() uint16 {
return packet.id
func (packet *UDPPacket) Token() uint16 {
return packet.token
}

func (packet *UDPPacket) Chunk() []byte {
Expand Down
25 changes: 25 additions & 0 deletions proxy/socks/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,21 @@ func (m *portMap) removePorts(removedPorts <-chan interface{}) {
}
}

func (m *portMap) popPort(token uint16) *net.UDPAddr {
m.access.Lock()
defer m.access.Unlock()
addr, exists := m.data[token]
if !exists {
return nil
}
delete(m.data, token)
return addr
}

var (
ports = newPortMap()

udpConn *net.UDPConn
)

func (server *SocksServer) ListenUDP(port uint16) error {
Expand All @@ -73,6 +86,7 @@ func (server *SocksServer) ListenUDP(port uint16) error {
}

go server.AcceptPackets(conn)
udpConn = conn
return nil
}

Expand Down Expand Up @@ -100,3 +114,14 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
server.vPoint.DispatchToOutbound(udpPacket)
}
}

func (server *SocksServer) Dispatch(packet v2net.Packet) {
if udpPacket, ok := packet.(*v2net.UDPPacket); ok {
token := udpPacket.Token()
addr := ports.popPort(token)
if udpConn != nil {
udpConn.WriteToUDP(udpPacket.Chunk(), addr)
}
}
// We don't expect TCP Packets here
}

0 comments on commit 544b99b

Please sign in to comment.