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
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/app/dispatcher"
|
2016-04-25 18:13:26 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2016-05-12 20:20:07 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2016-02-01 10:36:33 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-08-12 17:37:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-02-01 10:36:33 -05:00
|
|
|
"github.com/v2ray/v2ray-core/transport/ray"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TimedInboundRay) Monitor() {
|
|
|
|
for {
|
2016-05-12 20:20:07 -04:00
|
|
|
time.Sleep(time.Second * 16)
|
2016-05-10 18:43:02 -04:00
|
|
|
select {
|
|
|
|
case <-this.accessed:
|
|
|
|
default:
|
|
|
|
// Ray not accessed for a while, assuming communication is dead.
|
2016-05-16 14:53:18 -04:00
|
|
|
this.RLock()
|
|
|
|
if this.server == nil {
|
|
|
|
this.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.server.RemoveRay(this.name)
|
|
|
|
this.RUnlock()
|
2016-05-10 18:43:02 -04: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-12 20:20:07 -04:00
|
|
|
defer this.RUnlock()
|
2016-05-10 18:43:02 -04:00
|
|
|
if this.inboundRay == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case this.accessed <- true:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return this.inboundRay.InboundOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TimedInboundRay) Release() {
|
2016-05-12 20:20:07 -04:00
|
|
|
log.Debug("UDP Server: Releasing TimedInboundRay: ", this.name)
|
2016-05-10 18:43:02 -04: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 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-08-12 17:37:21 -04:00
|
|
|
meta *proxy.InboundHandlerMeta
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:37:21 -04:00
|
|
|
func NewUDPServer(meta *proxy.InboundHandlerMeta, 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-08-12 17:37:21 -04:00
|
|
|
meta: meta,
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 18:43:02 -04: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-12 20:20:07 -04:00
|
|
|
log.Debug("UDP Server: Locating existing connection for ", name)
|
2016-02-01 10:36:33 -05:00
|
|
|
this.RLock()
|
|
|
|
defer this.RUnlock()
|
2016-05-10 18:43:02 -04:00
|
|
|
if entry, found := this.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-08-14 11:08:01 -04: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 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-04-25 18:13:26 -04:00
|
|
|
if this.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-08-14 11:08:01 -04:00
|
|
|
inboundRay := this.packetDispatcher.DispatchToOutbound(this.meta, session)
|
2016-05-16 14:53:18 -04:00
|
|
|
timedInboundRay := NewTimedInboundRay(destString, inboundRay, this)
|
2016-05-12 20:20:07 -04:00
|
|
|
outputStream := timedInboundRay.InboundInput()
|
|
|
|
if outputStream != nil {
|
|
|
|
outputStream.Write(payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.Lock()
|
2016-05-10 18:43:02 -04:00
|
|
|
this.conns[destString] = timedInboundRay
|
2016-02-01 10:36:33 -05:00
|
|
|
this.Unlock()
|
2016-05-10 18:43:02 -04:00
|
|
|
go this.handleConnection(timedInboundRay, source, callback)
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
|
|
|
|
2016-05-10 18:43:02 -04:00
|
|
|
func (this *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
|
|
|
}
|