1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-04-19 01:12:23 -04:00

simplify tls config

This commit is contained in:
Darien Raymond 2017-12-17 00:53:17 +01:00
parent 9561301fea
commit 048ffbc7dc
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
7 changed files with 44 additions and 48 deletions

View File

@ -77,17 +77,10 @@ func DialKCP(ctx context.Context, dest net.Destination) (internet.Connection, er
var iConn internet.Connection = session var iConn internet.Connection = session
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil { if config := v2tls.ConfigFromContext(ctx, v2tls.WithDestination(dest)); config != nil {
switch securitySettings := securitySettings.(type) { tlsConn := tls.Client(iConn, config.GetTLSConfig())
case *v2tls.Config:
if dest.Address.Family().IsDomain() {
securitySettings.OverrideServerNameIfEmpty(dest.Address.Domain())
}
config := securitySettings.GetTLSConfig()
tlsConn := tls.Client(iConn, config)
iConn = tlsConn iConn = tlsConn
} }
}
return iConn, nil return iConn, nil
} }

View File

@ -59,13 +59,11 @@ func NewListener(ctx context.Context, address net.Address, port net.Port, addCon
config: kcpSettings, config: kcpSettings,
addConn: addConn, addConn: addConn,
} }
securitySettings := internet.SecuritySettingsFromContext(ctx)
if securitySettings != nil { if config := v2tls.ConfigFromContext(ctx); config != nil {
switch securitySettings := securitySettings.(type) { l.tlsConfig = config.GetTLSConfig()
case *v2tls.Config:
l.tlsConfig = securitySettings.GetTLSConfig()
}
} }
hub, err := udp.ListenUDP(address, port, udp.ListenOption{Callback: l.OnReceive, Concurrency: 2}) hub, err := udp.ListenUDP(address, port, udp.ListenOption{Callback: l.OnReceive, Concurrency: 2})
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -19,22 +19,16 @@ func getTCPSettingsFromContext(ctx context.Context) *Config {
} }
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) { func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
log.Trace(newError("dailing TCP to ", dest)) log.Trace(newError("dialing TCP to ", dest))
src := internet.DialerSourceFromContext(ctx) src := internet.DialerSourceFromContext(ctx)
conn, err := internet.DialSystem(ctx, src, dest) conn, err := internet.DialSystem(ctx, src, dest)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
tlsConfig, ok := securitySettings.(*tls.Config) if config := tls.ConfigFromContext(ctx, tls.WithDestination(dest)); config != nil {
if ok { conn = tls.Client(conn, config.GetTLSConfig())
if dest.Address.Family().IsDomain() {
tlsConfig.OverrideServerNameIfEmpty(dest.Address.Domain())
}
config := tlsConfig.GetTLSConfig()
conn = tls.Client(conn, config)
}
} }
tcpSettings := getTCPSettingsFromContext(ctx) tcpSettings := getTCPSettingsFromContext(ctx)

View File

@ -37,12 +37,11 @@ func ListenTCP(ctx context.Context, address net.Address, port net.Port, addConn
config: tcpSettings, config: tcpSettings,
addConn: addConn, addConn: addConn,
} }
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
tlsConfig, ok := securitySettings.(*tls.Config) if config := tls.ConfigFromContext(ctx); config != nil {
if ok { l.tlsConfig = config.GetTLSConfig()
l.tlsConfig = tlsConfig.GetTLSConfig()
}
} }
if tcpSettings.HeaderSettings != nil { if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance() headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil { if err != nil {

View File

@ -1,9 +1,12 @@
package tls package tls
import ( import (
"context"
"crypto/tls" "crypto/tls"
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
) )
var ( var (
@ -42,8 +45,26 @@ func (c *Config) GetTLSConfig() *tls.Config {
return config return config
} }
func (c *Config) OverrideServerNameIfEmpty(serverName string) { type Option func(*Config)
if len(c.ServerName) == 0 {
c.ServerName = serverName func WithDestination(dest net.Destination) Option {
return func(config *Config) {
if dest.Address.Family().IsDomain() && len(config.ServerName) == 0 {
config.ServerName = dest.Address.Domain()
}
} }
} }
func ConfigFromContext(ctx context.Context, opts ...Option) *Config {
securitySettings := internet.SecuritySettingsFromContext(ctx)
if securitySettings == nil {
return nil
}
if config, ok := securitySettings.(*Config); ok {
for _, opt := range opts {
opt(config)
}
return config
}
return nil
}

View File

@ -42,15 +42,9 @@ func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error)
protocol := "ws" protocol := "ws"
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil { if config := tls.ConfigFromContext(ctx, tls.WithDestination(dest)); config != nil {
tlsConfig, ok := securitySettings.(*tls.Config)
if ok {
protocol = "wss" protocol = "wss"
if dest.Address.Family().IsDomain() { dialer.TLSClientConfig = config.GetTLSConfig()
tlsConfig.OverrideServerNameIfEmpty(dest.Address.Domain())
}
dialer.TLSClientConfig = tlsConfig.GetTLSConfig()
}
} }
host := dest.NetAddr() host := dest.NetAddr()

View File

@ -59,11 +59,8 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, addConn i
config: wsSettings, config: wsSettings,
addConn: addConn, addConn: addConn,
} }
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil { if config := v2tls.ConfigFromContext(ctx); config != nil {
tlsConfig, ok := securitySettings.(*v2tls.Config) l.tlsConfig = config.GetTLSConfig()
if ok {
l.tlsConfig = tlsConfig.GetTLSConfig()
}
} }
err := l.listenws(address, port) err := l.listenws(address, port)