1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-20 02:46:10 -04:00
v2fly/transport/internet/tls/tls.go

79 lines
1.8 KiB
Go
Raw Normal View History

2017-04-08 19:43:25 -04:00
package tls
2017-04-20 05:00:15 -04:00
import (
2021-09-05 05:26:17 -04:00
"context"
2017-04-20 05:00:15 -04:00
"crypto/tls"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
2017-04-20 05:00:15 -04:00
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2017-04-20 05:00:15 -04:00
2021-05-19 17:28:52 -04:00
var _ buf.Writer = (*Conn)(nil)
2017-04-23 07:41:52 -04:00
type Conn struct {
*tls.Conn
2017-04-20 05:00:15 -04:00
}
func (c *Conn) GetConnectionApplicationProtocol() (string, error) {
if err := c.Handshake(); err != nil {
return "", err
}
return c.ConnectionState().NegotiatedProtocol, nil
}
func (c *Conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
2018-12-27 14:38:24 -05:00
mb = buf.Compact(mb)
mb, err := buf.WriteMultiBuffer(c, mb)
buf.ReleaseMulti(mb)
return err
2017-04-20 05:00:15 -04:00
}
func (c *Conn) HandshakeAddress() net.Address {
if err := c.Handshake(); err != nil {
return nil
}
state := c.ConnectionState()
if state.ServerName == "" {
return nil
}
return net.ParseAddress(state.ServerName)
}
2018-04-17 17:33:39 -04:00
// Client initiates a TLS client handshake on the given connection.
func Client(c net.Conn, config *tls.Config) *Conn {
2017-04-20 05:00:15 -04:00
tlsConn := tls.Client(c, config)
return &Conn{Conn: tlsConn}
2017-04-20 05:00:15 -04:00
}
2020-07-20 01:59:46 -04:00
/*
2019-02-16 18:58:02 -05:00
func copyConfig(c *tls.Config) *utls.Config {
return &utls.Config{
NextProtos: c.NextProtos,
ServerName: c.ServerName,
InsecureSkipVerify: c.InsecureSkipVerify,
MinVersion: utls.VersionTLS12,
MaxVersion: utls.VersionTLS12,
}
}
func UClient(c net.Conn, config *tls.Config) net.Conn {
uConfig := copyConfig(config)
return utls.Client(c, uConfig)
}
2020-07-20 01:59:46 -04:00
*/
2019-02-16 18:58:02 -05:00
2018-04-17 17:33:39 -04:00
// Server initiates a TLS server handshake on the given connection.
2017-04-20 05:00:15 -04:00
func Server(c net.Conn, config *tls.Config) net.Conn {
tlsConn := tls.Server(c, config)
return &Conn{Conn: tlsConn}
2017-04-20 05:00:15 -04:00
}
2021-09-05 05:26:17 -04:00
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewTLSSecurityEngineFromConfig(config.(*Config))
2021-09-05 05:26:17 -04:00
}))
}