config_json.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // +build json
  2. package point
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "io/ioutil"
  7. "os"
  8. "strings"
  9. "github.com/v2ray/v2ray-core/app/dns"
  10. "github.com/v2ray/v2ray-core/app/router"
  11. "github.com/v2ray/v2ray-core/common/log"
  12. v2net "github.com/v2ray/v2ray-core/common/net"
  13. "github.com/v2ray/v2ray-core/transport"
  14. )
  15. const (
  16. DefaultRefreshMinute = int(9999)
  17. )
  18. func (this *Config) UnmarshalJSON(data []byte) error {
  19. type JsonConfig struct {
  20. Port v2net.Port `json:"port"` // Port of this Point server.
  21. LogConfig *LogConfig `json:"log"`
  22. RouterConfig *router.Config `json:"routing"`
  23. DNSConfig *dns.Config `json:"dns"`
  24. InboundConfig *InboundConnectionConfig `json:"inbound"`
  25. OutboundConfig *OutboundConnectionConfig `json:"outbound"`
  26. InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
  27. OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
  28. Transport *transport.Config `json:"transport"`
  29. }
  30. jsonConfig := new(JsonConfig)
  31. if err := json.Unmarshal(data, jsonConfig); err != nil {
  32. return errors.New("Point: Failed to parse config: " + err.Error())
  33. }
  34. this.Port = jsonConfig.Port
  35. this.LogConfig = jsonConfig.LogConfig
  36. this.RouterConfig = jsonConfig.RouterConfig
  37. this.InboundConfig = jsonConfig.InboundConfig
  38. this.OutboundConfig = jsonConfig.OutboundConfig
  39. this.InboundDetours = jsonConfig.InboundDetours
  40. this.OutboundDetours = jsonConfig.OutboundDetours
  41. if jsonConfig.DNSConfig == nil {
  42. jsonConfig.DNSConfig = &dns.Config{
  43. NameServers: []v2net.Destination{
  44. v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
  45. },
  46. }
  47. }
  48. this.DNSConfig = jsonConfig.DNSConfig
  49. this.TransportConfig = jsonConfig.Transport
  50. return nil
  51. }
  52. func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
  53. type JsonConfig struct {
  54. Port uint16 `json:"port"`
  55. Listen *v2net.AddressJson `json:"listen"`
  56. Protocol string `json:"protocol"`
  57. Settings json.RawMessage `json:"settings"`
  58. }
  59. jsonConfig := new(JsonConfig)
  60. if err := json.Unmarshal(data, jsonConfig); err != nil {
  61. return errors.New("Point: Failed to parse inbound config: " + err.Error())
  62. }
  63. this.Port = v2net.Port(jsonConfig.Port)
  64. this.ListenOn = v2net.AnyIP
  65. if jsonConfig.Listen != nil {
  66. if jsonConfig.Listen.Address.IsDomain() {
  67. return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.Address.Domain())
  68. }
  69. this.ListenOn = jsonConfig.Listen.Address
  70. }
  71. this.Protocol = jsonConfig.Protocol
  72. this.Settings = jsonConfig.Settings
  73. return nil
  74. }
  75. func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
  76. type JsonConnectionConfig struct {
  77. Protocol string `json:"protocol"`
  78. SendThrough *v2net.AddressJson `json:"sendThrough"`
  79. Settings json.RawMessage `json:"settings"`
  80. }
  81. jsonConfig := new(JsonConnectionConfig)
  82. if err := json.Unmarshal(data, jsonConfig); err != nil {
  83. return errors.New("Point: Failed to parse outbound config: " + err.Error())
  84. }
  85. this.Protocol = jsonConfig.Protocol
  86. this.Settings = jsonConfig.Settings
  87. if jsonConfig.SendThrough != nil {
  88. address := jsonConfig.SendThrough.Address
  89. if address.IsDomain() {
  90. return errors.New("Point: Unable to send through: " + address.String())
  91. }
  92. this.SendThrough = address
  93. }
  94. return nil
  95. }
  96. func (this *LogConfig) UnmarshalJSON(data []byte) error {
  97. type JsonLogConfig struct {
  98. AccessLog string `json:"access"`
  99. ErrorLog string `json:"error"`
  100. LogLevel string `json:"loglevel"`
  101. }
  102. jsonConfig := new(JsonLogConfig)
  103. if err := json.Unmarshal(data, jsonConfig); err != nil {
  104. return errors.New("Point: Failed to parse log config: " + err.Error())
  105. }
  106. this.AccessLog = jsonConfig.AccessLog
  107. this.ErrorLog = jsonConfig.ErrorLog
  108. level := strings.ToLower(jsonConfig.LogLevel)
  109. switch level {
  110. case "debug":
  111. this.LogLevel = log.DebugLevel
  112. case "info":
  113. this.LogLevel = log.InfoLevel
  114. case "error":
  115. this.LogLevel = log.ErrorLevel
  116. case "none":
  117. this.LogLevel = log.NoneLevel
  118. default:
  119. this.LogLevel = log.WarningLevel
  120. }
  121. return nil
  122. }
  123. func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
  124. type JsonInboundDetourAllocationConfig struct {
  125. Strategy string `json:"strategy"`
  126. Concurrency int `json:"concurrency"`
  127. RefreshMin int `json:"refresh"`
  128. }
  129. jsonConfig := new(JsonInboundDetourAllocationConfig)
  130. if err := json.Unmarshal(data, jsonConfig); err != nil {
  131. return errors.New("Point: Failed to parse inbound detour allocation config: " + err.Error())
  132. }
  133. this.Strategy = jsonConfig.Strategy
  134. this.Concurrency = jsonConfig.Concurrency
  135. this.Refresh = jsonConfig.RefreshMin
  136. if this.Strategy == AllocationStrategyRandom {
  137. if this.Refresh == 0 {
  138. this.Refresh = 5
  139. }
  140. if this.Concurrency == 0 {
  141. this.Concurrency = 3
  142. }
  143. }
  144. if this.Refresh == 0 {
  145. this.Refresh = DefaultRefreshMinute
  146. }
  147. return nil
  148. }
  149. func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
  150. type JsonInboundDetourConfig struct {
  151. Protocol string `json:"protocol"`
  152. PortRange *v2net.PortRange `json:"port"`
  153. ListenOn *v2net.AddressJson `json:"listen"`
  154. Settings json.RawMessage `json:"settings"`
  155. Tag string `json:"tag"`
  156. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  157. }
  158. jsonConfig := new(JsonInboundDetourConfig)
  159. if err := json.Unmarshal(data, jsonConfig); err != nil {
  160. return errors.New("Point: Failed to parse inbound detour config: " + err.Error())
  161. }
  162. if jsonConfig.PortRange == nil {
  163. log.Error("Point: Port range not specified in InboundDetour.")
  164. return ErrorBadConfiguration
  165. }
  166. this.ListenOn = v2net.AnyIP
  167. if jsonConfig.ListenOn != nil {
  168. if jsonConfig.ListenOn.Address.IsDomain() {
  169. return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.Address.Domain())
  170. }
  171. this.ListenOn = jsonConfig.ListenOn.Address
  172. }
  173. this.Protocol = jsonConfig.Protocol
  174. this.PortRange = *jsonConfig.PortRange
  175. this.Settings = jsonConfig.Settings
  176. this.Tag = jsonConfig.Tag
  177. this.Allocation = jsonConfig.Allocation
  178. if this.Allocation == nil {
  179. this.Allocation = &InboundDetourAllocationConfig{
  180. Strategy: AllocationStrategyAlways,
  181. Refresh: DefaultRefreshMinute,
  182. }
  183. }
  184. return nil
  185. }
  186. func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
  187. type JsonOutboundDetourConfig struct {
  188. Protocol string `json:"protocol"`
  189. SendThrough *v2net.AddressJson `json:"sendThrough"`
  190. Tag string `json:"tag"`
  191. Settings json.RawMessage `json:"settings"`
  192. }
  193. jsonConfig := new(JsonOutboundDetourConfig)
  194. if err := json.Unmarshal(data, jsonConfig); err != nil {
  195. return errors.New("Point: Failed to parse outbound detour config: " + err.Error())
  196. }
  197. this.Protocol = jsonConfig.Protocol
  198. this.Tag = jsonConfig.Tag
  199. this.Settings = jsonConfig.Settings
  200. if jsonConfig.SendThrough != nil {
  201. address := jsonConfig.SendThrough.Address
  202. if address.IsDomain() {
  203. return errors.New("Point: Unable to send through: " + address.String())
  204. }
  205. this.SendThrough = address
  206. }
  207. return nil
  208. }
  209. func JsonLoadConfig(file string) (*Config, error) {
  210. fixedFile := os.ExpandEnv(file)
  211. rawConfig, err := ioutil.ReadFile(fixedFile)
  212. if err != nil {
  213. log.Error("Failed to read server config file (", file, "): ", file, err)
  214. return nil, err
  215. }
  216. jsonConfig := &Config{}
  217. err = json.Unmarshal(rawConfig, jsonConfig)
  218. if err != nil {
  219. log.Error("Failed to load server config: ", err)
  220. return nil, err
  221. }
  222. return jsonConfig, err
  223. }
  224. func init() {
  225. configLoader = JsonLoadConfig
  226. }