cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2018.12.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tunnelrpc/pogs/tunnelrpc.go

214lines · modecode

1package pogs
2
3import (
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
15type Authentication struct {
16 Key string
17 Email string
18 OriginCAKey string
19}
20
21func MarshalAuthentication(s tunnelrpc.Authentication, p *Authentication) error {
22 return pogs.Insert(tunnelrpc.Authentication_TypeID, s.Struct, p)
23}
24
25func 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
31type TunnelRegistration struct {
32 Err string
33 Url string
34 LogLines []string
35 PermanentFailure bool
36 TunnelID string `capnp:"tunnelID"`
37}
38
39func MarshalTunnelRegistration(s tunnelrpc.TunnelRegistration, p *TunnelRegistration) error {
40 return pogs.Insert(tunnelrpc.TunnelRegistration_TypeID, s.Struct, p)
41}
42
43func 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
49type 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
64func MarshalRegistrationOptions(s tunnelrpc.RegistrationOptions, p *RegistrationOptions) error {
65 return pogs.Insert(tunnelrpc.RegistrationOptions_TypeID, s.Struct, p)
66}
67
68func 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
74type Tag struct {
75 Name string `json:"name"`
76 Value string `json:"value"`
77}
78
79type ServerInfo struct {
80 LocationName string
81}
82
83func MarshalServerInfo(s tunnelrpc.ServerInfo, p *ServerInfo) error {
84 return pogs.Insert(tunnelrpc.ServerInfo_TypeID, s.Struct, p)
85}
86
87func 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
93type 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
99func TunnelServer_ServerToClient(s TunnelServer) tunnelrpc.TunnelServer {
100 return tunnelrpc.TunnelServer_ServerToClient(TunnelServer_PogsImpl{s})
101}
102
103type TunnelServer_PogsImpl struct {
104 impl TunnelServer
105}
106
107func (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
137func (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
150func (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
157type TunnelServer_PogsClient struct {
158 Client capnp.Client
159 Conn *rpc.Conn
160}
161
162func (c TunnelServer_PogsClient) Close() error {
163 return c.Conn.Close()
164}
165
166func (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
194func (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
206func (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