| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | package mocks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 14:46:18 +02:00
										 |  |  | 	"github.com/v2ray/v2ray-core/common/alloc" | 
					
						
							| 
									
										
										
										
											2015-09-19 23:54:36 +02:00
										 |  |  | 	v2net "github.com/v2ray/v2ray-core/common/net" | 
					
						
							| 
									
										
										
										
											2015-10-30 00:11:29 +01:00
										 |  |  | 	"github.com/v2ray/v2ray-core/proxy/common/connhandler" | 
					
						
							| 
									
										
										
										
											2015-10-14 14:51:19 +02:00
										 |  |  | 	"github.com/v2ray/v2ray-core/transport/ray" | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type OutboundConnectionHandler struct { | 
					
						
							|  |  |  | 	Data2Send   *bytes.Buffer | 
					
						
							|  |  |  | 	Data2Return []byte | 
					
						
							| 
									
										
										
										
											2015-09-20 18:22:29 +02:00
										 |  |  | 	Destination v2net.Destination | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-14 14:51:19 +02:00
										 |  |  | func (handler *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.OutboundRay) error { | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	input := ray.OutboundInput() | 
					
						
							|  |  |  | 	output := ray.OutboundOutput() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-07 00:30:44 +02:00
										 |  |  | 	handler.Destination = packet.Destination() | 
					
						
							|  |  |  | 	if packet.Chunk() != nil { | 
					
						
							| 
									
										
										
										
											2015-10-08 14:46:18 +02:00
										 |  |  | 		handler.Data2Send.Write(packet.Chunk().Value) | 
					
						
							| 
									
										
										
										
											2015-10-07 00:30:44 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	go func() { | 
					
						
							|  |  |  | 		for { | 
					
						
							|  |  |  | 			data, open := <-input | 
					
						
							|  |  |  | 			if !open { | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-08 14:46:18 +02:00
										 |  |  | 			handler.Data2Send.Write(data.Value) | 
					
						
							|  |  |  | 			data.Release() | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-10-08 14:46:18 +02:00
										 |  |  | 		response := alloc.NewBuffer() | 
					
						
							|  |  |  | 		response.Clear() | 
					
						
							|  |  |  | 		response.Append(handler.Data2Return) | 
					
						
							|  |  |  | 		output <- response | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 		close(output) | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-30 00:11:29 +01:00
										 |  |  | func (handler *OutboundConnectionHandler) Create(config interface{}) (connhandler.OutboundConnectionHandler, error) { | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	return handler, nil | 
					
						
							|  |  |  | } |