saving current changes

This commit is contained in:
2023-06-22 16:36:48 +01:00
parent 9499f7ae92
commit 09d418956f
2 changed files with 90 additions and 90 deletions

View File

@@ -357,7 +357,7 @@ func (c *Connection) send(packet *protocol.PacketRaw, receiverPublicKey *btcec.P
// Send Traverse message if the peer is behind a NAT or firewall and this is the first message. Only for Announcement. // Send Traverse message if the peer is behind a NAT or firewall and this is the first message. Only for Announcement.
if err == nil && isFirstPacket && (c.IsBehindNAT() || c.Firewall) && c.traversePeer != nil && packet.Command == protocol.CommandAnnouncement { if err == nil && isFirstPacket && (c.IsBehindNAT() || c.Firewall) && c.traversePeer != nil && packet.Command == protocol.CommandAnnouncement {
c.traversePeer.sendTraverse(packet, receiverPublicKey) err = c.traversePeer.sendTraverse(packet, receiverPublicKey)
} }
return err return err

View File

@@ -6,119 +6,119 @@ Author: Peter Kleissner
package webapi package webapi
import ( import (
"fmt" "fmt"
"time" "time"
"github.com/PeernetOfficial/core" "github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/blockchain" "github.com/PeernetOfficial/core/blockchain"
) )
// queryRecentShared returns recently shared files on the network from random peers until the limit is reached. // queryRecentShared returns recently shared files on the network from random peers until the limit is reached.
func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int, limitPeer, offsetTotal, limitTotal uint64, nodeID []byte, nodeIDState bool) (files []blockchain.BlockRecordFile) { func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int, limitPeer, offsetTotal, limitTotal uint64, nodeID []byte, nodeIDState bool) (files []blockchain.BlockRecordFile) {
if limitPeer == 0 { if limitPeer == 0 {
limitPeer = 1 limitPeer = 1
} }
// Assign peer list as an empty array // Assign peer list as an empty array
var peerList []*core.PeerInfo var peerList []*core.PeerInfo
// check if the NodeID is provided or not // check if the NodeID is provided or not
if len(nodeID) == 0 && !nodeIDState { if len(nodeID) == 0 && !nodeIDState {
// Use the peer list to know about active peers. Random order! // Use the peer list to know about active peers. Random order!
peerList = api.Backend.PeerlistGet() peerList = api.Backend.PeerlistGet()
} else { } else {
// Get peer information based on the NodeID provided // Get peer information based on the NodeID provided
_, peer, err := api.Backend.FindNode(nodeID, time.Second*5) _, peer, err := api.Backend.FindNode(nodeID, time.Second*5)
if err != nil { if err != nil {
return return
} }
peerList = append(peerList, peer) peerList = append(peerList, peer)
} }
// Files from peers exceeding the limit. It is used if from all peers the total limit is not reached. // Files from peers exceeding the limit. It is used if from all peers the total limit is not reached.
var filesSeconday []blockchain.BlockRecordFile var filesSeconday []blockchain.BlockRecordFile
for _, peer := range peerList { for _, peer := range peerList {
if peer.BlockchainHeight == 0 { if peer.BlockchainHeight == 0 {
continue continue
} }
var filesFromPeer uint64 var filesFromPeer uint64
var Name string var Name string
ProfileNameFound := false ProfileNameFound := false
// First iteration of the entire blockchain to search for the profile // First iteration of the entire blockchain to search for the profile
// image and Username of the user // image and Username of the user
for blockN1 := peer.BlockchainHeight - 1; blockN1 > 0; blockN1-- { for blockN1 := peer.BlockchainHeight - 1; blockN1 > 0; blockN1-- {
blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN1) blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN1)
if !found { if !found {
continue continue
} }
// Adding profile image and Username to the output // Adding profile image and Username to the output
for raw := range blockDecoded.Block.RecordsRaw { for raw := range blockDecoded.Block.RecordsRaw {
if blockDecoded.Block.RecordsRaw[raw].Type == blockchain.ProfileName && !ProfileNameFound { if blockDecoded.Block.RecordsRaw[raw].Type == blockchain.ProfileName && !ProfileNameFound {
Name = string(blockDecoded.Block.RecordsRaw[raw].Data[:]) Name = string(blockDecoded.Block.RecordsRaw[raw].Data[:])
ProfileNameFound = true ProfileNameFound = true
} }
} }
} }
// decode blocks from top down // decode blocks from top down
blockLoop: blockLoop:
for blockN := peer.BlockchainHeight - 1; blockN > 0; blockN-- { for blockN := peer.BlockchainHeight - 1; blockN > 0; blockN-- {
blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN) blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN)
if !found { if !found {
continue continue
} }
for _, record := range blockDecoded.RecordsDecoded { for _, record := range blockDecoded.RecordsDecoded {
if file, ok := record.(blockchain.BlockRecordFile); ok && isFileTypeMatchBlock(&file, fileType) { if file, ok := record.(blockchain.BlockRecordFile); ok && isFileTypeMatchBlock(&file, fileType) {
// add the tags 'Shared By Count' and 'Shared By GeoIP' // add the tags 'Shared By Count' and 'Shared By GeoIP'
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1)) file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid { if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
sharedByGeoIP := fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude) sharedByGeoIP := fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude)
file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP)) file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP))
} }
file.Username = Name file.Username = Name
// found a new file! append. // found a new file! append.
if filesFromPeer < limitPeer { if filesFromPeer < limitPeer {
filesFromPeer++ filesFromPeer++
if offsetTotal > 0 { if offsetTotal > 0 {
offsetTotal-- offsetTotal--
continue continue
} }
files = append(files, file) files = append(files, file)
if uint64(len(files)) >= limitTotal { if uint64(len(files)) >= limitTotal {
return return
} }
} else if uint64(len(filesSeconday)) < limitTotal-uint64(len(files)) { } else if uint64(len(filesSeconday)) < limitTotal-uint64(len(files)) {
filesSeconday = append(filesSeconday, file) filesSeconday = append(filesSeconday, file)
} else { } else {
break blockLoop break blockLoop
} }
} }
} }
} }
} }
files = append(files, filesSeconday...) files = append(files, filesSeconday...)
return return
} }
// isFileTypeMatchBlock checks if the file type matches. -1 = accept any. -2 = core.TypeBinary, core.TypeCompressed, core.TypeContainer, core.TypeExecutable. // isFileTypeMatchBlock checks if the file type matches. -1 = accept any. -2 = core.TypeBinary, core.TypeCompressed, core.TypeContainer, core.TypeExecutable.
func isFileTypeMatchBlock(file *blockchain.BlockRecordFile, fileType int) bool { func isFileTypeMatchBlock(file *blockchain.BlockRecordFile, fileType int) bool {
if fileType == -1 { if fileType == -1 {
return true return true
} else if fileType == -2 { } else if fileType == -2 {
return file.Type == core.TypeBinary || file.Type == core.TypeCompressed || file.Type == core.TypeContainer || file.Type == core.TypeExecutable return file.Type == core.TypeBinary || file.Type == core.TypeCompressed || file.Type == core.TypeContainer || file.Type == core.TypeExecutable
} }
return file.Type == uint8(fileType) return file.Type == uint8(fileType)
} }