2016-06-14 16:54:08 -04:00
|
|
|
package udp
|
2016-02-01 10:36:33 -05:00
|
|
|
|
|
|
|
import (
|
2017-01-26 14:46:44 -05:00
|
|
|
"context"
|
2016-02-01 10:36:33 -05:00
|
|
|
"sync"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app/dispatcher"
|
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/transport/ray"
|
2016-02-01 10:36:33 -05:00
|
|
|
)
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
type ResponseCallback func(payload *buf.Buffer)
|
2016-02-01 10:36:33 -05:00
|
|
|
|
2017-01-27 08:45:16 -05:00
|
|
|
type Dispatcher struct {
|
2016-02-01 10:36:33 -05:00
|
|
|
sync.RWMutex
|
2017-01-26 14:46:44 -05:00
|
|
|
conns map[string]ray.InboundRay
|
2017-01-13 07:53:44 -05:00
|
|
|
packetDispatcher dispatcher.Interface
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 08:45:16 -05:00
|
|
|
func NewDispatcher(packetDispatcher dispatcher.Interface) *Dispatcher {
|
|
|
|
return &Dispatcher{
|
2017-01-26 14:46:44 -05:00
|
|
|
conns: make(map[string]ray.InboundRay),
|
2016-02-01 10:36:33 -05:00
|
|
|
packetDispatcher: packetDispatcher,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 08:45:16 -05:00
|
|
|
func (v *Dispatcher) RemoveRay(name string) {
|
2016-11-27 15:39:09 -05:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
2017-01-26 14:46:44 -05:00
|
|
|
if conn, found := v.conns[name]; found {
|
|
|
|
conn.InboundInput().Close()
|
|
|
|
conn.InboundOutput().Close()
|
|
|
|
delete(v.conns, name)
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 08:45:16 -05:00
|
|
|
func (v *Dispatcher) getInboundRay(ctx context.Context, dest v2net.Destination) (ray.InboundRay, bool) {
|
2017-01-26 14:46:44 -05:00
|
|
|
destString := dest.String()
|
2017-01-06 05:40:59 -05:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
if entry, found := v.conns[destString]; found {
|
2017-01-06 05:40:59 -05:00
|
|
|
return entry, true
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("UDP|Server: establishing new connection for ", dest)
|
2017-01-26 14:46:44 -05:00
|
|
|
ctx = proxy.ContextWithDestination(ctx, dest)
|
|
|
|
return v.packetDispatcher.DispatchToOutbound(ctx), false
|
2017-01-06 05:40:59 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 08:45:16 -05:00
|
|
|
func (v *Dispatcher) Dispatch(ctx context.Context, destination v2net.Destination, payload *buf.Buffer, callback ResponseCallback) {
|
2016-08-14 11:08:01 -04:00
|
|
|
// TODO: Add user to destString
|
2017-01-26 14:46:44 -05:00
|
|
|
destString := destination.String()
|
2017-01-06 05:40:59 -05:00
|
|
|
log.Debug("UDP|Server: Dispatch request: ", destString)
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
inboundRay, existing := v.getInboundRay(ctx, destination)
|
2017-01-06 05:40:59 -05:00
|
|
|
outputStream := inboundRay.InboundInput()
|
2016-05-12 20:20:07 -04:00
|
|
|
if outputStream != nil {
|
2017-01-26 14:46:44 -05:00
|
|
|
if err := outputStream.Write(payload); err != nil {
|
|
|
|
v.RemoveRay(destString)
|
|
|
|
}
|
2016-05-12 20:20:07 -04:00
|
|
|
}
|
2017-01-06 05:40:59 -05:00
|
|
|
if !existing {
|
2017-01-26 14:46:44 -05:00
|
|
|
go func() {
|
|
|
|
handleInput(inboundRay.InboundOutput(), callback)
|
|
|
|
v.RemoveRay(destString)
|
|
|
|
}()
|
2017-01-06 05:40:59 -05:00
|
|
|
}
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func handleInput(input ray.InputStream, callback ResponseCallback) {
|
2016-04-18 12:44:10 -04:00
|
|
|
for {
|
2017-01-26 14:46:44 -05:00
|
|
|
data, err := input.Read()
|
2016-04-18 12:44:10 -04:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
callback(data)
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
|
|
|
}
|