v2fly/proxy/dokodemo/dokodemo.go

151 lines
3.6 KiB
Go
Raw Normal View History

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