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

41 lines
811 B
Go
Raw Normal View History

2017-04-08 19:43:25 -04:00
package tls
2017-04-20 05:00:15 -04:00
import (
"crypto/tls"
"net"
"v2ray.com/core/common/buf"
)
2017-12-02 19:04:57 -05:00
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg tls -path Transport,Internet,TLS
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 {
net.Conn
2017-11-09 16:33:15 -05:00
mergingWriter *buf.BufferedWriter
2017-04-20 05:00:15 -04:00
}
2017-04-23 07:41:52 -04:00
func (c *conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
2017-04-20 05:00:15 -04:00
if c.mergingWriter == nil {
2017-11-09 16:33:15 -05:00
c.mergingWriter = buf.NewBufferedWriter(buf.NewWriter(c.Conn))
}
if err := c.mergingWriter.WriteMultiBuffer(mb); err != nil {
return err
2017-04-20 05:00:15 -04:00
}
2017-11-09 16:33:15 -05:00
return c.mergingWriter.Flush()
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}
}
func Server(c net.Conn, config *tls.Config) net.Conn {
tlsConn := tls.Server(c, config)
return &conn{Conn: tlsConn}
}