diff --git a/channelAcceptor.go b/channelAcceptor.go index 9811c21..96c2acf 100644 --- a/channelAcceptor.go +++ b/channelAcceptor.go @@ -60,7 +60,7 @@ func dispatchChannelAcceptor(ctx context.Context, conn *grpc.ClientConn, client } } else { - log.Infof("❌ [%s mode] Deny channel from %s", Configuration.Mode, trimPubKey(req.NodePubkey)) + log.Infof("❌ [%s mode] Reject channel from %s", Configuration.Mode, trimPubKey(req.NodePubkey)) res = lnrpc.ChannelAcceptResponse{Accept: false, Error: Configuration.RejectMessage} } diff --git a/htlcInterceptor.go b/htlcInterceptor.go index 51f7d15..46d8ca2 100644 --- a/htlcInterceptor.go +++ b/htlcInterceptor.go @@ -1,79 +1,10 @@ package main import ( - "context" - "encoding/hex" - "sync" - - "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" log "github.com/sirupsen/logrus" - "google.golang.org/grpc" ) -func dispatchHTLCInterceptor(ctx context.Context, conn *grpc.ClientConn) { - // wait group for channel acceptor - defer ctx.Value(ctxKeyWaitGroup).(*sync.WaitGroup).Done() - // get the lnd grpc connection - client := lnrpc.NewLightningClient(conn) - - acceptClient, err := client.ChannelAcceptor(ctx) - if err != nil { - panic(err) - } - log.Infof("Listening for incoming channel requests") - for { - req := lnrpc.ChannelAcceptRequest{} - err = acceptClient.RecvMsg(&req) - if err != nil { - log.Errorf(err.Error()) - } - log.Infof("New channel request from %s", hex.EncodeToString(req.NodePubkey)) - - var accept bool - - if Configuration.Mode == "whitelist" { - accept = false - for _, pubkey := range Configuration.Whitelist { - if hex.EncodeToString(req.NodePubkey) == pubkey { - accept = true - break - } - } - } else if Configuration.Mode == "blacklist" { - accept = true - for _, pubkey := range Configuration.Blacklist { - if hex.EncodeToString(req.NodePubkey) == pubkey { - accept = false - break - } - } - } - - res := lnrpc.ChannelAcceptResponse{} - if accept { - log.Infof("✅ [%s mode] Allow channel from %s", Configuration.Mode, trimPubKey(req.NodePubkey)) - res = lnrpc.ChannelAcceptResponse{Accept: true, - PendingChanId: req.PendingChanId, - CsvDelay: req.CsvDelay, - MaxHtlcCount: req.MaxAcceptedHtlcs, - ReserveSat: req.ChannelReserve, - InFlightMaxMsat: req.MaxValueInFlight, - MinHtlcIn: req.MinHtlc, - } - - } else { - log.Infof("❌ [%s mode] Deny channel from %s", Configuration.Mode, trimPubKey(req.NodePubkey)) - res = lnrpc.ChannelAcceptResponse{Accept: false, - Error: Configuration.RejectMessage} - } - err = acceptClient.Send(&res) - if err != nil { - log.Errorf(err.Error()) - } - } -} - func processHtlcEvents(stream routerrpc.Router_SubscribeHtlcEventsClient) error { for { event, err := stream.Recv() @@ -87,10 +18,10 @@ func processHtlcEvents(stream routerrpc.Router_SubscribeHtlcEventsClient) error switch event.Event.(type) { case *routerrpc.HtlcEvent_SettleEvent: - log.Infof("Settle %s %s", event.IncomingChannelId, event.IncomingHtlcId) + log.Infof("Event: Settle %d %d", event.IncomingChannelId, event.IncomingHtlcId) case *routerrpc.HtlcEvent_ForwardFailEvent: - log.Infof("ForwardFail %s %s", event.IncomingChannelId, event.IncomingHtlcId) + log.Infof("Event: ForwardFail %d %d", event.IncomingChannelId, event.IncomingHtlcId) } } } @@ -102,30 +33,17 @@ func processInterceptor(interceptor routerrpc.Router_HtlcInterceptorClient) erro return err } - // resumeChan := make(chan bool) - - print("I'm here") - // p.interceptChan <- interceptEvent{ - // circuitKey: circuitKey{ - // channel: event.IncomingCircuitKey.ChanId, - // htlc: event.IncomingCircuitKey.HtlcId, - // }, - // valueMsat: int64(event.OutgoingAmountMsat), - // resume: resumeChan, - // } - - // resume, ok := <-resumeChan - // if !ok { - // return errors.New("resume channel closed") - // } - resume := true + // decision for routing + accept := false response := &routerrpc.ForwardHtlcInterceptResponse{ IncomingCircuitKey: event.IncomingCircuitKey, } - if resume { + if accept { + log.Infof("✅ Accept HTLC (%d sat, %s)", event.IncomingAmountMsat/1000, event.IncomingCircuitKey.String()) response.Action = routerrpc.ResolveHoldForwardAction_RESUME } else { + log.Infof("❌ Reject HTLC (%d sat, %s)", event.IncomingAmountMsat/1000, event.IncomingCircuitKey.String()) response.Action = routerrpc.ResolveHoldForwardAction_FAIL } diff --git a/main.go b/main.go index 40d00d6..35db6dc 100644 --- a/main.go +++ b/main.go @@ -60,30 +60,24 @@ func trimPubKey(pubkey []byte) string { func main() { ctx := context.Background() - // conn, err := getClientConnection(ctx) - // if err != nil { - // panic(err) - // } + conn, err := getClientConnection(ctx) + if err != nil { + panic(err) + } var wg sync.WaitGroup ctx = context.WithValue(ctx, ctxKeyWaitGroup, &wg) wg.Add(1) - // client := lnrpc.NewLightningClient(conn) + // channel acceptor // go dispatchChannelAcceptor(ctx, conn, client) - // htlc interceptor - var router routerrpc.RouterClient + // htlc event subscriber, reports on incoming htlc events + router := routerrpc.NewRouterClient(conn) stream, err := router.SubscribeHtlcEvents(ctx, &routerrpc.SubscribeHtlcEventsRequest{}) if err != nil { return } - interceptor, err := router.HtlcInterceptor(ctx) - if err != nil { - return - } - - log.Info("HTLC Interceptor registered") go func() { err := processHtlcEvents(stream) @@ -93,6 +87,14 @@ func main() { } }() + // interceptor, decide whether to accept or reject + interceptor, err := router.HtlcInterceptor(ctx) + if err != nil { + return + } + + log.Info("HTLC Interceptor registered") + go func() { err := processInterceptor(interceptor) if err != nil {