| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | package json | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2015-09-20 11:45:40 +02:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-20 00:50:21 +02:00
										 |  |  | 	"github.com/v2ray/v2ray-core/common/log" | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 	"github.com/v2ray/v2ray-core/config" | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ConnectionConfig struct { | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 	ProtocolString  string          `json:"protocol"` | 
					
						
							|  |  |  | 	SettingsMessage json.RawMessage `json:"settings"` | 
					
						
							| 
									
										
										
										
											2015-10-29 23:35:54 +01:00
										 |  |  | 	Type            config.Type     `json:"-"` | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (config *ConnectionConfig) Protocol() string { | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	return config.ProtocolString | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-29 23:35:54 +01:00
										 |  |  | func (config *ConnectionConfig) Settings() interface{} { | 
					
						
							|  |  |  | 	creator, found := configCache[getConfigKey(config.Protocol(), config.Type)] | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 	if !found { | 
					
						
							|  |  |  | 		panic("Unknown protocol " + config.Protocol()) | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 	configObj := creator() | 
					
						
							|  |  |  | 	err := json.Unmarshal(config.SettingsMessage, configObj) | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 		log.Error("Unable to parse connection config: %v", err) | 
					
						
							|  |  |  | 		panic("Failed to parse connection config.") | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 	return configObj | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-09 17:43:27 +02:00
										 |  |  | type LogConfig struct { | 
					
						
							|  |  |  | 	AccessLogValue string `json:"access"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (config *LogConfig) AccessLog() string { | 
					
						
							|  |  |  | 	return config.AccessLogValue | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | // Config is the config for Point server. | 
					
						
							|  |  |  | type Config struct { | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	PortValue           uint16            `json:"port"` // Port of this Point server. | 
					
						
							| 
									
										
										
										
											2015-10-09 17:43:27 +02:00
										 |  |  | 	LogConfigValue      *LogConfig        `json:"log"` | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | 	InboundConfigValue  *ConnectionConfig `json:"inbound"` | 
					
						
							|  |  |  | 	OutboundConfigValue *ConnectionConfig `json:"outbound"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (config *Config) Port() uint16 { | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	return config.PortValue | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-09 17:43:27 +02:00
										 |  |  | func (config *Config) LogConfig() config.LogConfig { | 
					
						
							| 
									
										
										
										
											2015-10-10 12:14:21 +02:00
										 |  |  | 	if config.LogConfigValue == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-09 17:43:27 +02:00
										 |  |  | 	return config.LogConfigValue | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | func (config *Config) InboundConfig() config.ConnectionConfig { | 
					
						
							| 
									
										
										
										
											2015-10-10 12:14:21 +02:00
										 |  |  | 	if config.InboundConfigValue == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	return config.InboundConfigValue | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | func (config *Config) OutboundConfig() config.ConnectionConfig { | 
					
						
							| 
									
										
										
										
											2015-10-10 12:14:21 +02:00
										 |  |  | 	if config.OutboundConfigValue == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	return config.OutboundConfigValue | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func LoadConfig(file string) (*Config, error) { | 
					
						
							| 
									
										
										
										
											2015-09-20 11:45:40 +02:00
										 |  |  | 	fixedFile := os.ExpandEnv(file) | 
					
						
							|  |  |  | 	rawConfig, err := ioutil.ReadFile(fixedFile) | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 		log.Error("Failed to read server config file (%s): %v", file, err) | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 18:27:25 +02:00
										 |  |  | 	jsonConfig := &Config{} | 
					
						
							|  |  |  | 	err = json.Unmarshal(rawConfig, jsonConfig) | 
					
						
							| 
									
										
										
										
											2015-10-04 16:53:37 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-10-06 23:11:08 +02:00
										 |  |  | 		log.Error("Failed to load server config: %v", err) | 
					
						
							| 
									
										
										
										
											2015-10-04 16:53:37 +02:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-19 15:35:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-29 23:35:54 +01:00
										 |  |  | 	jsonConfig.InboundConfigValue.Type = config.TypeInbound | 
					
						
							|  |  |  | 	jsonConfig.OutboundConfigValue.Type = config.TypeOutbound | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 18:27:25 +02:00
										 |  |  | 	return jsonConfig, err | 
					
						
							| 
									
										
										
										
											2015-09-19 15:07:10 +02:00
										 |  |  | } |