2016-10-17 12:35:13 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2016-12-09 12:42:06 +00:00
|
|
|
|
2016-12-04 08:10:47 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-12-15 10:51:09 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-10-17 12:35:13 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/internet/kcp"
|
|
|
|
"v2ray.com/core/transport/internet/tcp"
|
|
|
|
"v2ray.com/core/transport/internet/tls"
|
2016-12-22 23:30:46 +00:00
|
|
|
"v2ray.com/core/transport/internet/websocket"
|
2016-10-17 12:35:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
kcpHeaderLoader = NewJSONConfigLoader(ConfigCreatorCache{
|
2017-01-03 21:46:22 +00:00
|
|
|
"none": func() interface{} { return new(NoOpAuthenticator) },
|
|
|
|
"srtp": func() interface{} { return new(SRTPAuthenticator) },
|
|
|
|
"utp": func() interface{} { return new(UTPAuthenticator) },
|
|
|
|
"wechat-video": func() interface{} { return new(WechatVideoAuthenticator) },
|
2016-10-17 12:35:13 +00:00
|
|
|
}, "type", "")
|
2016-11-04 09:49:18 +00:00
|
|
|
|
|
|
|
tcpHeaderLoader = NewJSONConfigLoader(ConfigCreatorCache{
|
2016-11-05 00:15:32 +00:00
|
|
|
"none": func() interface{} { return new(NoOpConnectionAuthenticator) },
|
2016-11-04 09:49:18 +00:00
|
|
|
"http": func() interface{} { return new(HTTPAuthenticator) },
|
|
|
|
}, "type", "")
|
2016-10-17 12:35:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type KCPConfig struct {
|
|
|
|
Mtu *uint32 `json:"mtu"`
|
|
|
|
Tti *uint32 `json:"tti"`
|
|
|
|
UpCap *uint32 `json:"uplinkCapacity"`
|
|
|
|
DownCap *uint32 `json:"downlinkCapacity"`
|
|
|
|
Congestion *bool `json:"congestion"`
|
|
|
|
ReadBufferSize *uint32 `json:"readBufferSize"`
|
|
|
|
WriteBufferSize *uint32 `json:"writeBufferSize"`
|
|
|
|
HeaderConfig json.RawMessage `json:"header"`
|
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
func (v *KCPConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-17 12:35:13 +00:00
|
|
|
config := new(kcp.Config)
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.Mtu != nil {
|
|
|
|
mtu := *v.Mtu
|
2016-10-17 12:35:13 +00:00
|
|
|
if mtu < 576 || mtu > 1460 {
|
2017-02-02 13:42:31 +00:00
|
|
|
return nil, errors.Format("Config: Invalid mKCP MTU size: %d", mtu)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
config.Mtu = &kcp.MTU{Value: mtu}
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.Tti != nil {
|
|
|
|
tti := *v.Tti
|
2016-10-17 12:35:13 +00:00
|
|
|
if tti < 10 || tti > 100 {
|
2017-02-02 13:42:31 +00:00
|
|
|
return nil, errors.Format("Config: Invalid mKCP TTI: %d", tti)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
config.Tti = &kcp.TTI{Value: tti}
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.UpCap != nil {
|
|
|
|
config.UplinkCapacity = &kcp.UplinkCapacity{Value: *v.UpCap}
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.DownCap != nil {
|
|
|
|
config.DownlinkCapacity = &kcp.DownlinkCapacity{Value: *v.DownCap}
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.Congestion != nil {
|
|
|
|
config.Congestion = *v.Congestion
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.ReadBufferSize != nil {
|
|
|
|
size := *v.ReadBufferSize
|
2016-10-17 12:35:13 +00:00
|
|
|
if size > 0 {
|
|
|
|
config.ReadBuffer = &kcp.ReadBuffer{Size: size * 1024 * 1024}
|
|
|
|
} else {
|
|
|
|
config.ReadBuffer = &kcp.ReadBuffer{Size: 512 * 1024}
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.WriteBufferSize != nil {
|
|
|
|
size := *v.WriteBufferSize
|
2016-10-17 12:35:13 +00:00
|
|
|
if size > 0 {
|
|
|
|
config.WriteBuffer = &kcp.WriteBuffer{Size: size * 1024 * 1024}
|
|
|
|
} else {
|
|
|
|
config.WriteBuffer = &kcp.WriteBuffer{Size: 512 * 1024}
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if len(v.HeaderConfig) > 0 {
|
|
|
|
headerConfig, _, err := kcpHeaderLoader.Load(v.HeaderConfig)
|
2016-10-17 12:35:13 +00:00
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Config: Invalid mKCP header config.").Base(err)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
ts, err := headerConfig.(Buildable).Build()
|
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Config: Invalid mKCP header config.").Base(err)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
config.HeaderConfig = ts
|
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TCPConfig struct {
|
2016-11-04 09:49:18 +00:00
|
|
|
ConnectionReuse *bool `json:"connectionReuse"`
|
|
|
|
HeaderConfig json.RawMessage `json:"header"`
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
func (v *TCPConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-17 12:35:13 +00:00
|
|
|
config := new(tcp.Config)
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.ConnectionReuse != nil {
|
2016-10-17 12:35:13 +00:00
|
|
|
config.ConnectionReuse = &tcp.ConnectionReuse{
|
2016-11-27 20:39:09 +00:00
|
|
|
Enable: *v.ConnectionReuse,
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if len(v.HeaderConfig) > 0 {
|
|
|
|
headerConfig, _, err := tcpHeaderLoader.Load(v.HeaderConfig)
|
2016-11-04 09:49:18 +00:00
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Config: Invalid TCP header config.").Base(err)
|
2016-11-04 09:49:18 +00:00
|
|
|
}
|
|
|
|
ts, err := headerConfig.(Buildable).Build()
|
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Config: Invalid TCP header config.").Base(err)
|
2016-11-04 09:49:18 +00:00
|
|
|
}
|
|
|
|
config.HeaderSettings = ts
|
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebSocketConfig struct {
|
|
|
|
ConnectionReuse *bool `json:"connectionReuse"`
|
|
|
|
Path string `json:"Path"`
|
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
func (v *WebSocketConfig) Build() (*serial.TypedMessage, error) {
|
2016-12-22 23:30:46 +00:00
|
|
|
config := &websocket.Config{
|
2016-11-27 20:39:09 +00:00
|
|
|
Path: v.Path,
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.ConnectionReuse != nil {
|
2016-12-22 23:30:46 +00:00
|
|
|
config.ConnectionReuse = &websocket.ConnectionReuse{
|
2016-11-27 20:39:09 +00:00
|
|
|
Enable: *v.ConnectionReuse,
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TLSCertConfig struct {
|
|
|
|
CertFile string `json:"certificateFile"`
|
|
|
|
KeyFile string `json:"keyFile"`
|
|
|
|
}
|
|
|
|
type TLSConfig struct {
|
2016-12-11 22:58:37 +00:00
|
|
|
Insecure bool `json:"allowInsecure"`
|
|
|
|
Certs []*TLSCertConfig `json:"certificates"`
|
|
|
|
ServerName string `json:"serverName"`
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
func (v *TLSConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-17 12:35:13 +00:00
|
|
|
config := new(tls.Config)
|
2016-11-27 20:39:09 +00:00
|
|
|
config.Certificate = make([]*tls.Certificate, len(v.Certs))
|
|
|
|
for idx, certConf := range v.Certs {
|
2016-10-17 12:35:13 +00:00
|
|
|
cert, err := ioutil.ReadFile(certConf.CertFile)
|
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Failed to load TLS certificate file: ", certConf.CertFile).Base(err)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
key, err := ioutil.ReadFile(certConf.KeyFile)
|
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Failed to load TLS key file: ", certConf.KeyFile).Base(err)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
config.Certificate[idx] = &tls.Certificate{
|
|
|
|
Key: key,
|
|
|
|
Certificate: cert,
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
config.AllowInsecure = v.Insecure
|
2016-12-11 22:58:37 +00:00
|
|
|
if len(v.ServerName) > 0 {
|
|
|
|
config.ServerName = v.ServerName
|
|
|
|
}
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 11:54:34 +00:00
|
|
|
type TransportProtocol string
|
|
|
|
|
|
|
|
func (p TransportProtocol) Build() (internet.TransportProtocol, error) {
|
|
|
|
switch strings.ToLower(string(p)) {
|
|
|
|
case "tcp":
|
|
|
|
return internet.TransportProtocol_TCP, nil
|
|
|
|
case "kcp", "mkcp":
|
|
|
|
return internet.TransportProtocol_MKCP, nil
|
|
|
|
case "ws", "websocket":
|
|
|
|
return internet.TransportProtocol_WebSocket, nil
|
|
|
|
default:
|
|
|
|
return internet.TransportProtocol_TCP, errors.New("Config: unknown transport protocol: ", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:35:13 +00:00
|
|
|
type StreamConfig struct {
|
2017-01-12 11:54:34 +00:00
|
|
|
Network *TransportProtocol `json:"network"`
|
|
|
|
Security string `json:"security"`
|
|
|
|
TLSSettings *TLSConfig `json:"tlsSettings"`
|
|
|
|
TCPSettings *TCPConfig `json:"tcpSettings"`
|
|
|
|
KCPSettings *KCPConfig `json:"kcpSettings"`
|
|
|
|
WSSettings *WebSocketConfig `json:"wsSettings"`
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *StreamConfig) Build() (*internet.StreamConfig, error) {
|
2016-10-17 12:35:13 +00:00
|
|
|
config := &internet.StreamConfig{
|
2017-01-12 11:54:34 +00:00
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.Network != nil {
|
2017-01-12 11:54:34 +00:00
|
|
|
protocol, err := (*v.Network).Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Protocol = protocol
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if strings.ToLower(v.Security) == "tls" {
|
|
|
|
tlsSettings := v.TLSSettings
|
2016-10-17 12:35:13 +00:00
|
|
|
if tlsSettings == nil {
|
|
|
|
tlsSettings = &TLSConfig{}
|
|
|
|
}
|
|
|
|
ts, err := tlsSettings.Build()
|
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Failed to build TLS config.").Base(err)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
config.SecuritySettings = append(config.SecuritySettings, ts)
|
2016-12-09 12:42:06 +00:00
|
|
|
config.SecurityType = ts.Type
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.TCPSettings != nil {
|
|
|
|
ts, err := v.TCPSettings.Build()
|
2016-10-18 19:57:40 +00:00
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Failed to build TCP config.").Base(err)
|
2016-10-18 19:57:40 +00:00
|
|
|
}
|
2017-01-16 13:18:33 +00:00
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
2017-01-12 11:54:34 +00:00
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
2016-10-18 19:57:40 +00:00
|
|
|
Settings: ts,
|
|
|
|
})
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.KCPSettings != nil {
|
|
|
|
ts, err := v.KCPSettings.Build()
|
2016-10-18 19:57:40 +00:00
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Failed to build mKCP config.").Base(err)
|
2016-10-18 19:57:40 +00:00
|
|
|
}
|
2017-01-16 13:18:33 +00:00
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
2017-01-12 11:54:34 +00:00
|
|
|
Protocol: internet.TransportProtocol_MKCP,
|
2016-10-18 19:57:40 +00:00
|
|
|
Settings: ts,
|
|
|
|
})
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.WSSettings != nil {
|
|
|
|
ts, err := v.WSSettings.Build()
|
2016-10-18 19:57:40 +00:00
|
|
|
if err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
return nil, errors.New("Failed to build WebSocket config.").Base(err)
|
2016-10-18 19:57:40 +00:00
|
|
|
}
|
2017-01-16 13:18:33 +00:00
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
2017-01-12 11:54:34 +00:00
|
|
|
Protocol: internet.TransportProtocol_WebSocket,
|
2016-10-18 19:57:40 +00:00
|
|
|
Settings: ts,
|
|
|
|
})
|
|
|
|
}
|
2016-10-17 12:35:13 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
2016-11-10 22:41:28 +00:00
|
|
|
|
|
|
|
type ProxyConfig struct {
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *ProxyConfig) Build() (*internet.ProxyConfig, error) {
|
|
|
|
if len(v.Tag) == 0 {
|
2016-11-10 22:41:28 +00:00
|
|
|
return nil, errors.New("Proxy tag is not set.")
|
|
|
|
}
|
|
|
|
return &internet.ProxyConfig{
|
2016-11-27 20:39:09 +00:00
|
|
|
Tag: v.Tag,
|
2016-11-10 22:41:28 +00:00
|
|
|
}, nil
|
|
|
|
}
|