2024-05-14 04:22:06 +00:00
|
|
|
package pogs
|
2021-07-08 09:29:49 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-13 17:46:52 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-07-08 09:29:49 +00:00
|
|
|
capnp "zombiezen.com/go/capnproto2"
|
|
|
|
"zombiezen.com/go/capnproto2/pogs"
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/proto"
|
2021-07-08 09:29:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectionType indicates the type of underlying connection proxied within the QUIC stream.
|
|
|
|
type ConnectionType uint16
|
|
|
|
|
|
|
|
const (
|
|
|
|
ConnectionTypeHTTP ConnectionType = iota
|
|
|
|
ConnectionTypeWebsocket
|
|
|
|
ConnectionTypeTCP
|
|
|
|
)
|
|
|
|
|
2022-06-13 17:46:52 +00:00
|
|
|
func (c ConnectionType) String() string {
|
|
|
|
switch c {
|
|
|
|
case ConnectionTypeHTTP:
|
|
|
|
return "http"
|
|
|
|
case ConnectionTypeWebsocket:
|
|
|
|
return "ws"
|
|
|
|
case ConnectionTypeTCP:
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("invalid ConnectionType: %d", c))
|
|
|
|
}
|
|
|
|
|
2021-07-08 09:29:49 +00:00
|
|
|
// ConnectRequest is the representation of metadata sent at the start of a QUIC application handshake.
|
|
|
|
type ConnectRequest struct {
|
|
|
|
Dest string `capnp:"dest"`
|
|
|
|
Type ConnectionType `capnp:"type"`
|
|
|
|
Metadata []Metadata `capnp:"metadata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata is a representation of key value based data sent via RequestMeta.
|
|
|
|
type Metadata struct {
|
|
|
|
Key string `capnp:"key"`
|
|
|
|
Val string `capnp:"val"`
|
|
|
|
}
|
|
|
|
|
2021-08-03 07:09:56 +00:00
|
|
|
// MetadataMap returns a map format of []Metadata.
|
|
|
|
func (r *ConnectRequest) MetadataMap() map[string]string {
|
|
|
|
metadataMap := make(map[string]string)
|
|
|
|
for _, metadata := range r.Metadata {
|
|
|
|
metadataMap[metadata.Key] = metadata.Val
|
|
|
|
}
|
|
|
|
return metadataMap
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (r *ConnectRequest) FromPogs(msg *capnp.Message) error {
|
|
|
|
metadata, err := proto.ReadRootConnectRequest(msg)
|
2021-07-08 09:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-14 04:22:06 +00:00
|
|
|
return pogs.Extract(r, proto.ConnectRequest_TypeID, metadata.Struct)
|
2021-07-08 09:29:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (r *ConnectRequest) ToPogs() (*capnp.Message, error) {
|
2021-07-08 09:29:49 +00:00
|
|
|
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
root, err := proto.NewRootConnectRequest(seg)
|
2021-07-08 09:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
if err := pogs.Insert(proto.ConnectRequest_TypeID, root.Struct, r); err != nil {
|
2021-07-08 09:29:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectResponse is a representation of metadata sent as a response to a QUIC application handshake.
|
|
|
|
type ConnectResponse struct {
|
|
|
|
Error string `capnp:"error"`
|
|
|
|
Metadata []Metadata `capnp:"metadata"`
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (r *ConnectResponse) FromPogs(msg *capnp.Message) error {
|
|
|
|
metadata, err := proto.ReadRootConnectResponse(msg)
|
2021-07-08 09:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-14 04:22:06 +00:00
|
|
|
return pogs.Extract(r, proto.ConnectResponse_TypeID, metadata.Struct)
|
2021-07-08 09:29:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (r *ConnectResponse) ToPogs() (*capnp.Message, error) {
|
2021-07-08 09:29:49 +00:00
|
|
|
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
root, err := proto.NewRootConnectResponse(seg)
|
2021-07-08 09:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
if err := pogs.Insert(proto.ConnectResponse_TypeID, root.Struct, r); err != nil {
|
2021-07-08 09:29:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|