-
Notifications
You must be signed in to change notification settings - Fork 369
Add proxy protocol v2 client-side support #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
defanator
wants to merge
5
commits into
nginx:main
Choose a base branch
from
defanator:proxy_proto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−2
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
772d8f6
Add proxy protocol support
defanator 460eaad
Update dependency go-proxyproto to v0.8.1
defanator 323402b
Merge branch 'main' into proxy_proto
vepatel d25d5f6
Merge branch 'main' into proxy_proto
vepatel 58e8b77
Merge branch 'main' into proxy_proto
vepatel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,6 +30,8 @@ import ( | |||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"github.com/prometheus/exporter-toolkit/web" | ||||||||||||||||||||||||||||||||
"github.com/prometheus/exporter-toolkit/web/kingpinflag" | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
proxyproto "github.com/pires/go-proxyproto" | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// positiveDuration is a wrapper of time.Duration to ensure only positive values are accepted. | ||||||||||||||||||||||||||||||||
|
@@ -90,6 +92,7 @@ var ( | |||||||||||||||||||||||||||||||
sslCaCert = kingpin.Flag("nginx.ssl-ca-cert", "Path to the PEM encoded CA certificate file used to validate the servers SSL certificate.").Default("").Envar("SSL_CA_CERT").String() | ||||||||||||||||||||||||||||||||
sslClientCert = kingpin.Flag("nginx.ssl-client-cert", "Path to the PEM encoded client certificate file to use when connecting to the server.").Default("").Envar("SSL_CLIENT_CERT").String() | ||||||||||||||||||||||||||||||||
sslClientKey = kingpin.Flag("nginx.ssl-client-key", "Path to the PEM encoded client certificate key file to use when connecting to the server.").Default("").Envar("SSL_CLIENT_KEY").String() | ||||||||||||||||||||||||||||||||
useProxyProto = kingpin.Flag("nginx.proxy-protocol", "Pass proxy protocol payload to nginx listeners.").Default("false").Envar("PROXY_PROTOCOL").Bool() | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// Custom command-line flags. | ||||||||||||||||||||||||||||||||
timeout = createPositiveDurationFlag(kingpin.Flag("nginx.timeout", "A timeout for scraping metrics from NGINX or NGINX Plus.").Default("5s").Envar("TIMEOUT").HintOptions("5s", "10s", "30s", "1m", "5m")) | ||||||||||||||||||||||||||||||||
|
@@ -223,17 +226,65 @@ func main() { | |||||||||||||||||||||||||||||||
func registerCollector(logger *slog.Logger, transport *http.Transport, | ||||||||||||||||||||||||||||||||
addr string, labels map[string]string, | ||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||
var socketPath string | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if strings.HasPrefix(addr, "unix:") { | ||||||||||||||||||||||||||||||||
socketPath, requestPath, err := parseUnixSocketAddress(addr) | ||||||||||||||||||||||||||||||||
var err error | ||||||||||||||||||||||||||||||||
var requestPath string | ||||||||||||||||||||||||||||||||
socketPath, requestPath, err = parseUnixSocketAddress(addr) | ||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||
logger.Error("parsing unix domain socket scrape address failed", "uri", addr, "error", err.Error()) | ||||||||||||||||||||||||||||||||
os.Exit(1) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
addr = "http://unix" + requestPath | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if !*useProxyProto && socketPath != "" { | ||||||||||||||||||||||||||||||||
transport.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) { | ||||||||||||||||||||||||||||||||
return net.Dial("unix", socketPath) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
addr = "http://unix" + requestPath | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if *useProxyProto { | ||||||||||||||||||||||||||||||||
transport.DialContext = func(_ context.Context, network, addr string) (net.Conn, error) { | ||||||||||||||||||||||||||||||||
if socketPath != "" { | ||||||||||||||||||||||||||||||||
network = "unix" | ||||||||||||||||||||||||||||||||
addr = socketPath | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
conn, err := (&net.Dialer{}).Dial(network, addr) | ||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||
return nil, fmt.Errorf("dialing %s %s: %w", network, addr, err) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
localAddr := conn.LocalAddr() | ||||||||||||||||||||||||||||||||
remoteAddr := conn.RemoteAddr() | ||||||||||||||||||||||||||||||||
transportProtocol := proxyproto.TCPv4 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
switch addr := remoteAddr.(type) { | ||||||||||||||||||||||||||||||||
case *net.TCPAddr: | ||||||||||||||||||||||||||||||||
if addr.IP.To4() == nil { | ||||||||||||||||||||||||||||||||
transportProtocol = proxyproto.TCPv6 | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
case *net.UnixAddr: | ||||||||||||||||||||||||||||||||
transportProtocol = proxyproto.UnixStream | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
header := &proxyproto.Header{ | ||||||||||||||||||||||||||||||||
Version: 2, | ||||||||||||||||||||||||||||||||
Command: proxyproto.PROXY, | ||||||||||||||||||||||||||||||||
TransportProtocol: transportProtocol, | ||||||||||||||||||||||||||||||||
SourceAddr: localAddr, | ||||||||||||||||||||||||||||||||
DestinationAddr: remoteAddr, | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
_, err = header.WriteTo(conn) | ||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||
return nil, fmt.Errorf("writing proxyproto header: %w", err) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The number of bytes written is being ignored. Consider logging or validating the bytes written to ensure the complete header was sent, or add a comment explaining why this return value is intentionally ignored.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return conn, nil | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
userAgent := fmt.Sprintf("NGINX-Prometheus-Exporter/v%v", common_version.Version) | ||||||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name 'addr' shadows the outer 'addr' parameter, which could lead to confusion. The switch should use a different variable name like 'tcpAddr' or 'remoteAddrTyped'.
Copilot uses AI. Check for mistakes.