1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 14:56:33 -04:00
v2fly/proxy/dokodemo/dokodemo.go

208 lines
5.2 KiB
Go
Raw Normal View History

2015-10-30 10:56:46 -04:00
package dokodemo
import (
"sync"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/registry"
"v2ray.com/core/transport/internet"
"v2ray.com/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
2016-09-20 04:44:44 -04:00
if this.config.Network.HasNetwork(v2net.Network_TCP) {
2016-06-04 08:25:13 -04:00
err := this.ListenTCP()
2015-10-30 10:56:46 -04:00
if err != nil {
return err
}
}
2016-09-20 04:44:44 -04:00
if this.config.Network.HasNetwork(v2net.Network_UDP) {
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-08-12 17:37:21 -04:00
this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher)
2016-08-16 07:04:07 -04:00
udpHub, err := udp.ListenUDP(
this.meta.Address, this.meta.Port, udp.ListenOption{
Callback: this.handleUDPPackets,
ReceiveOriginalDest: this.config.FollowRedirect,
})
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-08-15 11:44:46 -04:00
func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.SessionInfo) {
if session.Destination == nil && this.address != nil && this.port > 0 {
session.Destination = v2net.UDPDestination(this.address, this.port)
}
if session.Destination == nil {
log.Info("Dokodemo: Unknown destination, stop forwarding...")
return
}
this.udpServer.Dispatch(session, payload, this.handleUDPResponse)
2016-05-16 02:09:28 -04:00
}
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
2016-08-14 11:08:01 -04:00
ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{
2016-08-14 17:20:23 -04:00
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
2016-08-14 11:08:01 -04:00
Destination: dest,
})
2016-04-18 12:44:10 -04:00
defer ray.InboundOutput().Release()
2015-10-30 10:56:46 -04:00
2016-08-16 11:00:04 -04:00
var wg sync.WaitGroup
2015-10-30 10:56:46 -04:00
reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
2016-05-04 18:24:18 -04:00
defer reader.Release()
2016-08-16 11:00:04 -04:00
wg.Add(1)
2016-04-18 12:44:10 -04:00
go func() {
v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release()
v2io.Pipe(v2reader, ray.InboundInput())
2016-08-16 11:00:04 -04:00
wg.Done()
2016-04-18 12:44:10 -04:00
ray.InboundInput().Close()
}()
2015-10-30 10:56:46 -04:00
2016-08-16 11:00:04 -04:00
wg.Add(1)
2016-04-18 12:44:10 -04:00
go func() {
v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release()
v2io.Pipe(ray.InboundOutput(), v2writer)
2016-08-16 11:00:04 -04:00
wg.Done()
2016-04-18 12:44:10 -04:00
}()
2015-10-30 10:56:46 -04:00
2016-08-16 11:00:04 -04:00
wg.Wait()
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() {
registry.MustRegisterInboundHandlerCreator("dokodemo-door", new(Factory))
2016-05-22 13:32:37 -04:00
}