2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2017-04-08 19:43:25 -04:00
|
|
|
package tls
|
|
|
|
|
2017-04-20 05:00:15 -04:00
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/buf"
|
2018-07-14 07:58:24 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2019-02-16 18:58:02 -05:00
|
|
|
|
2019-10-18 22:22:49 -04:00
|
|
|
utls "github.com/refraction-networking/utls"
|
2017-04-20 05:00:15 -04:00
|
|
|
)
|
|
|
|
|
2018-09-30 17:08:41 -04:00
|
|
|
//go:generate errorgen
|
2017-04-20 05:00:15 -04:00
|
|
|
|
2017-04-23 07:41:52 -04:00
|
|
|
var (
|
2017-11-09 16:33:15 -05:00
|
|
|
_ buf.Writer = (*conn)(nil)
|
2017-04-23 07:41:52 -04:00
|
|
|
)
|
|
|
|
|
2017-04-20 05:00:15 -04:00
|
|
|
type conn struct {
|
2018-07-14 07:58:24 -04:00
|
|
|
*tls.Conn
|
2017-04-20 05:00:15 -04:00
|
|
|
}
|
|
|
|
|
2017-04-23 07:41:52 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-14 07:58:24 -04:00
|
|
|
func (c *conn) HandshakeAddress() net.Address {
|
|
|
|
if err := c.Handshake(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
state := c.Conn.ConnectionState()
|
2019-06-14 09:47:28 -04:00
|
|
|
if state.ServerName == "" {
|
2018-07-14 07:58:24 -04:00
|
|
|
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.
|
2017-04-20 05:00:15 -04:00
|
|
|
func Client(c net.Conn, config *tls.Config) net.Conn {
|
|
|
|
tlsConn := tls.Client(c, config)
|
|
|
|
return &conn{Conn: tlsConn}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
}
|