Files
teleport/lib/srv/app/tcpserver.go
T
rosstimothy c2fc9a9375 Add idle connection timeouts to http clients and servers (#22885)
Sets `http.Server.IdleTimeout` and `http.Client.IdleConnTimeout`
on clients and servers which didn't have them set. A default of
360s was chosen to be on par with the default of an NLB without
being identical.

This was added as another safety measure to prevent leaking any
idle connections indefinitely as seen in #22757.

`apidefaults.DefaultDialTimeout` was also renamed to
`apidefaults.DefaultIOTimeout` to better reflect its usage.
2023-03-10 16:47:06 +00:00

69 lines
2.0 KiB
Go

/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package app
import (
"context"
"net"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
apidefaults "github.com/gravitational/teleport/api/defaults"
apitypes "github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/srv/app/common"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
)
type tcpServer struct {
audit common.Audit
hostID string
log logrus.FieldLogger
}
// handleConnection handles connection from a TCP application.
func (s *tcpServer) handleConnection(ctx context.Context, clientConn net.Conn, identity *tlsca.Identity, app apitypes.Application) error {
addr, err := utils.ParseAddr(app.GetURI())
if err != nil {
return trace.Wrap(err)
}
if addr.AddrNetwork != "tcp" {
return trace.BadParameter(`unexpected app %q address network, expected "tcp": %+v`, app.GetName(), addr)
}
dialer := net.Dialer{
Timeout: apidefaults.DefaultIOTimeout,
}
serverConn, err := dialer.DialContext(ctx, addr.AddrNetwork, addr.String())
if err != nil {
return trace.Wrap(err)
}
if err := s.audit.OnSessionStart(ctx, s.hostID, identity, app); err != nil {
return trace.Wrap(err)
}
defer func() {
if err := s.audit.OnSessionEnd(ctx, s.hostID, identity, app); err != nil {
s.log.WithError(err).Warnf("Failed to emit session end event for app %v.", app.GetName())
}
}()
err = utils.ProxyConn(ctx, clientConn, serverConn)
if err != nil {
return trace.Wrap(err)
}
return nil
}