v2ray-core/transport/internet/connection_json.go

68 lines
1.8 KiB
Go
Raw Normal View History

2016-06-14 20:54:08 +00:00
// +build json
package internet
import (
2016-07-10 13:27:58 +00:00
"crypto/tls"
2016-06-14 20:54:08 +00:00
"encoding/json"
2016-07-10 13:27:58 +00:00
"errors"
"strings"
2016-06-14 20:54:08 +00:00
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
2016-06-14 20:54:08 +00:00
)
2016-07-10 13:27:58 +00:00
func (this *TLSSettings) UnmarshalJSON(data []byte) error {
type JSONCertConfig struct {
2016-07-10 13:29:03 +00:00
CertFile string `json:"certificateFile"`
2016-07-10 13:27:58 +00:00
KeyFile string `json:"keyFile"`
}
type JSONConfig struct {
2016-07-10 13:30:48 +00:00
Insecure bool `json:"allowInsecure"`
Certs []*JSONCertConfig `json:"certificates"`
2016-07-10 13:27:58 +00: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 13:30:48 +00:00
this.AllowInsecure = jsonConfig.Insecure
2016-07-10 13:27:58 +00:00
return nil
}
2016-06-14 20:54:08 +00:00
func (this *StreamSettings) UnmarshalJSON(data []byte) error {
type JSONConfig struct {
2016-07-10 13:27:58 +00:00
Network v2net.NetworkList `json:"network"`
Security string `json:"security"`
TLSSettings *TLSSettings `json:"tlsSettings"`
2016-06-14 20:54:08 +00:00
}
this.Type = StreamConnectionTypeRawTCP
jsonConfig := new(JSONConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
2016-09-20 08:44:44 +00:00
if jsonConfig.Network.HasNetwork(v2net.Network_KCP) {
2016-06-14 20:54:08 +00:00
this.Type |= StreamConnectionTypeKCP
}
2016-09-20 08:44:44 +00:00
if jsonConfig.Network.HasNetwork(v2net.Network_WebSocket) {
2016-08-13 13:42:18 +00:00
this.Type |= StreamConnectionTypeWebSocket
}
2016-09-20 08:44:44 +00:00
if jsonConfig.Network.HasNetwork(v2net.Network_TCP) {
2016-06-14 20:54:08 +00:00
this.Type |= StreamConnectionTypeTCP
}
2016-07-10 13:27:58 +00:00
this.Security = StreamSecurityTypeNone
if strings.ToLower(jsonConfig.Security) == "tls" {
this.Security = StreamSecurityTypeTLS
}
if jsonConfig.TLSSettings != nil {
this.TLSSettings = jsonConfig.TLSSettings
}
2016-06-14 20:54:08 +00:00
return nil
}