2016-06-14 20:54:08 +00:00
|
|
|
package udp
|
2016-02-01 15:36:33 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-26 19:46:44 +00:00
|
|
|
"context"
|
2016-02-01 15:36:33 +00:00
|
|
|
"sync"
|
2017-11-14 23:36:14 +00:00
|
|
|
"time"
|
2016-02-01 15:36:33 +00:00
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
"v2ray.com/core"
|
2018-04-16 22:31:10 +00:00
|
|
|
"v2ray.com/core/common"
|
2017-02-03 21:35:09 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2018-06-24 23:09:02 +00:00
|
|
|
"v2ray.com/core/common/session"
|
2017-11-14 23:36:14 +00:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-02-01 15:36:33 +00:00
|
|
|
)
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
type ResponseCallback func(payload *buf.Buffer)
|
2016-02-01 15:36:33 +00:00
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
type connEntry struct {
|
2018-04-16 22:31:10 +00:00
|
|
|
link *core.Link
|
|
|
|
timer signal.ActivityUpdater
|
|
|
|
cancel context.CancelFunc
|
2017-11-14 23:36:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 13:45:16 +00:00
|
|
|
type Dispatcher struct {
|
2016-02-01 15:36:33 +00:00
|
|
|
sync.RWMutex
|
2017-11-14 23:36:14 +00:00
|
|
|
conns map[net.Destination]*connEntry
|
2018-01-10 11:22:37 +00:00
|
|
|
dispatcher core.Dispatcher
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
func NewDispatcher(dispatcher core.Dispatcher) *Dispatcher {
|
2017-01-27 13:45:16 +00:00
|
|
|
return &Dispatcher{
|
2017-11-14 23:36:14 +00:00
|
|
|
conns: make(map[net.Destination]*connEntry),
|
2017-02-03 21:35:09 +00:00
|
|
|
dispatcher: dispatcher,
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:56:57 +00:00
|
|
|
func (v *Dispatcher) RemoveRay(dest net.Destination) {
|
2016-11-27 20:39:09 +00:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
2017-02-04 20:55:59 +00:00
|
|
|
if conn, found := v.conns[dest]; found {
|
2018-04-16 22:31:10 +00:00
|
|
|
common.Close(conn.link.Reader)
|
|
|
|
common.Close(conn.link.Writer)
|
2017-02-04 20:55:59 +00:00
|
|
|
delete(v.conns, dest)
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
func (v *Dispatcher) getInboundRay(dest net.Destination, callback ResponseCallback) *connEntry {
|
2017-01-06 10:40:59 +00:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
2017-02-04 20:55:59 +00:00
|
|
|
if entry, found := v.conns[dest]; found {
|
2017-11-14 23:36:14 +00:00
|
|
|
return entry
|
2017-01-06 10:40:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 20:28:12 +00:00
|
|
|
newError("establishing new connection for ", dest).WriteToLog()
|
2017-11-14 23:36:14 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
removeRay := func() {
|
|
|
|
cancel()
|
|
|
|
v.RemoveRay(dest)
|
|
|
|
}
|
|
|
|
timer := signal.CancelAfterInactivity(ctx, removeRay, time.Second*4)
|
2018-04-16 22:31:10 +00:00
|
|
|
link, _ := v.dispatcher.Dispatch(ctx, dest)
|
2017-11-14 23:36:14 +00:00
|
|
|
entry := &connEntry{
|
2018-04-16 22:31:10 +00:00
|
|
|
link: link,
|
|
|
|
timer: timer,
|
|
|
|
cancel: removeRay,
|
2017-11-14 23:36:14 +00:00
|
|
|
}
|
|
|
|
v.conns[dest] = entry
|
|
|
|
go handleInput(ctx, entry, callback)
|
|
|
|
return entry
|
2017-01-06 10:40:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 10:56:57 +00:00
|
|
|
func (v *Dispatcher) Dispatch(ctx context.Context, destination net.Destination, payload *buf.Buffer, callback ResponseCallback) {
|
2016-08-14 15:08:01 +00:00
|
|
|
// TODO: Add user to destString
|
2018-06-24 23:09:02 +00:00
|
|
|
newError("dispatch request to: ", destination).AtDebug().WriteToLog(session.ExportIDToError(ctx))
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
conn := v.getInboundRay(destination, callback)
|
2018-04-16 22:31:10 +00:00
|
|
|
outputStream := conn.link.Writer
|
2016-05-13 00:20:07 +00:00
|
|
|
if outputStream != nil {
|
2017-11-09 21:33:15 +00:00
|
|
|
if err := outputStream.WriteMultiBuffer(buf.NewMultiBufferValue(payload)); err != nil {
|
2018-06-24 23:09:02 +00:00
|
|
|
newError("failed to write first UDP payload").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
2017-11-14 23:36:14 +00:00
|
|
|
conn.cancel()
|
|
|
|
return
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
2016-05-13 00:20:07 +00:00
|
|
|
}
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
func handleInput(ctx context.Context, conn *connEntry, callback ResponseCallback) {
|
2018-04-16 22:31:10 +00:00
|
|
|
input := conn.link.Reader
|
2017-11-14 23:36:14 +00:00
|
|
|
timer := conn.timer
|
|
|
|
|
2016-04-18 16:44:10 +00:00
|
|
|
for {
|
2017-11-14 23:36:14 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
mb, err := input.ReadMultiBuffer()
|
2016-04-18 16:44:10 +00:00
|
|
|
if err != nil {
|
2018-06-24 23:09:02 +00:00
|
|
|
newError("failed to handle UDP input").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
2017-11-14 23:36:14 +00:00
|
|
|
conn.cancel()
|
|
|
|
return
|
2016-04-18 16:44:10 +00:00
|
|
|
}
|
2017-11-14 23:36:14 +00:00
|
|
|
timer.Update()
|
2017-04-15 19:07:23 +00:00
|
|
|
for _, b := range mb {
|
|
|
|
callback(b)
|
|
|
|
}
|
2016-02-01 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|