From 301b0ccff7ed3043a04ffda9e6cc3593b6aa126d Mon Sep 17 00:00:00 2001 From: v2ray Date: Sat, 7 May 2016 10:06:12 +0200 Subject: [PATCH] refine cert config in http --- proxy/http/config.go | 12 +++++++++--- proxy/http/config_json.go | 28 +++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/proxy/http/config.go b/proxy/http/config.go index 3a07dd367..839d3d67e 100644 --- a/proxy/http/config.go +++ b/proxy/http/config.go @@ -1,13 +1,19 @@ package http import ( + "crypto/tls" + v2net "github.com/v2ray/v2ray-core/common/net" ) +type CertificateConfig struct { + Domain string + Certificate tls.Certificate +} + type TlsConfig struct { - Enabled bool - CertFile string - KeyFile string + Enabled bool + Certs []*CertificateConfig } type Config struct { diff --git a/proxy/http/config_json.go b/proxy/http/config_json.go index 9fbd51789..ba83acda4 100644 --- a/proxy/http/config_json.go +++ b/proxy/http/config_json.go @@ -3,17 +3,36 @@ package http import ( + "crypto/tls" "encoding/json" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy/internal/config" ) +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 +} + func (this *TlsConfig) UnmarshalJSON(data []byte) error { type JsonConfig struct { - Enabled bool - CertFile string - KeyFile string + Enabled bool `json:"enable"` + Certs []*CertificateConfig `json:"certs"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { @@ -21,8 +40,7 @@ func (this *TlsConfig) UnmarshalJSON(data []byte) error { } this.Enabled = jsonConfig.Enabled - this.CertFile = jsonConfig.CertFile - this.KeyFile = jsonConfig.KeyFile + this.Certs = jsonConfig.Certs return nil }