2020-01-08 10:45:27 +02:00

69 lines
1.8 KiB
Go

package serverutils
import (
"io/ioutil"
"net"
"os"
"sync"
"time"
"github.com/kaspanet/kaspad/config"
"github.com/kaspanet/kaspad/connmgr"
"github.com/kaspanet/kaspad/peer"
"github.com/kaspanet/kaspad/util"
)
// Peer extends the peer to maintain state shared by the server and
// the blockmanager.
type Peer struct {
*peer.Peer
// The following variables must only be used atomically
FeeFilter int64
relayMtx sync.Mutex
DynamicBanScore connmgr.DynamicBanScore
quit chan struct{}
DisableRelayTx bool
// The following chans are used to sync blockmanager and server.
txProcessed chan struct{}
blockProcessed chan struct{}
}
// KaspadLookup resolves the IP of the given host using the correct DNS lookup
// function depending on the configuration options.
func KaspadLookup(host string) ([]net.IP, error) {
return config.ActiveConfig().Lookup(host)
}
// GenCertPair generates a key/cert pair to the paths provided.
func GenCertPair(certFile, keyFile string) error {
log.Infof("Generating TLS certificates...")
org := "kaspad autogenerated cert"
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
cert, key, err := util.NewTLSCertPair(org, validUntil, nil)
if err != nil {
return err
}
// Write cert and key files.
if err = ioutil.WriteFile(certFile, cert, 0666); err != nil {
return err
}
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile)
return err
}
log.Infof("Done generating TLS certificates")
return nil
}
// KaspadDial connects to the address on the named network using the appropriate
// dial function depending on the address and configuration options.
func KaspadDial(addr net.Addr) (net.Conn, error) {
return config.ActiveConfig().Dial(addr.Network(), addr.String(), config.DefaultConnectTimeout)
}