fix udp for dokodemo and shadowsocks

pull/82/head
v2ray 2016-02-01 21:34:07 +01:00
parent 48aa9af631
commit 5e2583ec8d
2 changed files with 14 additions and 16 deletions

View File

@ -23,6 +23,7 @@ type DokodemoDoor struct {
packetDispatcher dispatcher.PacketDispatcher packetDispatcher dispatcher.PacketDispatcher
tcpListener *hub.TCPHub tcpListener *hub.TCPHub
udpHub *hub.UDPHub udpHub *hub.UDPHub
udpServer *hub.UDPServer
listeningPort v2net.Port listeningPort v2net.Port
} }
@ -89,26 +90,23 @@ func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
} }
this.udpMutex.Lock() this.udpMutex.Lock()
this.udpHub = udpHub this.udpHub = udpHub
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
this.udpMutex.Unlock() this.udpMutex.Unlock()
return nil return nil
} }
func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, dest v2net.Destination) { func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, dest v2net.Destination) {
packet := v2net.NewPacket(v2net.UDPDestination(this.address, this.port), payload, false) packet := v2net.NewPacket(v2net.UDPDestination(this.address, this.port), payload, false)
ray := this.packetDispatcher.DispatchToOutbound(packet) this.udpServer.Dispatch(dest, packet, func(packet v2net.Packet) {
close(ray.InboundInput()) defer packet.Chunk().Release()
for resp := range ray.InboundOutput() {
this.udpMutex.RLock() this.udpMutex.RLock()
if !this.accepting { if !this.accepting {
this.udpMutex.RUnlock() this.udpMutex.RUnlock()
resp.Release()
return return
} }
this.udpHub.WriteTo(resp.Value, dest) this.udpHub.WriteTo(packet.Chunk().Value, packet.Destination())
this.udpMutex.RUnlock() this.udpMutex.RUnlock()
resp.Release() })
}
} }
func (this *DokodemoDoor) ListenTCP(port v2net.Port) error { func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {

View File

@ -26,6 +26,7 @@ type Shadowsocks struct {
accepting bool accepting bool
tcpHub *hub.TCPHub tcpHub *hub.TCPHub
udpHub *hub.UDPHub udpHub *hub.UDPHub
udpServer *hub.UDPServer
} }
func NewShadowsocks(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Shadowsocks { func NewShadowsocks(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Shadowsocks {
@ -77,6 +78,7 @@ func (this *Shadowsocks) Listen(port v2net.Port) error {
log.Error("Shadowsocks: Failed to listen UDP on port ", port, ": ", err) log.Error("Shadowsocks: Failed to listen UDP on port ", port, ": ", err)
} }
this.udpHub = udpHub this.udpHub = udpHub
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
} }
return nil return nil
@ -107,12 +109,12 @@ func (this *Shadowsocks) handlerUDPPayload(payload *alloc.Buffer, source v2net.D
log.Info("Shadowsocks: Tunnelling request to ", dest) log.Info("Shadowsocks: Tunnelling request to ", dest)
packet := v2net.NewPacket(dest, request.UDPPayload, false) packet := v2net.NewPacket(dest, request.UDPPayload, false)
ray := this.packetDispatcher.DispatchToOutbound(packet) this.udpServer.Dispatch(source, packet, func(packet v2net.Packet) {
close(ray.InboundInput()) defer packet.Chunk().Release()
for respChunk := range ray.InboundOutput() {
response := alloc.NewBuffer().Slice(0, this.config.Cipher.IVSize()) response := alloc.NewBuffer().Slice(0, this.config.Cipher.IVSize())
defer response.Release()
rand.Read(response.Value) rand.Read(response.Value)
respIv := response.Value respIv := response.Value
@ -135,8 +137,7 @@ func (this *Shadowsocks) handlerUDPPayload(payload *alloc.Buffer, source v2net.D
} }
writer.Write(request.Port.Bytes()) writer.Write(request.Port.Bytes())
writer.Write(respChunk.Value) writer.Write(packet.Chunk().Value)
respChunk.Release()
if request.OTA { if request.OTA {
respAuth := NewAuthenticator(HeaderKeyGenerator(key, respIv)) respAuth := NewAuthenticator(HeaderKeyGenerator(key, respIv))
@ -144,8 +145,7 @@ func (this *Shadowsocks) handlerUDPPayload(payload *alloc.Buffer, source v2net.D
} }
this.udpHub.WriteTo(response.Value, source) this.udpHub.WriteTo(response.Value, source)
response.Release() })
}
} }
func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) { func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {