| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | package internal | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/v2ray/v2ray-core/app" | 
					
						
							| 
									
										
										
										
											2016-01-02 23:32:18 +01:00
										 |  |  | 	"github.com/v2ray/v2ray-core/proxy" | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							| 
									
										
										
										
											2016-01-25 17:27:15 +01:00
										 |  |  | 	inboundFactories  = make(map[string]InboundHandlerCreator) | 
					
						
							|  |  |  | 	outboundFactories = make(map[string]OutboundHandlerCreator) | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ErrorProxyNotFound    = errors.New("Proxy not found.") | 
					
						
							|  |  |  | 	ErrorNameExists       = errors.New("Proxy with the same name already exists.") | 
					
						
							|  |  |  | 	ErrorBadConfiguration = errors.New("Bad proxy configuration.") | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-25 17:27:15 +01:00
										 |  |  | func RegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) error { | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 	if _, found := inboundFactories[name]; found { | 
					
						
							|  |  |  | 		return ErrorNameExists | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inboundFactories[name] = creator | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-25 17:29:26 +01:00
										 |  |  | func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) { | 
					
						
							| 
									
										
										
										
											2016-01-25 17:20:44 +01:00
										 |  |  | 	if err := RegisterInboundHandlerCreator(name, creator); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-06 16:23:54 +01:00
										 |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-25 17:27:15 +01:00
										 |  |  | func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) error { | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 	if _, found := outboundFactories[name]; found { | 
					
						
							|  |  |  | 		return ErrorNameExists | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	outboundFactories[name] = creator | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-25 17:29:26 +01:00
										 |  |  | func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) { | 
					
						
							| 
									
										
										
										
											2016-01-25 17:20:44 +01:00
										 |  |  | 	if err := RegisterOutboundHandlerCreator(name, creator); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-06 16:23:54 +01:00
										 |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-04 14:25:13 +02:00
										 |  |  | func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 	creator, found := inboundFactories[name] | 
					
						
							|  |  |  | 	if !found { | 
					
						
							|  |  |  | 		return nil, ErrorProxyNotFound | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(rawConfig) > 0 { | 
					
						
							| 
									
										
										
										
											2016-06-10 22:26:39 +02:00
										 |  |  | 		proxyConfig, err := CreateInboundConfig(name, rawConfig) | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-04 14:25:13 +02:00
										 |  |  | 		return creator(space, proxyConfig, meta) | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-04 14:25:13 +02:00
										 |  |  | 	return creator(space, nil, meta) | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-04 14:25:13 +02:00
										 |  |  | func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 	creator, found := outboundFactories[name] | 
					
						
							|  |  |  | 	if !found { | 
					
						
							| 
									
										
										
										
											2016-06-09 09:36:03 +08:00
										 |  |  | 		return nil, ErrorProxyNotFound | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(rawConfig) > 0 { | 
					
						
							| 
									
										
										
										
											2016-06-10 22:26:39 +02:00
										 |  |  | 		proxyConfig, err := CreateOutboundConfig(name, rawConfig) | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-04 14:25:13 +02:00
										 |  |  | 		return creator(space, proxyConfig, meta) | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-04 14:25:13 +02:00
										 |  |  | 	return creator(space, nil, meta) | 
					
						
							| 
									
										
										
										
											2016-01-02 23:08:36 +01:00
										 |  |  | } |