-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
proxy.go
71 lines (62 loc) · 2.02 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
// Package sqlproxyccl implements a server to proxy SQL connections.
package sqlproxyccl
import (
"net"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/errors"
"github.com/jackc/pgproto3/v2"
)
const pgAcceptSSLRequest = 'S'
// See https://www.postgresql.org/docs/9.1/protocol-message-formats.html.
var pgSSLRequest = []int32{8, 80877103}
// sendErrToClientAndUpdateMetrics simply combines the update of the metrics and
// the transmission of the err back to the client.
func updateMetricsAndSendErrToClient(err error, conn net.Conn, metrics *metrics) {
metrics.updateForError(err)
SendErrToClient(conn, err)
}
func toPgError(err error) *pgproto3.ErrorResponse {
if getErrorCode(err) != codeNone {
var msg string
switch getErrorCode(err) {
// These are send as is.
case codeExpiredClientConnection,
codeBackendDialFailed,
codeParamsRoutingFailed,
codeClientDisconnected,
codeBackendDisconnected,
codeAuthFailed,
codeProxyRefusedConnection,
codeUnavailable:
msg = err.Error()
// The rest - the message sent back is sanitized.
case codeUnexpectedInsecureStartupMessage:
msg = "server requires encryption"
}
return &pgproto3.ErrorResponse{
Severity: "FATAL",
Code: pgcode.ProxyConnectionError.String(),
Message: msg,
Hint: errors.FlattenHints(err),
}
}
// Return a generic "internal server error" message.
return &pgproto3.ErrorResponse{
Severity: "FATAL",
Code: pgcode.ProxyConnectionError.String(),
Message: "internal server error",
}
}
// SendErrToClient will encode and pass back to the SQL client an error message.
// It can be called by the implementors of proxyHandler to give more
// information to the end user in case of a problem.
var SendErrToClient = func(conn net.Conn, err error) {
if err == nil || conn == nil {
return
}
_, _ = conn.Write(toPgError(err).Encode(nil))
}