mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-03 15:55:20 -05:00
tls config
This commit is contained in:
@@ -1,42 +1,7 @@
|
||||
package http
|
||||
|
||||
import "crypto/tls"
|
||||
|
||||
// CertificateConfig is the config for TLS certificates used in HTTP proxy.
|
||||
type CertificateConfig struct {
|
||||
Domain string
|
||||
Certificate tls.Certificate
|
||||
}
|
||||
|
||||
// TlsConfig is the config for TLS connections.
|
||||
type TLSConfig struct {
|
||||
Enabled bool
|
||||
Certs []*CertificateConfig
|
||||
}
|
||||
|
||||
// GetConfig returns corresponding tls.Config.
|
||||
func (this *TLSConfig) GetConfig() *tls.Config {
|
||||
if !this.Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
config := &tls.Config{
|
||||
InsecureSkipVerify: false,
|
||||
}
|
||||
|
||||
config.Certificates = make([]tls.Certificate, len(this.Certs))
|
||||
for index, cert := range this.Certs {
|
||||
config.Certificates[index] = cert.Certificate
|
||||
}
|
||||
|
||||
config.BuildNameToCertificate()
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// Config for HTTP proxy server.
|
||||
type Config struct {
|
||||
TLSConfig *TLSConfig
|
||||
}
|
||||
|
||||
// ClientConfig for HTTP proxy client.
|
||||
|
||||
@@ -3,62 +3,21 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/v2ray/v2ray-core/proxy/internal"
|
||||
)
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler
|
||||
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 errors.New("HTTP: Failed to parse certificate config: " + err.Error())
|
||||
}
|
||||
|
||||
cert, err := tls.LoadX509KeyPair(jsonConfig.CertFile, jsonConfig.KeyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.Domain = jsonConfig.Domain
|
||||
this.Certificate = cert
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler
|
||||
func (this *TLSConfig) UnmarshalJSON(data []byte) error {
|
||||
type JsonConfig struct {
|
||||
Enabled bool `json:"enable"`
|
||||
Certs []*CertificateConfig `json:"certs"`
|
||||
}
|
||||
jsonConfig := new(JsonConfig)
|
||||
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
||||
return errors.New("HTTP: Failed to parse TLS config: " + err.Error())
|
||||
}
|
||||
|
||||
this.Enabled = jsonConfig.Enabled
|
||||
this.Certs = jsonConfig.Certs
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler
|
||||
func (this *Config) UnmarshalJSON(data []byte) error {
|
||||
type JsonConfig struct {
|
||||
Tls *TLSConfig `json:"tls"`
|
||||
}
|
||||
jsonConfig := new(JsonConfig)
|
||||
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
||||
return errors.New("HTTP: Failed to parse config: " + err.Error())
|
||||
}
|
||||
|
||||
this.TLSConfig = jsonConfig.Tls
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user