github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/pingconn/tls.go (about) 1 /* 2 Copyright 2023 Gravitational, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package pingconn 18 19 import ( 20 "crypto/tls" 21 22 "github.com/gravitational/trace" 23 ) 24 25 // NewTLS returns a ping connection wrapping the provided tls.Conn. 26 func NewTLS(conn *tls.Conn) *PingTLSConn { 27 return &PingTLSConn{ 28 Conn: conn, 29 ping: New(conn), 30 } 31 } 32 33 // PingTLSConn wraps a tls.Conn and adds ping capabilities to it. 34 type PingTLSConn struct { 35 *tls.Conn 36 37 ping *PingConn 38 } 39 40 // Read reads content from the underlying connection, discarding any ping 41 // messages it finds. 42 func (c *PingTLSConn) Read(p []byte) (int, error) { 43 n, err := c.ping.Read(p) 44 return n, trace.Wrap(err) 45 } 46 47 // WritePing writes the ping packet to the connection. 48 func (c *PingTLSConn) WritePing() error { 49 return trace.Wrap(c.ping.WritePing()) 50 } 51 52 // Write writes provided content to the underlying connection with proper 53 // protocol fields. 54 func (c *PingTLSConn) Write(p []byte) (int, error) { 55 n, err := c.ping.Write(p) 56 return n, trace.Wrap(err) 57 }