v2ray-core/proxy/http/config_json.go

68 lines
1.6 KiB
Go
Raw Normal View History

// +build json
package http
import (
2016-05-07 08:06:12 +00:00
"crypto/tls"
2016-01-26 10:28:09 +00:00
"encoding/json"
2016-06-11 20:52:37 +00:00
"errors"
2016-01-26 10:28:09 +00:00
2016-06-10 20:26:39 +00:00
"github.com/v2ray/v2ray-core/proxy/internal"
)
2016-05-29 14:46:31 +00:00
// UnmarshalJSON implements json.Unmarshaler
2016-05-07 08:06:12 +00: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 {
2016-06-11 20:52:37 +00:00
return errors.New("HTTP: Failed to parse certificate config: " + err.Error())
2016-05-07 08:06:12 +00:00
}
cert, err := tls.LoadX509KeyPair(jsonConfig.CertFile, jsonConfig.KeyFile)
if err != nil {
return err
}
this.Domain = jsonConfig.Domain
this.Certificate = cert
2016-05-07 08:06:56 +00:00
return nil
2016-05-07 08:06:12 +00:00
}
2016-05-29 14:46:31 +00:00
// UnmarshalJSON implements json.Unmarshaler
func (this *TLSConfig) UnmarshalJSON(data []byte) error {
2016-04-25 13:30:28 +00:00
type JsonConfig struct {
2016-05-07 08:06:12 +00:00
Enabled bool `json:"enable"`
Certs []*CertificateConfig `json:"certs"`
2016-04-25 13:30:28 +00:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("HTTP: Failed to parse TLS config: " + err.Error())
2016-04-25 13:30:28 +00:00
}
this.Enabled = jsonConfig.Enabled
2016-05-07 08:06:12 +00:00
this.Certs = jsonConfig.Certs
2016-04-25 13:30:28 +00:00
return nil
}
2016-05-29 14:46:31 +00:00
// UnmarshalJSON implements json.Unmarshaler
2016-01-26 10:28:09 +00:00
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-05-29 14:46:31 +00:00
Tls *TLSConfig `json:"tls"`
2016-01-26 10:28:09 +00:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("HTTP: Failed to parse config: " + err.Error())
2016-01-26 10:28:09 +00:00
}
2016-05-29 14:46:31 +00:00
this.TLSConfig = jsonConfig.Tls
2016-04-25 13:30:28 +00:00
2016-01-26 10:28:09 +00:00
return nil
}
func init() {
2016-06-10 20:26:39 +00:00
internal.RegisterInboundConfig("http", func() interface{} { return new(Config) })
}