added ip table
This commit is contained in:
parent
d611869f0e
commit
9b57c5f787
@ -10415,6 +10415,7 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
addClosestSupernodeInDB: function(flo_addr, KB=KBucket) {
|
addClosestSupernodeInDB: function(flo_addr, KB=KBucket) {
|
||||||
return new Promise((resolve, reject)=>{
|
return new Promise((resolve, reject)=>{
|
||||||
const supernodeSeeds = localbitcoinplusplus.master_configurations.supernodeSeeds;
|
const supernodeSeeds = localbitcoinplusplus.master_configurations.supernodeSeeds;
|
||||||
@ -10446,6 +10447,7 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Encryption -->
|
<!-- Encryption -->
|
||||||
@ -13689,9 +13691,23 @@
|
|||||||
async handle_backup_server_messages(evt) {
|
async handle_backup_server_messages(evt) {
|
||||||
var response = evt.data;
|
var response = evt.data;
|
||||||
console.log('backup response: '+response);
|
console.log('backup response: '+response);
|
||||||
|
|
||||||
|
// let isRequestToLinkIp = response.search("linkMyLocalIPToMyFloId");
|
||||||
|
// if (isRequestToLinkIp>=0) {
|
||||||
|
// linkIpToFloId(response);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
var res_pos = response.indexOf('{');
|
var res_pos = response.indexOf('{');
|
||||||
if (res_pos >= 0) {
|
if (res_pos >= 0) {
|
||||||
|
let isRequestToLinkIp = response.search("linkMyLocalIPToMyFloId");
|
||||||
|
let incoming_msg_local_ip = ``;
|
||||||
|
if (isRequestToLinkIp>=0) {
|
||||||
|
let index_of_ip = response.indexOf(' ');
|
||||||
|
if (incoming_msg_local_ip>=0) {
|
||||||
|
incoming_msg_local_ip = response.substr(0, index_of_ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
var res = response.substr(res_pos);
|
var res = response.substr(res_pos);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@ -14034,7 +14050,36 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
case "requestSupernodesToRemoveAUserFloIdFromTheirKBucket":
|
||||||
|
RM_RPC.filter_legit_backup_requests(su_backup_db_data.trader_flo_address,
|
||||||
|
function (is_valid_request) {
|
||||||
|
if(!is_valid_request) return false;
|
||||||
|
if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") {
|
||||||
|
const removeKBReq = res_obj.params[0];
|
||||||
|
const currentSupernodeFloIdOfUser = removeKBReq.currentSupernodeFloId;
|
||||||
|
if(localbitcoinplusplus.wallets.my_local_flo_address == currentSupernodeFloIdOfUser) return;
|
||||||
|
const userKbucketObject_id_array = Object.values(removeKBReq.redundantKbucketNodeU8Id);
|
||||||
|
const userKBId = new Uint8Array(userKbucketObject_id_array);
|
||||||
|
KBucket.remove(userKBId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "link_My_Local_IP_To_My_Flo_Id":
|
||||||
|
if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") {
|
||||||
|
const req_params = res_obj.params[0];
|
||||||
|
if(typeof req_params.requesters_pub_key !== "string") return;
|
||||||
|
let flo_addr_for_pubkey = bitjs.FLO_TEST.pubkey2address(req_params.requesters_pub_key);
|
||||||
|
if(typeof flo_addr_for_pubkey !== "string") return;
|
||||||
|
if(flo_addr_for_pubkey !== res_obj.globalParams.senderFloId) return;
|
||||||
|
updateinDB('ipTable', {
|
||||||
|
'flo_public_key': req_params.requesters_pub_key,
|
||||||
|
'temporary_ip': incoming_msg_local_ip
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.warn(e);
|
console.warn(e);
|
||||||
@ -14046,12 +14091,18 @@
|
|||||||
this.ws_connection = new WebSocket(this.ws_url);
|
this.ws_connection = new WebSocket(this.ws_url);
|
||||||
const switchMyWS = new backupSupernodesWebSocketObject();
|
const switchMyWS = new backupSupernodesWebSocketObject();
|
||||||
this.ws_connection.onopen = function (evt) {
|
this.ws_connection.onopen = function (evt) {
|
||||||
showMessage(`Connected to backup Supernode sever: ${this.ws_url}.`);
|
reactor.addEventListener('backup_supernode_up', function() {
|
||||||
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, true);
|
showMessage(`Connected to backup Supernode sever: ${evt.srcElement.url}.`);
|
||||||
|
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, true);
|
||||||
|
});
|
||||||
|
reactor.dispatchEvent('backup_supernode_up');
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
this.ws_connection.onclose = function (evt) {
|
this.ws_connection.onclose = function (evt) {
|
||||||
showMessage(`Disconnected to backup Supernode sever: ${this.ws_url}.`);
|
reactor.addEventListener('backup_supernode_down', function() {
|
||||||
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, false);
|
showMessage(`Disconnected to backup Supernode sever: ${evt.srcElement.url}.`);
|
||||||
|
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, false);
|
||||||
|
});
|
||||||
|
reactor.dispatchEvent('backup_supernode_down');
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
this.ws_connection.onmessage = function (evt) {
|
this.ws_connection.onmessage = function (evt) {
|
||||||
this.handle_backup_server_messages(evt);
|
this.handle_backup_server_messages(evt);
|
||||||
@ -14104,6 +14155,8 @@
|
|||||||
|| localbitcoinplusplus.master_configurations.supernodesPubKeys
|
|| localbitcoinplusplus.master_configurations.supernodesPubKeys
|
||||||
.includes(user_flo_address)) return false;
|
.includes(user_flo_address)) return false;
|
||||||
|
|
||||||
|
let ideal_supernode = '';
|
||||||
|
|
||||||
const myClosestSupernodesArray = await readAllDB(`myClosestSupernodes`);
|
const myClosestSupernodesArray = await readAllDB(`myClosestSupernodes`);
|
||||||
|
|
||||||
// Learn about the previous connection. If it was primary supernode continue
|
// Learn about the previous connection. If it was primary supernode continue
|
||||||
@ -14116,7 +14169,7 @@
|
|||||||
|
|
||||||
// Get the status of last_connect_supernode
|
// Get the status of last_connect_supernode
|
||||||
if(status_of_last_connected_backup_su[0].is_live===true) {
|
if(status_of_last_connected_backup_su[0].is_live===true) {
|
||||||
await startWebSocket(`ws://${status_of_last_connected_backup_su[0].ip}:${status_of_last_connected_backup_su[0].port}`);
|
ideal_supernode = `ws://${status_of_last_connected_backup_su[0].ip}:${status_of_last_connected_backup_su[0].port}`;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Connection to last connected backup supernode failed,
|
// Connection to last connected backup supernode failed,
|
||||||
@ -14133,10 +14186,11 @@
|
|||||||
if(ww==disconnected_url) z = true;
|
if(ww==disconnected_url) z = true;
|
||||||
})
|
})
|
||||||
|
|
||||||
let nextClosestSupernode = `ws://${nextClosestSupernodeElem[0].ip}:${nextClosestSupernodeElem[0].port}`;
|
ideal_supernode = `ws://${nextClosestSupernodeElem[0].ip}:${nextClosestSupernodeElem[0].port}`;
|
||||||
|
|
||||||
await startWebSocket(nextClosestSupernode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await startWebSocket(ideal_supernode);
|
||||||
|
|
||||||
if(websocket.readyState===1) {
|
if(websocket.readyState===1) {
|
||||||
|
|
||||||
@ -14161,7 +14215,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
function startWebSocket(wsUri) {
|
function startWebSocket(wsUri) {
|
||||||
|
|
||||||
@ -14183,38 +14237,52 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onOpen(evt) {
|
function onOpen(evt) {
|
||||||
showMessage(`Connected successfully to Supernode: ${evt.srcElement.url}`);
|
reactor.addEventListener('primary_supernode_up', function() {
|
||||||
writeToScreen("CONNECTED");
|
const switchMyWS = new backupSupernodesWebSocketObject();
|
||||||
const switchMyWS = new backupSupernodesWebSocketObject();
|
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, true);
|
||||||
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, true);
|
showMessage(`Connected successfully to Supernode: ${evt.srcElement.url}`);
|
||||||
|
writeToScreen("CONNECTED");
|
||||||
|
});
|
||||||
|
reactor.dispatchEvent('primary_supernode_up');
|
||||||
}
|
}
|
||||||
|
|
||||||
function onClose(evt) {
|
function onClose(evt) {
|
||||||
|
|
||||||
showMessage(`Disconnected to Supernode server: ${evt.srcElement.url}`);
|
reactor.addEventListener('primary_supernode_down', function() {
|
||||||
writeToScreen("DISCONNECTED");
|
showMessage(`Disconnected to Supernode server: ${evt.srcElement.url}`);
|
||||||
|
writeToScreen("DISCONNECTED");
|
||||||
const switchMyWS = new backupSupernodesWebSocketObject();
|
|
||||||
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, false);
|
const switchMyWS = new backupSupernodesWebSocketObject();
|
||||||
showMessage(`INFO: Waiting for primary Supernode connection to come back within 20 seconds.`);
|
switchMyWS.updateSupernodeAvailabilityStatus(evt.srcElement.url, false);
|
||||||
localbitcoinplusplus.actions.delay(20000).then(async ()=>{
|
showMessage(`INFO: Waiting for primary Supernode connection to come back within 20 seconds.`);
|
||||||
const disconnectedWSServerFloId = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url);
|
localbitcoinplusplus.actions.delay(20000).then(async ()=>{
|
||||||
const getSubjectSupernodeDetails = await readDBbyIndex('myClosestSupernodes', 'trader_flo_address', disconnectedWSServerFloId);
|
const disconnectedWSServerFloId = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url);
|
||||||
if (typeof getSubjectSupernodeDetails=="object" && getSubjectSupernodeDetails[0].is_live!==true) {
|
const getSubjectSupernodeDetails = await readDBbyIndex('myClosestSupernodes', 'trader_flo_address', disconnectedWSServerFloId);
|
||||||
showMessage(`INFO: Connection to primary Supernode failed. Attempting to connect to secondary Supernode.`);
|
if (typeof getSubjectSupernodeDetails=="object" && getSubjectSupernodeDetails[0].is_live!==true) {
|
||||||
switchMyWS.switchToBackupWS(evt.srcElement.url);
|
showMessage(`INFO: Connection to primary Supernode failed. Attempting to connect to secondary Supernode.`);
|
||||||
}
|
switchMyWS.switchToBackupWS(evt.srcElement.url);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
reactor.dispatchEvent('primary_supernode_down');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onMessage(evt) {
|
async function onMessage(evt) {
|
||||||
var response = evt.data;
|
var response = evt.data;
|
||||||
//let is_user_leaving = response.search("-- left");
|
|
||||||
//console.log("is_user_leaving", is_user_leaving);
|
|
||||||
|
|
||||||
var res_pos = response.indexOf('{');
|
var res_pos = response.indexOf('{');
|
||||||
if (res_pos >= 0) {
|
if (res_pos >= 0) {
|
||||||
|
|
||||||
|
let isRequestToLinkIp = response.search("linkMyLocalIPToMyFloId");
|
||||||
|
let incoming_msg_local_ip = ``;
|
||||||
|
if (isRequestToLinkIp>=0) {
|
||||||
|
let index_of_ip = response.indexOf(' ');
|
||||||
|
if (incoming_msg_local_ip>=0) {
|
||||||
|
incoming_msg_local_ip = response.substr(0, index_of_ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var res = response.substr(res_pos);
|
var res = response.substr(res_pos);
|
||||||
try {
|
try {
|
||||||
var res_obj = JSON.parse(res);
|
var res_obj = JSON.parse(res);
|
||||||
@ -14938,12 +15006,23 @@
|
|||||||
newKbucketObject_id_array = Object.values(newKbucketObject.id);
|
newKbucketObject_id_array = Object.values(newKbucketObject.id);
|
||||||
newKbucketObject_idu8 = new Uint8Array(newKbucketObject_id_array);
|
newKbucketObject_idu8 = new Uint8Array(newKbucketObject_id_array);
|
||||||
if (localbitcoinplusplus.wallets.my_local_flo_address !== my_closest_su[0].data.id) {
|
if (localbitcoinplusplus.wallets.my_local_flo_address !== my_closest_su[0].data.id) {
|
||||||
|
// User is connected to backup supernode
|
||||||
localbitcoinplusplus.kademlia.addNewUserNodeInKbucket("FLO_TEST",
|
localbitcoinplusplus.kademlia.addNewUserNodeInKbucket("FLO_TEST",
|
||||||
newKbucketObject_idu8, newKbucketObject.data);
|
newKbucketObject_idu8, newKbucketObject.data);
|
||||||
} else {
|
} else {
|
||||||
|
// User is connected to primary supernode
|
||||||
localbitcoinplusplus.kademlia.addNewUserNodeInKbucketAndDB("FLO_TEST",
|
localbitcoinplusplus.kademlia.addNewUserNodeInKbucketAndDB("FLO_TEST",
|
||||||
newKbucketObject_idu8, newKbucketObject.data);
|
newKbucketObject_idu8, newKbucketObject.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let removeRedundantKNode = localbitcoinplusplus.rpc.prototype
|
||||||
|
.send_rpc
|
||||||
|
.call(this, "requestSupernodesToRemoveAUserFloIdFromTheirKBucket", {
|
||||||
|
redundantKbucketNodeU8Id: newKbucketObject_idu8,
|
||||||
|
currentSupernodeFloId: localbitcoinplusplus.wallets.my_local_flo_address
|
||||||
|
});
|
||||||
|
doSend(removeRedundantKNode);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
mss = `WARNING: Failed to add ${res_obj.globalParams.senderFloId} to KBucket.`;
|
mss = `WARNING: Failed to add ${res_obj.globalParams.senderFloId} to KBucket.`;
|
||||||
showMessage(mss)
|
showMessage(mss)
|
||||||
@ -14970,7 +15049,21 @@
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "link_My_Local_IP_To_My_Flo_Id":
|
||||||
|
if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") {
|
||||||
|
const req_params = res_obj.params[0];
|
||||||
|
if(typeof req_params.requesters_pub_key !== "string") return;
|
||||||
|
let flo_addr_for_pubkey = bitjs.FLO_TEST.pubkey2address(req_params.requesters_pub_key);
|
||||||
|
if(typeof flo_addr_for_pubkey !== "string") return;
|
||||||
|
if(flo_addr_for_pubkey !== res_obj.globalParams.senderFloId) return;
|
||||||
|
updateinDB('ipTable', {
|
||||||
|
'flo_public_key': req_params.requesters_pub_key,
|
||||||
|
'temporary_ip': incoming_msg_local_ip
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case "supernode_to_supernode_backup_request":
|
case "supernode_to_supernode_backup_request":
|
||||||
|
|
||||||
@ -15057,7 +15150,7 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "messageBroadcasting":
|
case "messageBroadcasting":
|
||||||
console.log(res_obj);
|
console.log(res_obj);
|
||||||
try {
|
try {
|
||||||
@ -15317,9 +15410,14 @@
|
|||||||
message: null
|
message: null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ipTable = {
|
||||||
|
flo_public_key: null,
|
||||||
|
temporary_ip: null
|
||||||
|
}
|
||||||
|
|
||||||
var db;
|
var db;
|
||||||
const DBName = "localbitcoinDB";
|
const DBName = "localbitcoinDB";
|
||||||
const request = window.indexedDB.open(DBName, 1);
|
const request = window.indexedDB.open(DBName, 2);
|
||||||
|
|
||||||
request.onerror = function (event) {
|
request.onerror = function (event) {
|
||||||
//https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox
|
//https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox
|
||||||
@ -15490,6 +15588,11 @@
|
|||||||
unique: false
|
unique: false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (!db.objectStoreNames.contains('ipTable')) {
|
||||||
|
var objectStore = db.createObjectStore("ipTable", {
|
||||||
|
keyPath: 'flo_public_key'
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function readDB(tablename, id) {
|
function readDB(tablename, id) {
|
||||||
@ -15804,6 +15907,11 @@
|
|||||||
unique: false
|
unique: false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (!db.objectStoreNames.contains('ipTable')) {
|
||||||
|
var objectStore = db.createObjectStore("ipTable", {
|
||||||
|
keyPath: 'flo_public_key'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
|
|
||||||
@ -16078,6 +16186,9 @@
|
|||||||
const MY_LOCAL_FLO_PUBLIC_KEY = localbitcoinplusplus.wallets.my_local_flo_public_key =
|
const MY_LOCAL_FLO_PUBLIC_KEY = localbitcoinplusplus.wallets.my_local_flo_public_key =
|
||||||
idbData.myLocalFLOPublicKey;
|
idbData.myLocalFLOPublicKey;
|
||||||
|
|
||||||
|
// Send request to others to link your flo id to your local ip
|
||||||
|
linkMyLocalIPToMyFloId();
|
||||||
|
|
||||||
// restore k-bucket
|
// restore k-bucket
|
||||||
const dbObj = await localbitcoinplusplus.kademlia.restoreKbucket(MY_LOCAL_FLO_ADDRESS, "FLO_TEST", KBucket);
|
const dbObj = await localbitcoinplusplus.kademlia.restoreKbucket(MY_LOCAL_FLO_ADDRESS, "FLO_TEST", KBucket);
|
||||||
|
|
||||||
@ -16755,9 +16866,31 @@
|
|||||||
});
|
});
|
||||||
doSend(send_backup_request);
|
doSend(send_backup_request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function linkMyLocalIPToMyFloId() {
|
||||||
|
const RM_RPC = new localbitcoinplusplus.rpc;
|
||||||
|
let request = RM_RPC.send_rpc.call(this,
|
||||||
|
"link_My_Local_IP_To_My_Flo_Id", {
|
||||||
|
"JOB": "linkMyLocalIPToMyFloId",
|
||||||
|
requesters_pub_key: localbitcoinplusplus.wallets.my_local_flo_public_key,
|
||||||
|
requesters_flo_address: localbitcoinplusplus.wallets.my_local_flo_address
|
||||||
|
});
|
||||||
|
doSend(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
function linkIpToFloId(params) {
|
||||||
|
console.log(params);
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
reactor.registerEvent('primary_supernode_up');
|
||||||
|
reactor.registerEvent('primary_supernode_down');
|
||||||
|
reactor.registerEvent('backup_supernode_up');
|
||||||
|
reactor.registerEvent('backup_supernode_down');
|
||||||
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
@ -84,4 +84,4 @@ int main(void) {
|
|||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user