mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 23:40:23 +00:00
TUN-8415: Refactor capnp rpc into a single module
Combines the tunnelrpc and quic/schema capnp files into the same module. To help reduce future issues with capnp id generation, capnpids are provided in the capnp files from the existing capnp struct ids generated in the go files. Reduces the overall interface of the Capnp methods to the rest of the code by providing an interface that will handle the quic protocol selection. Introduces a new `rpc-timeout` config that will allow all of the SessionManager and ConfigurationManager RPC requests to have a timeout. The timeout for these values is set to 5 seconds as non of these operations for the managers should take a long time to complete. Removed the RPC-specific logger as it never provided good debugging value as the RPC method names were not visible in the logs.
This commit is contained in:
69
tunnelrpc/quic/cloudflared_server.go
Normal file
69
tunnelrpc/quic/cloudflared_server.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package quic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"zombiezen.com/go/capnproto2/rpc"
|
||||
|
||||
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
||||
)
|
||||
|
||||
// HandleRequestFunc wraps the proxied request from the upstream and also provides methods on the stream to
|
||||
// handle the response back.
|
||||
type HandleRequestFunc = func(ctx context.Context, stream *RequestServerStream) error
|
||||
|
||||
// CloudflaredServer provides a handler interface for a client to provide methods to handle the different types of
|
||||
// requests that can be communicated by the stream.
|
||||
type CloudflaredServer struct {
|
||||
handleRequest HandleRequestFunc
|
||||
sessionManager pogs.SessionManager
|
||||
configManager pogs.ConfigurationManager
|
||||
responseTimeout time.Duration
|
||||
}
|
||||
|
||||
func NewCloudflaredServer(handleRequest HandleRequestFunc, sessionManager pogs.SessionManager, configManager pogs.ConfigurationManager, responseTimeout time.Duration) *CloudflaredServer {
|
||||
return &CloudflaredServer{
|
||||
handleRequest: handleRequest,
|
||||
sessionManager: sessionManager,
|
||||
configManager: configManager,
|
||||
responseTimeout: responseTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
// Serve executes the defined handlers in ServerStream on the provided stream if it is a proper RPC stream with the
|
||||
// correct preamble protocol signature.
|
||||
func (s *CloudflaredServer) Serve(ctx context.Context, stream io.ReadWriteCloser) error {
|
||||
signature, err := determineProtocol(stream)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch signature {
|
||||
case dataStreamProtocolSignature:
|
||||
return s.handleRequest(ctx, &RequestServerStream{stream})
|
||||
case rpcStreamProtocolSignature:
|
||||
return s.handleRPC(ctx, stream)
|
||||
default:
|
||||
return fmt.Errorf("unknown protocol %v", signature)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CloudflaredServer) handleRPC(ctx context.Context, stream io.ReadWriteCloser) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.responseTimeout)
|
||||
defer cancel()
|
||||
transport := rpc.StreamTransport(stream)
|
||||
defer transport.Close()
|
||||
|
||||
main := pogs.CloudflaredServer_ServerToClient(s.sessionManager, s.configManager)
|
||||
rpcConn := rpc.NewConn(transport, rpc.MainInterface(main.Client))
|
||||
defer rpcConn.Close()
|
||||
|
||||
// We ignore the errors here because if cloudflared fails to handle a request, we will just move on.
|
||||
select {
|
||||
case <-rpcConn.Done():
|
||||
case <-ctx.Done():
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user