1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/tools/conf/transport_authenticators.go

195 lines
4.7 KiB
Go
Raw Normal View History

2016-10-17 08:35:13 -04:00
package conf
import (
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-12-15 05:51:09 -05:00
"v2ray.com/core/common/serial"
2016-12-08 10:27:41 -05: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 16:46:22 -05:00
"v2ray.com/core/transport/internet/headers/wechat"
2016-10-17 08:35:13 -04:00
)
type NoOpAuthenticator struct{}
2016-12-15 05:51:09 -05:00
func (NoOpAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(noop.Config)), nil
2016-10-17 08:35:13 -04:00
}
2016-11-04 20:15:32 -04:00
type NoOpConnectionAuthenticator struct{}
2016-12-15 05:51:09 -05:00
func (NoOpConnectionAuthenticator) Build() (*serial.TypedMessage, error) {
2017-01-16 08:17:59 -05:00
return serial.ToTypedMessage(new(noop.ConnectionConfig)), nil
2016-11-04 20:15:32 -04:00
}
2016-10-17 08:35:13 -04:00
type SRTPAuthenticator struct{}
2016-12-15 05:51:09 -05:00
func (SRTPAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(srtp.Config)), nil
2016-10-17 08:35:13 -04:00
}
type UTPAuthenticator struct{}
2016-12-15 05:51:09 -05:00
func (UTPAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(utp.Config)), nil
2016-10-17 08:35:13 -04:00
}
2016-11-04 05:49:18 -04:00
2017-01-03 16:46:22 -05:00
type WechatVideoAuthenticator struct{}
func (WechatVideoAuthenticator) Build() (*serial.TypedMessage, error) {
return serial.ToTypedMessage(new(wechat.VideoConfig)), nil
}
2016-11-04 05:49:18 -04:00
type HTTPAuthenticatorRequest struct {
2016-11-06 09:48:25 -05:00
Version string `json:"version"`
Method string `json:"method"`
Path StringList `json:"path"`
2016-11-04 20:28:15 -04:00
Headers map[string]*StringList `json:"headers"`
2016-11-04 05:49:18 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *HTTPAuthenticatorRequest) Build() (*http.RequestConfig, error) {
2016-11-04 05:49:18 -04:00
config := &http.RequestConfig{
Uri: []string{"/"},
Header: []*http.Header{
{
Name: "Host",
Value: []string{"www.baidu.com", "www.bing.com"},
},
2016-11-05 19:42:54 -04: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 07:38:32 -05:00
{
Name: "Accept-Encoding",
Value: []string{"gzip, deflate"},
},
2016-11-06 07:55:06 -05:00
{
Name: "Connection",
Value: []string{"keep-alive"},
},
{
Name: "Pragma",
Value: []string{"no-cache"},
},
2016-11-04 05:49:18 -04:00
},
}
2016-11-27 15:39:09 -05:00
if len(v.Version) > 0 {
config.Version = &http.Version{Value: v.Version}
2016-11-04 05:49:18 -04:00
}
2016-11-27 15:39:09 -05:00
if len(v.Method) > 0 {
config.Method = &http.Method{Value: v.Method}
2016-11-04 05:49:18 -04:00
}
2016-11-27 15:39:09 -05:00
if len(v.Path) > 0 {
config.Uri = append([]string(nil), (v.Path)...)
2016-11-04 05:49:18 -04:00
}
2016-11-27 15:39:09 -05:00
if len(v.Headers) > 0 {
config.Header = make([]*http.Header, 0, len(v.Headers))
for key, value := range v.Headers {
2016-11-06 09:48:25 -05:00
if value == nil {
2017-02-02 08:42:31 -05:00
return nil, errors.New("Config: Empty HTTP header value: " + key)
2016-11-06 09:48:25 -05:00
}
2016-11-04 20:28:15 -04:00
config.Header = append(config.Header, &http.Header{
Name: key,
Value: append([]string(nil), (*value)...),
})
2016-11-04 05:49:18 -04:00
}
}
return config, nil
}
type HTTPAuthenticatorResponse struct {
2016-11-06 09:48:25 -05:00
Version string `json:"version"`
Status string `json:"status"`
Reason string `json:"reason"`
2016-11-04 20:28:15 -04:00
Headers map[string]*StringList `json:"headers"`
2016-11-04 05:49:18 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
2016-11-04 05:49:18 -04: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 07:55:06 -05:00
{
Name: "Connection",
Value: []string{"keep-alive"},
},
{
Name: "Pragma",
Value: []string{"no-cache"},
},
2016-12-14 06:25:22 -05:00
{
Name: "Cache-Control",
Value: []string{"private", "no-cache"},
},
2016-11-04 05:49:18 -04:00
},
}
2016-11-27 15:39:09 -05:00
if len(v.Version) > 0 {
config.Version = &http.Version{Value: v.Version}
2016-11-04 05:49:18 -04:00
}
2016-11-27 15:39:09 -05:00
if len(v.Status) > 0 || len(v.Reason) > 0 {
2016-11-04 05:49:43 -04:00
config.Status = &http.Status{
2016-11-04 05:49:18 -04:00
Code: "200",
Reason: "OK",
}
2016-11-27 15:39:09 -05:00
if len(v.Status) > 0 {
config.Status.Code = v.Status
2016-11-04 05:49:18 -04:00
}
2016-11-27 15:39:09 -05:00
if len(v.Reason) > 0 {
config.Status.Reason = v.Reason
2016-11-04 05:49:18 -04:00
}
}
2016-11-27 15:39:09 -05:00
if len(v.Headers) > 0 {
config.Header = make([]*http.Header, 0, len(v.Headers))
for key, value := range v.Headers {
2016-11-06 09:48:25 -05:00
if value == nil {
2017-02-02 08:42:31 -05:00
return nil, errors.New("Config: Empty HTTP header value: " + key)
2016-11-06 09:48:25 -05:00
}
2016-11-04 20:28:15 -04:00
config.Header = append(config.Header, &http.Header{
Name: key,
Value: append([]string(nil), (*value)...),
})
2016-11-04 05:49:18 -04:00
}
}
return config, nil
}
type HTTPAuthenticator struct {
2016-11-06 09:48:25 -05:00
Request HTTPAuthenticatorRequest `json:"request"`
Response HTTPAuthenticatorResponse `json:"response"`
2016-11-04 05:49:18 -04:00
}
2016-12-15 05:51:09 -05:00
func (v *HTTPAuthenticator) Build() (*serial.TypedMessage, error) {
2016-11-04 05:49:18 -04:00
config := new(http.Config)
2016-11-27 15:39:09 -05:00
requestConfig, err := v.Request.Build()
2016-11-05 10:14:55 -04:00
if err != nil {
return nil, err
}
config.Request = requestConfig
2016-11-04 05:49:18 -04:00
2016-11-27 15:39:09 -05:00
responseConfig, err := v.Response.Build()
2016-11-06 09:48:25 -05:00
if err != nil {
return nil, err
2016-11-04 05:49:18 -04:00
}
2016-11-06 09:48:25 -05:00
config.Response = responseConfig
2016-11-04 05:49:18 -04:00
2016-12-15 05:51:09 -05:00
return serial.ToTypedMessage(config), nil
2016-11-04 05:49:18 -04:00
}