1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 09:17:55 -04:00
v2fly/transport/internet/connection_json.go

45 lines
1.1 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
// +build json
package internet
import (
"encoding/json"
2016-07-10 09:27:58 -04:00
"strings"
2016-06-14 16:54:08 -04:00
2016-10-02 17:43:58 -04:00
"errors"
"github.com/golang/protobuf/ptypes"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
2016-10-02 17:43:58 -04:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-06-14 16:54:08 -04:00
)
2016-10-02 17:43:58 -04:00
func (this *StreamConfig) UnmarshalJSON(data []byte) error {
2016-07-10 09:27:58 -04:00
type JSONConfig struct {
2016-10-02 17:43:58 -04:00
Network *v2net.Network `json:"network"`
Security string `json:"security"`
TLSSettings *v2tls.Config `json:"tlsSettings"`
2016-07-10 09:27:58 -04:00
}
2016-10-02 17:43:58 -04:00
this.Network = v2net.Network_RawTCP
2016-07-10 09:27:58 -04:00
jsonConfig := new(JSONConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
2016-10-02 17:43:58 -04:00
if jsonConfig.Network != nil {
this.Network = *jsonConfig.Network
2016-06-14 16:54:08 -04:00
}
2016-10-02 17:43:58 -04:00
this.SecurityType = SecurityType_None
2016-07-10 09:27:58 -04:00
if strings.ToLower(jsonConfig.Security) == "tls" {
2016-10-02 17:43:58 -04:00
this.SecurityType = SecurityType_TLS
2016-07-10 09:27:58 -04:00
}
if jsonConfig.TLSSettings != nil {
2016-10-02 17:43:58 -04:00
anyTLSSettings, err := ptypes.MarshalAny(jsonConfig.TLSSettings)
if err != nil {
return errors.New("Internet: Failed to parse TLS settings: " + err.Error())
}
this.SecuritySettings = append(this.SecuritySettings, &SecuritySettings{
Type: SecurityType_TLS,
Settings: anyTLSSettings,
})
2016-07-10 09:27:58 -04:00
}
2016-06-14 16:54:08 -04:00
return nil
}