2015-09-22 17:50:05 -04:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/alloc"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/proxy/socks/protocol"
|
|
|
|
"v2ray.com/core/transport/internet/udp"
|
2015-09-22 17:50:05 -04:00
|
|
|
)
|
|
|
|
|
2016-06-04 08:25:13 -04:00
|
|
|
func (this *Server) listenUDP() error {
|
2016-08-12 17:37:21 -04:00
|
|
|
this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher)
|
2016-08-15 11:44:46 -04:00
|
|
|
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, udp.ListenOption{Callback: this.handleUDPPayload})
|
2015-09-22 17:50:05 -04:00
|
|
|
if err != nil {
|
2016-06-04 08:25:13 -04:00
|
|
|
log.Error("Socks: Failed to listen on udp ", this.meta.Address, ":", this.meta.Port)
|
2015-09-22 17:50:05 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-04 02:40:24 -05:00
|
|
|
this.udpMutex.Lock()
|
2016-08-28 17:46:50 -04:00
|
|
|
this.udpAddress = v2net.UDPDestination(this.config.GetNetAddress(), this.meta.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-08-15 11:44:46 -04:00
|
|
|
func (this *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) {
|
|
|
|
source := session.Source
|
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")
|
2016-06-05 09:02:15 -04:00
|
|
|
log.Access(source, request.Destination, log.AccessAccepted, "")
|
2016-08-14 11:08:01 -04:00
|
|
|
this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: 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-09-20 05:53:05 -04:00
|
|
|
Address: request.Destination().Address,
|
|
|
|
Port: request.Destination().Port,
|
2016-04-25 18:13:26 -04:00
|
|
|
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
|
|
|
|
2016-07-29 17:18:58 -04:00
|
|
|
udpMessage := alloc.NewLocalBuffer(2048).Clear()
|
2016-02-03 16:43:09 -05:00
|
|
|
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
|
|
|
}
|