cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2018.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tunnelrpc/pogs/tunnelrpc.go

211lines · modecode

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