Skip to content

Commit 698781b

Browse files
committed
Fix host:port joining
1 parent 20b4112 commit 698781b

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

cmd/agent/app/options/options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package options
1818

1919
import (
2020
"fmt"
21+
"net"
2122
"net/url"
2223
"os"
24+
"strconv"
2325
"time"
2426

2527
"github.com/google/uuid"
@@ -81,7 +83,7 @@ type GrpcProxyAgentOptions struct {
8183

8284
func (o *GrpcProxyAgentOptions) ClientSetConfig(dialOptions ...grpc.DialOption) *agent.ClientSetConfig {
8385
return &agent.ClientSetConfig{
84-
Address: fmt.Sprintf("%s:%d", o.ProxyServerHost, o.ProxyServerPort),
86+
Address: net.JoinHostPort(o.ProxyServerHost, strconv.Itoa(o.ProxyServerPort)),
8587
AgentID: o.AgentID,
8688
AgentIdentifiers: o.AgentIdentifiers,
8789
SyncInterval: o.SyncInterval,

cmd/agent/app/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ func (a *Agent) runAdminServer(o *options.GrpcProxyAgentOptions) error {
182182
if err != nil {
183183
host = r.Host
184184
}
185-
http.Redirect(w, r, fmt.Sprintf("%s:%d%s", host, o.HealthServerPort, r.URL.Path), http.StatusMovedPermanently)
185+
dest := *r.URL
186+
dest.Host = net.JoinHostPort(host, strconv.Itoa(o.HealthServerPort))
187+
http.Redirect(w, r, dest.String(), http.StatusMovedPermanently)
186188
}))
187189
if o.EnableProfiling {
188190
muxHandler.HandleFunc("/debug/pprof", util.RedirectTo("/debug/pprof/"))

cmd/test-client/main.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"crypto/tls"
2323
"flag"
2424
"fmt"
25-
"golang.org/x/net/http2"
2625
"io"
2726
"net"
2827
"net/http"
28+
"net/url"
2929
"os"
3030
"os/signal"
3131
runpprof "runtime/pprof"
@@ -35,6 +35,7 @@ import (
3535

3636
"github.com/spf13/cobra"
3737
"github.com/spf13/pflag"
38+
"golang.org/x/net/http2"
3839
"google.golang.org/grpc"
3940
"google.golang.org/grpc/credentials"
4041
"k8s.io/klog/v2"
@@ -369,10 +370,14 @@ func configureHTTP2Transport(t *http.Transport) error {
369370
}
370371

371372
func (c *Client) makeRequest(o *GrpcProxyClientOptions, client *http.Client) error {
372-
requestURL := fmt.Sprintf("%s://%s:%d/%s", o.requestProto, o.requestHost, o.requestPort, o.requestPath)
373-
request, err := http.NewRequest("GET", requestURL, nil)
373+
requestURL := url.URL{
374+
Scheme: o.requestProto,
375+
Host: joinHostPort(o.requestHost, o.requestPort),
376+
Path: o.requestPath,
377+
}
378+
request, err := http.NewRequest("GET", requestURL.String(), nil)
374379
if err != nil {
375-
return fmt.Errorf("failed to create request %s to send, got %v", requestURL, err)
380+
return fmt.Errorf("failed to create request %v to send, got %v", requestURL, err)
376381
}
377382
response, err := client.Do(request)
378383
if err != nil {
@@ -439,13 +444,13 @@ func (c *Client) getUDSDialer(o *GrpcProxyClientOptions) (func(ctx context.Conte
439444
return nil, fmt.Errorf("failed to create tunnel %s, got %v", o.proxyUdsName, err)
440445
}
441446

442-
requestAddress := fmt.Sprintf("%s:%d", o.requestHost, o.requestPort)
447+
requestAddress := joinHostPort(o.requestHost, o.requestPort)
443448
proxyConn, err = tunnel.DialContext(ctx, "tcp", requestAddress)
444449
if err != nil {
445450
return nil, fmt.Errorf("failed to dial request %s, got %v", requestAddress, err)
446451
}
447452
case "http-connect":
448-
requestAddress := fmt.Sprintf("%s:%d", o.requestHost, o.requestPort)
453+
requestAddress := joinHostPort(o.requestHost, o.requestPort)
449454

450455
proxyConn, err = net.Dial("unix", o.proxyUdsName)
451456
if err != nil {
@@ -509,20 +514,20 @@ func (c *Client) getMTLSDialer(o *GrpcProxyClientOptions) (func(ctx context.Cont
509514
case "grpc":
510515
transportCreds := credentials.NewTLS(tlsConfig)
511516
dialOption := grpc.WithTransportCredentials(transportCreds)
512-
serverAddress := fmt.Sprintf("%s:%d", o.proxyHost, o.proxyPort)
517+
serverAddress := joinHostPort(o.proxyHost, o.proxyPort)
513518
tunnel, err := client.CreateSingleUseGrpcTunnel(ctx, serverAddress, dialOption)
514519
if err != nil {
515520
return nil, fmt.Errorf("failed to create tunnel %s, got %v", serverAddress, err)
516521
}
517522

518-
requestAddress := fmt.Sprintf("%s:%d", o.requestHost, o.requestPort)
523+
requestAddress := joinHostPort(o.requestHost, o.requestPort)
519524
proxyConn, err = tunnel.DialContext(ctx, "tcp", requestAddress)
520525
if err != nil {
521526
return nil, fmt.Errorf("failed to dial request %s, got %v", requestAddress, err)
522527
}
523528
case "http-connect":
524-
proxyAddress := fmt.Sprintf("%s:%d", o.proxyHost, o.proxyPort)
525-
requestAddress := fmt.Sprintf("%s:%d", o.requestHost, o.requestPort)
529+
proxyAddress := joinHostPort(o.proxyHost, o.proxyPort)
530+
requestAddress := joinHostPort(o.requestHost, o.requestPort)
526531

527532
proxyConn, err = tls.Dial("tcp", proxyAddress, tlsConfig)
528533
if err != nil {
@@ -555,3 +560,7 @@ func (c *Client) getMTLSDialer(o *GrpcProxyClientOptions) (func(ctx context.Cont
555560
return proxyConn, nil
556561
}, nil
557562
}
563+
564+
func joinHostPort(host string, port int) string {
565+
return net.JoinHostPort(host, strconv.Itoa(port))
566+
}

0 commit comments

Comments
 (0)