mirror of
https://github.com/callebtc/electronwall.git
synced 2025-12-18 23:54:23 +01:00
initial commit
This commit is contained in:
31
config.go
Normal file
31
config.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jinzhu/configor"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Configuration = struct {
|
||||||
|
Host string `yaml:"host"`
|
||||||
|
MacaroonPath string `yaml:"macaroon_path"`
|
||||||
|
TLSPath string `yaml:"tls_path"`
|
||||||
|
Accept []string `yaml:"accept"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
err := configor.Load(&Configuration, "config.yaml")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
checkConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkConfig() {
|
||||||
|
if Configuration.Host == "" {
|
||||||
|
panic(fmt.Errorf("no host specified in config.yaml"))
|
||||||
|
}
|
||||||
|
if len(Configuration.Accept) == 0 {
|
||||||
|
panic(fmt.Errorf("no accepted pubkeys specified in config.yaml"))
|
||||||
|
}
|
||||||
|
}
|
||||||
6
config.yaml
Normal file
6
config.yaml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
host: "127.0.0.1:10009"
|
||||||
|
macaroon_path: "/home/bitcoin/.lnd/data/chain/bitcoin/regtest/admin.macaroon"
|
||||||
|
tls_path: "/home/bitcoin/.lnd/tls.cert"
|
||||||
|
accept:
|
||||||
|
- "03de70865239e99460041e127647b37101b9eb335b3c22de95c944671f0dabc2d0"
|
||||||
|
- "0307299a290529c5ccb3a5e3bd2eb504daf64cc65c6d65b582c01cbd7e5ede14b6"
|
||||||
11
go.mod
Normal file
11
go.mod
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
module github.com/callebtc/lnd_whitelist
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/jinzhu/configor v1.2.1
|
||||||
|
github.com/lightningnetwork/lnd v0.14.3-beta
|
||||||
|
github.com/sirupsen/logrus v1.8.1
|
||||||
|
google.golang.org/grpc v1.46.2
|
||||||
|
gopkg.in/macaroon.v2 v2.1.0
|
||||||
|
)
|
||||||
97
main.go
Normal file
97
main.go
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
|
"github.com/lightningnetwork/lnd/macaroons"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
|
"gopkg.in/macaroon.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// gets the lnd grpc connection
|
||||||
|
func getClientConnection(ctx context.Context) (*grpc.ClientConn, error) {
|
||||||
|
creds, err := credentials.NewClientTLSFromFile(Configuration.TLSPath, "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
macBytes, err := ioutil.ReadFile(Configuration.MacaroonPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mac := &macaroon.Macaroon{}
|
||||||
|
if err := mac.UnmarshalBinary(macBytes); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cred, err := macaroons.NewMacaroonCredential(mac)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
opts := []grpc.DialOption{
|
||||||
|
grpc.WithTransportCredentials(creds),
|
||||||
|
grpc.WithBlock(),
|
||||||
|
grpc.WithPerRPCCredentials(cred),
|
||||||
|
}
|
||||||
|
conn, err := grpc.DialContext(ctx, Configuration.Host, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
log.Infof("Connected to %s", Configuration.Host)
|
||||||
|
return conn, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
func main() {
|
||||||
|
conn, err := getClientConnection(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
client := lnrpc.NewLightningClient(conn)
|
||||||
|
acceptClient, err := client.ChannelAcceptor(context.Background())
|
||||||
|
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))
|
||||||
|
|
||||||
|
accept := false
|
||||||
|
for _, pubkey := range Configuration.Accept {
|
||||||
|
if hex.EncodeToString(req.NodePubkey) == pubkey {
|
||||||
|
accept = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res := lnrpc.ChannelAcceptResponse{}
|
||||||
|
if accept {
|
||||||
|
log.Infof("Accepting channel request from %s", hex.EncodeToString(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("Rejecting channel request from %s", hex.EncodeToString(req.NodePubkey))
|
||||||
|
res = lnrpc.ChannelAcceptResponse{Accept: false}
|
||||||
|
}
|
||||||
|
err = acceptClient.Send(&res)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user