Merge branch 'master' into directory

This commit is contained in:
Akilan Selvacoumar
2023-02-12 09:46:45 +00:00
committed by GitHub
22 changed files with 156 additions and 139 deletions

View File

@@ -18,7 +18,7 @@ import (
// If it is corrupted, it will log the error and exit the process.
func (backend *Backend) initUserBlockchain() {
var err error
backend.UserBlockchain, err = blockchain.Init(backend.peerPrivateKey, backend.Config.BlockchainMain)
backend.UserBlockchain, err = blockchain.Init(backend.PeerPrivateKey, backend.Config.BlockchainMain)
if err != nil {
backend.LogError("initUserBlockchain", "error: %s\n", err.Error())
@@ -32,7 +32,7 @@ func (backend *Backend) userBlockchainUpdateSearchIndex() {
if newVersion != oldVersion || newHeight < oldHeight {
// invalidate search index data for the user's blockchain
backend.SearchIndex.UnindexBlockchain(backend.peerPublicKey)
backend.SearchIndex.UnindexBlockchain(backend.PeerPublicKey)
// reindex everything
for blockN := uint64(0); blockN < newHeight; blockN++ {
@@ -41,7 +41,7 @@ func (backend *Backend) userBlockchainUpdateSearchIndex() {
continue
}
backend.SearchIndex.IndexNewBlock(backend.peerPublicKey, newVersion, blockN, raw)
backend.SearchIndex.IndexNewBlock(backend.PeerPublicKey, newVersion, blockN, raw)
}
return
@@ -55,7 +55,7 @@ func (backend *Backend) userBlockchainUpdateSearchIndex() {
continue
}
backend.SearchIndex.IndexNewBlock(backend.peerPublicKey, newVersion, blockN, raw)
backend.SearchIndex.IndexNewBlock(backend.PeerPublicKey, newVersion, blockN, raw)
}
}
}
@@ -64,7 +64,7 @@ func (backend *Backend) userBlockchainUpdateSearchIndex() {
// ReadBlock reads a block and decodes the records. This may be a block of the user's blockchain, or any other that is cached in the global blockchain cache.
func (backend *Backend) ReadBlock(PublicKey *btcec.PublicKey, Version, BlockNumber uint64) (decoded *blockchain.BlockDecoded, raw []byte, found bool, err error) {
// requesting a block from the user's blockchain?
if PublicKey.IsEqual(backend.peerPublicKey) {
if PublicKey.IsEqual(backend.PeerPublicKey) {
_, _, version := backend.UserBlockchain.Header()
if Version != version {
return nil, nil, false, nil

View File

@@ -56,7 +56,7 @@ loopSeedList:
continue
}
if peer.publicKey.IsEqual(backend.peerPublicKey) { // skip if self
if peer.publicKey.IsEqual(backend.PeerPublicKey) { // skip if self
continue
}
@@ -287,7 +287,7 @@ func (backend *Backend) isReturnedPeerBadQuality(record *protocol.PeerRecord) bo
}
// Must not be self. There is no point that a remote peer would return self
if record.PublicKey.IsEqual(backend.peerPublicKey) {
if record.PublicKey.IsEqual(backend.PeerPublicKey) {
//fmt.Printf("IsReturnedPeerBadQuality received self peer\n")
return true
}

View File

@@ -99,13 +99,13 @@ func (peer *PeerInfo) cmdTraverseReceive(msg *protocol.MessageTraverse) {
// ---- fork packetWorker to decode and validate embedded packet ---
// Due to missing connection and other embedded details in the message (such as ports), the packet is not just simply queued to rawPacketsIncoming.
decoded, senderPublicKey, err := protocol.PacketDecrypt(msg.EmbeddedPacketRaw, peer.Backend.peerPublicKey)
decoded, senderPublicKey, err := protocol.PacketDecrypt(msg.EmbeddedPacketRaw, peer.Backend.PeerPublicKey)
if err != nil {
return
}
if !senderPublicKey.IsEqual(msg.SignerPublicKey) {
return
} else if senderPublicKey.IsEqual(peer.Backend.peerPublicKey) {
} else if senderPublicKey.IsEqual(peer.Backend.PeerPublicKey) {
return
} else if decoded.Protocol != 0 {
return

View File

@@ -288,7 +288,7 @@ func (peer *PeerInfo) cmdGetBlock(msg *protocol.MessageGetBlock, connection *Con
switch msg.Control {
case protocol.GetBlockControlRequestStart:
// Currently only support the local blockchain.
if !msg.BlockchainPublicKey.IsEqual(peer.Backend.peerPublicKey) {
if !msg.BlockchainPublicKey.IsEqual(peer.Backend.PeerPublicKey) {
peer.sendGetBlock(nil, protocol.GetBlockControlNotAvailable, msg.BlockchainPublicKey, 0, 0, nil, msg.Sequence, uuid.UUID{}, false)
return
} else if _, height, _ := peer.Backend.UserBlockchain.Header(); height == 0 {

View File

@@ -43,7 +43,7 @@ type Config struct {
PrivateKey string `yaml:"PrivateKey"` // The Private Key, hex encoded so it can be copied manually
// Initial peer seed list
SeedList []peerSeed `yaml:"SeedList"`
SeedList []PeerSeed `yaml:"SeedList"`
AutoUpdateSeedList bool `yaml:"AutoUpdateSeedList"`
SeedListVersion int `yaml:"SeedListVersion"`
@@ -61,8 +61,8 @@ type Config struct {
LimitTotalRecords uint64 `yaml:"LimitTotalRecords"` // Record count limit. 0 = unlimited. Max Records * Max Block Size = Size Limit.
}
// peerSeed is a singl peer entry from the config's seed list
type peerSeed struct {
// PeerSeed is a singl peer entry from the config's seed list
type PeerSeed struct {
PublicKey string `yaml:"PublicKey"` // Public key = peer ID. Hex encoded.
Address []string `yaml:"Address"` // IP:Port
}

View File

@@ -346,7 +346,7 @@ func (c *Connection) send(packet *protocol.PacketRaw, receiverPublicKey *btcec.P
c.backend.Filters.PacketOut(packet, receiverPublicKey, c)
raw, err := protocol.PacketEncrypt(c.backend.peerPrivateKey, receiverPublicKey, packet)
raw, err := protocol.PacketEncrypt(c.backend.PeerPrivateKey, receiverPublicKey, packet)
if err != nil {
return err
}

View File

@@ -90,12 +90,12 @@ func (peer *PeerInfo) sendTraverse(packet *protocol.PacketRaw, receiverEnd *btce
// self-reported ports are not set, as this isn't sent via a specific network but a relay
//packet.SetSelfReportedPorts(c.Network.SelfReportedPorts())
embeddedPacketRaw, err := protocol.PacketEncrypt(peer.Backend.peerPrivateKey, receiverEnd, packet)
embeddedPacketRaw, err := protocol.PacketEncrypt(peer.Backend.PeerPrivateKey, receiverEnd, packet)
if err != nil {
return err
}
packetRaw, err := protocol.EncodeTraverse(peer.Backend.peerPrivateKey, embeddedPacketRaw, receiverEnd, peer.PublicKey)
packetRaw, err := protocol.EncodeTraverse(peer.Backend.PeerPrivateKey, embeddedPacketRaw, receiverEnd, peer.PublicKey)
if err != nil {
return err
}
@@ -118,7 +118,7 @@ func (peer *PeerInfo) sendTransfer(data []byte, control, transferProtocol uint8,
return peer.sendLite(raw)
}
packetRaw, err := protocol.EncodeTransfer(peer.Backend.peerPrivateKey, data, control, transferProtocol, hash, offset, limit, transferID)
packetRaw, err := protocol.EncodeTransfer(peer.Backend.PeerPrivateKey, data, control, transferProtocol, hash, offset, limit, transferID)
if err != nil {
return err
}
@@ -141,7 +141,7 @@ func (peer *PeerInfo) sendGetBlock(data []byte, control uint8, blockchainPublicK
return peer.sendLite(raw)
}
packetRaw, err := protocol.EncodeGetBlock(peer.Backend.peerPrivateKey, data, control, blockchainPublicKey, limitBlockCount, maxBlockSize, targetBlocks, transferID)
packetRaw, err := protocol.EncodeGetBlock(peer.Backend.PeerPrivateKey, data, control, blockchainPublicKey, limitBlockCount, maxBlockSize, targetBlocks, transferID)
if err != nil {
return err
}

View File

@@ -98,7 +98,7 @@ func (network *Network) BroadcastIPv4Send() (err error) {
return errors.New("error encoding broadcast announcement")
}
raw, err := protocol.PacketEncrypt(network.backend.peerPrivateKey, ipv4BroadcastPublicKey, &protocol.PacketRaw{Protocol: protocol.ProtocolVersion, Command: protocol.CommandLocalDiscovery, Payload: packets[0]})
raw, err := protocol.PacketEncrypt(network.backend.PeerPrivateKey, ipv4BroadcastPublicKey, &protocol.PacketRaw{Protocol: protocol.ProtocolVersion, Command: protocol.CommandLocalDiscovery, Payload: packets[0]})
if err != nil {
return err
}

View File

@@ -143,7 +143,7 @@ func (network *Network) MulticastIPv6Send() (err error) {
return errors.New("error encoding multicast announcement")
}
raw, err := protocol.PacketEncrypt(network.backend.peerPrivateKey, ipv6MulticastPublicKey, &protocol.PacketRaw{Protocol: protocol.ProtocolVersion, Command: protocol.CommandLocalDiscovery, Payload: packets[0]})
raw, err := protocol.PacketEncrypt(network.backend.PeerPrivateKey, ipv6MulticastPublicKey, &protocol.PacketRaw{Protocol: protocol.ProtocolVersion, Command: protocol.CommandLocalDiscovery, Payload: packets[0]})
if err != nil {
return err
}

View File

@@ -125,7 +125,7 @@ func (network *Network) Listen() {
if isLite, err := network.networkGroup.LiteRouter.IsPacketLite(buffer[:length]); isLite && err != nil {
continue
} else if isLite {
network.networkGroup.litePacketsIncoming <- networkWire{network: network, sender: sender, raw: buffer[:length], receiverPublicKey: network.backend.peerPublicKey, unicast: true}
network.networkGroup.litePacketsIncoming <- networkWire{network: network, sender: sender, raw: buffer[:length], receiverPublicKey: network.backend.PeerPublicKey, unicast: true}
continue
}
@@ -135,7 +135,7 @@ func (network *Network) Listen() {
}
// send the packet to a channel which is processed by multiple workers.
network.networkGroup.rawPacketsIncoming <- networkWire{network: network, sender: sender, raw: buffer[:length], receiverPublicKey: network.backend.peerPublicKey, unicast: true}
network.networkGroup.rawPacketsIncoming <- networkWire{network: network, sender: sender, raw: buffer[:length], receiverPublicKey: network.backend.PeerPublicKey, unicast: true}
}
}
@@ -149,7 +149,7 @@ func (nets *Networks) packetWorker() {
}
// immediately discard message if sender = self
if senderPublicKey.IsEqual(nets.backend.peerPublicKey) {
if senderPublicKey.IsEqual(nets.backend.PeerPublicKey) {
continue
}
@@ -283,9 +283,9 @@ func (nets *Networks) packetWorker() {
case protocol.CommandTraverse:
if traverse, _ := protocol.DecodeTraverse(raw); traverse != nil {
nets.backend.Filters.MessageIn(peer, raw, traverse)
if traverse.TargetPeer.IsEqual(nets.backend.peerPublicKey) && traverse.AuthorizedRelayPeer.IsEqual(peer.PublicKey) {
if traverse.TargetPeer.IsEqual(nets.backend.PeerPublicKey) && traverse.AuthorizedRelayPeer.IsEqual(peer.PublicKey) {
peer.cmdTraverseReceive(traverse)
} else if traverse.AuthorizedRelayPeer.IsEqual(nets.backend.peerPublicKey) {
} else if traverse.AuthorizedRelayPeer.IsEqual(nets.backend.PeerPublicKey) {
peer.cmdTraverseForward(traverse)
}
}

View File

@@ -28,8 +28,8 @@ func (backend *Backend) initPeerID() {
if len(backend.Config.PrivateKey) > 0 {
configPK, err := hex.DecodeString(backend.Config.PrivateKey)
if err == nil {
backend.peerPrivateKey, backend.peerPublicKey = btcec.PrivKeyFromBytes(btcec.S256(), configPK)
backend.nodeID = protocol.PublicKey2NodeID(backend.peerPublicKey)
backend.PeerPrivateKey, backend.PeerPublicKey = btcec.PrivKeyFromBytes(btcec.S256(), configPK)
backend.nodeID = protocol.PublicKey2NodeID(backend.PeerPublicKey)
if backend.Config.AutoUpdateSeedList {
backend.configUpdateSeedList()
@@ -43,15 +43,15 @@ func (backend *Backend) initPeerID() {
// if the peer ID is empty, create a new user public-private key pair
var err error
backend.peerPrivateKey, backend.peerPublicKey, err = Secp256k1NewPrivateKey()
backend.PeerPrivateKey, backend.PeerPublicKey, err = Secp256k1NewPrivateKey()
if err != nil {
backend.LogError("initPeerID", "generating public-private key pairs: %s\n", err.Error())
os.Exit(ExitPrivateKeyCreate)
}
backend.nodeID = protocol.PublicKey2NodeID(backend.peerPublicKey)
backend.nodeID = protocol.PublicKey2NodeID(backend.PeerPublicKey)
// save the newly generated private key into the config
backend.Config.PrivateKey = hex.EncodeToString(backend.peerPrivateKey.Serialize())
backend.Config.PrivateKey = hex.EncodeToString(backend.PeerPrivateKey.Serialize())
backend.SaveConfig()
}
@@ -68,7 +68,7 @@ func Secp256k1NewPrivateKey() (privateKey *btcec.PrivateKey, publicKey *btcec.Pu
// ExportPrivateKey returns the peers public and private key
func (backend *Backend) ExportPrivateKey() (privateKey *btcec.PrivateKey, publicKey *btcec.PublicKey) {
return backend.peerPrivateKey, backend.peerPublicKey
return backend.PeerPrivateKey, backend.PeerPublicKey
}
// SelfNodeID returns the node ID used for DHT
@@ -250,7 +250,7 @@ func (peerSource *PeerInfo) records2Nodes(records []protocol.PeerRecord) (nodes
// selfPeerRecord returns self as peer record
func (backend *Backend) selfPeerRecord() (result protocol.PeerRecord) {
return protocol.PeerRecord{
PublicKey: backend.peerPublicKey,
PublicKey: backend.PeerPublicKey,
NodeID: backend.nodeID,
//IP: network.address.IP,
//Port: uint16(network.address.Port),

View File

@@ -101,8 +101,8 @@ type Backend struct {
nodesDHT *dht.DHT // Nodes connected in the DHT.
// peerID is the current peer's ID. It is a ECDSA (secp256k1) 257-bit public key.
peerPrivateKey *btcec.PrivateKey
peerPublicKey *btcec.PublicKey
PeerPrivateKey *btcec.PrivateKey
PeerPublicKey *btcec.PublicKey
// The node ID is the blake3 hash of the public key compressed form.
nodeID []byte

View File

@@ -27,9 +27,9 @@ Request: GET /blockchain/header
Result: 200 with JSON structure apiResponsePeerSelf
*/
func (api *WebapiInstance) apiBlockchainHeaderFunc(w http.ResponseWriter, r *http.Request) {
publicKey, height, version := api.backend.UserBlockchain.Header()
publicKey, height, version := api.Backend.UserBlockchain.Header()
EncodeJSON(api.backend, w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())})
EncodeJSON(api.Backend, w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())})
}
type apiBlockRecordRaw struct {
@@ -67,9 +67,9 @@ func (api *WebapiInstance) apiBlockchainAppend(w http.ResponseWriter, r *http.Re
records = append(records, blockchain.BlockRecordRaw{Type: record.Type, Data: record.Data})
}
newHeight, newVersion, status := api.backend.UserBlockchain.Append(records)
newHeight, newVersion, status := api.Backend.UserBlockchain.Append(records)
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
type apiBlockchainBlock struct {
@@ -96,7 +96,7 @@ func (api *WebapiInstance) apiBlockchainRead(w http.ResponseWriter, r *http.Requ
return
}
block, status, _ := api.backend.UserBlockchain.Read(uint64(blockN))
block, status, _ := api.Backend.UserBlockchain.Read(uint64(blockN))
result := apiBlockchainBlock{Status: status}
if status == 0 {
@@ -118,5 +118,5 @@ func (api *WebapiInstance) apiBlockchainRead(w http.ResponseWriter, r *http.Requ
}
}
EncodeJSON(api.backend, w, r, result)
EncodeJSON(api.Backend, w, r, result)
}

View File

@@ -76,11 +76,11 @@ func (api *WebapiInstance) apiDownloadStart(w http.ResponseWriter, r *http.Reque
return
}
info := &downloadInfo{backend: api.backend, api: api, id: uuid.New(), created: time.Now(), hash: hash, nodeID: nodeID}
info := &downloadInfo{backend: api.Backend, api: api, id: uuid.New(), created: time.Now(), hash: hash, nodeID: nodeID}
// create the file immediately
if info.initDiskFile(filePath) != nil {
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseFileInvalid})
EncodeJSON(api.Backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseFileInvalid})
return
}
@@ -90,7 +90,7 @@ func (api *WebapiInstance) apiDownloadStart(w http.ResponseWriter, r *http.Reque
// start the download!
go info.Start()
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseSuccess, ID: info.id, DownloadStatus: DownloadWaitMetadata})
EncodeJSON(api.Backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseSuccess, ID: info.id, DownloadStatus: DownloadWaitMetadata})
}
/*
@@ -109,7 +109,7 @@ func (api *WebapiInstance) apiDownloadStatus(w http.ResponseWriter, r *http.Requ
info := api.downloadLookup(id)
if info == nil {
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
EncodeJSON(api.Backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
return
}
@@ -132,7 +132,7 @@ func (api *WebapiInstance) apiDownloadStatus(w http.ResponseWriter, r *http.Requ
info.RUnlock()
EncodeJSON(api.backend, w, r, response)
EncodeJSON(api.Backend, w, r, response)
}
/*
@@ -154,7 +154,7 @@ func (api *WebapiInstance) apiDownloadAction(w http.ResponseWriter, r *http.Requ
info := api.downloadLookup(id)
if info == nil {
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
EncodeJSON(api.Backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
return
}
@@ -171,7 +171,7 @@ func (api *WebapiInstance) apiDownloadAction(w http.ResponseWriter, r *http.Requ
apiStatus = info.Cancel()
}
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: apiStatus, ID: info.id, DownloadStatus: info.status})
EncodeJSON(api.Backend, w, r, apiResponseDownloadStatus{APIStatus: apiStatus, ID: info.id, DownloadStatus: info.status})
}
// ---- download tracking ----

View File

@@ -213,9 +213,9 @@ func (api *WebapiInstance) apiFileFormat(w http.ResponseWriter, r *http.Request)
fileType, fileFormat, err := FileDetectType(filePath)
if err != nil {
EncodeJSON(api.backend, w, r, apiResponseFileFormat{Status: 1})
EncodeJSON(api.Backend, w, r, apiResponseFileFormat{Status: 1})
return
}
EncodeJSON(api.backend, w, r, apiResponseFileFormat{Status: 0, FileType: fileType, FileFormat: fileFormat})
EncodeJSON(api.Backend, w, r, apiResponseFileFormat{Status: 0, FileType: fileType, FileFormat: fileFormat})
}

View File

@@ -26,13 +26,16 @@ Instead of providing the node ID, the peer ID is also accepted in the &node= par
The default timeout for connecting to the peer is 10 seconds.
Request: GET /file/read?hash=[hash]&node=[node ID]
Optional: &offset=[offset]&limit=[limit] or via Range header.
Optional: &timeout=[seconds]
Optional: &offset=[offset]&limit=[limit] or via Range header.
Optional: &timeout=[seconds]
Response: 200 with the content
206 with partial content
400 if the parameters are invalid
404 if the file was not found or other error on transfer initiate
502 if unable to find or connect to the remote peer in time
206 with partial content
400 if the parameters are invalid
404 if the file was not found or other error on transfer initiate
502 if unable to find or connect to the remote peer in time
*/
func (api *WebapiInstance) apiFileRead(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@@ -69,7 +72,7 @@ func (api *WebapiInstance) apiFileRead(w http.ResponseWriter, r *http.Request) {
}
// Is the file available in the local warehouse? In that case requesting it from the remote is unnecessary.
if serveFileFromWarehouse(api.backend, w, fileHash, uint64(offset), uint64(limit), ranges) {
if serveFileFromWarehouse(api.Backend, w, fileHash, uint64(offset), uint64(limit), ranges) {
return
}
@@ -77,9 +80,9 @@ func (api *WebapiInstance) apiFileRead(w http.ResponseWriter, r *http.Request) {
var peer *core.PeerInfo
if valid2 {
peer, err = PeerConnectNode(api.backend, nodeID, timeout)
peer, err = PeerConnectNode(api.Backend, nodeID, timeout)
} else if err3 == nil {
peer, err = PeerConnectPublicKey(api.backend, publicKey, timeout)
peer, err = PeerConnectPublicKey(api.Backend, publicKey, timeout)
}
if err != nil {
w.WriteHeader(http.StatusBadGateway)
@@ -139,13 +142,16 @@ The default timeout for connecting to the peer is 10 seconds.
Formats: 14 = Video
Request: GET /file/view?hash=[hash]&node=[node ID]&format=[format]
Optional: &offset=[offset]&limit=[limit] or via Range header.
Optional: &timeout=[seconds]
Optional: &offset=[offset]&limit=[limit] or via Range header.
Optional: &timeout=[seconds]
Response: 200 with the content
206 with partial content
400 if the parameters are invalid
404 if the file was not found or other error on transfer initiate
502 if unable to find or connect to the remote peer in time
206 with partial content
400 if the parameters are invalid
404 if the file was not found or other error on transfer initiate
502 if unable to find or connect to the remote peer in time
*/
func (api *WebapiInstance) apiFileView(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@@ -193,7 +199,7 @@ func (api *WebapiInstance) apiFileView(w http.ResponseWriter, r *http.Request) {
// Is the file available in the local warehouse? In that case requesting it from the remote is unnecessary.
if !localCacheDisable {
if serveFileFromWarehouse(api.backend, w, fileHash, uint64(offset), uint64(limit), ranges) {
if serveFileFromWarehouse(api.Backend, w, fileHash, uint64(offset), uint64(limit), ranges) {
return
}
}
@@ -202,9 +208,9 @@ func (api *WebapiInstance) apiFileView(w http.ResponseWriter, r *http.Request) {
var peer *core.PeerInfo
if valid2 {
peer, err = PeerConnectNode(api.backend, nodeID, timeout)
peer, err = PeerConnectNode(api.Backend, nodeID, timeout)
} else if err3 == nil {
peer, err = PeerConnectPublicKey(api.backend, publicKey, timeout)
peer, err = PeerConnectPublicKey(api.Backend, publicKey, timeout)
}
if err != nil {
w.WriteHeader(http.StatusBadGateway)

View File

@@ -130,7 +130,8 @@ In case the function aborts, the blockchain remains unchanged.
Request: POST /blockchain/file/add with JSON structure apiBlockAddFiles
Response: 200 with JSON structure apiBlockchainBlockStatus
400 if invalid input
400 if invalid input
*/
func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) {
var input apiBlockAddFiles
@@ -154,8 +155,8 @@ func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.R
if _, err := warehouse.ValidateHash(file.Hash); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
} else if _, fileSize, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
} else if _, fileSize, status, _ := api.Backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return
} else {
file.Size = fileSize
@@ -168,17 +169,17 @@ func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.R
blockRecord := blockRecordFileFromAPI(file)
// Set the merkle tree info as appropriate.
if !setFileMerkleInfo(api.backend, &blockRecord) {
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
if !setFileMerkleInfo(api.Backend, &blockRecord) {
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return
}
filesAdd = append(filesAdd, blockRecord)
}
newHeight, newVersion, status := api.backend.UserBlockchain.AddFiles(filesAdd)
newHeight, newVersion, status := api.Backend.UserBlockchain.AddFiles(filesAdd)
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
/*
@@ -188,7 +189,7 @@ Request: GET /blockchain/file/list
Response: 200 with JSON structure apiBlockAddFiles
*/
func (api *WebapiInstance) apiBlockchainFileList(w http.ResponseWriter, r *http.Request) {
files, status := api.backend.UserBlockchain.ListFiles()
files, status := api.Backend.UserBlockchain.ListFiles()
var result apiBlockAddFiles
@@ -198,7 +199,7 @@ func (api *WebapiInstance) apiBlockchainFileList(w http.ResponseWriter, r *http.
result.Status = status
EncodeJSON(api.backend, w, r, result)
EncodeJSON(api.Backend, w, r, result)
}
/*
@@ -220,18 +221,18 @@ func (api *WebapiInstance) apiBlockchainFileDelete(w http.ResponseWriter, r *htt
deleteIDs = append(deleteIDs, input.Files[n].ID)
}
newHeight, newVersion, deletedFiles, status := api.backend.UserBlockchain.DeleteFiles(deleteIDs)
newHeight, newVersion, deletedFiles, status := api.Backend.UserBlockchain.DeleteFiles(deleteIDs)
// If successfully deleted from the blockchain, delete from the Warehouse in case there are no other references.
if status == blockchain.StatusOK {
for n := range deletedFiles {
if files, status := api.backend.UserBlockchain.FileExists(deletedFiles[n].Hash); status == blockchain.StatusOK && len(files) == 0 {
api.backend.UserWarehouse.DeleteFile(deletedFiles[n].Hash)
if files, status := api.Backend.UserBlockchain.FileExists(deletedFiles[n].Hash); status == blockchain.StatusOK && len(files) == 0 {
api.Backend.UserWarehouse.DeleteFile(deletedFiles[n].Hash)
}
}
}
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
/*
@@ -239,7 +240,8 @@ apiBlockchainSelfUpdateFile updates files that are already published on the bloc
Request: POST /blockchain/file/update with JSON structure apiBlockAddFiles
Response: 200 with JSON structure apiBlockchainBlockStatus
400 if invalid input
400 if invalid input
*/
func (api *WebapiInstance) apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) {
var input apiBlockAddFiles
@@ -263,8 +265,8 @@ func (api *WebapiInstance) apiBlockchainFileUpdate(w http.ResponseWriter, r *htt
if _, err := warehouse.ValidateHash(file.Hash); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
} else if _, fileSize, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
} else if _, fileSize, status, _ := api.Backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return
} else {
file.Size = fileSize
@@ -277,17 +279,17 @@ func (api *WebapiInstance) apiBlockchainFileUpdate(w http.ResponseWriter, r *htt
blockRecord := blockRecordFileFromAPI(file)
// Set the merkle tree info as appropriate.
if !setFileMerkleInfo(api.backend, &blockRecord) {
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
if !setFileMerkleInfo(api.Backend, &blockRecord) {
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return
}
filesAdd = append(filesAdd, blockRecord)
}
newHeight, newVersion, status := api.backend.UserBlockchain.ReplaceFiles(filesAdd)
newHeight, newVersion, status := api.Backend.UserBlockchain.ReplaceFiles(filesAdd)
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
// ---- metadata functions ----

View File

@@ -35,14 +35,14 @@ Request: GET /profile/list
Response: 200 with JSON structure apiProfileData
*/
func (api *WebapiInstance) apiProfileList(w http.ResponseWriter, r *http.Request) {
fields, status := api.backend.UserBlockchain.ProfileList()
fields, status := api.Backend.UserBlockchain.ProfileList()
result := apiProfileData{Status: status}
for n := range fields {
result.Fields = append(result.Fields, blockRecordProfileToAPI(fields[n]))
}
EncodeJSON(api.backend, w, r, result)
EncodeJSON(api.Backend, w, r, result)
}
/*
@@ -63,11 +63,11 @@ func (api *WebapiInstance) apiProfileRead(w http.ResponseWriter, r *http.Request
var result apiProfileData
var data []byte
if data, result.Status = api.backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
if data, result.Status = api.Backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
}
EncodeJSON(api.backend, w, r, result)
EncodeJSON(api.Backend, w, r, result)
}
/*
@@ -88,9 +88,9 @@ func (api *WebapiInstance) apiProfileWrite(w http.ResponseWriter, r *http.Reques
fields = append(fields, blockRecordProfileFromAPI(input.Fields[n]))
}
newHeight, newVersion, status := api.backend.UserBlockchain.ProfileWrite(fields)
newHeight, newVersion, status := api.Backend.UserBlockchain.ProfileWrite(fields)
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
/*
@@ -111,9 +111,9 @@ func (api *WebapiInstance) apiProfileDelete(w http.ResponseWriter, r *http.Reque
fields = append(fields, input.Fields[n].Type)
}
newHeight, newVersion, status := api.backend.UserBlockchain.ProfileDelete(fields)
newHeight, newVersion, status := api.Backend.UserBlockchain.ProfileDelete(fields)
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
// --- conversion from core to API data ---

View File

@@ -32,18 +32,18 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob)
}
func (job *SearchJob) localSearch(api *WebapiInstance, term string) {
if api.backend.SearchIndex == nil {
if api.Backend.SearchIndex == nil {
job.Status = SearchStatusNoIndex
return
}
results := api.backend.SearchIndex.Search(term)
results := api.Backend.SearchIndex.Search(term)
job.ResultSync.Lock()
resultLoop:
for _, result := range results {
file, _, found, err := api.backend.ReadFile(result.PublicKey, result.BlockchainVersion, result.BlockNumber, result.FileID)
file, _, found, err := api.Backend.ReadFile(result.PublicKey, result.BlockchainVersion, result.BlockNumber, result.FileID)
if err != nil || !found {
continue
}
@@ -55,10 +55,10 @@ resultLoop:
}
}
if bytes.Equal(file.NodeID, api.backend.SelfNodeID()) {
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 {
} 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 {

View File

@@ -80,10 +80,10 @@ apiSearch submits a search request
Request: POST /search with JSON SearchRequest
Result: 200 on success with JSON SearchRequestResponse
400 on invalid JSON
400 on invalid JSON
*/
func (api *WebapiInstance) apiSearch(w http.ResponseWriter, r *http.Request) {
var input SearchRequest
if err := DecodeJSON(w, r, &input); err != nil {
return
@@ -115,14 +115,16 @@ If reset is set, all results will be filtered and sorted according to the settin
Request: GET /search/result?id=[UUID]&limit=[max records]
Optional parameters:
&reset=[0|1] to reset the filters or sort orders with any of the below parameters (all required):
&filetype=[File Type]
&fileformat=[File Format]
&from=[Date From]&to=[Date To]
&sizemin=[Minimum file size]
&sizemax=[Maximum file size]
&sort=[sort order]
&offset=[absolute offset] with &limit=[records] to get items pagination style. Returned items (and ones before) are automatically frozen.
&reset=[0|1] to reset the filters or sort orders with any of the below parameters (all required):
&filetype=[File Type]
&fileformat=[File Format]
&from=[Date From]&to=[Date To]
&sizemin=[Minimum file size]
&sizemax=[Maximum file size]
&sort=[sort order]
&offset=[absolute offset] with &limit=[records] to get items pagination style. Returned items (and ones before) are automatically frozen.
Result: 200 with JSON structure SearchResult. Check the field status.
*/
func (api *WebapiInstance) apiSearchResult(w http.ResponseWriter, r *http.Request) {
@@ -209,7 +211,8 @@ apiSearchResultStream provides a websocket to receive results as stream.
Request: GET /search/result/ws?id=[UUID]&limit=[optional max records]
Result: If successful, upgrades to a websocket and sends JSON structure SearchResult messages.
Limit is optional. Not used if ommitted or 0.
Limit is optional. Not used if ommitted or 0.
*/
func (api *WebapiInstance) apiSearchResultStream(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@@ -293,8 +296,9 @@ apiSearchTerminate terminates a search
Request: GET /search/terminate?id=[UUID]
Response: 204 Empty
400 Invalid input
404 ID not found
400 Invalid input
404 ID not found
*/
func (api *WebapiInstance) apiSearchTerminate(w http.ResponseWriter, r *http.Request) {

View File

@@ -33,7 +33,7 @@ Request: GET /status
Result: 200 with JSON structure Status
*/
func (api *WebapiInstance) apiStatus(w http.ResponseWriter, r *http.Request) {
status := apiResponseStatus{Status: 0, CountPeerList: api.backend.PeerlistCount()}
status := apiResponseStatus{Status: 0, CountPeerList: api.Backend.PeerlistCount()}
status.CountNetwork = status.CountPeerList // For now always same as CountPeerList, until native Statistics message to root peers is available.
// Connected: If at leat 2 peers.
@@ -41,7 +41,7 @@ func (api *WebapiInstance) apiStatus(w http.ResponseWriter, r *http.Request) {
// Instead, the core should keep a count of "active peers".
status.IsConnected = status.CountPeerList >= 2
EncodeJSON(api.backend, w, r, status)
EncodeJSON(api.Backend, w, r, status)
}
type apiResponsePeerSelf struct {
@@ -56,12 +56,12 @@ Result: 200 with JSON structure apiResponsePeerSelf
*/
func (api *WebapiInstance) apiAccountInfo(w http.ResponseWriter, r *http.Request) {
response := apiResponsePeerSelf{}
response.NodeID = hex.EncodeToString(api.backend.SelfNodeID())
response.NodeID = hex.EncodeToString(api.Backend.SelfNodeID())
_, publicKey := api.backend.ExportPrivateKey()
_, publicKey := api.Backend.ExportPrivateKey()
response.PeerID = hex.EncodeToString(publicKey.SerializeCompressed())
EncodeJSON(api.backend, w, r, response)
EncodeJSON(api.Backend, w, r, response)
}
/*
@@ -78,7 +78,7 @@ func (api *WebapiInstance) apiAccountDelete(w http.ResponseWriter, r *http.Reque
return
}
api.backend.DeleteAccount()
api.Backend.DeleteAccount()
w.WriteHeader(http.StatusOK)
}
@@ -95,7 +95,7 @@ func (api *WebapiInstance) apiStatusPeers(w http.ResponseWriter, r *http.Request
var peers []apiResponsePeerInfo
// query all nodes
for _, peer := range api.backend.PeerlistGet() {
for _, peer := range api.Backend.PeerlistGet() {
peerInfo := apiResponsePeerInfo{
PeerID: peer.PublicKey.SerializeCompressed(),
NodeID: peer.NodeID,
@@ -112,7 +112,7 @@ func (api *WebapiInstance) apiStatusPeers(w http.ResponseWriter, r *http.Request
peers = append(peers, peerInfo)
}
EncodeJSON(api.backend, w, r, peers)
EncodeJSON(api.Backend, w, r, peers)
}
type apiResponsePeerInfo struct {

View File

@@ -26,13 +26,13 @@ Request: POST /warehouse/create with raw data to create as new file
Response: 200 with JSON structure WarehouseResult
*/
func (api *WebapiInstance) apiWarehouseCreateFile(w http.ResponseWriter, r *http.Request) {
hash, status, err := api.backend.UserWarehouse.CreateFile(r.Body, 0)
hash, status, err := api.Backend.UserWarehouse.CreateFile(r.Body, 0)
if err != nil {
api.backend.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
api.Backend.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
}
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
EncodeJSON(api.Backend, w, r, WarehouseResult{Status: status, Hash: hash})
}
/*
@@ -51,23 +51,26 @@ func (api *WebapiInstance) apiWarehouseCreateFilePath(w http.ResponseWriter, r *
return
}
hash, status, err := api.backend.UserWarehouse.CreateFileFromPath(filePath)
hash, status, err := api.Backend.UserWarehouse.CreateFileFromPath(filePath)
if err != nil {
api.backend.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
api.Backend.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
}
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
EncodeJSON(api.Backend, w, r, WarehouseResult{Status: status, Hash: hash})
}
/*
apiWarehouseReadFile reads a file in the warehouse.
Request: GET /warehouse/read?hash=[hash]
Optional parameters &offset=[file offset]&limit=[read limit in bytes]
Optional parameters &offset=[file offset]&limit=[read limit in bytes]
Response: 200 with the raw file data
404 if file was not found
500 in case of internal error opening the file
404 if file was not found
500 in case of internal error opening the file
*/
func (api *WebapiInstance) apiWarehouseReadFile(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@@ -80,7 +83,7 @@ func (api *WebapiInstance) apiWarehouseReadFile(w http.ResponseWriter, r *http.R
offset, _ := strconv.Atoi(r.Form.Get("offset"))
limit, _ := strconv.Atoi(r.Form.Get("limit"))
status, bytesRead, err := api.backend.UserWarehouse.ReadFile(hash, int64(offset), int64(limit), w)
status, bytesRead, err := api.Backend.UserWarehouse.ReadFile(hash, int64(offset), int64(limit), w)
switch status {
case warehouse.StatusFileNotFound:
@@ -94,7 +97,7 @@ func (api *WebapiInstance) apiWarehouseReadFile(w http.ResponseWriter, r *http.R
}
if err != nil {
api.backend.LogError("warehouse.ReadFile", "status %d read %d error: %v", status, bytesRead, err)
api.Backend.LogError("warehouse.ReadFile", "status %d read %d error: %v", status, bytesRead, err)
}
}
@@ -112,13 +115,13 @@ func (api *WebapiInstance) apiWarehouseDeleteFile(w http.ResponseWriter, r *http
return
}
status, err := api.backend.UserWarehouse.DeleteFile(hash)
status, err := api.Backend.UserWarehouse.DeleteFile(hash)
if err != nil {
api.backend.LogError("warehouse.DeleteFile", "status %d error: %v", status, err)
api.Backend.LogError("warehouse.DeleteFile", "status %d error: %v", status, err)
}
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
EncodeJSON(api.Backend, w, r, WarehouseResult{Status: status, Hash: hash})
}
/*
@@ -126,7 +129,9 @@ apiWarehouseReadFilePath reads a file from the warehouse and stores it to the ta
The path must include the full directory and file name.
Request: GET /warehouse/read/path?hash=[hash]&path=[target path on disk]
Optional parameters &offset=[file offset]&limit=[read limit in bytes]
Optional parameters &offset=[file offset]&limit=[read limit in bytes]
Response: 200 with JSON structure WarehouseResult
*/
func (api *WebapiInstance) apiWarehouseReadFilePath(w http.ResponseWriter, r *http.Request) {
@@ -141,11 +146,11 @@ func (api *WebapiInstance) apiWarehouseReadFilePath(w http.ResponseWriter, r *ht
offset, _ := strconv.Atoi(r.Form.Get("offset"))
limit, _ := strconv.Atoi(r.Form.Get("limit"))
status, bytesRead, err := api.backend.UserWarehouse.ReadFileToDisk(hash, int64(offset), int64(limit), targetFile)
status, bytesRead, err := api.Backend.UserWarehouse.ReadFileToDisk(hash, int64(offset), int64(limit), targetFile)
if err != nil {
api.backend.LogError("warehouse.ReadFileToDisk", "status %d read %d error: %v", status, bytesRead, err)
api.Backend.LogError("warehouse.ReadFileToDisk", "status %d read %d error: %v", status, bytesRead, err)
}
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
EncodeJSON(api.Backend, w, r, WarehouseResult{Status: status, Hash: hash})
}