2016-06-14 22:54:08 +02:00
|
|
|
package udp
|
2016-02-01 15:36:33 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-26 20:46:44 +01:00
|
|
|
"context"
|
2016-02-01 15:36:33 +00:00
|
|
|
"sync"
|
|
|
|
|
2016-08-20 20:55:45 +02:00
|
|
|
"v2ray.com/core/app/dispatcher"
|
2017-02-01 21:35:40 +01:00
|
|
|
"v2ray.com/core/app/log"
|
2017-02-03 22:35:09 +01:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-08-20 20:55:45 +02:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/ray"
|
2016-02-01 15:36:33 +00:00
|
|
|
)
|
|
|
|
|
2017-01-26 20:46:44 +01:00
|
|
|
type ResponseCallback func(payload *buf.Buffer)
|
2016-02-01 15:36:33 +00:00
|
|
|
|
2017-01-27 14:45:16 +01:00
|
|
|
type Dispatcher struct {
|
2016-02-01 15:36:33 +00:00
|
|
|
sync.RWMutex
|
2017-02-04 21:55:59 +01:00
|
|
|
conns map[v2net.Destination]ray.InboundRay
|
2017-02-03 22:35:09 +01:00
|
|
|
dispatcher dispatcher.Interface
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 22:35:09 +01:00
|
|
|
func NewDispatcher(dispatcher dispatcher.Interface) *Dispatcher {
|
2017-01-27 14:45:16 +01:00
|
|
|
return &Dispatcher{
|
2017-02-04 21:55:59 +01:00
|
|
|
conns: make(map[v2net.Destination]ray.InboundRay),
|
2017-02-03 22:35:09 +01:00
|
|
|
dispatcher: dispatcher,
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 21:55:59 +01:00
|
|
|
func (v *Dispatcher) RemoveRay(dest v2net.Destination) {
|
2016-11-27 21:39:09 +01:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
2017-02-04 21:55:59 +01:00
|
|
|
if conn, found := v.conns[dest]; found {
|
2017-01-26 20:46:44 +01:00
|
|
|
conn.InboundInput().Close()
|
|
|
|
conn.InboundOutput().Close()
|
2017-02-04 21:55:59 +01:00
|
|
|
delete(v.conns, dest)
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 14:45:16 +01:00
|
|
|
func (v *Dispatcher) getInboundRay(ctx context.Context, dest v2net.Destination) (ray.InboundRay, bool) {
|
2017-01-06 11:40:59 +01:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
2017-02-04 21:55:59 +01:00
|
|
|
if entry, found := v.conns[dest]; found {
|
2017-01-06 11:40:59 +01:00
|
|
|
return entry, true
|
|
|
|
}
|
|
|
|
|
2017-04-09 01:43:25 +02:00
|
|
|
log.Trace(newError("establishing new connection for ", dest))
|
2017-02-03 22:35:09 +01:00
|
|
|
inboundRay, _ := v.dispatcher.Dispatch(ctx, dest)
|
2017-02-04 21:55:59 +01:00
|
|
|
v.conns[dest] = inboundRay
|
2017-02-03 22:35:09 +01:00
|
|
|
return inboundRay, false
|
2017-01-06 11:40:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-27 14:45:16 +01:00
|
|
|
func (v *Dispatcher) Dispatch(ctx context.Context, destination v2net.Destination, payload *buf.Buffer, callback ResponseCallback) {
|
2016-08-14 17:08:01 +02:00
|
|
|
// TODO: Add user to destString
|
2017-04-09 01:43:25 +02:00
|
|
|
log.Trace(newError("dispatch request to: ", destination).AtDebug())
|
2017-01-26 20:46:44 +01:00
|
|
|
|
|
|
|
inboundRay, existing := v.getInboundRay(ctx, destination)
|
2017-01-06 11:40:59 +01:00
|
|
|
outputStream := inboundRay.InboundInput()
|
2016-05-12 17:20:07 -07:00
|
|
|
if outputStream != nil {
|
2017-04-15 21:07:23 +02:00
|
|
|
if err := outputStream.Write(buf.NewMultiBufferValue(payload)); err != nil {
|
2017-02-04 21:55:59 +01:00
|
|
|
v.RemoveRay(destination)
|
2017-01-26 20:46:44 +01:00
|
|
|
}
|
2016-05-12 17:20:07 -07:00
|
|
|
}
|
2017-01-06 11:40:59 +01:00
|
|
|
if !existing {
|
2017-01-26 20:46:44 +01:00
|
|
|
go func() {
|
|
|
|
handleInput(inboundRay.InboundOutput(), callback)
|
2017-02-04 21:55:59 +01:00
|
|
|
v.RemoveRay(destination)
|
2017-01-26 20:46:44 +01:00
|
|
|
}()
|
2017-01-06 11:40:59 +01:00
|
|
|
}
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:46:44 +01:00
|
|
|
func handleInput(input ray.InputStream, callback ResponseCallback) {
|
2016-04-18 18:44:10 +02:00
|
|
|
for {
|
2017-04-15 21:07:23 +02:00
|
|
|
mb, err := input.Read()
|
2016-04-18 18:44:10 +02:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2017-04-15 21:07:23 +02:00
|
|
|
for _, b := range mb {
|
|
|
|
callback(b)
|
|
|
|
}
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|