diff --git a/config.go b/config.go index fd2375d67..13f13ec68 100644 --- a/config.go +++ b/config.go @@ -77,6 +77,8 @@ const ( // DefaultMacaroonFilename is the default file name for the // autogenerated lit macaroon. DefaultMacaroonFilename = "lit.macaroon" + + defaultFirstLNCConnTimeout = 10 * time.Minute ) var ( @@ -153,6 +155,8 @@ type Config struct { MacaroonPath string `long:"macaroonpath" description:"Path to write the macaroon for litd's RPC and REST services if it doesn't exist."` + FirstLNCConnDeadline time.Duration `long:"firstlncconndeadline" description:"The duration after a new LNC session will be revoked if no connection is made with it. This only applies for the first connection which is made using the pairing phrase. "` + // Network is the Bitcoin network we're running on. This will be parsed // before the configuration is loaded and will set the correct flag on // `lnd.bitcoin.mainnet|testnet|regtest` and also for the other daemons. @@ -302,21 +306,22 @@ func defaultConfig() *Config { TLSCertPath: poolDefaultConfig.TLSCertPath, }, }, - Network: DefaultNetwork, - LndMode: DefaultLndMode, - Lnd: &lndDefaultConfig, - LitDir: DefaultLitDir, - LetsEncryptListen: defaultLetsEncryptListen, - LetsEncryptDir: defaultLetsEncryptDir, - MacaroonPath: DefaultMacaroonPath, - ConfigFile: defaultConfigFile, - FaradayMode: defaultFaradayMode, - Faraday: &faradayDefaultConfig, - faradayRpcConfig: &frdrpcserver.Config{}, - LoopMode: defaultLoopMode, - Loop: &loopDefaultConfig, - PoolMode: defaultPoolMode, - Pool: &poolDefaultConfig, + Network: DefaultNetwork, + LndMode: DefaultLndMode, + Lnd: &lndDefaultConfig, + LitDir: DefaultLitDir, + LetsEncryptListen: defaultLetsEncryptListen, + LetsEncryptDir: defaultLetsEncryptDir, + MacaroonPath: DefaultMacaroonPath, + ConfigFile: defaultConfigFile, + FaradayMode: defaultFaradayMode, + Faraday: &faradayDefaultConfig, + faradayRpcConfig: &frdrpcserver.Config{}, + LoopMode: defaultLoopMode, + Loop: &loopDefaultConfig, + PoolMode: defaultPoolMode, + Pool: &poolDefaultConfig, + FirstLNCConnDeadline: defaultFirstLNCConnTimeout, } } diff --git a/go.mod b/go.mod index 00e8aeb25..219569641 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/improbable-eng/grpc-web v0.12.0 github.com/jessevdk/go-flags v1.4.0 github.com/lightninglabs/faraday v0.2.8-alpha.0.20220624141723-ddd3cd123e62 - github.com/lightninglabs/lightning-node-connect v0.1.11-alpha + github.com/lightninglabs/lightning-node-connect v0.1.11-alpha.0.20220822151854-c072f70315d8 github.com/lightninglabs/lndclient v0.15.0-10 github.com/lightninglabs/loop v0.19.1-beta.0.20220623090540-08209f61e304 github.com/lightninglabs/loop/swapserverrpc v1.0.1 diff --git a/go.sum b/go.sum index 6650a1af0..c1ff6c5eb 100644 --- a/go.sum +++ b/go.sum @@ -541,8 +541,8 @@ github.com/lightninglabs/faraday v0.2.8-alpha.0.20220624141723-ddd3cd123e62 h1:t github.com/lightninglabs/faraday v0.2.8-alpha.0.20220624141723-ddd3cd123e62/go.mod h1:9kcDuyINNf4RB6vrmPLAMGZmYgN0oPxhdt3IicL9sQY= github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf h1:HZKvJUHlcXI/f/O0Avg7t8sqkPo78HFzjmeYFl6DPnc= github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk= -github.com/lightninglabs/lightning-node-connect v0.1.11-alpha h1:d+jKyAvCQMLq5O1IL6ONWM/l4C7Q3Q00HkdjDIG9uTg= -github.com/lightninglabs/lightning-node-connect v0.1.11-alpha/go.mod h1:dgyhE+O4GpWBhS7yIzKCm8LqFHX8/QRwJ6OWFrN+WnA= +github.com/lightninglabs/lightning-node-connect v0.1.11-alpha.0.20220822151854-c072f70315d8 h1:FYZ63sERRC2RjVbsAeO55uF7kTIYCClHmd59dZSsyx0= +github.com/lightninglabs/lightning-node-connect v0.1.11-alpha.0.20220822151854-c072f70315d8/go.mod h1:dgyhE+O4GpWBhS7yIzKCm8LqFHX8/QRwJ6OWFrN+WnA= github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2 h1:Er1miPZD2XZwcfE4xoS5AILqP1mj7kqnhbBSxW9BDxY= github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2/go.mod h1:antQGRDRJiuyQF6l+k6NECCSImgCpwaZapATth2Chv4= github.com/lightninglabs/lndclient v0.15.0-0/go.mod h1:ORS/YFe9hAXlzN/Uj+gvTmrnXEml6yD6dWwzCjpTJyQ= diff --git a/itest/litd_mode_integrated_test.go b/itest/litd_mode_integrated_test.go index 8656ec496..0bb0594e7 100644 --- a/itest/litd_mode_integrated_test.go +++ b/itest/litd_mode_integrated_test.go @@ -612,6 +612,10 @@ func runLNCAuthTest(t *testing.T, hostPort, tlsCertPath, macPath string, connectPhrase := strings.Split( sessResp.Session.PairingSecretMnemonic, " ", ) + + ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout) + defer cancel() + rawLNCConn, err := connectMailbox(ctxt, connectPhrase) require.NoError(t, err) diff --git a/itest/litd_test.go b/itest/litd_test.go index 7425945f6..a00ff5b71 100644 --- a/itest/litd_test.go +++ b/itest/litd_test.go @@ -8,10 +8,18 @@ import ( "time" "github.com/btcsuite/btcd/integration/rpctest" + "github.com/lightninglabs/aperture" + "github.com/lightninglabs/lightning-node-connect/gbn" + "github.com/lightninglabs/lightning-node-connect/mailbox" + "github.com/lightningnetwork/lnd" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/lntest" + "github.com/lightningnetwork/lnd/signal" "github.com/stretchr/testify/require" ) +var interceptor *signal.Interceptor + // TestLightningTerminal performs a series of integration tests amongst a // programmatically driven network of lnd nodes. func TestLightningTerminal(t *testing.T) { @@ -73,6 +81,7 @@ func TestLightningTerminal(t *testing.T) { // Now we can set up our test harness (LND instance), with the chain // backend we just created. ht := newHarnessTest(t, nil) + ht.setupLogging() binary := getLitdBinary() litdHarness, err = NewNetworkHarness(miner, chainBackend, binary) if err != nil { @@ -164,3 +173,24 @@ func TestLightningTerminal(t *testing.T) { } } } + +func (h *harnessTest) setupLogging() { + logWriter := build.NewRotatingLogWriter() + + if interceptor != nil { + return + } + + ic, err := signal.Intercept() + require.NoError(h.t, err) + interceptor = &ic + + aperture.SetupLoggers(logWriter, *interceptor) + lnd.AddSubLogger( + logWriter, mailbox.Subsystem, *interceptor, mailbox.UseLogger, + ) + lnd.AddSubLogger(logWriter, gbn.Subsystem, *interceptor, gbn.UseLogger) + + err = build.ParseAndSetDebugLevels("debug", logWriter) + require.NoError(h.t, err) +} diff --git a/litrpc/lit-sessions.pb.go b/litrpc/lit-sessions.pb.go index fe2ed19ed..7170f4e74 100644 --- a/litrpc/lit-sessions.pb.go +++ b/litrpc/lit-sessions.pb.go @@ -330,6 +330,7 @@ type Session struct { PairingSecretMnemonic string `protobuf:"bytes,8,opt,name=pairing_secret_mnemonic,json=pairingSecretMnemonic,proto3" json:"pairing_secret_mnemonic,omitempty"` LocalPublicKey []byte `protobuf:"bytes,9,opt,name=local_public_key,json=localPublicKey,proto3" json:"local_public_key,omitempty"` RemotePublicKey []byte `protobuf:"bytes,10,opt,name=remote_public_key,json=remotePublicKey,proto3" json:"remote_public_key,omitempty"` + CreatedAt uint64 `protobuf:"varint,11,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` } func (x *Session) Reset() { @@ -434,6 +435,13 @@ func (x *Session) GetRemotePublicKey() []byte { return nil } +func (x *Session) GetCreatedAt() uint64 { + if x != nil { + return x.CreatedAt + } + return 0 +} + type ListSessionsRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -638,7 +646,7 @@ var file_lit_sessions_proto_rawDesc = []byte{ 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x29, 0x0a, 0x07, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x07, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, - 0x22, 0xd4, 0x03, 0x0a, 0x07, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x14, 0x0a, 0x05, + 0x22, 0xf7, 0x03, 0x0a, 0x07, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x12, 0x39, 0x0a, 0x0d, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x14, 0x2e, 0x6c, 0x69, 0x74, 0x72, @@ -667,50 +675,52 @@ var file_lit_sessions_proto_rawDesc = []byte{ 0x61, 0x6c, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x12, 0x2a, 0x0a, 0x11, 0x72, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x5f, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x6b, 0x65, 0x79, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0f, 0x72, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x50, 0x75, - 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x22, 0x15, 0x0a, 0x13, 0x4c, 0x69, 0x73, 0x74, 0x53, - 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x43, - 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2b, 0x0a, 0x08, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, - 0x6e, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, - 0x63, 0x2e, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x73, 0x65, 0x73, 0x73, 0x69, - 0x6f, 0x6e, 0x73, 0x22, 0x40, 0x0a, 0x14, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, 0x73, - 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x28, 0x0a, 0x10, 0x6c, - 0x6f, 0x63, 0x61, 0x6c, 0x5f, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x6b, 0x65, 0x79, 0x18, - 0x08, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x50, 0x75, 0x62, 0x6c, - 0x69, 0x63, 0x4b, 0x65, 0x79, 0x22, 0x17, 0x0a, 0x15, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, 0x53, - 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2a, 0x72, - 0x0a, 0x0b, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1a, 0x0a, - 0x16, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x4d, 0x41, 0x43, 0x41, 0x52, 0x4f, 0x4f, 0x4e, 0x5f, 0x52, - 0x45, 0x41, 0x44, 0x4f, 0x4e, 0x4c, 0x59, 0x10, 0x00, 0x12, 0x17, 0x0a, 0x13, 0x54, 0x59, 0x50, - 0x45, 0x5f, 0x4d, 0x41, 0x43, 0x41, 0x52, 0x4f, 0x4f, 0x4e, 0x5f, 0x41, 0x44, 0x4d, 0x49, 0x4e, - 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x4d, 0x41, 0x43, 0x41, 0x52, - 0x4f, 0x4f, 0x4e, 0x5f, 0x43, 0x55, 0x53, 0x54, 0x4f, 0x4d, 0x10, 0x02, 0x12, 0x14, 0x0a, 0x10, - 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x49, 0x5f, 0x50, 0x41, 0x53, 0x53, 0x57, 0x4f, 0x52, 0x44, - 0x10, 0x03, 0x2a, 0x59, 0x0a, 0x0c, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x61, - 0x74, 0x65, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x54, 0x41, 0x54, 0x45, 0x5f, 0x43, 0x52, 0x45, 0x41, - 0x54, 0x45, 0x44, 0x10, 0x00, 0x12, 0x10, 0x0a, 0x0c, 0x53, 0x54, 0x41, 0x54, 0x45, 0x5f, 0x49, - 0x4e, 0x5f, 0x55, 0x53, 0x45, 0x10, 0x01, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x54, 0x41, 0x54, 0x45, - 0x5f, 0x52, 0x45, 0x56, 0x4f, 0x4b, 0x45, 0x44, 0x10, 0x02, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x54, - 0x41, 0x54, 0x45, 0x5f, 0x45, 0x58, 0x50, 0x49, 0x52, 0x45, 0x44, 0x10, 0x03, 0x32, 0xe8, 0x01, - 0x0a, 0x08, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x43, 0x0a, 0x0a, 0x41, 0x64, - 0x64, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x19, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, - 0x63, 0x2e, 0x41, 0x64, 0x64, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x41, 0x64, 0x64, - 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, - 0x49, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x12, - 0x1b, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x65, 0x73, - 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x6c, - 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, - 0x6e, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x4c, 0x0a, 0x0d, 0x52, 0x65, - 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1c, 0x2e, 0x6c, 0x69, - 0x74, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, 0x73, 0x73, 0x69, - 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x6c, 0x69, 0x74, 0x72, - 0x70, 0x63, 0x2e, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x34, 0x5a, 0x32, 0x67, 0x69, 0x74, 0x68, - 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x6e, 0x69, 0x6e, 0x67, - 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x6e, 0x69, 0x6e, 0x67, 0x2d, 0x74, - 0x65, 0x72, 0x6d, 0x69, 0x6e, 0x61, 0x6c, 0x2f, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x12, 0x21, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, + 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x04, 0x42, 0x02, 0x30, 0x01, 0x52, + 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x15, 0x0a, 0x13, 0x4c, 0x69, + 0x73, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x22, 0x43, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, + 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2b, 0x0a, 0x08, 0x73, 0x65, 0x73, + 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x6c, 0x69, + 0x74, 0x72, 0x70, 0x63, 0x2e, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x73, 0x65, + 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x40, 0x0a, 0x14, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, + 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x28, + 0x0a, 0x10, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x5f, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x6b, + 0x65, 0x79, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x50, + 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x22, 0x17, 0x0a, 0x15, 0x52, 0x65, 0x76, 0x6f, + 0x6b, 0x65, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x2a, 0x72, 0x0a, 0x0b, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, + 0x12, 0x1a, 0x0a, 0x16, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x4d, 0x41, 0x43, 0x41, 0x52, 0x4f, 0x4f, + 0x4e, 0x5f, 0x52, 0x45, 0x41, 0x44, 0x4f, 0x4e, 0x4c, 0x59, 0x10, 0x00, 0x12, 0x17, 0x0a, 0x13, + 0x54, 0x59, 0x50, 0x45, 0x5f, 0x4d, 0x41, 0x43, 0x41, 0x52, 0x4f, 0x4f, 0x4e, 0x5f, 0x41, 0x44, + 0x4d, 0x49, 0x4e, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x4d, 0x41, + 0x43, 0x41, 0x52, 0x4f, 0x4f, 0x4e, 0x5f, 0x43, 0x55, 0x53, 0x54, 0x4f, 0x4d, 0x10, 0x02, 0x12, + 0x14, 0x0a, 0x10, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x49, 0x5f, 0x50, 0x41, 0x53, 0x53, 0x57, + 0x4f, 0x52, 0x44, 0x10, 0x03, 0x2a, 0x59, 0x0a, 0x0c, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, + 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x54, 0x41, 0x54, 0x45, 0x5f, 0x43, + 0x52, 0x45, 0x41, 0x54, 0x45, 0x44, 0x10, 0x00, 0x12, 0x10, 0x0a, 0x0c, 0x53, 0x54, 0x41, 0x54, + 0x45, 0x5f, 0x49, 0x4e, 0x5f, 0x55, 0x53, 0x45, 0x10, 0x01, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x54, + 0x41, 0x54, 0x45, 0x5f, 0x52, 0x45, 0x56, 0x4f, 0x4b, 0x45, 0x44, 0x10, 0x02, 0x12, 0x11, 0x0a, + 0x0d, 0x53, 0x54, 0x41, 0x54, 0x45, 0x5f, 0x45, 0x58, 0x50, 0x49, 0x52, 0x45, 0x44, 0x10, 0x03, + 0x32, 0xe8, 0x01, 0x0a, 0x08, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x43, 0x0a, + 0x0a, 0x41, 0x64, 0x64, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x19, 0x2e, 0x6c, 0x69, + 0x74, 0x72, 0x70, 0x63, 0x2e, 0x41, 0x64, 0x64, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, + 0x41, 0x64, 0x64, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x49, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, + 0x6e, 0x73, 0x12, 0x1b, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, + 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x1c, 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x65, 0x73, + 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x4c, 0x0a, + 0x0d, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1c, + 0x2e, 0x6c, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, + 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x6c, + 0x69, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x65, 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, 0x73, 0x73, + 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x34, 0x5a, 0x32, 0x67, + 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x6e, + 0x69, 0x6e, 0x67, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x6e, 0x69, 0x6e, + 0x67, 0x2d, 0x74, 0x65, 0x72, 0x6d, 0x69, 0x6e, 0x61, 0x6c, 0x2f, 0x6c, 0x69, 0x74, 0x72, 0x70, + 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/litrpc/lit-sessions.proto b/litrpc/lit-sessions.proto index e1df131ed..234d726d8 100644 --- a/litrpc/lit-sessions.proto +++ b/litrpc/lit-sessions.proto @@ -74,6 +74,8 @@ message Session { bytes local_public_key = 9; bytes remote_public_key = 10; + + uint64 created_at = 11 [jstype = JS_STRING]; } message ListSessionsRequest { diff --git a/session/interface.go b/session/interface.go index ac8468c44..37f4a6275 100644 --- a/session/interface.go +++ b/session/interface.go @@ -43,6 +43,7 @@ type Session struct { State State Type Type Expiry time.Time + CreatedAt time.Time ServerAddr string DevServer bool MacaroonRootKey uint64 @@ -78,6 +79,7 @@ func NewSession(label string, typ Type, expiry time.Time, serverAddr string, State: StateCreated, Type: typ, Expiry: expiry, + CreatedAt: time.Now(), ServerAddr: serverAddr, DevServer: devServer, MacaroonRootKey: macRootKey, diff --git a/session/server.go b/session/server.go index 1581cb3db..daa7243ee 100644 --- a/session/server.go +++ b/session/server.go @@ -33,7 +33,8 @@ func newMailboxSession() *mailboxSession { func (m *mailboxSession) start(session *Session, serverCreator GRPCServerCreator, authData []byte, - onUpdate func(sess *Session) error) error { + onUpdate func(sess *Session) error, + onNewStatus func(s mailbox.ServerStatus)) error { tlsConfig := &tls.Config{} if session.DevServer { @@ -52,7 +53,7 @@ func (m *mailboxSession) start(session *Session, // Start the mailbox gRPC server. mailboxServer, err := mailbox.NewServer( - session.ServerAddr, keys, + session.ServerAddr, keys, onNewStatus, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithKeepaliveParams(keepalive.ClientParameters{ Time: 2 * time.Minute, @@ -104,7 +105,8 @@ func NewServer(serverCreator GRPCServerCreator) *Server { } func (s *Server) StartSession(session *Session, authData []byte, - onUpdate func(sess *Session) error) (chan struct{}, error) { + onUpdate func(sess *Session) error, + onNewStatus func(s mailbox.ServerStatus)) (chan struct{}, error) { s.activeSessionsMtx.Lock() defer s.activeSessionsMtx.Unlock() @@ -121,7 +123,7 @@ func (s *Server) StartSession(session *Session, authData []byte, s.activeSessions[id] = sess return sess.quit, sess.start( - session, s.serverCreator, authData, onUpdate, + session, s.serverCreator, authData, onUpdate, onNewStatus, ) } diff --git a/session/tlv.go b/session/tlv.go index b297b23d7..2cae453db 100644 --- a/session/tlv.go +++ b/session/tlv.go @@ -24,6 +24,7 @@ const ( typeLocalPrivateKey tlv.Type = 10 typeRemotePublicKey tlv.Type = 11 typeMacaroonRecipe tlv.Type = 12 + typeCreatedAt tlv.Type = 13 // typeMacaroon is no longer used, but we leave it defined for backwards // compatibility. @@ -56,6 +57,7 @@ func SerializeSession(w io.Writer, session *Session) error { devServer = uint8(0) pairingSecret = session.PairingSecret[:] privateKey = session.LocalPrivateKey.Serialize() + createdAt = uint64(session.CreatedAt.Unix()) ) if session.DevServer { @@ -99,6 +101,10 @@ func SerializeSession(w io.Writer, session *Session) error { )) } + tlvRecords = append( + tlvRecords, tlv.MakePrimitiveRecord(typeCreatedAt, &createdAt), + ) + tlvStream, err := tlv.NewStream(tlvRecords...) if err != nil { return err @@ -115,7 +121,7 @@ func DeserializeSession(r io.Reader) (*Session, error) { label, serverAddr []byte pairingSecret, privateKey []byte state, typ, devServer uint8 - expiry uint64 + expiry, createdAt uint64 macRecipe MacaroonRecipe ) tlvStream, err := tlv.NewStream( @@ -137,6 +143,7 @@ func DeserializeSession(r io.Reader) (*Session, error) { typeMacaroonRecipe, &macRecipe, nil, macaroonRecipeEncoder, macaroonRecipeDecoder, ), + tlv.MakePrimitiveRecord(typeCreatedAt, &createdAt), ) if err != nil { return nil, err @@ -151,6 +158,7 @@ func DeserializeSession(r io.Reader) (*Session, error) { session.State = State(state) session.Type = Type(typ) session.Expiry = time.Unix(int64(expiry), 0) + session.CreatedAt = time.Unix(int64(createdAt), 0) session.ServerAddr = string(serverAddr) session.DevServer = devServer == 1 diff --git a/session/tlv_test.go b/session/tlv_test.go index 005ca588d..52f370035 100644 --- a/session/tlv_test.go +++ b/session/tlv_test.go @@ -99,6 +99,9 @@ func TestSerializeDeserializeSession(t *testing.T) { ) session.Expiry = time.Time{} deserializedSession.Expiry = time.Time{} + session.CreatedAt = time.Time{} + deserializedSession.CreatedAt = time.Time{} + require.Equal(t, session, deserializedSession) }) } diff --git a/session_rpcserver.go b/session_rpcserver.go index 547c71345..ed4032157 100644 --- a/session_rpcserver.go +++ b/session_rpcserver.go @@ -38,6 +38,7 @@ type sessionRpcServerConfig struct { registerGrpcServers func(server *grpc.Server) superMacBaker func(ctx context.Context, rootKeyID uint64, recipe *session.MacaroonRecipe) (string, error) + firstConnectionDeadline time.Duration } // newSessionRPCServer creates a new sessionRpcServer using the passed config. @@ -214,10 +215,57 @@ func (s *sessionRpcServer) resumeSession(sess *session.Session) error { return nil } - authData := []byte(fmt.Sprintf("%s: %s", HeaderMacaroon, mac)) + var ( + onNewStatus func(s mailbox.ServerStatus) + firstConnTimout = make(chan struct{}) + ) + + // If this is the first time the session is being spun up then we will + // kick off a timer to revoke the session after a timeout unless an + // initial connection is made. We identify such a session as one that + // we do not yet have a static remote pub key for. + if sess.RemotePublicKey == nil { + deadline := sess.CreatedAt.Add(s.cfg.firstConnectionDeadline) + if deadline.Before(time.Now()) { + log.Debugf("Deadline for session %x has already "+ + "passed. Revoking session", pubKeyBytes) + + return s.db.RevokeSession(pubKey) + } + + // Start the deadline timer. + deadlineDuration := time.Until(deadline) + deadlineTimer := time.AfterFunc(deadlineDuration, func() { + close(firstConnTimout) + }) + + log.Warnf("Kicking off deadline timer for first connection "+ + "for session %x. A successful connection must be "+ + "made in the next %s", pubKeyBytes, deadlineDuration) + + var stopTimerOnce sync.Once + onNewStatus = func(s mailbox.ServerStatus) { + // We will only stop the timer if the server status + // indicates that the client has successfully connected. + if s != mailbox.ServerStatusInUse { + return + } + + // Stop the deadline timer. + stopTimerOnce.Do(func() { + log.Debugf("First connection for session %x "+ + "made in a timely manner", + sess.LocalPublicKey. + SerializeCompressed()) + + deadlineTimer.Stop() + }) + } + } + authData := []byte(fmt.Sprintf("%s: %s", HeaderMacaroon, mac)) sessionClosedSub, err := s.sessionServer.StartSession( - sess, authData, s.db.StoreSession, + sess, authData, s.db.StoreSession, onNewStatus, ) if err != nil { return err @@ -232,22 +280,31 @@ func (s *sessionRpcServer) resumeSession(sess *session.Session) error { select { case <-s.quit: + return + case <-sessionClosedSub: + return + case <-ticker.C: log.Debugf("Stopping expired session %x with "+ "type %d", pubKeyBytes, sess.Type) - err = s.sessionServer.StopSession(pubKey) - if err != nil { - log.Debugf("Error stopping session: "+ - "%v", err) - } + case <-firstConnTimout: + log.Debugf("Deadline exceeded for first connection "+ + "for session %x. Stopping and revoking.", + pubKeyBytes) + } - err = s.db.RevokeSession(pubKey) - if err != nil { - log.Debugf("error revoking session: "+ - "%v", err) - } + err = s.sessionServer.StopSession(pubKey) + if err != nil { + log.Debugf("Error stopping session: "+ + "%v", err) + } + + err = s.db.RevokeSession(pubKey) + if err != nil { + log.Debugf("error revoking session: "+ + "%v", err) } }() @@ -333,6 +390,7 @@ func marshalRPCSession(sess *session.Session) (*litrpc.Session, error) { PairingSecretMnemonic: strings.Join(mnemonic[:], " "), LocalPublicKey: sess.LocalPublicKey.SerializeCompressed(), RemotePublicKey: remotePubKey, + CreatedAt: uint64(sess.CreatedAt.Unix()), }, nil } diff --git a/terminal.go b/terminal.go index f69c98d3a..710266db5 100644 --- a/terminal.go +++ b/terminal.go @@ -232,6 +232,7 @@ func (g *LightningTerminal) Run() error { recipe.Permissions, recipe.Caveats, ) }, + firstConnectionDeadline: g.cfg.FirstLNCConnDeadline, }) if err != nil { return fmt.Errorf("could not create new session rpc "+