mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-09 07:36:43 +00:00

* [NOD-754] Fix staticcheck errors * [NOD-754] Remove some unused exported functions * [NOD-754] Fix staticcheck errors * [NOD-754] Don't panic if out/in close fails * [NOD-754] Wrap outside errors with custom message
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package serverutils
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/config"
|
|
"github.com/kaspanet/kaspad/util"
|
|
)
|
|
|
|
// 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)
|
|
}
|