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

214 lines
5.4 KiB
Go
Raw Normal View History

2015-10-30 14:56:46 +00:00
package dokodemo
import (
"sync"
2016-11-21 20:13:01 +00:00
"errors"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
2016-10-16 12:22:21 +00:00
"v2ray.com/core/common/loader"
2016-08-20 18:55:45 +00:00
"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 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,
2016-09-22 14:49:20 +00:00
address: config.GetPredefinedAddress(),
port: v2net.Port(config.Port),
2016-06-04 12:25:13 +00:00
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) {
2016-11-21 20:13:01 +00:00
return errors.New("Dokodemo: Dispatcher is not found in the space.")
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
2016-09-22 14:49:20 +00:00
if this.config.NetworkList.HasNetwork(v2net.Network_TCP) {
2016-06-04 12:25:13 +00:00
err := this.ListenTCP()
2015-10-30 14:56:46 +00:00
if err != nil {
return err
}
}
2016-09-22 14:49:20 +00:00
if this.config.NetworkList.HasNetwork(v2net.Network_UDP) {
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-11-13 13:33:00 +00:00
this.udpServer = udp.NewUDPServer(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,
2016-11-18 20:30:03 +00:00
Concurrency: 2,
2016-08-16 11:04:07 +00:00
})
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) {
2016-09-20 09:53:05 +00:00
if session.Destination.Network == v2net.Network_Unknown && this.address != nil && this.port > 0 {
2016-08-15 15:44:46 +00:00
session.Destination = v2net.UDPDestination(this.address, this.port)
}
2016-09-20 09:53:05 +00:00
if session.Destination.Network == v2net.Network_Unknown {
2016-08-15 15:44:46 +00:00
log.Info("Dokodemo: Unknown destination, stop forwarding...")
return
}
2016-11-13 13:33:00 +00:00
session.Inbound = this.meta
2016-08-15 15:44:46 +00:00
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)
2016-09-20 09:53:05 +00:00
if originalDest.Network != v2net.Network_Unknown {
2016-06-11 23:30:56 +00:00
log.Info("Dokodemo: Following redirect to: ", originalDest)
dest = originalDest
}
}
2016-09-20 09:53:05 +00:00
if dest.Network == v2net.Network_Unknown && this.address != nil && this.port > v2net.Port(0) {
2016-06-12 20:54:33 +00:00
dest = v2net.TCPDestination(this.address, this.port)
}
2016-09-20 09:53:05 +00:00
if dest.Network == v2net.Network_Unknown {
2016-06-12 20:54:33 +00:00
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-11-13 13:33:00 +00:00
ray := this.packetDispatcher.DispatchToOutbound(&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-11-13 13:33:00 +00:00
Inbound: this.meta,
2016-08-14 15:08:01 +00:00
})
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{}
2016-10-02 21:43:58 +00:00
func (this *Factory) StreamCapability() v2net.NetworkList {
return v2net.NetworkList{
Network: []v2net.Network{v2net.Network_RawTCP},
}
2016-06-14 20:54:08 +00:00
}
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() {
2016-10-16 12:22:21 +00:00
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
2016-05-22 17:32:37 +00:00
}