mirror of
https://github.com/PeernetOfficial/Cmd.git
synced 2026-07-16 18:37:51 +01:00
Update to latest core.
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/dht"
|
||||
"github.com/PeernetOfficial/core/protocol"
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
|
||||
@@ -128,17 +129,17 @@ func filterIncomingRequest(peer *core.PeerInfo, Action int, Key []byte, Info int
|
||||
|
||||
requestType := "UNKNOWN"
|
||||
switch Action {
|
||||
case core.ActionFindSelf:
|
||||
case protocol.ActionFindSelf:
|
||||
requestType = "FIND_SELF"
|
||||
case core.ActionFindPeer:
|
||||
case protocol.ActionFindPeer:
|
||||
requestType = "FIND_PEER"
|
||||
case core.ActionFindValue:
|
||||
case protocol.ActionFindValue:
|
||||
requestType = "FIND_VALUE"
|
||||
case core.ActionInfoStore:
|
||||
case protocol.ActionInfoStore:
|
||||
requestType = "INFO_STORE"
|
||||
}
|
||||
|
||||
if Action == core.ActionFindSelf && bytes.Equal(peer.NodeID, Key) {
|
||||
if Action == protocol.ActionFindSelf && bytes.Equal(peer.NodeID, Key) {
|
||||
fmt.Printf("Info request from %s %s\n", hex.EncodeToString(peer.NodeID), requestType)
|
||||
} else {
|
||||
fmt.Printf("Info request from %s %s for key %s\n", hex.EncodeToString(peer.NodeID), requestType, hex.EncodeToString(Key))
|
||||
@@ -147,7 +148,7 @@ func filterIncomingRequest(peer *core.PeerInfo, Action int, Key []byte, Info int
|
||||
|
||||
// ---- filter for incoming and outgoing packets ----
|
||||
|
||||
func filterMessageIn(peer *core.PeerInfo, raw *core.MessageRaw, message interface{}) {
|
||||
func filterMessageIn(peer *core.PeerInfo, raw *protocol.MessageRaw, message interface{}) {
|
||||
if !hashIsMonitored(peer.NodeID) {
|
||||
// TODO: For Announcement/Response also check data, Traverse the final target
|
||||
return
|
||||
@@ -156,19 +157,19 @@ func filterMessageIn(peer *core.PeerInfo, raw *core.MessageRaw, message interfac
|
||||
commandA := "Unknown"
|
||||
|
||||
switch raw.Command {
|
||||
case core.CommandAnnouncement:
|
||||
case protocol.CommandAnnouncement:
|
||||
commandA = "Announcement"
|
||||
case core.CommandResponse:
|
||||
case protocol.CommandResponse:
|
||||
commandA = "Response"
|
||||
case core.CommandPing:
|
||||
case protocol.CommandPing:
|
||||
commandA = "Ping"
|
||||
case core.CommandPong:
|
||||
case protocol.CommandPong:
|
||||
commandA = "Pong"
|
||||
case core.CommandLocalDiscovery:
|
||||
case protocol.CommandLocalDiscovery:
|
||||
commandA = "Local Discovery"
|
||||
case core.CommandTraverse:
|
||||
case protocol.CommandTraverse:
|
||||
commandA = "Traverse"
|
||||
case core.CommandChat:
|
||||
case protocol.CommandChat:
|
||||
commandA = "Chat"
|
||||
}
|
||||
|
||||
@@ -181,7 +182,7 @@ func filterMessageIn(peer *core.PeerInfo, raw *core.MessageRaw, message interfac
|
||||
|
||||
if message == nil {
|
||||
output += "(no message decoded)\n"
|
||||
} else if announce, ok := message.(*core.MessageAnnouncement); ok {
|
||||
} else if announce, ok := message.(*protocol.MessageAnnouncement); ok {
|
||||
output += fmt.Sprintf("Fields:\n Protocol supported %d\n", announce.Protocol)
|
||||
output += fmt.Sprintf(" Feature bits %d\n", announce.Features)
|
||||
output += fmt.Sprintf(" Action bits %d\n", announce.Actions)
|
||||
@@ -209,7 +210,7 @@ func filterMessageIn(peer *core.PeerInfo, raw *core.MessageRaw, message interfac
|
||||
for _, info := range announce.InfoStoreFiles {
|
||||
output += fmt.Sprintf(" - Info store %s, type %d, size %d\n", hex.EncodeToString(info.ID.Hash), info.Type, info.Size)
|
||||
}
|
||||
} else if response, ok := message.(*core.MessageResponse); ok {
|
||||
} else if response, ok := message.(*protocol.MessageResponse); ok {
|
||||
output += fmt.Sprintf("Fields:\n Protocol supported %d\n", response.Protocol)
|
||||
output += fmt.Sprintf(" Feature bits %d\n", response.Features)
|
||||
output += fmt.Sprintf(" Action bits %d\n", response.Actions)
|
||||
@@ -238,7 +239,7 @@ func filterMessageIn(peer *core.PeerInfo, raw *core.MessageRaw, message interfac
|
||||
for _, hash := range response.HashesNotFound {
|
||||
output += fmt.Sprintf(" - Hash not found %s\n", hex.EncodeToString(hash))
|
||||
}
|
||||
} else if traverse, ok := message.(*core.MessageTraverse); ok {
|
||||
} else if traverse, ok := message.(*protocol.MessageTraverse); ok {
|
||||
output += fmt.Sprintf("Fields:\n Target Peer %s\n", hex.EncodeToString(traverse.TargetPeer.SerializeCompressed()))
|
||||
output += fmt.Sprintf(" Authorized Relay Peer %s\n", hex.EncodeToString(traverse.AuthorizedRelayPeer.SerializeCompressed()))
|
||||
output += fmt.Sprintf(" Signer Public Key %s\n", hex.EncodeToString(traverse.SignerPublicKey.SerializeCompressed()))
|
||||
@@ -256,7 +257,7 @@ func filterMessageIn(peer *core.PeerInfo, raw *core.MessageRaw, message interfac
|
||||
fmt.Printf("%s", output)
|
||||
}
|
||||
|
||||
func outputPeerRecord(record *core.PeerRecord) (output string) {
|
||||
func outputPeerRecord(record *protocol.PeerRecord) (output string) {
|
||||
output += fmt.Sprintf(" * Peer ID %s\n", hex.EncodeToString(record.PublicKey.SerializeCompressed()))
|
||||
output += fmt.Sprintf(" Node ID %s\n", hex.EncodeToString(record.NodeID))
|
||||
if record.IPv4 != nil && !record.IPv4.IsUnspecified() {
|
||||
@@ -276,7 +277,7 @@ func outputPeerRecord(record *core.PeerRecord) (output string) {
|
||||
return
|
||||
}
|
||||
|
||||
func outputOutgoingMessage(peer *core.PeerInfo, packet *core.PacketRaw) {
|
||||
func outputOutgoingMessage(peer *core.PeerInfo, packet *protocol.PacketRaw) {
|
||||
if !hashIsMonitored(peer.NodeID) {
|
||||
// TODO: For Announcement/Response also check data, Traverse the final target
|
||||
return
|
||||
@@ -285,19 +286,19 @@ func outputOutgoingMessage(peer *core.PeerInfo, packet *core.PacketRaw) {
|
||||
commandA := "Unknown"
|
||||
|
||||
switch packet.Command {
|
||||
case core.CommandAnnouncement:
|
||||
case protocol.CommandAnnouncement:
|
||||
commandA = "Announcement"
|
||||
case core.CommandResponse:
|
||||
case protocol.CommandResponse:
|
||||
commandA = "Response"
|
||||
case core.CommandPing:
|
||||
case protocol.CommandPing:
|
||||
commandA = "Ping"
|
||||
case core.CommandPong:
|
||||
case protocol.CommandPong:
|
||||
commandA = "Pong"
|
||||
case core.CommandLocalDiscovery:
|
||||
case protocol.CommandLocalDiscovery:
|
||||
commandA = "Local Discovery"
|
||||
case core.CommandTraverse:
|
||||
case protocol.CommandTraverse:
|
||||
commandA = "Traverse"
|
||||
case core.CommandChat:
|
||||
case protocol.CommandChat:
|
||||
commandA = "Chat"
|
||||
}
|
||||
|
||||
@@ -311,26 +312,26 @@ func outputOutgoingMessage(peer *core.PeerInfo, packet *core.PacketRaw) {
|
||||
fmt.Printf("%s", output)
|
||||
}
|
||||
|
||||
func filterMessageOutAnnouncement(receiverPublicKey *btcec.PublicKey, peer *core.PeerInfo, packet *core.PacketRaw, findSelf bool, findPeer []core.KeyHash, findValue []core.KeyHash, files []core.InfoStore) {
|
||||
func filterMessageOutAnnouncement(receiverPublicKey *btcec.PublicKey, peer *core.PeerInfo, packet *protocol.PacketRaw, findSelf bool, findPeer []protocol.KeyHash, findValue []protocol.KeyHash, files []protocol.InfoStore) {
|
||||
if peer == nil {
|
||||
peer = &core.PeerInfo{PublicKey: receiverPublicKey, NodeID: core.PublicKey2NodeID(receiverPublicKey)}
|
||||
peer = &core.PeerInfo{PublicKey: receiverPublicKey, NodeID: protocol.PublicKey2NodeID(receiverPublicKey)}
|
||||
}
|
||||
|
||||
outputOutgoingMessage(peer, packet)
|
||||
}
|
||||
|
||||
func filterMessageOutResponse(peer *core.PeerInfo, packet *core.PacketRaw, hash2Peers []core.Hash2Peer, filesEmbed []core.EmbeddedFileData, hashesNotFound [][]byte) {
|
||||
func filterMessageOutResponse(peer *core.PeerInfo, packet *protocol.PacketRaw, hash2Peers []protocol.Hash2Peer, filesEmbed []protocol.EmbeddedFileData, hashesNotFound [][]byte) {
|
||||
outputOutgoingMessage(peer, packet)
|
||||
}
|
||||
|
||||
func filterMessageOutTraverse(peer *core.PeerInfo, packet *core.PacketRaw, embeddedPacket *core.PacketRaw, receiverEnd *btcec.PublicKey) {
|
||||
func filterMessageOutTraverse(peer *core.PeerInfo, packet *protocol.PacketRaw, embeddedPacket *protocol.PacketRaw, receiverEnd *btcec.PublicKey) {
|
||||
outputOutgoingMessage(peer, packet)
|
||||
}
|
||||
|
||||
func filterMessageOutPing(peer *core.PeerInfo, packet *core.PacketRaw, connection *core.Connection) {
|
||||
func filterMessageOutPing(peer *core.PeerInfo, packet *protocol.PacketRaw, connection *core.Connection) {
|
||||
outputOutgoingMessage(peer, packet)
|
||||
}
|
||||
|
||||
func filterMessageOutPong(peer *core.PeerInfo, packet *core.PacketRaw) {
|
||||
func filterMessageOutPong(peer *core.PeerInfo, packet *protocol.PacketRaw) {
|
||||
outputOutgoingMessage(peer, packet)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/dht"
|
||||
"github.com/PeernetOfficial/core/protocol"
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
|
||||
@@ -109,17 +110,17 @@ func userCommands(input io.Reader, output io.Writer, terminateSignal chan struct
|
||||
|
||||
features := ""
|
||||
featureSupport := core.FeatureSupport()
|
||||
if featureSupport&(1<<core.FeatureIPv4Listen) > 0 {
|
||||
if featureSupport&(1<<protocol.FeatureIPv4Listen) > 0 {
|
||||
features = "IPv4"
|
||||
}
|
||||
if featureSupport&(1<<core.FeatureIPv6Listen) > 0 {
|
||||
if featureSupport&(1<<protocol.FeatureIPv6Listen) > 0 {
|
||||
if len(features) > 0 {
|
||||
features += ", "
|
||||
}
|
||||
features += "IPv6"
|
||||
}
|
||||
|
||||
fmt.Fprintf(output, "User Agent: %s\nFeatures: %s\n\n", core.UserAgent, features)
|
||||
fmt.Fprintf(output, "User Agent: %s\nFeatures: %s\n\n", core.SelfUserAgent(), features)
|
||||
|
||||
fmt.Fprintf(output, "Listen Address Multicast IP out External Address\n")
|
||||
|
||||
@@ -283,7 +284,7 @@ func userCommands(input io.Reader, output io.Writer, terminateSignal chan struct
|
||||
continue
|
||||
}
|
||||
|
||||
nodeID = core.PublicKey2NodeID(publicKey)
|
||||
nodeID = protocol.PublicKey2NodeID(publicKey)
|
||||
} else {
|
||||
// Node ID was supplied.
|
||||
if nodeID, err = hex.DecodeString(text); err != nil || len(nodeID) != 256/8 {
|
||||
|
||||
5
Main.go
5
Main.go
@@ -51,7 +51,6 @@ func init() {
|
||||
|
||||
monitorKeys = make(map[string]struct{})
|
||||
|
||||
core.UserAgent = appName + "/" + core.Version
|
||||
core.Filters.LogError = logError
|
||||
core.Filters.DHTSearchStatus = filterSearchStatus
|
||||
core.Filters.IncomingRequest = filterIncomingRequest
|
||||
@@ -62,7 +61,9 @@ func init() {
|
||||
core.Filters.MessageOutPing = filterMessageOutPing
|
||||
core.Filters.MessageOutPong = filterMessageOutPong
|
||||
|
||||
core.Init()
|
||||
userAgent := appName + "/" + core.Version
|
||||
|
||||
core.Init(userAgent)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
110
Test_test.go
110
Test_test.go
@@ -1,110 +0,0 @@
|
||||
// Random tests for debugging. Not actual functionality tests.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
)
|
||||
|
||||
/*
|
||||
func TestSignCompact(t *testing.T) {
|
||||
privateKey, publicKey, err := core.Secp256k1NewPrivateKey()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var compact1 []byte
|
||||
data := []byte{1, 2, 3}
|
||||
|
||||
time1 := time.Now()
|
||||
|
||||
compact1 = make([]byte, 0)
|
||||
compact1, err = btcec.SignCompact(btcec.S256(), privateKey, hashData(data), true)
|
||||
|
||||
timeT := time.Since(time1)
|
||||
fmt.Printf("length %d time %s\n", len(compact1), timeT.String())
|
||||
|
||||
time2 := time.Now()
|
||||
|
||||
key2, _, err := btcec.RecoverCompact(btcec.S256(), compact1, hashData(data))
|
||||
|
||||
timeT2 := time.Since(time2)
|
||||
fmt.Printf("VALID PUBLIC KEY: %t\n", key2.IsEqual(publicKey))
|
||||
fmt.Printf("time %s\n", timeT2.String())
|
||||
}
|
||||
*/
|
||||
|
||||
func TestEncryption1(t *testing.T) {
|
||||
privateKey, publicKey, err := core.Secp256k1NewPrivateKey()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
packet1 := core.PacketRaw{Protocol: 0, Command: 1, Payload: []byte{1, 2, 3}}
|
||||
|
||||
raw1, err := core.PacketEncrypt(privateKey, publicKey, &packet1)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
packet1d, _, err := core.PacketDecrypt(raw1, publicKey)
|
||||
if err != nil {
|
||||
fmt.Printf("Error %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("%d\n", packet1d.Command)
|
||||
}
|
||||
|
||||
/*
|
||||
func BenchmarkHash(b *testing.B) {
|
||||
length := 20000
|
||||
|
||||
b.Run("blake3", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(length))
|
||||
buf := make([]byte, length)
|
||||
rand.Read(buf)
|
||||
for i := 0; i < b.N; i++ {
|
||||
blake3.Sum256(buf)
|
||||
}
|
||||
})
|
||||
b.Run("SHA256 double", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(length))
|
||||
buf := make([]byte, length)
|
||||
rand.Read(buf)
|
||||
for i := 0; i < b.N; i++ {
|
||||
chainhash.DoubleHashB(buf)
|
||||
}
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
func TestEncryption2(t *testing.T) {
|
||||
privateKey, publicKey, err := core.Secp256k1NewPrivateKey()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
packet1 := core.PacketRaw{Protocol: 0, Command: 0}
|
||||
|
||||
for n := 0; n < 1000; n++ {
|
||||
raw1, err := core.PacketEncrypt(privateKey, publicKey, &packet1)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
packet1d, _, err := core.PacketDecrypt(raw1, publicKey)
|
||||
if err != nil {
|
||||
fmt.Printf("Error %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Printf("Command %d payload %v\n", packet1d.Command, packet1d.Payload)
|
||||
}
|
||||
|
||||
}
|
||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module github.com/PeernetOfficial/Cmd
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/PeernetOfficial/core v0.0.0-20211013040040-c2f110169ac9
|
||||
github.com/PeernetOfficial/core v0.0.0-20211018025754-f954c4777b40
|
||||
github.com/btcsuite/btcd v0.22.0-beta.0.20210625194946-86a17263b0ff
|
||||
github.com/gorilla/websocket v1.4.3-0.20210424162022-e8629af678b7
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
github.com/PeernetOfficial/core v0.0.0-20211013040040-c2f110169ac9 h1:oxbAATvOJ2kNJU7bkhdkJ0uWuLq/gmbeJaM92zSLR3c=
|
||||
github.com/PeernetOfficial/core v0.0.0-20211013040040-c2f110169ac9/go.mod h1:u5RB1/k3V5C6PwNuVeQLVAkW8agwpjmmiucWZwk08s4=
|
||||
github.com/PeernetOfficial/core v0.0.0-20211018025754-f954c4777b40 h1:eTYe8vK3DOiZ9bJddhXSNKUJv2nImY8x30wPChzBI78=
|
||||
github.com/PeernetOfficial/core v0.0.0-20211018025754-f954c4777b40/go.mod h1:u5RB1/k3V5C6PwNuVeQLVAkW8agwpjmmiucWZwk08s4=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/akrylysov/pogreb v0.10.1 h1:FqlR8VR7uCbJdfUob916tPM+idpKgeESDXOA1K0DK4w=
|
||||
github.com/akrylysov/pogreb v0.10.1/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI=
|
||||
|
||||
Reference in New Issue
Block a user