| 
									
										
										
										
											2017-01-13 00:56:21 +01:00
										 |  |  | package proxy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"v2ray.com/core/common/net" | 
					
						
							| 
									
										
										
										
											2017-01-13 00:56:21 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type key int | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2017-02-03 22:35:09 +01:00
										 |  |  | 	sourceKey key = iota | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | 	targetKey | 
					
						
							|  |  |  | 	originalTargetKey | 
					
						
							|  |  |  | 	inboundEntryPointKey | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | 	inboundTagKey | 
					
						
							|  |  |  | 	resolvedIPsKey | 
					
						
							| 
									
										
										
										
											2017-01-13 00:56:21 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-23 18:42:38 +02:00
										 |  |  | // ContextWithSource creates a new context with given source. | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | func ContextWithSource(ctx context.Context, src net.Destination) context.Context { | 
					
						
							|  |  |  | 	return context.WithValue(ctx, sourceKey, src) | 
					
						
							| 
									
										
										
										
											2017-01-13 00:56:21 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-23 18:42:38 +02:00
										 |  |  | // SourceFromContext retreives source from the given context. | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func SourceFromContext(ctx context.Context) (net.Destination, bool) { | 
					
						
							|  |  |  | 	v, ok := ctx.Value(sourceKey).(net.Destination) | 
					
						
							|  |  |  | 	return v, ok | 
					
						
							| 
									
										
										
										
											2017-01-13 00:56:21 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-15 00:48:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func ContextWithOriginalTarget(ctx context.Context, dest net.Destination) context.Context { | 
					
						
							|  |  |  | 	return context.WithValue(ctx, originalTargetKey, dest) | 
					
						
							| 
									
										
										
										
											2017-01-15 00:48:37 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func OriginalTargetFromContext(ctx context.Context) (net.Destination, bool) { | 
					
						
							|  |  |  | 	v, ok := ctx.Value(originalTargetKey).(net.Destination) | 
					
						
							|  |  |  | 	return v, ok | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func ContextWithTarget(ctx context.Context, dest net.Destination) context.Context { | 
					
						
							|  |  |  | 	return context.WithValue(ctx, targetKey, dest) | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func TargetFromContext(ctx context.Context) (net.Destination, bool) { | 
					
						
							|  |  |  | 	v, ok := ctx.Value(targetKey).(net.Destination) | 
					
						
							|  |  |  | 	return v, ok | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func ContextWithInboundEntryPoint(ctx context.Context, dest net.Destination) context.Context { | 
					
						
							|  |  |  | 	return context.WithValue(ctx, inboundEntryPointKey, dest) | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func InboundEntryPointFromContext(ctx context.Context) (net.Destination, bool) { | 
					
						
							|  |  |  | 	v, ok := ctx.Value(inboundEntryPointKey).(net.Destination) | 
					
						
							|  |  |  | 	return v, ok | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ContextWithInboundTag(ctx context.Context, tag string) context.Context { | 
					
						
							|  |  |  | 	return context.WithValue(ctx, inboundTagKey, tag) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 22:49:38 +01:00
										 |  |  | func InboundTagFromContext(ctx context.Context) (string, bool) { | 
					
						
							|  |  |  | 	v, ok := ctx.Value(inboundTagKey).(string) | 
					
						
							|  |  |  | 	return v, ok | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:55:47 +01:00
										 |  |  | type IPResolver interface { | 
					
						
							|  |  |  | 	Resolve() []net.Address | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:55:47 +01:00
										 |  |  | func ContextWithResolveIPs(ctx context.Context, f IPResolver) context.Context { | 
					
						
							|  |  |  | 	return context.WithValue(ctx, resolvedIPsKey, f) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ResolvedIPsFromContext(ctx context.Context) (IPResolver, bool) { | 
					
						
							|  |  |  | 	ips, ok := ctx.Value(resolvedIPsKey).(IPResolver) | 
					
						
							| 
									
										
										
										
											2017-01-26 20:46:44 +01:00
										 |  |  | 	return ips, ok | 
					
						
							|  |  |  | } |