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

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
package serverutils
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"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. For example, addresses will
|
|
// be resolved using tor when the --proxy flag was specified unless --noonion
|
|
// was also specified in which case the normal system DNS resolver will be used.
|
|
//
|
|
// Any attempt to resolve a tor address (.onion) will return an error since they
|
|
// are not intended to be resolved outside of the tor proxy.
|
|
func KaspadLookup(host string) ([]net.IP, error) {
|
|
if strings.HasSuffix(host, ".onion") {
|
|
return nil, errors.Errorf("attempt to resolve tor address %s", host)
|
|
}
|
|
|
|
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. For
|
|
// example, .onion addresses will be dialed using the onion specific proxy if
|
|
// one was specified, but will otherwise use the normal dial function (which
|
|
// could itself use a proxy or not).
|
|
func KaspadDial(addr net.Addr) (net.Conn, error) {
|
|
if strings.Contains(addr.String(), ".onion:") {
|
|
return config.ActiveConfig().OnionDial(addr.Network(), addr.String(),
|
|
config.DefaultConnectTimeout)
|
|
}
|
|
return config.ActiveConfig().Dial(addr.Network(), addr.String(), config.DefaultConnectTimeout)
|
|
}
|