1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 00:08:08 -04:00
v2fly/transport/internet/connection_json.go

65 lines
1.7 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
// +build json
package internet
import (
2016-07-10 09:27:58 -04:00
"crypto/tls"
2016-06-14 16:54:08 -04:00
"encoding/json"
2016-07-10 09:27:58 -04:00
"errors"
"strings"
2016-06-14 16:54:08 -04:00
v2net "github.com/v2ray/v2ray-core/common/net"
)
2016-07-10 09:27:58 -04:00
func (this *TLSSettings) UnmarshalJSON(data []byte) error {
type JSONCertConfig struct {
2016-07-10 09:29:03 -04:00
CertFile string `json:"certificateFile"`
2016-07-10 09:27:58 -04:00
KeyFile string `json:"keyFile"`
}
type JSONConfig struct {
2016-07-10 09:30:48 -04:00
Insecure bool `json:"allowInsecure"`
Certs []*JSONCertConfig `json:"certificates"`
2016-07-10 09:27:58 -04:00
}
jsonConfig := new(JSONConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Certs = make([]tls.Certificate, len(jsonConfig.Certs))
for idx, certConf := range jsonConfig.Certs {
cert, err := tls.LoadX509KeyPair(certConf.CertFile, certConf.KeyFile)
if err != nil {
return errors.New("Internet|TLS: Failed to load certificate file: " + err.Error())
}
this.Certs[idx] = cert
}
2016-07-10 09:30:48 -04:00
this.AllowInsecure = jsonConfig.Insecure
2016-07-10 09:27:58 -04:00
return nil
}
2016-06-14 16:54:08 -04:00
func (this *StreamSettings) UnmarshalJSON(data []byte) error {
type JSONConfig struct {
2016-07-10 09:27:58 -04:00
Network v2net.NetworkList `json:"network"`
Security string `json:"security"`
TLSSettings *TLSSettings `json:"tlsSettings"`
2016-06-14 16:54:08 -04:00
}
this.Type = StreamConnectionTypeRawTCP
jsonConfig := new(JSONConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
if jsonConfig.Network.HasNetwork(v2net.KCPNetwork) {
this.Type |= StreamConnectionTypeKCP
}
if jsonConfig.Network.HasNetwork(v2net.TCPNetwork) {
this.Type |= StreamConnectionTypeTCP
}
2016-07-10 09:27:58 -04:00
this.Security = StreamSecurityTypeNone
if strings.ToLower(jsonConfig.Security) == "tls" {
this.Security = StreamSecurityTypeTLS
}
if jsonConfig.TLSSettings != nil {
this.TLSSettings = jsonConfig.TLSSettings
}
2016-06-14 16:54:08 -04:00
return nil
}