| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | package dokodemo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | 	"github.com/v2ray/v2ray-core/app/dispatcher" | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01: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 15:56:46 +01:00
										 |  |  | 	"github.com/v2ray/v2ray-core/common/log" | 
					
						
							|  |  |  | 	v2net "github.com/v2ray/v2ray-core/common/net" | 
					
						
							| 
									
										
										
										
											2016-01-19 23:41:40 +01: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 15:56:46 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type DokodemoDoor struct { | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | 	tcpMutex         sync.RWMutex | 
					
						
							|  |  |  | 	udpMutex         sync.RWMutex | 
					
						
							|  |  |  | 	config           *Config | 
					
						
							|  |  |  | 	accepting        bool | 
					
						
							|  |  |  | 	address          v2net.Address | 
					
						
							|  |  |  | 	port             v2net.Port | 
					
						
							|  |  |  | 	packetDispatcher dispatcher.PacketDispatcher | 
					
						
							|  |  |  | 	tcpListener      *hub.TCPHub | 
					
						
							|  |  |  | 	udpHub           *hub.UDPHub | 
					
						
							| 
									
										
										
										
											2016-02-01 21:34:07 +01:00
										 |  |  | 	udpServer        *hub.UDPServer | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | 	listeningPort    v2net.Port | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | func NewDokodemoDoor(config *Config, packetDispatcher dispatcher.PacketDispatcher) *DokodemoDoor { | 
					
						
							| 
									
										
										
										
											2015-12-06 18:21:15 +01:00
										 |  |  | 	return &DokodemoDoor{ | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | 		config:           config, | 
					
						
							|  |  |  | 		packetDispatcher: packetDispatcher, | 
					
						
							|  |  |  | 		address:          config.Address, | 
					
						
							|  |  |  | 		port:             config.Port, | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 23:41:40 +01:00
										 |  |  | func (this *DokodemoDoor) Port() v2net.Port { | 
					
						
							|  |  |  | 	return this.listeningPort | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 23:30:37 +01:00
										 |  |  | func (this *DokodemoDoor) Close() { | 
					
						
							|  |  |  | 	this.accepting = false | 
					
						
							|  |  |  | 	if this.tcpListener != nil { | 
					
						
							| 
									
										
										
										
											2016-01-04 00:33:25 +01:00
										 |  |  | 		this.tcpMutex.Lock() | 
					
						
							| 
									
										
										
										
											2016-01-27 22:11:31 +01:00
										 |  |  | 		this.tcpListener.Close() | 
					
						
							| 
									
										
										
										
											2016-01-03 23:30:37 +01:00
										 |  |  | 		this.tcpListener = nil | 
					
						
							| 
									
										
										
										
											2016-01-04 00:33:25 +01:00
										 |  |  | 		this.tcpMutex.Unlock() | 
					
						
							| 
									
										
										
										
											2016-01-03 23:30:37 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-28 16:43:47 +00:00
										 |  |  | 	if this.udpHub != nil { | 
					
						
							| 
									
										
										
										
											2016-01-04 00:33:25 +01:00
										 |  |  | 		this.udpMutex.Lock() | 
					
						
							| 
									
										
										
										
											2016-01-28 16:43:47 +00:00
										 |  |  | 		this.udpHub.Close() | 
					
						
							|  |  |  | 		this.udpHub = nil | 
					
						
							| 
									
										
										
										
											2016-01-04 00:33:25 +01:00
										 |  |  | 		this.udpMutex.Unlock() | 
					
						
							| 
									
										
										
										
											2016-01-03 23:30:37 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:44:01 +00:00
										 |  |  | func (this *DokodemoDoor) Listen(port v2net.Port) error { | 
					
						
							| 
									
										
										
										
											2016-01-19 23:41:40 +01:00
										 |  |  | 	if this.accepting { | 
					
						
							|  |  |  | 		if this.listeningPort == port { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return proxy.ErrorAlreadyListening | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	this.listeningPort = port | 
					
						
							| 
									
										
										
										
											2015-11-11 00:08:43 +01:00
										 |  |  | 	this.accepting = true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 12:43:06 +01:00
										 |  |  | 	if this.config.Network.HasNetwork(v2net.TCPNetwork) { | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 		err := this.ListenTCP(port) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-15 12:43:06 +01:00
										 |  |  | 	if this.config.Network.HasNetwork(v2net.UDPNetwork) { | 
					
						
							| 
									
										
										
										
											2015-11-11 00:08:43 +01:00
										 |  |  | 		err := this.ListenUDP(port) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:44:01 +00:00
										 |  |  | func (this *DokodemoDoor) ListenUDP(port v2net.Port) error { | 
					
						
							| 
									
										
										
										
											2016-02-03 22:42:44 +01:00
										 |  |  | 	this.udpServer = hub.NewUDPServer(this.packetDispatcher) | 
					
						
							| 
									
										
										
										
											2016-01-28 16:43:47 +00:00
										 |  |  | 	udpHub, err := hub.ListenUDP(port, this.handleUDPPackets) | 
					
						
							| 
									
										
										
										
											2015-11-11 00:08:43 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-18 12:24:33 +01:00
										 |  |  | 		log.Error("Dokodemo failed to listen on port ", port, ": ", err) | 
					
						
							| 
									
										
										
										
											2015-11-11 00:08:43 +01:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-04 08:40:24 +01:00
										 |  |  | 	this.udpMutex.Lock() | 
					
						
							| 
									
										
										
										
											2016-01-28 16:43:47 +00:00
										 |  |  | 	this.udpHub = udpHub | 
					
						
							| 
									
										
										
										
											2016-01-04 08:40:24 +01:00
										 |  |  | 	this.udpMutex.Unlock() | 
					
						
							| 
									
										
										
										
											2015-11-11 00:08:43 +01: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) | 
					
						
							| 
									
										
										
										
											2016-02-01 21:34:07 +01:00
										 |  |  | 	this.udpServer.Dispatch(dest, packet, func(packet v2net.Packet) { | 
					
						
							|  |  |  | 		defer packet.Chunk().Release() | 
					
						
							| 
									
										
										
										
											2016-01-04 00:33:25 +01:00
										 |  |  | 		this.udpMutex.RLock() | 
					
						
							|  |  |  | 		if !this.accepting { | 
					
						
							| 
									
										
										
										
											2016-01-04 01:19:27 +01:00
										 |  |  | 			this.udpMutex.RUnlock() | 
					
						
							| 
									
										
										
										
											2016-01-04 00:33:25 +01:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2016-01-03 23:30:37 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-01 21:34:07 +01:00
										 |  |  | 		this.udpHub.WriteTo(packet.Chunk().Value, packet.Destination()) | 
					
						
							| 
									
										
										
										
											2016-01-04 00:33:25 +01:00
										 |  |  | 		this.udpMutex.RUnlock() | 
					
						
							| 
									
										
										
										
											2016-02-01 21:34:07 +01:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-11-11 00:08:43 +01: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 15:56:46 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-27 22:11:31 +01:00
										 |  |  | 		log.Error("Dokodemo: Failed to listen on port ", port, ": ", err) | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-04 08:40:24 +01:00
										 |  |  | 	this.tcpMutex.Lock() | 
					
						
							| 
									
										
										
										
											2016-01-03 23:30:37 +01:00
										 |  |  | 	this.tcpListener = tcpListener | 
					
						
							| 
									
										
										
										
											2016-01-04 08:40:24 +01:00
										 |  |  | 	this.tcpMutex.Unlock() | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 16:08:32 +00:00
										 |  |  | func (this *DokodemoDoor) HandleTCPConnection(conn *hub.TCPConn) { | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 	defer conn.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 23:53:38 +01:00
										 |  |  | 	packet := v2net.NewPacket(v2net.TCPDestination(this.address, this.port), nil, true) | 
					
						
							| 
									
										
										
										
											2016-01-31 17:01:28 +01:00
										 |  |  | 	ray := this.packetDispatcher.DispatchToOutbound(packet) | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var inputFinish, outputFinish sync.Mutex | 
					
						
							|  |  |  | 	inputFinish.Lock() | 
					
						
							|  |  |  | 	outputFinish.Lock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 12:43:06 +01:00
										 |  |  | 	reader := v2net.NewTimeOutReader(this.config.Timeout, conn) | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01: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 15:56:46 +01:00
										 |  |  | 	finish.Unlock() | 
					
						
							|  |  |  | 	close(input) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func dumpOutput(writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) { | 
					
						
							| 
									
										
										
										
											2016-02-01 12:22:29 +01:00
										 |  |  | 	v2io.ChanToRawWriter(writer, output) | 
					
						
							| 
									
										
										
										
											2015-10-30 15:56:46 +01:00
										 |  |  | 	finish.Unlock() | 
					
						
							|  |  |  | } |