1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-02 16:56:02 -04:00
v2fly/proxy/socks/server_udp.go

75 lines
2.2 KiB
Go
Raw Normal View History

package socks
import (
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-08-20 14:55:45 -04:00
"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"
)
2016-11-27 15:39:09 -05:00
func (v *Server) listenUDP() error {
2016-12-21 09:37:16 -05:00
v.udpServer = udp.NewServer(v.packetDispatcher)
2016-11-27 15:39:09 -05:00
udpHub, err := udp.ListenUDP(v.meta.Address, v.meta.Port, udp.ListenOption{Callback: v.handleUDPPayload})
if err != nil {
2016-12-15 09:36:50 -05:00
log.Error("Socks: Failed to listen on udp (", v.meta.Address, ":", v.meta.Port, "): ", err)
return err
}
2016-11-27 15:39:09 -05:00
v.udpMutex.Lock()
v.udpAddress = v2net.UDPDestination(v.config.GetNetAddress(), v.meta.Port)
v.udpHub = udpHub
v.udpMutex.Unlock()
return nil
}
2016-12-09 05:35:27 -05:00
func (v *Server) handleUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) {
2016-08-15 11:44:46 -04:00
source := session.Source
2016-02-03 16:43:09 -05:00
log.Info("Socks: Client UDP connection from ", source)
2016-12-06 05:03:42 -05:00
request, err := protocol.ReadUDPRequest(payload.Bytes())
2016-02-03 16:43:09 -05:00
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-12-09 05:35:27 -05:00
v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: v.meta}, request.Data, func(destination v2net.Destination, payload *buf.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-12-09 06:08:25 -05:00
udpMessage := buf.NewLocal(2048)
2016-02-03 16:43:09 -05:00
response.Write(udpMessage)
2016-11-27 15:39:09 -05:00
v.udpMutex.RLock()
if !v.accepting {
v.udpMutex.RUnlock()
2016-02-03 16:43:09 -05:00
return
}
2016-12-06 05:03:42 -05:00
nBytes, err := v.udpHub.WriteTo(udpMessage.Bytes(), destination)
2016-11-27 15:39:09 -05:00
v.udpMutex.RUnlock()
2016-02-03 16:43:09 -05:00
udpMessage.Release()
response.Data.Release()
if err != nil {
2016-04-25 18:13:26 -04:00
log.Error("Socks: failed to write UDP message (", nBytes, " bytes) to ", destination, ": ", err)
}
2016-02-03 16:43:09 -05:00
})
2015-09-28 15:32:07 -04:00
}