2015-09-22 17:50:05 -04:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-09-22 17:50:05 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
2016-02-01 10:36:33 -05:00
|
|
|
"github.com/v2ray/v2ray-core/transport/hub"
|
2015-09-22 17:50:05 -04:00
|
|
|
)
|
|
|
|
|
2016-04-25 18:33:16 -04:00
|
|
|
func (this *Server) listenUDP(port v2net.Port) error {
|
2016-02-03 16:43:09 -05:00
|
|
|
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
|
|
|
|
udpHub, err := hub.ListenUDP(port, this.handleUDPPayload)
|
2015-09-22 17:50:05 -04:00
|
|
|
if err != nil {
|
2016-02-03 16:43:09 -05:00
|
|
|
log.Error("Socks: Failed to listen on udp port ", port)
|
2015-09-22 17:50:05 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-04 02:40:24 -05:00
|
|
|
this.udpMutex.Lock()
|
2016-01-15 06:43:06 -05:00
|
|
|
this.udpAddress = v2net.UDPDestination(this.config.Address, port)
|
2016-02-03 16:43:09 -05:00
|
|
|
this.udpHub = udpHub
|
2016-01-04 02:40:24 -05:00
|
|
|
this.udpMutex.Unlock()
|
2015-09-22 17:50:05 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:33:16 -04:00
|
|
|
func (this *Server) handleUDPPayload(payload *alloc.Buffer, source v2net.Destination) {
|
2016-02-03 16:43:09 -05:00
|
|
|
log.Info("Socks: Client UDP connection from ", source)
|
|
|
|
request, err := protocol.ReadUDPRequest(payload.Value)
|
|
|
|
payload.Release()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks: Failed to parse UDP request: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.Data.Len() == 0 {
|
|
|
|
request.Data.Release()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.Fragment != 0 {
|
|
|
|
log.Warning("Socks: Dropping fragmented UDP packets.")
|
|
|
|
// TODO handle fragments
|
|
|
|
request.Data.Release()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:13:26 -04:00
|
|
|
log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes")
|
|
|
|
this.udpServer.Dispatch(source, request.Destination(), request.Data, func(destination v2net.Destination, payload *alloc.Buffer) {
|
2016-02-03 16:43:09 -05:00
|
|
|
response := &protocol.Socks5UDPRequest{
|
|
|
|
Fragment: 0,
|
2016-04-25 18:13:26 -04:00
|
|
|
Address: request.Destination().Address(),
|
|
|
|
Port: request.Destination().Port(),
|
|
|
|
Data: payload,
|
2016-02-03 16:43:09 -05:00
|
|
|
}
|
2016-04-25 18:13:26 -04:00
|
|
|
log.Info("Socks: Writing back UDP response with ", payload.Len(), " bytes to ", destination)
|
2016-02-03 16:43:09 -05:00
|
|
|
|
|
|
|
udpMessage := alloc.NewSmallBuffer().Clear()
|
|
|
|
response.Write(udpMessage)
|
|
|
|
|
2016-01-03 18:33:25 -05:00
|
|
|
this.udpMutex.RLock()
|
2016-01-03 17:30:37 -05:00
|
|
|
if !this.accepting {
|
2016-01-03 18:33:25 -05:00
|
|
|
this.udpMutex.RUnlock()
|
2016-02-03 16:43:09 -05:00
|
|
|
return
|
2016-01-03 17:30:37 -05:00
|
|
|
}
|
2016-04-25 18:13:26 -04:00
|
|
|
nBytes, err := this.udpHub.WriteTo(udpMessage.Value, destination)
|
2016-01-03 18:33:25 -05:00
|
|
|
this.udpMutex.RUnlock()
|
2016-02-03 16:43:09 -05:00
|
|
|
udpMessage.Release()
|
|
|
|
response.Data.Release()
|
2015-09-22 17:50:05 -04:00
|
|
|
if err != nil {
|
2016-04-25 18:13:26 -04:00
|
|
|
log.Error("Socks: failed to write UDP message (", nBytes, " bytes) to ", destination, ": ", err)
|
2015-09-22 17:50:05 -04:00
|
|
|
}
|
2016-02-03 16:43:09 -05:00
|
|
|
})
|
2015-09-28 15:32:07 -04:00
|
|
|
}
|