1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/transport/internet/udp/udp_server.go

172 lines
3.7 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package udp
2016-02-01 10:36:33 -05:00
import (
"sync"
2016-05-10 18:43:02 -04:00
"time"
2016-02-01 10:36:33 -05:00
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
2016-02-01 10:36:33 -05:00
)
2016-04-25 18:13:26 -04:00
type UDPResponseCallback func(destination v2net.Destination, payload *alloc.Buffer)
2016-02-01 10:36:33 -05:00
2016-05-10 18:43:02 -04:00
type TimedInboundRay struct {
name string
2016-02-01 10:36:33 -05:00
inboundRay ray.InboundRay
2016-05-10 18:43:02 -04:00
accessed chan bool
server *UDPServer
sync.RWMutex
}
2016-05-16 14:53:18 -04:00
func NewTimedInboundRay(name string, inboundRay ray.InboundRay, server *UDPServer) *TimedInboundRay {
2016-05-10 18:43:02 -04:00
r := &TimedInboundRay{
name: name,
inboundRay: inboundRay,
2016-05-16 14:53:18 -04:00
accessed: make(chan bool, 1),
server: server,
2016-05-10 18:43:02 -04:00
}
go r.Monitor()
return r
}
2016-11-27 15:39:09 -05:00
func (v *TimedInboundRay) Monitor() {
2016-05-10 18:43:02 -04:00
for {
2016-05-12 20:20:07 -04:00
time.Sleep(time.Second * 16)
2016-05-10 18:43:02 -04:00
select {
2016-11-27 15:39:09 -05:00
case <-v.accessed:
2016-05-10 18:43:02 -04:00
default:
// Ray not accessed for a while, assuming communication is dead.
2016-11-27 15:39:09 -05:00
v.RLock()
if v.server == nil {
v.RUnlock()
2016-05-16 14:53:18 -04:00
return
}
2016-11-27 15:39:09 -05:00
v.server.RemoveRay(v.name)
v.RUnlock()
v.Release()
2016-05-10 18:43:02 -04:00
return
}
}
}
2016-11-27 15:39:09 -05:00
func (v *TimedInboundRay) InboundInput() ray.OutputStream {
v.RLock()
defer v.RUnlock()
if v.inboundRay == nil {
2016-05-10 18:43:02 -04:00
return nil
}
select {
2016-11-27 15:39:09 -05:00
case v.accessed <- true:
2016-05-10 18:43:02 -04:00
default:
}
2016-11-27 15:39:09 -05:00
return v.inboundRay.InboundInput()
2016-05-10 18:43:02 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *TimedInboundRay) InboundOutput() ray.InputStream {
v.RLock()
defer v.RUnlock()
if v.inboundRay == nil {
2016-05-10 18:43:02 -04:00
return nil
}
select {
2016-11-27 15:39:09 -05:00
case v.accessed <- true:
2016-05-10 18:43:02 -04:00
default:
}
2016-11-27 15:39:09 -05:00
return v.inboundRay.InboundOutput()
2016-05-10 18:43:02 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *TimedInboundRay) Release() {
log.Debug("UDP Server: Releasing TimedInboundRay: ", v.name)
v.Lock()
defer v.Unlock()
if v.server == nil {
2016-05-10 18:43:02 -04:00
return
}
2016-11-27 15:39:09 -05:00
v.server = nil
v.inboundRay.InboundInput().Close()
v.inboundRay.InboundOutput().Release()
v.inboundRay = nil
2016-02-01 10:36:33 -05:00
}
type UDPServer struct {
sync.RWMutex
2016-05-10 18:43:02 -04:00
conns map[string]*TimedInboundRay
2016-02-01 10:36:33 -05:00
packetDispatcher dispatcher.PacketDispatcher
}
2016-11-13 08:33:00 -05:00
func NewUDPServer(packetDispatcher dispatcher.PacketDispatcher) *UDPServer {
2016-02-01 10:36:33 -05:00
return &UDPServer{
2016-05-10 18:43:02 -04:00
conns: make(map[string]*TimedInboundRay),
2016-02-01 10:36:33 -05:00
packetDispatcher: packetDispatcher,
}
}
2016-11-27 15:39:09 -05:00
func (v *UDPServer) RemoveRay(name string) {
v.Lock()
defer v.Unlock()
delete(v.conns, name)
2016-05-10 18:43:02 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer) bool {
2016-05-12 20:20:07 -04:00
log.Debug("UDP Server: Locating existing connection for ", name)
2016-11-27 15:39:09 -05:00
v.RLock()
defer v.RUnlock()
if entry, found := v.conns[name]; found {
2016-05-12 20:20:07 -04:00
outputStream := entry.InboundInput()
if outputStream == nil {
return false
}
err := outputStream.Write(payload)
2016-05-10 18:43:02 -04:00
if err != nil {
2016-05-16 14:53:18 -04:00
go entry.Release()
2016-05-10 18:43:02 -04:00
return false
}
2016-02-01 10:36:33 -05:00
return true
}
return false
}
2016-11-27 15:39:09 -05:00
func (v *UDPServer) Dispatch(session *proxy.SessionInfo, payload *alloc.Buffer, callback UDPResponseCallback) {
2016-08-14 11:08:01 -04:00
source := session.Source
destination := session.Destination
// TODO: Add user to destString
2016-06-03 18:38:22 -04:00
destString := source.String() + "-" + destination.String()
2016-05-12 20:20:07 -04:00
log.Debug("UDP Server: Dispatch request: ", destString)
2016-11-27 15:39:09 -05:00
if v.locateExistingAndDispatch(destString, payload) {
2016-02-01 10:36:33 -05:00
return
}
2016-05-12 20:20:07 -04:00
log.Info("UDP Server: establishing new connection for ", destString)
2016-11-27 15:39:09 -05:00
inboundRay := v.packetDispatcher.DispatchToOutbound(session)
timedInboundRay := NewTimedInboundRay(destString, inboundRay, v)
2016-05-12 20:20:07 -04:00
outputStream := timedInboundRay.InboundInput()
if outputStream != nil {
outputStream.Write(payload)
}
2016-11-27 15:39:09 -05:00
v.Lock()
v.conns[destString] = timedInboundRay
v.Unlock()
go v.handleConnection(timedInboundRay, source, callback)
2016-02-01 10:36:33 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *UDPServer) handleConnection(inboundRay *TimedInboundRay, source v2net.Destination, callback UDPResponseCallback) {
2016-04-18 12:44:10 -04:00
for {
2016-05-12 20:20:07 -04:00
inputStream := inboundRay.InboundOutput()
if inputStream == nil {
break
}
2016-04-18 12:44:10 -04:00
data, err := inboundRay.InboundOutput().Read()
if err != nil {
break
}
2016-04-25 18:13:26 -04:00
callback(source, data)
2016-02-01 10:36:33 -05:00
}
2016-05-10 18:43:02 -04:00
inboundRay.Release()
2016-02-01 10:36:33 -05:00
}