Skip to content

Commit 3ce40f5

Browse files
committed
rpc_proxy: remote subserver remote conn creation
Remove the code for creating remote connections to loop, faraday and pool subservers from the rpcProxy. This is now handled by the subserver manager.
1 parent c4562cb commit 3ce40f5

File tree

2 files changed

+18
-91
lines changed

2 files changed

+18
-91
lines changed

rpc_proxy.go

Lines changed: 15 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (e *proxyErr) Unwrap() error {
6161
// component.
6262
func newRpcProxy(cfg *Config, validator macaroons.MacaroonValidator,
6363
superMacValidator session.SuperMacaroonValidator,
64-
permsMgr *perms.Manager) *rpcProxy {
64+
permsMgr *perms.Manager, subServerMgr *subservers.Manager) *rpcProxy {
6565

6666
// The gRPC web calls are protected by HTTP basic auth which is defined
6767
// by base64(username:password). Because we only have a password, we
@@ -81,6 +81,7 @@ func newRpcProxy(cfg *Config, validator macaroons.MacaroonValidator,
8181
permsMgr: permsMgr,
8282
macValidator: validator,
8383
superMacValidator: superMacValidator,
84+
subServerMgr: subServerMgr,
8485
}
8586
p.grpcServer = grpc.NewServer(
8687
// From the grpxProxy doc: This codec is *crucial* to the
@@ -151,64 +152,26 @@ type rpcProxy struct {
151152
// must only ever be used atomically.
152153
started int32
153154

154-
cfg *Config
155-
basicAuth string
156-
permsMgr *perms.Manager
155+
cfg *Config
156+
basicAuth string
157+
permsMgr *perms.Manager
158+
subServerMgr *subservers.Manager
157159

158160
macValidator macaroons.MacaroonValidator
159161
superMacValidator session.SuperMacaroonValidator
160162

161163
superMacaroon string
162164

163-
lndConn *grpc.ClientConn
164-
faradayConn *grpc.ClientConn
165-
loopConn *grpc.ClientConn
166-
poolConn *grpc.ClientConn
165+
lndConn *grpc.ClientConn
167166

168167
grpcServer *grpc.Server
169168
grpcWebProxy *grpcweb.WrappedGrpcServer
170169
}
171170

172171
// Start creates initial connection to lnd.
173172
func (p *rpcProxy) Start(lndConn *grpc.ClientConn) error {
174-
var err error
175173
p.lndConn = lndConn
176174

177-
// Make sure we can connect to all the daemons that are configured to be
178-
// running in remote mode.
179-
if p.cfg.faradayRemote {
180-
p.faradayConn, err = dialBackend(
181-
"faraday", p.cfg.Remote.Faraday.RPCServer,
182-
lncfg.CleanAndExpandPath(
183-
p.cfg.Remote.Faraday.TLSCertPath,
184-
),
185-
)
186-
if err != nil {
187-
return fmt.Errorf("could not dial remote faraday: %v",
188-
err)
189-
}
190-
}
191-
192-
if p.cfg.loopRemote {
193-
p.loopConn, err = dialBackend(
194-
"loop", p.cfg.Remote.Loop.RPCServer,
195-
lncfg.CleanAndExpandPath(p.cfg.Remote.Loop.TLSCertPath),
196-
)
197-
if err != nil {
198-
return fmt.Errorf("could not dial remote loop: %v", err)
199-
}
200-
}
201-
202-
if p.cfg.poolRemote {
203-
p.poolConn, err = dialBackend(
204-
"pool", p.cfg.Remote.Pool.RPCServer,
205-
lncfg.CleanAndExpandPath(p.cfg.Remote.Pool.TLSCertPath),
206-
)
207-
if err != nil {
208-
return fmt.Errorf("could not dial remote pool: %v", err)
209-
}
210-
}
211-
212175
atomic.CompareAndSwapInt32(&p.started, 0, 1)
213176

214177
return nil
@@ -224,27 +187,6 @@ func (p *rpcProxy) hasStarted() bool {
224187
func (p *rpcProxy) Stop() error {
225188
p.grpcServer.Stop()
226189

227-
if p.faradayConn != nil {
228-
if err := p.faradayConn.Close(); err != nil {
229-
log.Errorf("Error closing faraday connection: %v", err)
230-
return err
231-
}
232-
}
233-
234-
if p.loopConn != nil {
235-
if err := p.loopConn.Close(); err != nil {
236-
log.Errorf("Error closing loop connection: %v", err)
237-
return err
238-
}
239-
}
240-
241-
if p.poolConn != nil {
242-
if err := p.poolConn.Close(); err != nil {
243-
log.Errorf("Error closing pool connection: %v", err)
244-
return err
245-
}
246-
}
247-
248190
return nil
249191
}
250192

@@ -362,38 +304,21 @@ func (p *rpcProxy) makeDirector(allowLitRPC bool) func(ctx context.Context,
362304
// since it must either be an lnd call or something that'll be
363305
// handled by the integrated daemons that are hooking into lnd's
364306
// gRPC server.
365-
isFaraday := p.permsMgr.IsSubServerURI(
366-
subservers.FARADAY, requestURI,
367-
)
368-
isLoop := p.permsMgr.IsSubServerURI(
369-
subservers.LOOP, requestURI,
370-
)
371-
isPool := p.permsMgr.IsSubServerURI(
372-
subservers.POOL, requestURI,
373-
)
374-
isLit := p.permsMgr.IsSubServerURI(
375-
subservers.LIT, requestURI,
376-
)
377-
switch {
378-
case isFaraday && p.cfg.faradayRemote:
379-
return outCtx, p.faradayConn, nil
380-
381-
case isLoop && p.cfg.loopRemote:
382-
return outCtx, p.loopConn, nil
383-
384-
case isPool && p.cfg.poolRemote:
385-
return outCtx, p.poolConn, nil
307+
handled, conn := p.subServerMgr.GetRemoteConn(requestURI)
308+
if handled {
309+
return outCtx, conn, nil
310+
}
386311

387312
// Calls to LiT session RPC aren't allowed in some cases.
388-
case isLit && !allowLitRPC:
313+
isLit := p.permsMgr.IsSubServerURI(subservers.LIT, requestURI)
314+
if isLit && !allowLitRPC {
389315
return outCtx, nil, status.Errorf(
390316
codes.Unimplemented, "unknown service %s",
391317
requestURI,
392318
)
393-
394-
default:
395-
return outCtx, p.lndConn, nil
396319
}
320+
321+
return outCtx, p.lndConn, nil
397322
}
398323
}
399324

terminal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ func (g *LightningTerminal) Run() error {
236236

237237
// Construct the rpcProxy. It must be initialised before the main web
238238
// server is started.
239-
g.rpcProxy = newRpcProxy(g.cfg, g, g.validateSuperMacaroon, g.permsMgr)
239+
g.rpcProxy = newRpcProxy(
240+
g.cfg, g, g.validateSuperMacaroon, g.permsMgr, g.subServerMgr,
241+
)
240242

241243
// Start the main web server that dispatches requests either to the
242244
// static UI file server or the RPC proxy. This makes it possible to

0 commit comments

Comments
 (0)