2015-10-30 10:56:46 -04:00
|
|
|
package dokodemo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app/dispatcher"
|
2015-10-30 10:56:46 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2016-01-29 08:39:55 -05:00
|
|
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
2015-10-30 10:56:46 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-01-19 17:41:40 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-01-28 11:08:32 -05:00
|
|
|
"github.com/v2ray/v2ray-core/transport/hub"
|
2015-10-30 10:56:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type DokodemoDoor struct {
|
2016-01-31 11:01:28 -05:00
|
|
|
tcpMutex sync.RWMutex
|
|
|
|
udpMutex sync.RWMutex
|
|
|
|
config *Config
|
|
|
|
accepting bool
|
|
|
|
address v2net.Address
|
|
|
|
port v2net.Port
|
|
|
|
packetDispatcher dispatcher.PacketDispatcher
|
|
|
|
tcpListener *hub.TCPHub
|
|
|
|
udpHub *hub.UDPHub
|
2016-02-01 15:34:07 -05:00
|
|
|
udpServer *hub.UDPServer
|
2016-01-31 11:01:28 -05:00
|
|
|
listeningPort v2net.Port
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
func NewDokodemoDoor(config *Config, packetDispatcher dispatcher.PacketDispatcher) *DokodemoDoor {
|
2015-12-06 12:21:15 -05:00
|
|
|
return &DokodemoDoor{
|
2016-01-31 11:01:28 -05:00
|
|
|
config: config,
|
|
|
|
packetDispatcher: packetDispatcher,
|
|
|
|
address: config.Address,
|
|
|
|
port: config.Port,
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 17:41:40 -05:00
|
|
|
func (this *DokodemoDoor) Port() v2net.Port {
|
|
|
|
return this.listeningPort
|
|
|
|
}
|
|
|
|
|
2016-01-03 17:30:37 -05:00
|
|
|
func (this *DokodemoDoor) Close() {
|
|
|
|
this.accepting = false
|
|
|
|
if this.tcpListener != nil {
|
2016-01-03 18:33:25 -05:00
|
|
|
this.tcpMutex.Lock()
|
2016-01-27 16:11:31 -05:00
|
|
|
this.tcpListener.Close()
|
2016-01-03 17:30:37 -05:00
|
|
|
this.tcpListener = nil
|
2016-01-03 18:33:25 -05:00
|
|
|
this.tcpMutex.Unlock()
|
2016-01-03 17:30:37 -05:00
|
|
|
}
|
2016-01-28 11:43:47 -05:00
|
|
|
if this.udpHub != nil {
|
2016-01-03 18:33:25 -05:00
|
|
|
this.udpMutex.Lock()
|
2016-01-28 11:43:47 -05:00
|
|
|
this.udpHub.Close()
|
|
|
|
this.udpHub = nil
|
2016-01-03 18:33:25 -05:00
|
|
|
this.udpMutex.Unlock()
|
2016-01-03 17:30:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 15:44:01 -05:00
|
|
|
func (this *DokodemoDoor) Listen(port v2net.Port) error {
|
2016-01-19 17:41:40 -05:00
|
|
|
if this.accepting {
|
|
|
|
if this.listeningPort == port {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return proxy.ErrorAlreadyListening
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.listeningPort = port
|
2015-11-10 18:08:43 -05:00
|
|
|
this.accepting = true
|
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
|
2015-10-30 10:56:46 -04:00
|
|
|
err := this.ListenTCP(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 06:43:06 -05:00
|
|
|
if this.config.Network.HasNetwork(v2net.UDPNetwork) {
|
2015-11-10 18:08:43 -05:00
|
|
|
err := this.ListenUDP(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 10:56:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-02 15:44:01 -05:00
|
|
|
func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
|
2016-02-03 16:42:44 -05:00
|
|
|
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
|
2016-01-28 11:43:47 -05:00
|
|
|
udpHub, err := hub.ListenUDP(port, this.handleUDPPackets)
|
2015-11-10 18:08:43 -05:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Dokodemo failed to listen on port ", port, ": ", err)
|
2015-11-10 18:08:43 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-01-04 02:40:24 -05:00
|
|
|
this.udpMutex.Lock()
|
2016-01-28 11:43:47 -05:00
|
|
|
this.udpHub = udpHub
|
2016-01-04 02:40:24 -05:00
|
|
|
this.udpMutex.Unlock()
|
2015-11-10 18:08:43 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-28 11:43:47 -05:00
|
|
|
func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, dest v2net.Destination) {
|
|
|
|
packet := v2net.NewPacket(v2net.UDPDestination(this.address, this.port), payload, false)
|
2016-02-01 15:34:07 -05:00
|
|
|
this.udpServer.Dispatch(dest, packet, func(packet v2net.Packet) {
|
|
|
|
defer packet.Chunk().Release()
|
2016-01-03 18:33:25 -05:00
|
|
|
this.udpMutex.RLock()
|
|
|
|
if !this.accepting {
|
2016-01-03 19:19:27 -05:00
|
|
|
this.udpMutex.RUnlock()
|
2016-01-03 18:33:25 -05:00
|
|
|
return
|
2016-01-03 17:30:37 -05:00
|
|
|
}
|
2016-02-01 15:34:07 -05:00
|
|
|
this.udpHub.WriteTo(packet.Chunk().Value, packet.Destination())
|
2016-01-03 18:33:25 -05:00
|
|
|
this.udpMutex.RUnlock()
|
2016-02-01 15:34:07 -05:00
|
|
|
})
|
2015-11-10 18:08:43 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 15:44:01 -05:00
|
|
|
func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
|
2016-01-28 11:08:32 -05:00
|
|
|
tcpListener, err := hub.ListenTCP(port, this.HandleTCPConnection)
|
2015-10-30 10:56:46 -04:00
|
|
|
if err != nil {
|
2016-01-27 16:11:31 -05:00
|
|
|
log.Error("Dokodemo: Failed to listen on port ", port, ": ", err)
|
2015-10-30 10:56:46 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-04 02:40:24 -05:00
|
|
|
this.tcpMutex.Lock()
|
2016-01-03 17:30:37 -05:00
|
|
|
this.tcpListener = tcpListener
|
2016-01-04 02:40:24 -05:00
|
|
|
this.tcpMutex.Unlock()
|
2015-10-30 10:56:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-28 11:08:32 -05:00
|
|
|
func (this *DokodemoDoor) HandleTCPConnection(conn *hub.TCPConn) {
|
2015-10-30 10:56:46 -04:00
|
|
|
defer conn.Close()
|
|
|
|
|
2015-12-16 17:53:38 -05:00
|
|
|
packet := v2net.NewPacket(v2net.TCPDestination(this.address, this.port), nil, true)
|
2016-01-31 11:01:28 -05:00
|
|
|
ray := this.packetDispatcher.DispatchToOutbound(packet)
|
2016-04-18 12:44:10 -04:00
|
|
|
defer ray.InboundOutput().Release()
|
2015-10-30 10:56:46 -04:00
|
|
|
|
|
|
|
var inputFinish, outputFinish sync.Mutex
|
|
|
|
inputFinish.Lock()
|
|
|
|
outputFinish.Lock()
|
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
|
2016-04-18 12:44:10 -04:00
|
|
|
go func() {
|
2016-04-18 13:01:24 -04:00
|
|
|
v2reader := v2io.NewAdaptiveReader(reader)
|
|
|
|
defer v2reader.Release()
|
|
|
|
|
|
|
|
v2io.Pipe(v2reader, ray.InboundInput())
|
2016-04-18 12:44:10 -04:00
|
|
|
inputFinish.Unlock()
|
|
|
|
ray.InboundInput().Close()
|
|
|
|
}()
|
2015-10-30 10:56:46 -04:00
|
|
|
|
2016-04-18 12:44:10 -04:00
|
|
|
go func() {
|
2016-04-18 13:01:24 -04:00
|
|
|
v2writer := v2io.NewAdaptiveWriter(conn)
|
|
|
|
defer v2writer.Release()
|
|
|
|
|
|
|
|
v2io.Pipe(ray.InboundOutput(), v2writer)
|
2016-04-18 12:44:10 -04:00
|
|
|
outputFinish.Unlock()
|
|
|
|
}()
|
2015-10-30 10:56:46 -04:00
|
|
|
|
2016-04-18 12:44:10 -04:00
|
|
|
outputFinish.Lock()
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|