forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
70 lines (56 loc) · 1.77 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package json
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/log"
)
type ConnectionConfig struct {
ProtocolString string `json:"protocol"`
File string `json:"file"`
}
func (config *ConnectionConfig) Protocol() string {
return config.ProtocolString
}
func (config *ConnectionConfig) Content() []byte {
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 {
PortValue uint16 `json:"port"` // Port of this Point server.
InboundConfigValue *ConnectionConfig `json:"inbound"`
OutboundConfigValue *ConnectionConfig `json:"outbound"`
}
func (config *Config) Port() uint16 {
return config.PortValue
}
func (config *Config) InboundConfig() core.ConnectionConfig {
return config.InboundConfigValue
}
func (config *Config) OutboundConfig() core.ConnectionConfig {
return config.OutboundConfigValue
}
func LoadConfig(file string) (*Config, error) {
rawConfig, err := ioutil.ReadFile(file)
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)
if !filepath.IsAbs(config.InboundConfigValue.File) && len(config.InboundConfigValue.File) > 0 {
config.InboundConfigValue.File = filepath.Join(filepath.Dir(file), config.InboundConfigValue.File)
}
if !filepath.IsAbs(config.OutboundConfigValue.File) && len(config.OutboundConfigValue.File) > 0 {
config.OutboundConfigValue.File = filepath.Join(filepath.Dir(file), config.OutboundConfigValue.File)
}
return config, err
}