mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
No code changes; export some functions/struct.
This commit is contained in:
@@ -92,7 +92,7 @@ func parseAddress(Address string) (remote *net.UDPAddr, err error) {
|
||||
// contact tries to contact the root peer on all networks
|
||||
func (peer *rootPeer) contact() {
|
||||
for _, address := range peer.addresses {
|
||||
sendAllNetworks(peer.publicKey, &packetRaw{Command: 0}, address)
|
||||
sendAllNetworks(peer.publicKey, &PacketRaw{Command: 0}, address)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
Commands.go
10
Commands.go
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
// packet2 is a high-level message between peers
|
||||
type packet2 struct {
|
||||
packetRaw
|
||||
PacketRaw
|
||||
SenderPublicKey *btcec.PublicKey // Sender Public Key, ECDSA (secp256k1) 257-bit
|
||||
network *Network // network which received the packet
|
||||
address *net.UDPAddr // address of the sender or receiver
|
||||
@@ -29,7 +29,7 @@ func (peer *PeerInfo) announcement(msg *packet2) {
|
||||
|
||||
// send the Response
|
||||
if added {
|
||||
peer.send(&packetRaw{Command: 1})
|
||||
peer.send(&PacketRaw{Command: 1})
|
||||
}
|
||||
|
||||
return
|
||||
@@ -37,7 +37,7 @@ func (peer *PeerInfo) announcement(msg *packet2) {
|
||||
fmt.Printf("Incoming secondary announcement from %s\n", msg.address.String())
|
||||
|
||||
// Announcement from existing peer means the peer most likely restarted
|
||||
peer.send(&packetRaw{Command: 1})
|
||||
peer.send(&PacketRaw{Command: 1})
|
||||
}
|
||||
|
||||
// response handles the response to the announcement
|
||||
@@ -54,7 +54,7 @@ func (peer *PeerInfo) response(msg *packet2) {
|
||||
|
||||
// chat handles a chat message [debug]
|
||||
func (peer *PeerInfo) chat(msg *packet2) {
|
||||
fmt.Printf("Chat from '%s': %s\n", msg.address.String(), string(msg.packetRaw.Payload))
|
||||
fmt.Printf("Chat from '%s': %s\n", msg.address.String(), string(msg.PacketRaw.Payload))
|
||||
}
|
||||
|
||||
// autoPingAll automatically pings all peers. This has multiple important reasons:
|
||||
@@ -68,6 +68,6 @@ func autoPingAll() {
|
||||
// SendChatAll sends a text message to all peers
|
||||
func SendChatAll(text string) {
|
||||
for _, peer := range PeerlistGet() {
|
||||
peer.send(&packetRaw{Command: 10, Payload: []byte(text)})
|
||||
peer.send(&PacketRaw{Command: 10, Payload: []byte(text)})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func initBroadcastIPv4() {
|
||||
}
|
||||
}
|
||||
|
||||
// BroadcastIPv4Join prepares sending Broadcasts
|
||||
// BroadcastIPv4 prepares sending Broadcasts
|
||||
func (network *Network) BroadcastIPv4() (err error) {
|
||||
if ipv4BroadcastPrivateKey == nil || ipv4BroadcastPublicKey == nil {
|
||||
return
|
||||
@@ -85,7 +85,7 @@ func (network *Network) BroadcastIPv4Listen() {
|
||||
|
||||
// BroadcastIPv4Send sends out a single broadcast messages to discover peers
|
||||
func (network *Network) BroadcastIPv4Send() (err error) {
|
||||
raw, err := packetEncrypt(peerPrivateKey, ipv4BroadcastPublicKey, &packetRaw{Protocol: 0, Command: 0})
|
||||
raw, err := PacketEncrypt(peerPrivateKey, ipv4BroadcastPublicKey, &PacketRaw{Protocol: 0, Command: 0})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ func (network *Network) MulticastIPv6Listen() {
|
||||
|
||||
// MulticastIPv6Send sends out a single multicast messages to discover peers at the same site
|
||||
func (network *Network) MulticastIPv6Send() (err error) {
|
||||
raw, err := packetEncrypt(peerPrivateKey, ipv6MulticastPublicKey, &packetRaw{Protocol: 0, Command: 0})
|
||||
raw, err := PacketEncrypt(peerPrivateKey, ipv6MulticastPublicKey, &PacketRaw{Protocol: 0, Command: 0})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ func (network *Network) Listen() {
|
||||
// packetWorker handles incoming packets.
|
||||
func packetWorker(packets <-chan networkWire) {
|
||||
for packet := range packets {
|
||||
decoded, senderPublicKey, err := packetDecrypt(packet.raw, packet.receiverPublicKey)
|
||||
decoded, senderPublicKey, err := PacketDecrypt(packet.raw, packet.receiverPublicKey)
|
||||
if err != nil {
|
||||
log.Printf("packetWorker Error decrypting packet from '%s': %s\n", packet.sender.String(), err.Error())
|
||||
continue
|
||||
@@ -137,7 +137,7 @@ func packetWorker(packets <-chan networkWire) {
|
||||
}
|
||||
|
||||
// process the packet
|
||||
message := &packet2{SenderPublicKey: senderPublicKey, packetRaw: *decoded, network: packet.network, address: packet.sender}
|
||||
message := &packet2{SenderPublicKey: senderPublicKey, PacketRaw: *decoded, network: packet.network, address: packet.sender}
|
||||
|
||||
switch decoded.Command {
|
||||
case 0: // Announce
|
||||
|
||||
@@ -30,8 +30,8 @@ import (
|
||||
"lukechampine.com/blake3"
|
||||
)
|
||||
|
||||
// packetRaw is a decrypted P2P message
|
||||
type packetRaw struct {
|
||||
// PacketRaw is a decrypted P2P message
|
||||
type PacketRaw struct {
|
||||
Protocol uint8 // Protocol version = 0
|
||||
Command uint8 // 0 = Announcement
|
||||
Payload []byte // Payload
|
||||
@@ -42,8 +42,8 @@ const packetLengthMin = 8 + signatureSize
|
||||
const signatureSize = 65
|
||||
const maxRandomGarbage = 20
|
||||
|
||||
// packetDecrypt decrypts the packet, verifies its signature and returns a high-level version of the packet.
|
||||
func packetDecrypt(raw []byte, receiverPublicKey *btcec.PublicKey) (packet *packetRaw, senderPublicKey *btcec.PublicKey, err error) {
|
||||
// PacketDecrypt decrypts the packet, verifies its signature and returns a high-level version of the packet.
|
||||
func PacketDecrypt(raw []byte, receiverPublicKey *btcec.PublicKey) (packet *PacketRaw, senderPublicKey *btcec.PublicKey, err error) {
|
||||
// Packet is assumed to be already checked for minimum length.
|
||||
|
||||
// Prepare Salsa20 nonce and key. Nonce = 2x first 4 bytes. For size reasons, only 4 bytes (instead of 8 bytes) is supplied in the packet.
|
||||
@@ -68,7 +68,7 @@ func packetDecrypt(raw []byte, receiverPublicKey *btcec.PublicKey) (packet *pack
|
||||
salsa20.XORKeyStream(bufferDecrypted[:], raw[4:len(raw)-signatureSize], nonce, keySalsa)
|
||||
|
||||
// copy all fields
|
||||
packet = &packetRaw{Protocol: bufferDecrypted[0], Command: bufferDecrypted[1]}
|
||||
packet = &PacketRaw{Protocol: bufferDecrypted[0], Command: bufferDecrypted[1]}
|
||||
|
||||
sizePayload := binary.LittleEndian.Uint16(bufferDecrypted[2:4])
|
||||
if int(sizePayload) > len(bufferDecrypted)-4 { // invalid length?
|
||||
@@ -82,8 +82,8 @@ func packetDecrypt(raw []byte, receiverPublicKey *btcec.PublicKey) (packet *pack
|
||||
return packet, senderPublicKey, nil
|
||||
}
|
||||
|
||||
// packetEncrypt encrypts a packet using the provided senders private key and receivers compressed public key.
|
||||
func packetEncrypt(senderPrivateKey *btcec.PrivateKey, receiverPublicKey *btcec.PublicKey, packet *packetRaw) (raw []byte, err error) {
|
||||
// PacketEncrypt encrypts a packet using the provided senders private key and receivers compressed public key.
|
||||
func PacketEncrypt(senderPrivateKey *btcec.PrivateKey, receiverPublicKey *btcec.PublicKey, packet *PacketRaw) (raw []byte, err error) {
|
||||
garbage := packetGarbage(packetLengthMin + len(packet.Payload))
|
||||
raw = make([]byte, packetLengthMin+len(packet.Payload)+len(garbage))
|
||||
|
||||
|
||||
@@ -151,14 +151,14 @@ func publicKey2Compressed(publicKey *btcec.PublicKey) [btcec.PubKeyBytesLenCompr
|
||||
}
|
||||
|
||||
// send sends a raw packet to the peer
|
||||
func (peer *PeerInfo) send(packet *packetRaw) (err error) {
|
||||
func (peer *PeerInfo) send(packet *PacketRaw) (err error) {
|
||||
if len(peer.Connections) == 0 {
|
||||
return errors.New("no valid connection to peer")
|
||||
}
|
||||
|
||||
packet.Protocol = 0
|
||||
|
||||
raw, err := packetEncrypt(peerPrivateKey, peer.PublicKey, packet)
|
||||
raw, err := PacketEncrypt(peerPrivateKey, peer.PublicKey, packet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -199,9 +199,9 @@ func (peer *PeerInfo) sendFailover(failed *Connection, raw []byte) (err error) {
|
||||
}
|
||||
|
||||
// sendAllNetworks sends a raw packet via all networks
|
||||
func sendAllNetworks(receiverPublicKey *btcec.PublicKey, packet *packetRaw, remote *net.UDPAddr) (err error) {
|
||||
func sendAllNetworks(receiverPublicKey *btcec.PublicKey, packet *PacketRaw, remote *net.UDPAddr) (err error) {
|
||||
packet.Protocol = 0
|
||||
raw, err := packetEncrypt(peerPrivateKey, receiverPublicKey, packet)
|
||||
raw, err := PacketEncrypt(peerPrivateKey, receiverPublicKey, packet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ Current version: 0.1 (pre-alpha)
|
||||
Before compiling, make sure to download and update all 3rd party packages:
|
||||
|
||||
```
|
||||
go get -u github.com/gofrs/uuid
|
||||
go get -u github.com/btcsuite/btcd/btcec
|
||||
go get -u github.com/libp2p/go-reuseport
|
||||
go get -u lukechampine.com/blake3
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
@@ -16,9 +16,11 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const configFile = "Settings.yaml"
|
||||
// Version is the current core library version
|
||||
const Version = "0.1"
|
||||
|
||||
const configFile = "Settings.yaml"
|
||||
|
||||
var config struct {
|
||||
LogFile string `yaml:"LogFile"` // Log file
|
||||
|
||||
|
||||
Reference in New Issue
Block a user