1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-28 18:25:23 +00:00
v2fly/proxy/dokodemo/dokodemo.go

153 lines
3.6 KiB
Go
Raw Normal View History

2015-10-30 14:56:46 +00:00
package dokodemo
import (
"io"
"sync"
"github.com/v2ray/v2ray-core/app"
"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-19 22:41:40 +00:00
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
config *Config
accepting bool
address v2net.Address
port v2net.Port
space app.Space
2016-01-28 19:47:00 +00:00
tcpListener *hub.TCPHub
2016-01-28 16:43:47 +00:00
udpHub *hub.UDPHub
2016-01-19 22:41:40 +00:00
listeningPort v2net.Port
2015-10-30 14:56:46 +00:00
}
func NewDokodemoDoor(space app.Space, config *Config) *DokodemoDoor {
2015-12-06 17:21:15 +00:00
return &DokodemoDoor{
config: config,
space: space,
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-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) {
packet := v2net.NewPacket(v2net.UDPDestination(this.address, this.port), payload, false)
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
close(ray.InboundInput())
for resp := range ray.InboundOutput() {
2016-01-03 23:33:25 +00:00
this.udpMutex.RLock()
if !this.accepting {
2016-01-04 00:19:27 +00:00
this.udpMutex.RUnlock()
2016-01-28 16:43:47 +00:00
resp.Release()
2016-01-03 23:33:25 +00:00
return
}
2016-01-28 16:43:47 +00:00
this.udpHub.WriteTo(resp.Value, dest)
2016-01-03 23:33:25 +00:00
this.udpMutex.RUnlock()
2016-01-28 16:43:47 +00:00
resp.Release()
2015-11-10 23:08:43 +00:00
}
}
2015-12-02 20:44:01 +00:00
func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
2016-01-28 16:08:32 +00:00
tcpListener, err := hub.ListenTCP(port, this.HandleTCPConnection)
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-01-28 16:08:32 +00:00
func (this *DokodemoDoor) HandleTCPConnection(conn *hub.TCPConn) {
2015-10-30 14:56:46 +00:00
defer conn.Close()
2015-12-16 22:53:38 +00:00
packet := v2net.NewPacket(v2net.TCPDestination(this.address, this.port), nil, true)
2015-12-05 21:55:45 +00:00
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
2015-10-30 14:56:46 +00:00
var inputFinish, outputFinish sync.Mutex
inputFinish.Lock()
outputFinish.Lock()
reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
2015-10-30 14:56:46 +00:00
go dumpInput(reader, ray.InboundInput(), &inputFinish)
go dumpOutput(conn, ray.InboundOutput(), &outputFinish)
outputFinish.Lock()
}
func dumpInput(reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
2016-01-29 13:39:55 +00:00
v2io.RawReaderToChan(input, reader)
2015-10-30 14:56:46 +00:00
finish.Unlock()
close(input)
}
func dumpOutput(writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) {
2016-01-29 13:39:55 +00:00
v2io.ChanToWriter(writer, output)
2015-10-30 14:56:46 +00:00
finish.Unlock()
}