@@ -3,17 +3,20 @@ package subservers
33import (
44 "context"
55 "fmt"
6+ "io/ioutil"
67 "sync"
78 "time"
89
910 "github.com/lightninglabs/lightning-terminal/perms"
1011 "github.com/lightninglabs/lndclient"
12+ "github.com/lightningnetwork/lnd/lncfg"
1113 "github.com/lightningnetwork/lnd/lnrpc"
1214 grpcProxy "github.com/mwitkow/grpc-proxy/proxy"
1315 "google.golang.org/grpc"
1416 "google.golang.org/grpc/backoff"
1517 "google.golang.org/grpc/credentials"
1618 "gopkg.in/macaroon-bakery.v2/bakery"
19+ "gopkg.in/macaroon.v2"
1720)
1821
1922var (
@@ -210,6 +213,32 @@ func (s *Manager) MacaroonPath(uri string) (bool, string) {
210213 return false , ""
211214}
212215
216+ // ReadRemoteMacaroon checks if any of the manager's sub-servers running in
217+ // remote mode owns the given uri and if so, the appropriate macaroon path is
218+ // returned for that sub-server.
219+ func (s * Manager ) ReadRemoteMacaroon (uri string ) (bool , []byte , error ) {
220+ s .mu .RLock ()
221+ defer s .mu .RUnlock ()
222+
223+ for _ , ss := range s .servers {
224+ if ! s .permsMgr .IsSubServerURI (ss .Name (), uri ) {
225+ continue
226+ }
227+
228+ if ! ss .Remote () {
229+ return false , nil , nil
230+ }
231+
232+ macBytes , err := readMacaroon (lncfg .CleanAndExpandPath (
233+ ss .RemoteConfig ().MacaroonPath ,
234+ ))
235+
236+ return true , macBytes , err
237+ }
238+
239+ return false , nil , nil
240+ }
241+
213242// Stop stops all the manager's sub-servers
214243func (s * Manager ) Stop () error {
215244 var returnErr error
@@ -259,3 +288,21 @@ func dialBackend(name, dialAddr, tlsCertPath string) (*grpc.ClientConn, error) {
259288 }
260289 return cc , nil
261290}
291+
292+ // readMacaroon tries to read the macaroon file at the specified path.
293+ func readMacaroon (macPath string ) ([]byte , error ) {
294+ // Load the specified macaroon file.
295+ macBytes , err := ioutil .ReadFile (macPath )
296+ if err != nil {
297+ return nil , fmt .Errorf ("unable to read macaroon path: %v" , err )
298+ }
299+
300+ // Make sure it actually is a macaroon by parsing it.
301+ mac := & macaroon.Macaroon {}
302+ if err := mac .UnmarshalBinary (macBytes ); err != nil {
303+ return nil , fmt .Errorf ("unable to decode macaroon: %v" , err )
304+ }
305+
306+ // It's a macaroon alright, let's return the binary data now.
307+ return macBytes , nil
308+ }
0 commit comments