2015-12-26 07:09:17 +01:00
|
|
|
package main
|
2016-01-14 06:41:46 +01:00
|
|
|
|
|
|
|
import (
|
2016-01-17 04:07:44 +01:00
|
|
|
"encoding/hex"
|
2016-01-14 06:41:46 +01:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2016-03-23 02:49:22 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-01-17 04:07:44 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lndc"
|
2016-01-16 19:38:48 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2016-05-15 16:17:44 +02:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2016-01-14 06:41:46 +01:00
|
|
|
|
2016-05-15 16:17:44 +02:00
|
|
|
"github.com/roasbeef/btcwallet/waddrmgr"
|
2016-01-14 06:41:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// server...
|
|
|
|
type server struct {
|
|
|
|
started int32 // atomic
|
|
|
|
shutdown int32 // atomic
|
|
|
|
|
2016-01-17 04:07:44 +01:00
|
|
|
longTermPriv *btcec.PrivateKey
|
|
|
|
|
|
|
|
listeners []net.Listener
|
|
|
|
peers map[int32]*peer
|
2016-01-14 06:41:46 +01:00
|
|
|
|
|
|
|
rpcServer *rpcServer
|
|
|
|
lnwallet *lnwallet.LightningWallet
|
2016-03-23 02:49:22 +01:00
|
|
|
chanDB *channeldb.DB
|
2016-01-14 06:41:46 +01:00
|
|
|
|
|
|
|
newPeers chan *peer
|
|
|
|
donePeers chan *peer
|
2016-01-17 04:07:44 +01:00
|
|
|
queries chan interface{}
|
2016-01-14 06:41:46 +01:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:07:44 +01:00
|
|
|
// newServer...
|
2016-03-23 02:49:22 +01:00
|
|
|
func newServer(listenAddrs []string, wallet *lnwallet.LightningWallet,
|
|
|
|
chanDB *channeldb.DB) (*server, error) {
|
|
|
|
|
|
|
|
privKey, err := getIdentityPrivKey(chanDB, wallet)
|
2016-01-17 04:07:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := make([]net.Listener, len(listenAddrs))
|
|
|
|
for i, addr := range listenAddrs {
|
|
|
|
listeners[i], err = lndc.NewListener(privKey, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &server{
|
2016-03-23 02:49:22 +01:00
|
|
|
chanDB: chanDB,
|
2016-01-17 04:07:44 +01:00
|
|
|
longTermPriv: privKey,
|
|
|
|
listeners: listeners,
|
|
|
|
peers: make(map[int32]*peer),
|
|
|
|
newPeers: make(chan *peer, 100),
|
|
|
|
donePeers: make(chan *peer, 100),
|
|
|
|
lnwallet: wallet,
|
|
|
|
queries: make(chan interface{}),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rpcServer = newRpcServer(s)
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2016-01-14 06:41:46 +01:00
|
|
|
// addPeer...
|
|
|
|
func (s *server) addPeer(p *peer) {
|
2016-01-17 04:09:02 +01:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore new peers if we're shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
p.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.peers[p.peerId] = p
|
2016-01-14 06:41:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// removePeer...
|
|
|
|
func (s *server) removePeer(p *peer) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// peerManager...
|
|
|
|
func (s *server) peerManager() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// New peers.
|
|
|
|
case p := <-s.newPeers:
|
|
|
|
s.addPeer(p)
|
|
|
|
// Finished peers.
|
|
|
|
case p := <-s.donePeers:
|
|
|
|
s.removePeer(p)
|
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:09:02 +01:00
|
|
|
// connectPeerMsg...
|
|
|
|
type connectPeerMsg struct {
|
2016-01-17 19:45:07 +01:00
|
|
|
addr *lndc.LNAdr
|
2016-01-17 04:09:02 +01:00
|
|
|
reply chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryHandler...
|
2016-01-14 06:41:46 +01:00
|
|
|
func (s *server) queryHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2016-01-17 04:09:02 +01:00
|
|
|
case query := <-s.queries:
|
|
|
|
switch msg := query.(type) {
|
|
|
|
case *connectPeerMsg:
|
|
|
|
addr := msg.addr
|
|
|
|
|
|
|
|
// Ensure we're not already connected to this
|
|
|
|
// peer.
|
|
|
|
for _, peer := range s.peers {
|
|
|
|
if peer.lightningAddr.String() ==
|
|
|
|
addr.String() {
|
|
|
|
msg.reply <- fmt.Errorf(
|
|
|
|
"already connected to peer: %v",
|
|
|
|
peer.lightningAddr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch a goroutine to connect to the requested
|
|
|
|
// peer so we can continue to handle queries.
|
|
|
|
go func() {
|
|
|
|
// For the lndc crypto handshake, we
|
|
|
|
// either need a compressed pubkey, or a
|
|
|
|
// 20-byte pkh.
|
|
|
|
var remoteId []byte
|
2016-01-17 19:45:07 +01:00
|
|
|
if addr.PubKey == nil {
|
2016-01-19 09:05:02 +01:00
|
|
|
remoteId = addr.Base58Adr.ScriptAddress()
|
2016-01-17 04:09:02 +01:00
|
|
|
} else {
|
2016-01-17 19:45:07 +01:00
|
|
|
remoteId = addr.PubKey.SerializeCompressed()
|
2016-01-17 04:09:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to connect to the remote
|
|
|
|
// node. If the we can't make the
|
|
|
|
// connection, or the crypto negotation
|
|
|
|
// breaks down, then return an error to the
|
|
|
|
// caller.
|
2016-01-17 19:45:07 +01:00
|
|
|
ipAddr := addr.NetAddr.String()
|
2016-01-17 20:20:40 +01:00
|
|
|
conn := lndc.NewConn(nil)
|
|
|
|
if err := conn.Dial(
|
|
|
|
s.longTermPriv, ipAddr, remoteId); err != nil {
|
2016-01-17 04:09:02 +01:00
|
|
|
msg.reply <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've established a connection,
|
|
|
|
// create a peer, and it to the set of
|
|
|
|
// currently active peers.
|
|
|
|
peer := newPeer(conn, s)
|
|
|
|
s.newPeers <- peer
|
|
|
|
|
|
|
|
msg.reply <- nil
|
|
|
|
}()
|
|
|
|
}
|
2016-01-14 06:41:46 +01:00
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:09:02 +01:00
|
|
|
// ConnectToPeer...
|
2016-01-17 19:45:07 +01:00
|
|
|
func (s *server) ConnectToPeer(addr *lndc.LNAdr) error {
|
2016-01-17 04:09:02 +01:00
|
|
|
reply := make(chan error, 1)
|
|
|
|
|
|
|
|
s.queries <- &connectPeerMsg{addr, reply}
|
|
|
|
|
|
|
|
return <-reply
|
|
|
|
}
|
|
|
|
|
2016-01-14 06:41:46 +01:00
|
|
|
// AddPeer...
|
|
|
|
func (s *server) AddPeer(p *peer) {
|
|
|
|
s.newPeers <- p
|
|
|
|
}
|
|
|
|
|
|
|
|
// listener...
|
|
|
|
func (s *server) listener(l net.Listener) {
|
2016-03-23 02:49:22 +01:00
|
|
|
srvrLog.Infof("Server listening on %s", l.Addr())
|
2016-01-14 06:41:46 +01:00
|
|
|
for atomic.LoadInt32(&s.shutdown) == 0 {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
2016-04-24 21:40:23 +02:00
|
|
|
// Only log the error message if we aren't currently
|
|
|
|
// shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) == 0 {
|
|
|
|
srvrLog.Errorf("Can't accept connection: %v", err)
|
|
|
|
}
|
2016-01-14 06:41:46 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-01-17 04:09:02 +01:00
|
|
|
|
2016-03-23 02:49:22 +01:00
|
|
|
srvrLog.Tracef("New inbound connection from %v", conn.RemoteAddr())
|
2016-01-17 04:09:02 +01:00
|
|
|
peer := newPeer(conn, s)
|
|
|
|
peer.Start()
|
2016-01-14 06:41:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start...
|
|
|
|
func (s *server) Start() {
|
|
|
|
// Already running?
|
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start all the listeners.
|
|
|
|
for _, l := range s.listeners {
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.listener(l)
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:09:02 +01:00
|
|
|
s.wg.Add(2)
|
2016-01-14 06:41:46 +01:00
|
|
|
go s.peerManager()
|
2016-01-17 04:09:02 +01:00
|
|
|
go s.queryHandler()
|
2016-01-14 06:41:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop...
|
|
|
|
func (s *server) Stop() error {
|
|
|
|
// Bail if we're already shutting down.
|
|
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop all the listeners.
|
|
|
|
for _, listener := range s.listeners {
|
|
|
|
if err := listener.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rpcServer.Stop()
|
2016-04-24 21:40:40 +02:00
|
|
|
s.lnwallet.Shutdown()
|
2016-01-14 06:41:46 +01:00
|
|
|
|
|
|
|
// Signal all the lingering goroutines to quit.
|
|
|
|
close(s.quit)
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-17 04:05:13 +01:00
|
|
|
|
|
|
|
// getIdentityPrivKey gets the identity private key out of the wallet DB.
|
2016-03-23 02:49:22 +01:00
|
|
|
func getIdentityPrivKey(c *channeldb.DB, w *lnwallet.LightningWallet) (*btcec.PrivateKey, error) {
|
|
|
|
adr, err := c.GetIdAdr()
|
2016-01-17 04:05:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 02:49:22 +01:00
|
|
|
ltndLog.Infof("got ID address: %s", adr.String())
|
|
|
|
adr2, err := w.Manager.Address(adr)
|
2016-01-17 04:05:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 02:49:22 +01:00
|
|
|
ltndLog.Infof("pubkey: %v", hex.EncodeToString(adr2.(waddrmgr.ManagedPubKeyAddress).PubKey().SerializeCompressed()))
|
2016-01-17 04:05:13 +01:00
|
|
|
priv, err := adr2.(waddrmgr.ManagedPubKeyAddress).PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return priv, nil
|
|
|
|
}
|
2016-03-23 02:49:22 +01:00
|
|
|
|
|
|
|
// WaitForShutdown blocks all goroutines have been stopped.
|
|
|
|
func (s *server) WaitForShutdown() {
|
|
|
|
s.wg.Wait()
|
|
|
|
}
|