1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00
v2fly/transport/internet/tls/tls.go

54 lines
1.1 KiB
Go
Raw Normal View History

2017-04-08 23:43:25 +00:00
package tls
2017-04-20 09:00:15 +00:00
import (
"crypto/tls"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2017-04-20 09:00:15 +00:00
)
2018-09-30 21:08:41 +00:00
//go:generate errorgen
2017-04-20 09:00:15 +00:00
2017-04-23 11:41:52 +00:00
var (
2017-11-09 21:33:15 +00:00
_ buf.Writer = (*conn)(nil)
2017-04-23 11:41:52 +00:00
)
2017-04-20 09:00:15 +00:00
type conn struct {
*tls.Conn
2017-04-20 09:00:15 +00:00
2017-11-09 21:33:15 +00:00
mergingWriter *buf.BufferedWriter
2017-04-20 09:00:15 +00:00
}
2017-04-23 11:41:52 +00:00
func (c *conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
2017-04-20 09:00:15 +00:00
if c.mergingWriter == nil {
2017-11-09 21:33:15 +00:00
c.mergingWriter = buf.NewBufferedWriter(buf.NewWriter(c.Conn))
}
if err := c.mergingWriter.WriteMultiBuffer(mb); err != nil {
return err
2017-04-20 09:00:15 +00:00
}
2017-11-09 21:33:15 +00:00
return c.mergingWriter.Flush()
2017-04-20 09:00:15 +00:00
}
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)
}
2018-04-17 21:33:39 +00:00
// Client initiates a TLS client handshake on the given connection.
2017-04-20 09:00:15 +00:00
func Client(c net.Conn, config *tls.Config) net.Conn {
tlsConn := tls.Client(c, config)
return &conn{Conn: tlsConn}
}
2018-04-17 21:33:39 +00:00
// Server initiates a TLS server handshake on the given connection.
2017-04-20 09:00:15 +00:00
func Server(c net.Conn, config *tls.Config) net.Conn {
tlsConn := tls.Server(c, config)
return &conn{Conn: tlsConn}
}