cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
tunnelrpc/pogs/tunnelrpc.go
214lines · modecode
| 1 | package pogs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/cloudflare/cloudflared/tunnelrpc" |
| 7 | |
| 8 | log "github.com/sirupsen/logrus" |
| 9 | "zombiezen.com/go/capnproto2" |
| 10 | "zombiezen.com/go/capnproto2/pogs" |
| 11 | "zombiezen.com/go/capnproto2/rpc" |
| 12 | "zombiezen.com/go/capnproto2/server" |
| 13 | ) |
| 14 | |
| 15 | type Authentication struct { |
| 16 | Key string |
| 17 | Email string |
| 18 | OriginCAKey string |
| 19 | } |
| 20 | |
| 21 | func MarshalAuthentication(s tunnelrpc.Authentication, p *Authentication) error { |
| 22 | return pogs.Insert(tunnelrpc.Authentication_TypeID, s.Struct, p) |
| 23 | } |
| 24 | |
| 25 | func UnmarshalAuthentication(s tunnelrpc.Authentication) (*Authentication, error) { |
| 26 | p := new(Authentication) |
| 27 | err := pogs.Extract(p, tunnelrpc.Authentication_TypeID, s.Struct) |
| 28 | return p, err |
| 29 | } |
| 30 | |
| 31 | type TunnelRegistration struct { |
| 32 | Err string |
| 33 | Url string |
| 34 | LogLines []string |
| 35 | PermanentFailure bool |
| 36 | TunnelID string `capnp:"tunnelID"` |
| 37 | } |
| 38 | |
| 39 | func MarshalTunnelRegistration(s tunnelrpc.TunnelRegistration, p *TunnelRegistration) error { |
| 40 | return pogs.Insert(tunnelrpc.TunnelRegistration_TypeID, s.Struct, p) |
| 41 | } |
| 42 | |
| 43 | func UnmarshalTunnelRegistration(s tunnelrpc.TunnelRegistration) (*TunnelRegistration, error) { |
| 44 | p := new(TunnelRegistration) |
| 45 | err := pogs.Extract(p, tunnelrpc.TunnelRegistration_TypeID, s.Struct) |
| 46 | return p, err |
| 47 | } |
| 48 | |
| 49 | type RegistrationOptions struct { |
| 50 | ClientID string `capnp:"clientId"` |
| 51 | Version string |
| 52 | OS string `capnp:"os"` |
| 53 | ExistingTunnelPolicy tunnelrpc.ExistingTunnelPolicy |
| 54 | PoolName string `capnp:"poolName"` |
| 55 | Tags []Tag |
| 56 | ConnectionID uint8 `capnp:"connectionId"` |
| 57 | OriginLocalIP string `capnp:"originLocalIp"` |
| 58 | IsAutoupdated bool `capnp:"isAutoupdated"` |
| 59 | RunFromTerminal bool `capnp:"runFromTerminal"` |
| 60 | CompressionQuality uint64 `capnp:"compressionQuality"` |
| 61 | UUID string `capnp:"uuid"` |
| 62 | } |
| 63 | |
| 64 | func MarshalRegistrationOptions(s tunnelrpc.RegistrationOptions, p *RegistrationOptions) error { |
| 65 | return pogs.Insert(tunnelrpc.RegistrationOptions_TypeID, s.Struct, p) |
| 66 | } |
| 67 | |
| 68 | func UnmarshalRegistrationOptions(s tunnelrpc.RegistrationOptions) (*RegistrationOptions, error) { |
| 69 | p := new(RegistrationOptions) |
| 70 | err := pogs.Extract(p, tunnelrpc.RegistrationOptions_TypeID, s.Struct) |
| 71 | return p, err |
| 72 | } |
| 73 | |
| 74 | type Tag struct { |
| 75 | Name string `json:"name"` |
| 76 | Value string `json:"value"` |
| 77 | } |
| 78 | |
| 79 | type ServerInfo struct { |
| 80 | LocationName string |
| 81 | } |
| 82 | |
| 83 | func MarshalServerInfo(s tunnelrpc.ServerInfo, p *ServerInfo) error { |
| 84 | return pogs.Insert(tunnelrpc.ServerInfo_TypeID, s.Struct, p) |
| 85 | } |
| 86 | |
| 87 | func UnmarshalServerInfo(s tunnelrpc.ServerInfo) (*ServerInfo, error) { |
| 88 | p := new(ServerInfo) |
| 89 | err := pogs.Extract(p, tunnelrpc.ServerInfo_TypeID, s.Struct) |
| 90 | return p, err |
| 91 | } |
| 92 | |
| 93 | type TunnelServer interface { |
| 94 | RegisterTunnel(ctx context.Context, originCert []byte, hostname string, options *RegistrationOptions) (*TunnelRegistration, error) |
| 95 | GetServerInfo(ctx context.Context) (*ServerInfo, error) |
| 96 | UnregisterTunnel(ctx context.Context, gracePeriodNanoSec int64) error |
| 97 | } |
| 98 | |
| 99 | func TunnelServer_ServerToClient(s TunnelServer) tunnelrpc.TunnelServer { |
| 100 | return tunnelrpc.TunnelServer_ServerToClient(TunnelServer_PogsImpl{s}) |
| 101 | } |
| 102 | |
| 103 | type TunnelServer_PogsImpl struct { |
| 104 | impl TunnelServer |
| 105 | } |
| 106 | |
| 107 | func (i TunnelServer_PogsImpl) RegisterTunnel(p tunnelrpc.TunnelServer_registerTunnel) error { |
| 108 | originCert, err := p.Params.OriginCert() |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | hostname, err := p.Params.Hostname() |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | options, err := p.Params.Options() |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | pogsOptions, err := UnmarshalRegistrationOptions(options) |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | server.Ack(p.Options) |
| 125 | registration, err := i.impl.RegisterTunnel(p.Ctx, originCert, hostname, pogsOptions) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | result, err := p.Results.NewResult() |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | log.Info(registration.TunnelID) |
| 134 | return MarshalTunnelRegistration(result, registration) |
| 135 | } |
| 136 | |
| 137 | func (i TunnelServer_PogsImpl) GetServerInfo(p tunnelrpc.TunnelServer_getServerInfo) error { |
| 138 | server.Ack(p.Options) |
| 139 | serverInfo, err := i.impl.GetServerInfo(p.Ctx) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | result, err := p.Results.NewResult() |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | return MarshalServerInfo(result, serverInfo) |
| 148 | } |
| 149 | |
| 150 | func (i TunnelServer_PogsImpl) UnregisterTunnel(p tunnelrpc.TunnelServer_unregisterTunnel) error { |
| 151 | gracePeriodNanoSec := p.Params.GracePeriodNanoSec() |
| 152 | server.Ack(p.Options) |
| 153 | return i.impl.UnregisterTunnel(p.Ctx, gracePeriodNanoSec) |
| 154 | |
| 155 | } |
| 156 | |
| 157 | type TunnelServer_PogsClient struct { |
| 158 | Client capnp.Client |
| 159 | Conn *rpc.Conn |
| 160 | } |
| 161 | |
| 162 | func (c TunnelServer_PogsClient) Close() error { |
| 163 | return c.Conn.Close() |
| 164 | } |
| 165 | |
| 166 | func (c TunnelServer_PogsClient) RegisterTunnel(ctx context.Context, originCert []byte, hostname string, options *RegistrationOptions) (*TunnelRegistration, error) { |
| 167 | client := tunnelrpc.TunnelServer{Client: c.Client} |
| 168 | promise := client.RegisterTunnel(ctx, func(p tunnelrpc.TunnelServer_registerTunnel_Params) error { |
| 169 | err := p.SetOriginCert(originCert) |
| 170 | if err != nil { |
| 171 | return err |
| 172 | } |
| 173 | err = p.SetHostname(hostname) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | registrationOptions, err := p.NewOptions() |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | err = MarshalRegistrationOptions(registrationOptions, options) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | return nil |
| 186 | }) |
| 187 | retval, err := promise.Result().Struct() |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | return UnmarshalTunnelRegistration(retval) |
| 192 | } |
| 193 | |
| 194 | func (c TunnelServer_PogsClient) GetServerInfo(ctx context.Context) (*ServerInfo, error) { |
| 195 | client := tunnelrpc.TunnelServer{Client: c.Client} |
| 196 | promise := client.GetServerInfo(ctx, func(p tunnelrpc.TunnelServer_getServerInfo_Params) error { |
| 197 | return nil |
| 198 | }) |
| 199 | retval, err := promise.Result().Struct() |
| 200 | if err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | return UnmarshalServerInfo(retval) |
| 204 | } |
| 205 | |
| 206 | func (c TunnelServer_PogsClient) UnregisterTunnel(ctx context.Context, gracePeriodNanoSec int64) error { |
| 207 | client := tunnelrpc.TunnelServer{Client: c.Client} |
| 208 | promise := client.UnregisterTunnel(ctx, func(p tunnelrpc.TunnelServer_unregisterTunnel_Params) error { |
| 209 | p.SetGracePeriodNanoSec(gracePeriodNanoSec) |
| 210 | return nil |
| 211 | }) |
| 212 | _, err := promise.Struct() |
| 213 | return err |
| 214 | } |
| 215 | |