v2fly/app/proxyman/inbound/worker.go

316 lines
6.3 KiB
Go
Raw Normal View History

package inbound
import (
"context"
"io"
"sync"
"sync/atomic"
"time"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common/buf"
"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() net.Port
2017-01-26 19:57:18 +00:00
Proxy() proxy.Inbound
}
type tcpWorker struct {
address net.Address
port net.Port
proxy proxy.Inbound
stream *internet.StreamConfig
recvOrigDest bool
tag string
dispatcher dispatcher.Interface
sniffers []proxyman.KnownProtocols
ctx context.Context
cancel context.CancelFunc
2017-02-26 13:38:41 +00:00
hub internet.Listener
}
func (w *tcpWorker) callback(conn internet.Connection) {
ctx, cancel := context.WithCancel(w.ctx)
if w.recvOrigDest {
2017-04-18 10:35:54 +00:00
dest, err := tcp.GetOriginalDestination(conn)
if err != nil {
2017-12-19 20:28:12 +00:00
newError("failed to get original destination").Base(err).WriteToLog()
2017-04-18 10:35:54 +00:00
}
if dest.IsValid() {
2017-02-09 21:49:38 +00:00
ctx = proxy.ContextWithOriginalTarget(ctx, dest)
}
}
if len(w.tag) > 0 {
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
}
ctx = proxy.ContextWithInboundEntryPoint(ctx, net.TCPDestination(w.address, w.port))
ctx = proxy.ContextWithSource(ctx, net.DestinationFromAddr(conn.RemoteAddr()))
if len(w.sniffers) > 0 {
ctx = proxyman.ContextWithProtocolSniffers(ctx, w.sniffers)
}
if err := w.proxy.Process(ctx, net.Network_TCP, conn, w.dispatcher); err != nil {
2017-12-19 20:28:12 +00:00
newError("connection ends").Base(err).WriteToLog()
}
cancel()
conn.Close()
}
2017-01-26 19:57:18 +00:00
func (w *tcpWorker) Proxy() proxy.Inbound {
return w.proxy
}
func (w *tcpWorker) Start() error {
ctx, cancel := context.WithCancel(context.Background())
w.ctx = ctx
w.cancel = cancel
2017-02-26 13:38:41 +00:00
ctx = internet.ContextWithStreamSettings(ctx, w.stream)
conns := make(chan internet.Connection, 16)
hub, err := internet.ListenTCP(ctx, w.address, w.port, conns)
if err != nil {
2017-07-25 21:12:54 +00:00
return newError("failed to listen TCP on ", w.port).Base(err)
}
2017-02-26 13:38:41 +00:00
go w.handleConnections(conns)
w.hub = hub
return nil
}
2017-02-26 13:38:41 +00:00
func (w *tcpWorker) handleConnections(conns <-chan internet.Connection) {
for {
select {
case <-w.ctx.Done():
w.hub.Close()
L:
2017-02-27 20:34:35 +00:00
for {
2017-02-26 13:38:41 +00:00
select {
case conn := <-conns:
conn.Close()
default:
break L
}
}
return
case conn := <-conns:
go w.callback(conn)
}
}
}
func (w *tcpWorker) Close() {
2017-02-05 07:44:08 +00:00
if w.hub != nil {
w.cancel()
}
}
func (w *tcpWorker) Port() net.Port {
return w.port
}
type udpConn struct {
lastActivityTime int64 // in seconds
2017-01-28 15:27:46 +00:00
input chan *buf.Buffer
output func([]byte) (int, error)
remote net.Addr
local net.Addr
2017-01-27 13:27:02 +00:00
cancel context.CancelFunc
}
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 15:27:46 +00:00
defer in.Release()
c.updateActivity()
2017-01-28 15:27:46 +00:00
return copy(buf, in.Bytes()), nil
}
2017-04-21 13:36:05 +00:00
// Write implements io.Writer.
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
}
2017-10-27 15:13:43 +00:00
type connId struct {
src net.Destination
dest net.Destination
}
type udpWorker struct {
sync.RWMutex
2017-01-26 19:57:18 +00:00
proxy proxy.Inbound
hub *udp.Hub
address net.Address
port net.Port
recvOrigDest bool
tag string
dispatcher dispatcher.Interface
ctx context.Context
cancel context.CancelFunc
2017-10-27 15:13:43 +00:00
activeConn map[connId]*udpConn
}
2017-10-27 15:13:43 +00:00
func (w *udpWorker) getConnection(id connId) (*udpConn, bool) {
w.Lock()
defer w.Unlock()
2017-10-27 15:13:43 +00:00
if conn, found := w.activeConn[id]; found {
return conn, true
}
conn := &udpConn{
2017-01-28 15:27:46 +00:00
input: make(chan *buf.Buffer, 32),
output: func(b []byte) (int, error) {
2017-10-27 15:13:43 +00:00
return w.hub.WriteTo(b, id.src)
},
remote: &net.UDPAddr{
2017-10-27 15:13:43 +00:00
IP: id.src.Address.IP(),
Port: int(id.src.Port),
},
local: &net.UDPAddr{
IP: w.address.IP(),
Port: int(w.port),
},
}
2017-10-27 15:13:43 +00:00
w.activeConn[id] = conn
conn.updateActivity()
return conn, false
}
func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest net.Destination) {
2017-10-27 15:13:43 +00:00
id := connId{
src: source,
dest: originalDest,
}
conn, existing := w.getConnection(id)
2017-01-28 15:27:46 +00:00
select {
case conn.input <- b:
default:
b.Release()
}
if !existing {
go func() {
ctx := w.ctx
ctx, cancel := context.WithCancel(ctx)
conn.cancel = cancel
if originalDest.IsValid() {
2017-02-09 21:49:38 +00:00
ctx = proxy.ContextWithOriginalTarget(ctx, originalDest)
}
if len(w.tag) > 0 {
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
}
ctx = proxy.ContextWithSource(ctx, source)
ctx = proxy.ContextWithInboundEntryPoint(ctx, net.UDPDestination(w.address, w.port))
if err := w.proxy.Process(ctx, net.Network_UDP, conn, w.dispatcher); err != nil {
2017-12-19 20:28:12 +00:00
newError("connection ends").Base(err).WriteToLog()
}
2017-10-27 15:13:43 +00:00
w.removeConn(id)
2017-01-27 13:27:02 +00:00
cancel()
}()
}
}
2017-10-27 15:13:43 +00:00
func (w *udpWorker) removeConn(id connId) {
w.Lock()
2017-10-27 15:13:43 +00:00
delete(w.activeConn, id)
w.Unlock()
}
func (w *udpWorker) Start() error {
2017-10-27 15:13:43 +00:00
w.activeConn = make(map[connId]*udpConn, 16)
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 13:27:02 +00:00
go w.monitor()
w.hub = h
return nil
}
func (w *udpWorker) Close() {
2017-02-05 07:38:25 +00:00
if w.hub != nil {
w.hub.Close()
w.cancel()
}
}
func (w *udpWorker) monitor() {
2017-05-08 22:01:15 +00:00
timer := time.NewTicker(time.Second * 16)
defer timer.Stop()
for {
select {
case <-w.ctx.Done():
return
2017-05-08 22:01:15 +00:00
case <-timer.C:
nowSec := time.Now().Unix()
w.Lock()
for addr, conn := range w.activeConn {
2017-02-27 20:34:35 +00:00
if nowSec-atomic.LoadInt64(&conn.lastActivityTime) > 8 {
2017-01-27 13:27:02 +00:00
delete(w.activeConn, addr)
conn.cancel()
}
}
2017-01-27 13:27:02 +00:00
w.Unlock()
}
}
}
func (w *udpWorker) Port() net.Port {
return w.port
}
2017-01-26 19:57:18 +00:00
func (w *udpWorker) Proxy() proxy.Inbound {
return w.proxy
}