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
|
|
|
|
2015-12-07 16:47:47 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app/router"
|
|
|
|
routerjson "github.com/v2ray/v2ray-core/app/router/json"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-12-02 15:44:01 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-10-30 17:42:24 -04:00
|
|
|
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
2015-12-06 10:41:41 -05:00
|
|
|
"github.com/v2ray/v2ray-core/shell/point"
|
2015-09-19 09:07:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the config for Point server.
|
|
|
|
type Config struct {
|
2015-12-07 16:47:47 -05:00
|
|
|
PortValue v2net.Port `json:"port"` // Port of this Point server.
|
|
|
|
LogConfigValue *LogConfig `json:"log"`
|
|
|
|
RouterConfigValue *routerjson.RouterConfig `json:"routing"`
|
|
|
|
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
|
|
|
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
|
|
|
InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
|
|
|
|
OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
2015-12-02 15:44:01 -05:00
|
|
|
func (config *Config) Port() v2net.Port {
|
2015-09-19 09:35:20 -04:00
|
|
|
return config.PortValue
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
2015-12-06 10:41:41 -05:00
|
|
|
func (config *Config) LogConfig() point.LogConfig {
|
2015-10-10 06:14:21 -04:00
|
|
|
if config.LogConfigValue == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-09 11:43:27 -04:00
|
|
|
return config.LogConfigValue
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:04:02 -05:00
|
|
|
func (this *Config) RouterConfig() router.Config {
|
2015-11-22 11:41:52 -05:00
|
|
|
if this.RouterConfigValue == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return this.RouterConfigValue
|
|
|
|
}
|
|
|
|
|
2015-12-06 10:41:41 -05:00
|
|
|
func (config *Config) InboundConfig() point.ConnectionConfig {
|
2015-10-10 06:14:21 -04:00
|
|
|
if config.InboundConfigValue == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-19 09:35:20 -04:00
|
|
|
return config.InboundConfigValue
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
2015-12-06 10:41:41 -05:00
|
|
|
func (config *Config) OutboundConfig() point.ConnectionConfig {
|
2015-10-10 06:14:21 -04:00
|
|
|
if config.OutboundConfigValue == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-19 09:35:20 -04:00
|
|
|
return config.OutboundConfigValue
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|
|
|
|
|
2015-12-06 10:41:41 -05:00
|
|
|
func (this *Config) InboundDetours() []point.InboundDetourConfig {
|
|
|
|
detours := make([]point.InboundDetourConfig, len(this.InboundDetoursValue))
|
2015-10-31 17:22:43 -04:00
|
|
|
for idx, detour := range this.InboundDetoursValue {
|
|
|
|
detours[idx] = detour
|
|
|
|
}
|
|
|
|
return detours
|
|
|
|
}
|
|
|
|
|
2015-12-06 10:41:41 -05:00
|
|
|
func (this *Config) OutboundDetours() []point.OutboundDetourConfig {
|
|
|
|
detours := make([]point.OutboundDetourConfig, len(this.OutboundDetoursValue))
|
2015-11-13 17:43:58 -05:00
|
|
|
for idx, detour := range this.OutboundDetoursValue {
|
|
|
|
detours[idx] = detour
|
|
|
|
}
|
|
|
|
return detours
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-10-06 17:11:08 -04:00
|
|
|
log.Error("Failed to read server config file (%s): %v", file, err)
|
2015-09-19 09:35:20 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-11 12:27:25 -04:00
|
|
|
jsonConfig := &Config{}
|
|
|
|
err = json.Unmarshal(rawConfig, jsonConfig)
|
2015-10-04 10:53:37 -04:00
|
|
|
if err != nil {
|
2015-10-06 17:11:08 -04:00
|
|
|
log.Error("Failed to load server config: %v", err)
|
2015-10-04 10:53:37 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-19 09:35:20 -04:00
|
|
|
|
2015-10-30 17:42:24 -04:00
|
|
|
jsonConfig.InboundConfigValue.Type = proxyconfig.TypeInbound
|
|
|
|
jsonConfig.OutboundConfigValue.Type = proxyconfig.TypeOutbound
|
2015-10-29 18:35:54 -04:00
|
|
|
|
2015-10-11 12:27:25 -04:00
|
|
|
return jsonConfig, err
|
2015-09-19 09:07:10 -04:00
|
|
|
}
|