v2ray-core/tools/conf/transport_authenticators.go

194 lines
4.6 KiB
Go
Raw Normal View History

2016-10-17 12:35:13 +00:00
package conf
import (
2016-12-15 10:51:09 +00:00
"v2ray.com/core/common/serial"
2016-12-08 15:27:41 +00:00
"v2ray.com/core/transport/internet/headers/http"
"v2ray.com/core/transport/internet/headers/noop"
"v2ray.com/core/transport/internet/headers/srtp"
"v2ray.com/core/transport/internet/headers/utp"
2017-01-03 21:46:22 +00:00
"v2ray.com/core/transport/internet/headers/wechat"
2016-10-17 12:35:13 +00:00
)
type NoOpAuthenticator struct{}
2016-12-15 10:51:09 +00:00
func (NoOpAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(noop.Config)), nil
2016-10-17 12:35:13 +00:00
}
2016-11-05 00:15:32 +00:00
type NoOpConnectionAuthenticator struct{}
2016-12-15 10:51:09 +00:00
func (NoOpConnectionAuthenticator) Build() (*serial.TypedMessage, error) {
2017-01-16 13:17:59 +00:00
return serial.ToTypedMessage(new(noop.ConnectionConfig)), nil
2016-11-05 00:15:32 +00:00
}
2016-10-17 12:35:13 +00:00
type SRTPAuthenticator struct{}
2016-12-15 10:51:09 +00:00
func (SRTPAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(srtp.Config)), nil
2016-10-17 12:35:13 +00:00
}
type UTPAuthenticator struct{}
2016-12-15 10:51:09 +00:00
func (UTPAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(utp.Config)), nil
2016-10-17 12:35:13 +00:00
}
2016-11-04 09:49:18 +00:00
2017-01-03 21:46:22 +00:00
type WechatVideoAuthenticator struct{}
func (WechatVideoAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(wechat.VideoConfig)), nil
}
2016-11-04 09:49:18 +00:00
type HTTPAuthenticatorRequest struct {
2016-11-06 14:48:25 +00:00
Version string `json:"version"`
Method string `json:"method"`
Path StringList `json:"path"`
2016-11-05 00:28:15 +00:00
Headers map[string]*StringList `json:"headers"`
2016-11-04 09:49:18 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *HTTPAuthenticatorRequest) Build() (*http.RequestConfig, error) {
2016-11-04 09:49:18 +00:00
config := &http.RequestConfig{
Uri: []string{"/"},
Header: []*http.Header{
{
Name: "Host",
Value: []string{"www.baidu.com", "www.bing.com"},
},
2016-11-05 23:42:54 +00:00
{
Name: "User-Agent",
Value: []string{
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.109 Mobile/14A456 Safari/601.1.46",
},
},
2016-11-06 12:38:32 +00:00
{
Name: "Accept-Encoding",
Value: []string{"gzip, deflate"},
},
2016-11-06 12:55:06 +00:00
{
Name: "Connection",
Value: []string{"keep-alive"},
},
{
Name: "Pragma",
Value: []string{"no-cache"},
},
2016-11-04 09:49:18 +00:00
},
}
2016-11-27 20:39:09 +00:00
if len(v.Version) > 0 {
config.Version = &http.Version{Value: v.Version}
2016-11-04 09:49:18 +00:00
}
2016-11-27 20:39:09 +00:00
if len(v.Method) > 0 {
config.Method = &http.Method{Value: v.Method}
2016-11-04 09:49:18 +00:00
}
2016-11-27 20:39:09 +00:00
if len(v.Path) > 0 {
config.Uri = append([]string(nil), (v.Path)...)
2016-11-04 09:49:18 +00:00
}
2016-11-27 20:39:09 +00:00
if len(v.Headers) > 0 {
config.Header = make([]*http.Header, 0, len(v.Headers))
for key, value := range v.Headers {
2016-11-06 14:48:25 +00:00
if value == nil {
2017-04-09 13:04:04 +00:00
return nil, newError("empty HTTP header value: " + key).AtError()
2016-11-06 14:48:25 +00:00
}
2016-11-05 00:28:15 +00:00
config.Header = append(config.Header, &http.Header{
Name: key,
Value: append([]string(nil), (*value)...),
})
2016-11-04 09:49:18 +00:00
}
}
return config, nil
}
type HTTPAuthenticatorResponse struct {
2016-11-06 14:48:25 +00:00
Version string `json:"version"`
Status string `json:"status"`
Reason string `json:"reason"`
2016-11-05 00:28:15 +00:00
Headers map[string]*StringList `json:"headers"`
2016-11-04 09:49:18 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
2016-11-04 09:49:18 +00:00
config := &http.ResponseConfig{
Header: []*http.Header{
{
Name: "Content-Type",
Value: []string{"application/octet-stream", "video/mpeg"},
},
{
Name: "Transfer-Encoding",
Value: []string{"chunked"},
},
2016-11-06 12:55:06 +00:00
{
Name: "Connection",
Value: []string{"keep-alive"},
},
{
Name: "Pragma",
Value: []string{"no-cache"},
},
2016-12-14 11:25:22 +00:00
{
Name: "Cache-Control",
Value: []string{"private", "no-cache"},
},
2016-11-04 09:49:18 +00:00
},
}
2016-11-27 20:39:09 +00:00
if len(v.Version) > 0 {
config.Version = &http.Version{Value: v.Version}
2016-11-04 09:49:18 +00:00
}
2016-11-27 20:39:09 +00:00
if len(v.Status) > 0 || len(v.Reason) > 0 {
2016-11-04 09:49:43 +00:00
config.Status = &http.Status{
2016-11-04 09:49:18 +00:00
Code: "200",
Reason: "OK",
}
2016-11-27 20:39:09 +00:00
if len(v.Status) > 0 {
config.Status.Code = v.Status
2016-11-04 09:49:18 +00:00
}
2016-11-27 20:39:09 +00:00
if len(v.Reason) > 0 {
config.Status.Reason = v.Reason
2016-11-04 09:49:18 +00:00
}
}
2016-11-27 20:39:09 +00:00
if len(v.Headers) > 0 {
config.Header = make([]*http.Header, 0, len(v.Headers))
for key, value := range v.Headers {
2016-11-06 14:48:25 +00:00
if value == nil {
2017-04-09 13:04:04 +00:00
return nil, newError("empty HTTP header value: " + key).AtError()
2016-11-06 14:48:25 +00:00
}
2016-11-05 00:28:15 +00:00
config.Header = append(config.Header, &http.Header{
Name: key,
Value: append([]string(nil), (*value)...),
})
2016-11-04 09:49:18 +00:00
}
}
return config, nil
}
type HTTPAuthenticator struct {
2016-11-06 14:48:25 +00:00
Request HTTPAuthenticatorRequest `json:"request"`
Response HTTPAuthenticatorResponse `json:"response"`
2016-11-04 09:49:18 +00:00
}
2016-12-15 10:51:09 +00:00
func (v *HTTPAuthenticator) Build() (*serial.TypedMessage, error) {
2016-11-04 09:49:18 +00:00
config := new(http.Config)
2016-11-27 20:39:09 +00:00
requestConfig, err := v.Request.Build()
2016-11-05 14:14:55 +00:00
if err != nil {
return nil, err
}
config.Request = requestConfig
2016-11-04 09:49:18 +00:00
2016-11-27 20:39:09 +00:00
responseConfig, err := v.Response.Build()
2016-11-06 14:48:25 +00:00
if err != nil {
return nil, err
2016-11-04 09:49:18 +00:00
}
2016-11-06 14:48:25 +00:00
config.Response = responseConfig
2016-11-04 09:49:18 +00:00
2016-12-15 10:51:09 +00:00
return serial.ToTypedMessage(config), nil
2016-11-04 09:49:18 +00:00
}