2017-01-26 14:46:44 -05:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2017-02-03 16:35:09 -05:00
|
|
|
"v2ray.com/core/app/dispatcher"
|
|
|
|
"v2ray.com/core/app/log"
|
2017-01-26 14:46:44 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/internet/tcp"
|
|
|
|
"v2ray.com/core/transport/internet/udp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type worker interface {
|
|
|
|
Start() error
|
|
|
|
Close()
|
|
|
|
Port() v2net.Port
|
2017-01-26 14:57:18 -05:00
|
|
|
Proxy() proxy.Inbound
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type tcpWorker struct {
|
2017-02-07 11:37:26 -05:00
|
|
|
address v2net.Address
|
|
|
|
port v2net.Port
|
|
|
|
proxy proxy.Inbound
|
|
|
|
stream *internet.StreamConfig
|
|
|
|
recvOrigDest bool
|
|
|
|
tag string
|
|
|
|
dispatcher dispatcher.Interface
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2017-02-26 08:38:41 -05:00
|
|
|
hub internet.Listener
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tcpWorker) callback(conn internet.Connection) {
|
|
|
|
ctx, cancel := context.WithCancel(w.ctx)
|
|
|
|
if w.recvOrigDest {
|
|
|
|
dest := tcp.GetOriginalDestination(conn)
|
|
|
|
if dest.IsValid() {
|
2017-02-09 16:49:38 -05:00
|
|
|
ctx = proxy.ContextWithOriginalTarget(ctx, dest)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(w.tag) > 0 {
|
|
|
|
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
|
|
|
|
}
|
2017-02-09 16:49:38 -05:00
|
|
|
ctx = proxy.ContextWithInboundEntryPoint(ctx, v2net.TCPDestination(w.address, w.port))
|
2017-01-29 01:58:29 -05:00
|
|
|
ctx = proxy.ContextWithSource(ctx, v2net.DestinationFromAddr(conn.RemoteAddr()))
|
2017-02-03 16:35:09 -05:00
|
|
|
if err := w.proxy.Process(ctx, v2net.Network_TCP, conn, w.dispatcher); err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
log.Trace(newError("connection ends").Base(err))
|
2017-02-03 16:35:09 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
cancel()
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:57:18 -05:00
|
|
|
func (w *tcpWorker) Proxy() proxy.Inbound {
|
2017-01-26 14:46:44 -05:00
|
|
|
return w.proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tcpWorker) Start() error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
w.ctx = ctx
|
|
|
|
w.cancel = cancel
|
2017-02-26 08:38:41 -05:00
|
|
|
ctx = internet.ContextWithStreamSettings(ctx, w.stream)
|
|
|
|
conns := make(chan internet.Connection, 16)
|
|
|
|
hub, err := internet.ListenTCP(ctx, w.address, w.port, conns)
|
2017-01-26 14:46:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-26 08:38:41 -05:00
|
|
|
go w.handleConnections(conns)
|
2017-01-26 14:46:44 -05:00
|
|
|
w.hub = hub
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-26 08:38:41 -05:00
|
|
|
func (w *tcpWorker) handleConnections(conns <-chan internet.Connection) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.ctx.Done():
|
|
|
|
w.hub.Close()
|
|
|
|
L:
|
2017-02-27 15:34:35 -05:00
|
|
|
for {
|
2017-02-26 08:38:41 -05:00
|
|
|
select {
|
|
|
|
case conn := <-conns:
|
|
|
|
conn.Close()
|
|
|
|
default:
|
|
|
|
break L
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case conn := <-conns:
|
|
|
|
go w.callback(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func (w *tcpWorker) Close() {
|
2017-02-05 02:44:08 -05:00
|
|
|
if w.hub != nil {
|
|
|
|
w.cancel()
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tcpWorker) Port() v2net.Port {
|
|
|
|
return w.port
|
|
|
|
}
|
|
|
|
|
|
|
|
type udpConn struct {
|
|
|
|
lastActivityTime int64 // in seconds
|
2017-01-28 10:27:46 -05:00
|
|
|
input chan *buf.Buffer
|
2017-01-26 14:46:44 -05:00
|
|
|
output func([]byte) (int, error)
|
|
|
|
remote net.Addr
|
|
|
|
local net.Addr
|
2017-01-27 08:27:02 -05:00
|
|
|
cancel context.CancelFunc
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpConn) updateActivity() {
|
|
|
|
atomic.StoreInt64(&c.lastActivityTime, time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpConn) Read(buf []byte) (int, error) {
|
|
|
|
in, open := <-c.input
|
|
|
|
if !open {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2017-01-28 10:27:46 -05:00
|
|
|
defer in.Release()
|
2017-01-26 14:46:44 -05:00
|
|
|
c.updateActivity()
|
2017-01-28 10:27:46 -05:00
|
|
|
return copy(buf, in.Bytes()), nil
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpConn) Write(buf []byte) (int, error) {
|
|
|
|
n, err := c.output(buf)
|
|
|
|
if err == nil {
|
|
|
|
c.updateActivity()
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpConn) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpConn) RemoteAddr() net.Addr {
|
|
|
|
return c.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpConn) LocalAddr() net.Addr {
|
|
|
|
return c.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*udpConn) SetDeadline(time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*udpConn) SetReadDeadline(time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*udpConn) SetWriteDeadline(time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type udpWorker struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
2017-01-26 14:57:18 -05:00
|
|
|
proxy proxy.Inbound
|
2017-01-26 14:46:44 -05:00
|
|
|
hub *udp.Hub
|
|
|
|
address v2net.Address
|
|
|
|
port v2net.Port
|
|
|
|
recvOrigDest bool
|
|
|
|
tag string
|
2017-02-03 16:35:09 -05:00
|
|
|
dispatcher dispatcher.Interface
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
activeConn map[v2net.Destination]*udpConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *udpWorker) getConnection(src v2net.Destination) (*udpConn, bool) {
|
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
|
|
|
if conn, found := w.activeConn[src]; found {
|
|
|
|
return conn, true
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := &udpConn{
|
2017-01-28 10:27:46 -05:00
|
|
|
input: make(chan *buf.Buffer, 32),
|
2017-01-26 14:46:44 -05:00
|
|
|
output: func(b []byte) (int, error) {
|
|
|
|
return w.hub.WriteTo(b, src)
|
|
|
|
},
|
|
|
|
remote: &net.UDPAddr{
|
|
|
|
IP: src.Address.IP(),
|
|
|
|
Port: int(src.Port),
|
|
|
|
},
|
|
|
|
local: &net.UDPAddr{
|
|
|
|
IP: w.address.IP(),
|
|
|
|
Port: int(w.port),
|
|
|
|
},
|
|
|
|
}
|
2017-01-27 08:10:36 -05:00
|
|
|
w.activeConn[src] = conn
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
conn.updateActivity()
|
|
|
|
return conn, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *udpWorker) callback(b *buf.Buffer, source v2net.Destination, originalDest v2net.Destination) {
|
|
|
|
conn, existing := w.getConnection(source)
|
2017-01-28 10:27:46 -05:00
|
|
|
select {
|
|
|
|
case conn.input <- b:
|
|
|
|
default:
|
|
|
|
b.Release()
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
if !existing {
|
|
|
|
go func() {
|
|
|
|
ctx := w.ctx
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
conn.cancel = cancel
|
|
|
|
if originalDest.IsValid() {
|
2017-02-09 16:49:38 -05:00
|
|
|
ctx = proxy.ContextWithOriginalTarget(ctx, originalDest)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
if len(w.tag) > 0 {
|
|
|
|
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
|
|
|
|
}
|
|
|
|
ctx = proxy.ContextWithSource(ctx, source)
|
2017-02-09 16:49:38 -05:00
|
|
|
ctx = proxy.ContextWithInboundEntryPoint(ctx, v2net.UDPDestination(w.address, w.port))
|
2017-02-03 16:35:09 -05:00
|
|
|
if err := w.proxy.Process(ctx, v2net.Network_UDP, conn, w.dispatcher); err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
log.Trace(newError("connection ends").Base(err))
|
2017-02-03 16:35:09 -05:00
|
|
|
}
|
2017-01-27 08:27:02 -05:00
|
|
|
w.removeConn(source)
|
|
|
|
cancel()
|
2017-01-26 14:46:44 -05:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *udpWorker) removeConn(src v2net.Destination) {
|
|
|
|
w.Lock()
|
|
|
|
delete(w.activeConn, src)
|
|
|
|
w.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *udpWorker) Start() error {
|
2017-01-27 08:10:36 -05:00
|
|
|
w.activeConn = make(map[v2net.Destination]*udpConn)
|
2017-01-26 14:46:44 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
w.ctx = ctx
|
|
|
|
w.cancel = cancel
|
|
|
|
h, err := udp.ListenUDP(w.address, w.port, udp.ListenOption{
|
|
|
|
Callback: w.callback,
|
|
|
|
ReceiveOriginalDest: w.recvOrigDest,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-27 08:27:02 -05:00
|
|
|
go w.monitor()
|
2017-01-26 14:46:44 -05:00
|
|
|
w.hub = h
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *udpWorker) Close() {
|
2017-02-05 02:38:25 -05:00
|
|
|
if w.hub != nil {
|
|
|
|
w.hub.Close()
|
|
|
|
w.cancel()
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *udpWorker) monitor() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second * 16):
|
|
|
|
nowSec := time.Now().Unix()
|
|
|
|
w.Lock()
|
|
|
|
for addr, conn := range w.activeConn {
|
2017-02-27 15:34:35 -05:00
|
|
|
if nowSec-atomic.LoadInt64(&conn.lastActivityTime) > 8 {
|
2017-01-27 08:27:02 -05:00
|
|
|
delete(w.activeConn, addr)
|
|
|
|
conn.cancel()
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 08:27:02 -05:00
|
|
|
w.Unlock()
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *udpWorker) Port() v2net.Port {
|
|
|
|
return w.port
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:57:18 -05:00
|
|
|
func (w *udpWorker) Proxy() proxy.Inbound {
|
2017-01-26 14:46:44 -05:00
|
|
|
return w.proxy
|
|
|
|
}
|