Skip to content

Commit 402679d

Browse files
authored
Merge pull request #99 from Jefftree/propagate-caller
Log user agent in proxy server
2 parents cce0631 + bd8983c commit 402679d

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

cmd/client/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type GrpcProxyClientOptions struct {
7070
proxyPort int
7171
proxyUdsName string
7272
mode string
73+
userAgent string
7374
}
7475

7576
func (o *GrpcProxyClientOptions) Flags() *pflag.FlagSet {
@@ -85,6 +86,7 @@ func (o *GrpcProxyClientOptions) Flags() *pflag.FlagSet {
8586
flags.IntVar(&o.proxyPort, "proxy-port", o.proxyPort, "The port the proxy server is listening on.")
8687
flags.StringVar(&o.proxyUdsName, "proxy-uds", o.proxyUdsName, "The UDS name to connect to.")
8788
flags.StringVar(&o.mode, "mode", o.mode, "Mode can be either 'grpc' or 'http-connect'.")
89+
flags.StringVar(&o.userAgent, "user-agent", o.userAgent, "User agent to pass to the proxy server")
8890

8991
return flags
9092
}
@@ -168,6 +170,7 @@ func newGrpcProxyClientOptions() *GrpcProxyClientOptions {
168170
proxyPort: 8090,
169171
proxyUdsName: "",
170172
mode: "grpc",
173+
userAgent: "test-client",
171174
}
172175
return &o
173176
}
@@ -261,7 +264,7 @@ func (c *Client) getUDSDialer(o *GrpcProxyClientOptions) (func(ctx context.Conte
261264
}
262265
return c, err
263266
})
264-
tunnel, err := client.CreateGrpcTunnel(o.proxyUdsName, dialOption, grpc.WithInsecure())
267+
tunnel, err := client.CreateGrpcTunnel(o.proxyUdsName, dialOption, grpc.WithInsecure(), grpc.WithUserAgent(o.userAgent))
265268
if err != nil {
266269
return nil, fmt.Errorf("failed to create tunnel %s, got %v", o.proxyUdsName, err)
267270
}
@@ -278,7 +281,7 @@ func (c *Client) getUDSDialer(o *GrpcProxyClientOptions) (func(ctx context.Conte
278281
if err != nil {
279282
return nil, fmt.Errorf("dialing proxy %q failed: %v", o.proxyUdsName, err)
280283
}
281-
fmt.Fprintf(proxyConn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", requestAddress, "127.0.0.1")
284+
fmt.Fprintf(proxyConn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n", requestAddress, "127.0.0.1", o.userAgent)
282285
br := bufio.NewReader(proxyConn)
283286
res, err := http.ReadResponse(br, nil)
284287
if err != nil {

pkg/server/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ func NewProxyServer(serverID string, serverCount int, agentAuthenticationOptions
185185

186186
// Proxy handles incoming streams from gRPC frontend.
187187
func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
188-
klog.Info("proxy request from client")
188+
md, ok := metadata.FromIncomingContext(stream.Context())
189+
if !ok {
190+
return fmt.Errorf("failed to get context")
191+
}
192+
userAgent := md.Get(header.UserAgent)
193+
klog.Infof("proxy request from client, userAgent %s", userAgent)
189194

190195
recvCh := make(chan *client.Packet, 10)
191196
stopCh := make(chan error)

pkg/server/tunnel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Tunnel struct {
3333
}
3434

3535
func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
36-
klog.Infof("Received %s request to %q", r.Method, r.Host)
36+
klog.Infof("Received %s request to %q from userAgent %s", r.Method, r.Host, r.UserAgent())
3737
if r.TLS != nil {
3838
klog.Infof("TLS CommonName: %v", r.TLS.PeerCertificates[0].Subject.CommonName)
3939
}

proto/header/header.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ const (
2727
// AuthenticationTokenContextSchemePrefix has a prefix for auth token's content.
2828
// (https://tools.ietf.org/html/rfc6750#section-2.1)
2929
AuthenticationTokenContextSchemePrefix = "Bearer "
30+
31+
// UserAgent is used to provide the client information in a proxy request
32+
UserAgent = "user-agent"
3033
)

0 commit comments

Comments
 (0)