1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/proxy/http/config_json.go

72 lines
1.5 KiB
Go
Raw Normal View History

// +build json
package http
import (
2016-05-07 04:06:12 -04:00
"crypto/tls"
2016-01-26 05:28:09 -05:00
"encoding/json"
"github.com/v2ray/v2ray-core/proxy/internal/config"
)
2016-05-29 10:46:31 -04:00
// UnmarshalJSON implements json.Unmarshaler
2016-05-07 04:06:12 -04:00
func (this *CertificateConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Domain string `json:"domain"`
CertFile string `json:"cert"`
KeyFile string `json:"key"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
cert, err := tls.LoadX509KeyPair(jsonConfig.CertFile, jsonConfig.KeyFile)
if err != nil {
return err
}
this.Domain = jsonConfig.Domain
this.Certificate = cert
2016-05-07 04:06:56 -04:00
return nil
2016-05-07 04:06:12 -04:00
}
2016-05-29 10:46:31 -04:00
// UnmarshalJSON implements json.Unmarshaler
func (this *TLSConfig) UnmarshalJSON(data []byte) error {
2016-04-25 09:30:28 -04:00
type JsonConfig struct {
2016-05-07 04:06:12 -04:00
Enabled bool `json:"enable"`
Certs []*CertificateConfig `json:"certs"`
2016-04-25 09:30:28 -04:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Enabled = jsonConfig.Enabled
2016-05-07 04:06:12 -04:00
this.Certs = jsonConfig.Certs
2016-04-25 09:30:28 -04:00
return nil
}
2016-05-29 10:46:31 -04:00
// UnmarshalJSON implements json.Unmarshaler
2016-01-26 05:28:09 -05:00
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-05-29 10:46:31 -04:00
Tls *TLSConfig `json:"tls"`
2016-01-26 05:28:09 -05:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
2016-05-29 10:46:31 -04:00
this.TLSConfig = jsonConfig.Tls
2016-04-25 09:30:28 -04:00
2016-01-26 05:28:09 -05:00
return nil
}
func init() {
config.RegisterInboundConfig("http",
func(data []byte) (interface{}, error) {
2016-01-26 05:28:09 -05:00
rawConfig := new(Config)
err := json.Unmarshal(data, rawConfig)
return rawConfig, err
})
}