v2ray-core/transport/internet/udp/udp_server.go

172 lines
3.9 KiB
Go
Raw Normal View History

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