You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/tls/tls.go

54 lines
1.1 KiB

package tls
import (
"crypto/tls"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
)
6 years ago
//go:generate errorgen
8 years ago
var (
_ buf.Writer = (*conn)(nil)
8 years ago
)
type conn struct {
*tls.Conn
mergingWriter *buf.BufferedWriter
}
8 years ago
func (c *conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
if c.mergingWriter == nil {
c.mergingWriter = buf.NewBufferedWriter(buf.NewWriter(c.Conn))
}
if err := c.mergingWriter.WriteMultiBuffer(mb); err != nil {
return err
}
return c.mergingWriter.Flush()
}
func (c *conn) HandshakeAddress() net.Address {
if err := c.Handshake(); err != nil {
return nil
}
state := c.Conn.ConnectionState()
if len(state.ServerName) == 0 {
return nil
}
return net.ParseAddress(state.ServerName)
}
7 years ago
// Client initiates a TLS client handshake on the given connection.
func Client(c net.Conn, config *tls.Config) net.Conn {
tlsConn := tls.Client(c, config)
return &conn{Conn: tlsConn}
}
7 years ago
// Server initiates a TLS server handshake on the given connection.
func Server(c net.Conn, config *tls.Config) net.Conn {
tlsConn := tls.Server(c, config)
return &conn{Conn: tlsConn}
}