2015-09-19 09:07:10 -04:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2015-09-19 09:35:20 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2015-09-20 05:45:40 -04:00
|
|
|
"os"
|
2015-09-19 09:35:20 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 09:07:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConnectionConfig struct {
|
|
|
|
ProtocolString string `json:"protocol"`
|
2015-09-19 09:35:20 -04:00
|
|
|
File string `json:"file"`
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *ConnectionConfig) Protocol() string {
|
2015-09-19 09:35:20 -04:00
|
|
|
return config.ProtocolString
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *ConnectionConfig) Content() []byte {
|
2015-09-19 09:35:20 -04:00
|
|
|
if len(config.File) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(config.File)
|
|
|
|
if err != nil {
|
|
|
|
panic(log.Error("Failed to read config file (%s): %v", config.File, err))
|
|
|
|
}
|
|
|
|
return content
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the config for Point server.
|
|
|
|
type Config struct {
|
2015-09-19 09:35:20 -04:00
|
|
|
PortValue uint16 `json:"port"` // Port of this Point server.
|
2015-09-19 09:07:10 -04:00
|
|
|
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
|
|
|
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) Port() uint16 {
|
2015-09-19 09:35:20 -04:00
|
|
|
return config.PortValue
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) InboundConfig() core.ConnectionConfig {
|
2015-09-19 09:35:20 -04:00
|
|
|
return config.InboundConfigValue
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) OutboundConfig() core.ConnectionConfig {
|
2015-09-19 09:35:20 -04:00
|
|
|
return config.OutboundConfigValue
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(file string) (*Config, error) {
|
2015-09-20 05:45:40 -04:00
|
|
|
fixedFile := os.ExpandEnv(file)
|
|
|
|
rawConfig, err := ioutil.ReadFile(fixedFile)
|
2015-09-19 09:35:20 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to read point config file (%s): %v", file, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-19 09:07:10 -04:00
|
|
|
config := &Config{}
|
|
|
|
err = json.Unmarshal(rawConfig, config)
|
2015-09-19 09:35:20 -04:00
|
|
|
|
|
|
|
if !filepath.IsAbs(config.InboundConfigValue.File) && len(config.InboundConfigValue.File) > 0 {
|
2015-09-20 05:45:40 -04:00
|
|
|
config.InboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.InboundConfigValue.File)
|
2015-09-19 09:35:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !filepath.IsAbs(config.OutboundConfigValue.File) && len(config.OutboundConfigValue.File) > 0 {
|
2015-09-20 05:45:40 -04:00
|
|
|
config.OutboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.OutboundConfigValue.File)
|
2015-09-19 09:35:20 -04:00
|
|
|
}
|
|
|
|
|
2015-09-19 09:07:10 -04:00
|
|
|
return config, err
|
|
|
|
}
|