current changes with fixing to detecting offline peers

This commit is contained in:
2023-06-30 01:09:42 +01:00
parent ae5f1d2fd7
commit 187621a29e
5 changed files with 56 additions and 39 deletions

View File

@@ -108,7 +108,7 @@ func (api *WebapiInstance) apiBlockchainRead(w http.ResponseWriter, r *http.Requ
for _, record := range block.RecordsDecoded {
switch v := record.(type) {
case blockchain.BlockRecordFile:
result.RecordsDecoded = append(result.RecordsDecoded, blockRecordFileToAPI(v))
result.RecordsDecoded = append(result.RecordsDecoded, blockRecordFileToAPI(v, true))
case blockchain.BlockRecordProfile:
result.RecordsDecoded = append(result.RecordsDecoded, blockRecordProfileToAPI(v))

View File

@@ -47,14 +47,13 @@ type apiFile struct {
}
// --- conversion from core to API data ---
func blockRecordFileToAPI(input blockchain.BlockRecordFile) (output apiFile) {
// Currently in a Hacky way for quick generalised filters
func blockRecordFileToAPI(input blockchain.BlockRecordFile, localNode bool) (output apiFile) {
output = apiFile{ID: input.ID, Hash: input.Hash, NodeID: input.NodeID, Type: input.Type, Format: input.Format, Size: input.Size, Username: input.Username, Metadata: []apiFileMetadata{}}
NumberOfNodesShared := false
for _, tag := range input.Tags {
if tag.Type == blockchain.TagSharedByCount && tag.Number() == 0 {
return apiFile{}
}
switch tag.Type {
case blockchain.TagName:
output.Name = tag.Text()
@@ -75,9 +74,7 @@ func blockRecordFileToAPI(input blockchain.BlockRecordFile) (output apiFile) {
case blockchain.TagSharedByCount:
output.Metadata = append(output.Metadata, apiFileMetadata{Type: tag.Type, Name: "Shared By Count", Number: tag.Number()})
// if a file has 0 peers sharing then do not add it to the list.
if tag.Number() == 0 {
return apiFile{}
}
NumberOfNodesShared = true
case blockchain.TagSharedByGeoIP:
output.Metadata = append(output.Metadata, apiFileMetadata{Type: tag.Type, Name: "Shared By GeoIP", Text: tag.Text()})
@@ -87,6 +84,10 @@ func blockRecordFileToAPI(input blockchain.BlockRecordFile) (output apiFile) {
}
}
if !NumberOfNodesShared && !localNode {
return apiFile{}
}
return output
}
@@ -214,7 +215,7 @@ func (api *WebapiInstance) apiBlockchainFileList(w http.ResponseWriter, r *http.
var result apiBlockAddFiles
for _, file := range files {
ApiFile := blockRecordFileToAPI(file)
ApiFile := blockRecordFileToAPI(file, true)
if ApiFile.NodeID == nil {
continue
}

View File

@@ -69,7 +69,7 @@ func (api *WebapiInstance) ExploreFileSharedByNodeThatSharedSimilarFile(fileType
// loop over results
for n := range resultFiles {
ApiFile := blockRecordFileToAPI(resultFiles[n])
ApiFile := blockRecordFileToAPI(resultFiles[n], false)
if ApiFile.NodeID == nil {
continue
}

View File

@@ -9,13 +9,12 @@ package webapi
import (
"bytes"
"fmt"
"github.com/PeernetOfficial/core/protocol"
"time"
"github.com/PeernetOfficial/core/blockchain"
)
func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob) {
func (api *WebapiInstance) dispatchSearch(input SearchRequest, NodeID []byte) (job *SearchJob) {
Timeout := input.Parse()
Filter := input.ToSearchFilter()
@@ -25,14 +24,14 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob)
// todo: create actual search clients!
job.Status = SearchStatusLive
go job.localSearch(api, input.Term)
go job.localSearch(api, input.Term, NodeID)
api.RemoveJobDefer(job, job.timeout+time.Minute*10)
return job
}
func (job *SearchJob) localSearch(api *WebapiInstance, term string) {
func (job *SearchJob) localSearch(api *WebapiInstance, term string, NodeID []byte) {
if api.Backend.SearchIndex == nil {
job.Status = SearchStatusNoIndex
return
@@ -45,11 +44,6 @@ func (job *SearchJob) localSearch(api *WebapiInstance, term string) {
resultLoop:
for _, result := range results {
// check if the NodeID filter is provided
if job.filtersStart.NodeID != nil && !(bytes.Equal(job.filtersStart.NodeID, protocol.PublicKey2NodeID(result.PublicKey))) {
continue
}
file, _, found, err := api.Backend.ReadFile(result.PublicKey, result.BlockchainVersion, result.BlockNumber, result.FileID)
if err != nil || !found {
continue
@@ -62,25 +56,33 @@ resultLoop:
}
}
if bytes.Equal(file.NodeID, api.Backend.SelfNodeID()) {
// Indicates data from the current user.
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
} else if peer := api.Backend.NodelistLookup(file.NodeID); peer != nil {
// add the tags 'Shared By Count' and 'Shared By GeoIP'
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
sharedByGeoIP := fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude)
file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP))
// if the NodeID filter is provided
if bytes.Equal(file.NodeID, NodeID) || bytes.Equal(NodeID, nil) {
if bytes.Equal(file.NodeID, api.Backend.SelfNodeID()) {
// Indicates data from the current user.
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
} else if peer := api.Backend.NodelistLookup(file.NodeID); peer != nil {
// Get current active connections
if len(peer.GetConnections(true)) > 0 {
// add the tags 'Shared By Count' and 'Shared By GeoIP'
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
sharedByGeoIP := fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude)
file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP))
}
}
}
}
// new result
newFile := blockRecordFileToAPI(file)
newFile := blockRecordFileToAPI(file, false)
job.Files = append(job.Files, &newFile)
job.AllFiles = append(job.AllFiles, &newFile)
job.requireSort = true
job.statsAdd(&newFile)
if newFile.NodeID != nil {
job.Files = append(job.Files, &newFile)
job.AllFiles = append(job.AllFiles, &newFile)
job.requireSort = true
job.statsAdd(&newFile)
}
}
job.Status = SearchStatusTerminated

View File

@@ -16,6 +16,7 @@ Author: Peter Kleissner
package webapi
import (
"bytes"
"net/http"
"strconv"
"time"
@@ -36,6 +37,7 @@ type SearchRequest struct {
FileFormat int `json:"fileformat"` // File format such as PDF, Word, Ebook, etc. See core.FormatX. -1 = not used.
SizeMin int `json:"sizemin"` // Min file size in bytes. -1 = not used.
SizeMax int `json:"sizemax"` // Max file size in bytes. -1 = not used.
NodeID string `json:"node"`
}
// Sort orders
@@ -89,6 +91,8 @@ func (api *WebapiInstance) apiSearch(w http.ResponseWriter, r *http.Request) {
return
}
NodeId, _ := DecodeBlake3Hash(r.URL.Query().Get("node"))
if input.Timeout <= 0 {
input.Timeout = 20
}
@@ -104,7 +108,7 @@ func (api *WebapiInstance) apiSearch(w http.ResponseWriter, r *http.Request) {
}
}
job := api.dispatchSearch(input)
job := api.dispatchSearch(input, NodeId)
EncodeJSON(api.Backend, w, r, SearchRequestResponse{Status: 0, ID: job.id})
}
@@ -391,11 +395,21 @@ func (api *WebapiInstance) ExploreHelper(fileType int, limit, offset int, nodeID
// loop over results
for n := range resultFiles {
ApiFile := blockRecordFileToAPI(resultFiles[n])
if ApiFile.NodeID == nil {
continue
if nodeIDState {
if bytes.Equal(resultFiles[n].NodeID, nodeID) {
ApiFile := blockRecordFileToAPI(resultFiles[n], false)
if ApiFile.NodeID == nil {
continue
}
result.Files = append(result.Files, ApiFile)
}
} else {
ApiFile := blockRecordFileToAPI(resultFiles[n], false)
if ApiFile.NodeID == nil {
continue
}
result.Files = append(result.Files, ApiFile)
}
result.Files = append(result.Files, ApiFile)
}
if len(result.Files) == 0 {