1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 09:56:18 -05:00
v2fly/proxy/dokodemo/dokodemo.go

195 lines
5.0 KiB
Go
Raw Normal View History

2015-10-30 10:56:46 -04:00
package dokodemo
import (
"sync"
2016-05-22 13:32:37 -04:00
"github.com/v2ray/v2ray-core/app"
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-05-22 13:32:37 -04:00
"github.com/v2ray/v2ray-core/proxy/internal"
2016-06-14 16:54:08 -04:00
"github.com/v2ray/v2ray-core/transport/internet"
"github.com/v2ray/v2ray-core/transport/internet/udp"
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
2016-06-14 16:54:08 -04:00
tcpListener *internet.TCPHub
udpHub *udp.UDPHub
udpServer *udp.UDPServer
2016-06-04 08:25:13 -04:00
meta *proxy.InboundHandlerMeta
2015-10-30 10:56:46 -04:00
}
2016-06-04 08:25:13 -04:00
func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *DokodemoDoor {
2016-05-22 13:32:37 -04:00
d := &DokodemoDoor{
2016-06-04 08:25:13 -04:00
config: config,
address: config.Address,
port: config.Port,
meta: meta,
2015-10-30 10:56:46 -04:00
}
2016-05-22 13:32:37 -04:00
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
log.Error("Dokodemo: Dispatcher is not found in the space.")
2016-06-27 02:53:35 -04:00
return app.ErrMissingApplication
2016-05-22 13:32:37 -04:00
}
d.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return nil
})
return d
2015-10-30 10:56:46 -04:00
}
2016-01-19 17:41:40 -05:00
func (this *DokodemoDoor) Port() v2net.Port {
2016-06-04 08:25:13 -04:00
return this.meta.Port
2016-01-19 17:41:40 -05:00
}
func (this *DokodemoDoor) Close() {
this.accepting = false
if this.tcpListener != nil {
2016-01-03 18:33:25 -05:00
this.tcpMutex.Lock()
this.tcpListener.Close()
this.tcpListener = nil
2016-01-03 18:33:25 -05:00
this.tcpMutex.Unlock()
}
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-06-03 18:38:22 -04:00
func (this *DokodemoDoor) Start() error {
2016-01-19 17:41:40 -05:00
if this.accepting {
2016-06-03 18:38:22 -04:00
return nil
2016-01-19 17:41:40 -05:00
}
2015-11-10 18:08:43 -05:00
this.accepting = true
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
2016-06-04 08:25:13 -04:00
err := this.ListenTCP()
2015-10-30 10:56:46 -04:00
if err != nil {
return err
}
}
if this.config.Network.HasNetwork(v2net.UDPNetwork) {
2016-06-04 08:25:13 -04:00
err := this.ListenUDP()
2015-11-10 18:08:43 -05:00
if err != nil {
return err
}
}
2015-10-30 10:56:46 -04:00
return nil
}
2016-06-04 08:25:13 -04:00
func (this *DokodemoDoor) ListenUDP() error {
2016-06-14 16:54:08 -04:00
this.udpServer = udp.NewUDPServer(this.packetDispatcher)
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPackets)
2015-11-10 18:08:43 -05:00
if err != nil {
2016-06-04 08:25:13 -04:00
log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.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) {
2016-05-16 02:09:28 -04:00
this.udpServer.Dispatch(dest, v2net.UDPDestination(this.address, this.port), payload, this.handleUDPResponse)
}
func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
defer payload.Release()
this.udpMutex.RLock()
defer this.udpMutex.RUnlock()
if !this.accepting {
return
}
this.udpHub.WriteTo(payload.Value, dest)
2015-11-10 18:08:43 -05:00
}
2016-06-04 08:25:13 -04:00
func (this *DokodemoDoor) ListenTCP() error {
2016-06-14 16:54:08 -04:00
tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, this.meta.StreamSettings)
2015-10-30 10:56:46 -04:00
if err != nil {
2016-06-04 08:25:13 -04:00
log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
2015-10-30 10:56:46 -04:00
return err
}
2016-01-04 02:40:24 -05:00
this.tcpMutex.Lock()
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-06-14 16:54:08 -04:00
func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
2015-10-30 10:56:46 -04:00
defer conn.Close()
2016-06-12 16:54:33 -04:00
var dest v2net.Destination
2016-06-11 19:30:56 -04:00
if this.config.FollowRedirect {
originalDest := GetOriginalDestination(conn)
if originalDest != nil {
log.Info("Dokodemo: Following redirect to: ", originalDest)
dest = originalDest
}
}
2016-06-12 16:54:33 -04:00
if dest == nil && this.address != nil && this.port > v2net.Port(0) {
dest = v2net.TCPDestination(this.address, this.port)
}
if dest == nil {
log.Info("Dokodemo: Unknown destination, stop forwarding...")
return
}
2016-06-14 16:54:08 -04:00
log.Info("Dokodemo: Handling request to ", dest)
2016-06-11 19:30:56 -04:00
ray := this.packetDispatcher.DispatchToOutbound(dest)
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()
reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
2016-05-04 18:24:18 -04:00
defer reader.Release()
2016-04-18 12:44:10 -04:00
go func() {
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() {
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()
2016-05-04 18:24:18 -04:00
inputFinish.Lock()
2015-10-30 10:56:46 -04:00
}
2016-05-22 13:32:37 -04:00
2016-06-14 16:54:08 -04:00
type Factory struct{}
func (this *Factory) StreamCapability() internet.StreamConnectionType {
return internet.StreamConnectionTypeRawTCP
}
func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
}
2016-05-22 13:32:37 -04:00
func init() {
2016-06-14 16:54:08 -04:00
internal.MustRegisterInboundHandlerCreator("dokodemo-door", new(Factory))
2016-05-22 13:32:37 -04:00
}