1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/config/json/json.go

73 lines
1.8 KiB
Go
Raw Normal View History

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"
)
type ConnectionConfig struct {
ProtocolString string `json:"protocol"`
2015-09-19 09:35:20 -04:00
File string `json:"file"`
}
func (config *ConnectionConfig) Protocol() string {
2015-09-19 09:35:20 -04:00
return config.ProtocolString
}
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
}
// 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.
InboundConfigValue *ConnectionConfig `json:"inbound"`
OutboundConfigValue *ConnectionConfig `json:"outbound"`
}
func (config *Config) Port() uint16 {
2015-09-19 09:35:20 -04:00
return config.PortValue
}
func (config *Config) InboundConfig() core.ConnectionConfig {
2015-09-19 09:35:20 -04:00
return config.InboundConfigValue
}
func (config *Config) OutboundConfig() core.ConnectionConfig {
2015-09-19 09:35:20 -04:00
return config.OutboundConfigValue
}
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
}
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
}
return config, err
}