v2ray-core/proxy/dokodemo/dokodemo.go

208 lines
5.3 KiB
Go
Raw Normal View History

2015-10-30 14:56:46 +00:00
package dokodemo
import (
"sync"
2016-05-22 17:32:37 +00:00
"github.com/v2ray/v2ray-core/app"
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"
"github.com/v2ray/v2ray-core/proxy/registry"
2016-06-14 20:54:08 +00:00
"github.com/v2ray/v2ray-core/transport/internet"
"github.com/v2ray/v2ray-core/transport/internet/udp"
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
2016-06-14 20:54:08 +00:00
tcpListener *internet.TCPHub
udpHub *udp.UDPHub
udpServer *udp.UDPServer
2016-06-04 12:25:13 +00:00
meta *proxy.InboundHandlerMeta
2015-10-30 14:56:46 +00:00
}
2016-06-04 12:25:13 +00:00
func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *DokodemoDoor {
2016-05-22 17:32:37 +00:00
d := &DokodemoDoor{
2016-06-04 12:25:13 +00:00
config: config,
address: config.Address,
port: config.Port,
meta: meta,
2015-10-30 14:56:46 +00:00
}
2016-05-22 17:32:37 +00:00
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
log.Error("Dokodemo: Dispatcher is not found in the space.")
2016-06-27 06:53:35 +00:00
return app.ErrMissingApplication
2016-05-22 17:32:37 +00:00
}
d.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return nil
})
return d
2015-10-30 14:56:46 +00:00
}
2016-01-19 22:41:40 +00:00
func (this *DokodemoDoor) Port() v2net.Port {
2016-06-04 12:25:13 +00:00
return this.meta.Port
2016-01-19 22:41:40 +00:00
}
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()
}
}
2016-06-03 22:38:22 +00:00
func (this *DokodemoDoor) Start() error {
2016-01-19 22:41:40 +00:00
if this.accepting {
2016-06-03 22:38:22 +00:00
return nil
2016-01-19 22:41:40 +00:00
}
2015-11-10 23:08:43 +00:00
this.accepting = true
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
2016-06-04 12:25:13 +00:00
err := this.ListenTCP()
2015-10-30 14:56:46 +00:00
if err != nil {
return err
}
}
if this.config.Network.HasNetwork(v2net.UDPNetwork) {
2016-06-04 12:25:13 +00:00
err := this.ListenUDP()
2015-11-10 23:08:43 +00:00
if err != nil {
return err
}
}
2015-10-30 14:56:46 +00:00
return nil
}
2016-06-04 12:25:13 +00:00
func (this *DokodemoDoor) ListenUDP() error {
2016-08-12 21:37:21 +00:00
this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher)
2016-08-16 11:04:07 +00:00
udpHub, err := udp.ListenUDP(
this.meta.Address, this.meta.Port, udp.ListenOption{
Callback: this.handleUDPPackets,
ReceiveOriginalDest: this.config.FollowRedirect,
})
2015-11-10 23:08:43 +00:00
if err != nil {
2016-06-04 12:25:13 +00:00
log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.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-08-15 15:44:46 +00: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 06:09:28 +00: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 23:08:43 +00:00
}
2016-06-04 12:25:13 +00:00
func (this *DokodemoDoor) ListenTCP() error {
2016-06-14 20:54:08 +00:00
tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, this.meta.StreamSettings)
2015-10-30 14:56:46 +00:00
if err != nil {
2016-06-04 12:25:13 +00:00
log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.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-06-14 20:54:08 +00:00
func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
2015-10-30 14:56:46 +00:00
defer conn.Close()
2016-06-12 20:54:33 +00:00
var dest v2net.Destination
2016-06-11 23:30:56 +00:00
if this.config.FollowRedirect {
originalDest := GetOriginalDestination(conn)
if originalDest != nil {
log.Info("Dokodemo: Following redirect to: ", originalDest)
dest = originalDest
}
}
2016-06-12 20:54:33 +00: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 20:54:08 +00:00
log.Info("Dokodemo: Handling request to ", dest)
2016-06-11 23:30:56 +00:00
2016-08-14 15:08:01 +00:00
ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{
2016-08-14 21:20:23 +00:00
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
2016-08-14 15:08:01 +00:00
Destination: dest,
})
2016-04-18 16:44:10 +00:00
defer ray.InboundOutput().Release()
2015-10-30 14:56:46 +00:00
2016-08-16 15:00:04 +00:00
var wg sync.WaitGroup
2015-10-30 14:56:46 +00:00
reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
2016-05-04 22:24:18 +00:00
defer reader.Release()
2016-08-16 15:00:04 +00:00
wg.Add(1)
2016-04-18 16:44:10 +00:00
go func() {
v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release()
v2io.Pipe(v2reader, ray.InboundInput())
2016-08-16 15:00:04 +00:00
wg.Done()
2016-04-18 16:44:10 +00:00
ray.InboundInput().Close()
}()
2015-10-30 14:56:46 +00:00
2016-08-16 15:00:04 +00:00
wg.Add(1)
2016-04-18 16:44:10 +00:00
go func() {
v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release()
v2io.Pipe(ray.InboundOutput(), v2writer)
2016-08-16 15:00:04 +00:00
wg.Done()
2016-04-18 16:44:10 +00:00
}()
2015-10-30 14:56:46 +00:00
2016-08-16 15:00:04 +00:00
wg.Wait()
2015-10-30 14:56:46 +00:00
}
2016-05-22 17:32:37 +00:00
2016-06-14 20:54:08 +00: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 17:32:37 +00:00
func init() {
registry.MustRegisterInboundHandlerCreator("dokodemo-door", new(Factory))
2016-05-22 17:32:37 +00:00
}