-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborgHUIAccessAPI.js
More file actions
200 lines (170 loc) · 7.23 KB
/
Copy pathborgHUIAccessAPI.js
File metadata and controls
200 lines (170 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const axios = require('axios');
const https = require('https');
class BorgAccessAPI {
constructor(net) {
this.net = net;
this.PTC_memRECEPTOR = "https://172.105.110.34:1335";
this.PTC_shardRECEPTOR = "https://139.177.195.184:13355";
this.PTC_ftreeRECEPTOR = "https://139.177.195.184:13381";
this.PTC_mailRECEPTOR = "https://139.177.195.184:13395/newREQ";
this.PTC_maxWordLength = 45;
// Create an Axios instance with an agent that ignores SSL cert verification
this.axiosInstance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
}
async sendRequest(url, reqType, data, treeType='repo') {
try {
var response = null;
switch (treeType) {
case 'repo':
response = await this.axiosInstance.post(url, { msg: { req: reqType, repo: data } });
break;
case 'qry':
response = await this.axiosInstance.post(url, { msg: { req: reqType, qry: data } });
console.log(url,{msg:{req:reqType,qry:data}});
break;
case 'memory':
response = await this.axiosInstance.post(url, { msg: { req: reqType, memory: data } });
break;
case 'shard':
response = await this.axiosInstance.post(url, { msg: { req: reqType, shard: data } });
break;
}
if (response){
return response.data;
}
return {error: 'treeType not found: '+treeType}
}
catch (error) {
return { error: error.message };
}
}
prepWords(str) {
if (!str || str.trim() === '') return null;
const words = [' i ', ' in ', ' on ', ' there ', ' is ', ' are ', ' as ', ' the ', ' a ', ' to ', ' and ', ' too ', ' of ', ' for '];
words.forEach(word => {
str = str.replace(new RegExp(word, 'gi'), ' ');
});
str = str.replace(/[\p{P}\p{S}]+/gu, " ").toLowerCase();
const list = str.split(' ').map(word => word.slice(0, this.PTC_maxWordLength));
const newStr = list.filter(word => word.trim() !== '').join(' ');
return newStr.length > 0 ? newStr : null;
}
async ftreeCreateRepo(muid, name, nCopys) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "createRepo", { from: muid, name, nCopys });
}
async ftreeCreateRepoFolder(muid, name, folder, parent) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "createRepoFolder", { from: muid, name, folder, parent });
}
async ftreeGetMyRepos(muid) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "getMyRepoList", { from: muid });
}
async ftreeGetMyRepoPath(muid, name, fname, folderID) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "getMyRepoFilePath", { from: muid, name, fname, folderID });
}
async ftreeGetMyRepoFiles(muid, name, fparentID = null) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "getMyRepoFiles", { from: muid, name, parentID: fparentID });
}
async ftreeGetFileFromRepo(muid, name, file, path, folderID) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "getRepoFileData", { from: muid, name, file, path, folderID });
}
async ftreeInsertFileToRepo(muid, name, file, path, folderID, nCopys) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "insertRSfile", { from: muid, name, file, path, folderID, nCopys });
}
async ftreeDeleteFileFromRepo(muid, name, file, path, nCopys) {
return this.sendRequest(this.PTC_ftreeRECEPTOR + "/netREQ", "deleteRSfile", { from: muid, name, file, path, nCopys });
}
async ptreeStoreShard(muid, hash, shard, encrypt = null, nCopys = 3, expires = null) {
return this.sendRequest(this.PTC_shardRECEPTOR + "/netREQ", "storeShard", { from: muid, hash, data: shard, encrypt, expires, nCopys },'shard');
}
async ptreeRequestShard(muid, hash, encrypted = null) {
return this.sendRequest(this.PTC_shardRECEPTOR + "/netREQ", "requestShard", { ownerID: muid, hash, encrypted },'shard');
}
async ptreeDeleteShard(muid, hash, encrypted = null, nCopys = 3) {
return this.sendRequest(this.PTC_shardRECEPTOR + "/netREQ", "deleteShard", { ownerID: muid, hash, nCopys },'shard');
}
async ptreeSearchMem(muid, str, type, scope = null, scopeID = null, qryLimit = null, qryOrder = null) {
const payload = {
ownerID: muid,
qryStr: this.prepWords(str),
qryType: type,
qryStyle: 'bestMatch',
timestamp: Math.floor(Date.now() / 1000),
reqScore: 0.0005,
nResults: 100,
nRows: 15,
pg: 1,
qryLimit: qryLimit || ' limit 40',
qryOrder: qryOrder || undefined
};
if (scope) {
payload.scope = scope;
payload.scopeID = scopeID;
}
payload.key = this.ptreeMakeSearchKey(payload);
return this.sendRequest(this.PTC_memRECEPTOR + "/netREQ", "searchMemory", payload,'qry');
}
async ptreeStoreMem(muid, acID, str, type = 'generic', nCopys = 3, weights = null) {
const payload = {
from: muid,
memID: acID,
memStr: str,
memType: type,
nCopys: nCopys,
weights: weights
};
return this.sendRequest(`${this.PTC_memRECEPTOR}/netREQ`, "storeMemory", payload,'memory');
}
ptreeMakeSearchKey(j) {
return require('crypto').createHash('sha256').update(JSON.stringify(j)).digest('hex');
}
async peerMailGetMyMsgs(muid, sig) {
const req = { req: 'getMyMail', ownMUID: muid, sig: sig };
try {
const response = await axios.post(this.PTC_mailRECEPTOR, { msg: req });
return response.data;
} catch (error) {
console.error("Error fetching mail:", error);
return null;
}
}
async peerMailSendMsg(toMuid, mail, sig) {
const req = { req: 'sendMail', toMUID: toMuid, sig: sig, mail: mail };
try {
const response = await axios.post(this.PTC_mailRECEPTOR, { msg: req });
return response.data;
} catch (error) {
console.error("Error sending mail:", error);
return null;
}
}
async peerMailGetInboxKey(muid) {
const req = { msg: { req: "getInBoxKey", ownMUID: muid } };
try {
const response = await axios.post(this.PTC_mailRECEPTOR, req);
return response.data;
} catch (error) {
console.error("Error retrieving inbox key:", error);
return null;
}
}
async peerMailRegisterInBox(muid, token, publicKey, sig) {
const p = {
inboxMUID: muid,
nCopies: 3,
sig: { ownMUID: muid, signature: sig, token: token, pubKey: publicKey }
};
const req = { msg: { req: "registerInBox", ...p } };
try {
const response = await axios.post(this.PTC_mailRECEPTOR, req);
return response.data;
} catch (error) {
console.error("Error registering inbox:", error);
return null;
}
}
}
module.exports.BorgAccessAPI = BorgAccessAPI;