-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d813eb9
commit 3d12225
Showing
1 changed file
with
136 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,154 @@ | ||
// contains encryption tools | ||
// made by cool-dev-guy with combination of link-lock code. | ||
document.setup = false; | ||
document.loadedIndex = 0; | ||
const proxy = "https://cool-chat-proxy.vercel.app/proxy"; | ||
const proxy_get = "https://cool-chat-proxy.vercel.app/proxy-get" | ||
function evaluateChatJoin(){ | ||
_joinUrl = document.getElementById("chatJoinInput").value; | ||
_Username = document.getElementById("chatUsernameInput").value; | ||
_publicUrl = document.getElementById("chatJoinInput2").value; | ||
if (_joinUrl!=null && _Username != null){ | ||
document.chatUsername = _Username; | ||
document.chatUri = _joinUrl; | ||
console.log('[App]Chat Joined as '+_Username) | ||
openTab('chatBox'); | ||
document.chatInterface = document.getElementById('chat'); | ||
document.chatInterface.scrollTop = document.chatInterface.scrollHeight; | ||
document.setup = true; | ||
document.chatString = '' | ||
|
||
const short_api = { | ||
'post':"https://api.shrtco.de/v2/shorten?url=", | ||
'fetch':"https://api.shrtco.de/v2/info?code=" | ||
} | ||
async function generateFragment(url, passwd, hint, useRandomSalt, useRandomIv) { | ||
const api = apiVersions[LATEST_API_VERSION]; | ||
if (getStorage('session') != _joinUrl){ | ||
sendStorage('session',_joinUrl) | ||
sendStorage('userMessageCount',0) | ||
} | ||
|
||
const salt = useRandomSalt ? await api.randomSalt() : null; | ||
const iv = useRandomIv ? await api.randomIv() : null; | ||
const encrypted = await api.encrypt(url, passwd, salt, iv); | ||
const output = { | ||
v: LATEST_API_VERSION, | ||
e: b64.binaryToBase64(new Uint8Array(encrypted)) | ||
createWebWorker(_publicUrl); | ||
} | ||
|
||
// Add the hint if there is one | ||
if (hint != "") { | ||
output["h"] = hint; | ||
|
||
else{ | ||
document.setup = false; | ||
} | ||
|
||
// Add the salt and/or initialization vector if randomly generated | ||
if (useRandomSalt) { | ||
output["s"] = b64.binaryToBase64(salt); | ||
} | ||
function sendStorage(variable,value){ | ||
if (typeof(Storage) !== "undefined") { | ||
localStorage.setItem(variable,value); | ||
} else { | ||
console.log('local storage not supported') | ||
} | ||
if (useRandomIv) { | ||
output["i"] = b64.binaryToBase64(iv); | ||
} | ||
function getStorage(variable){ | ||
if (typeof(Storage) !== "undefined") { | ||
return localStorage.getItem(variable); | ||
} else { | ||
console.log('local storage not supported'); | ||
return null; | ||
} | ||
|
||
// Return the base64-encoded output | ||
return b64.encode(JSON.stringify(output)); | ||
} | ||
async function encryptMessage(password,message){ | ||
// Check that password is successfully confirmed | ||
url = message; | ||
const confirmPassword = password; | ||
const confirmation = password; | ||
// if (password != confirmation) { | ||
// confirmPassword.setCustomValidity("Passwords do not match"); | ||
// confirmPassword.reportValidity(); | ||
// return; | ||
// } | ||
function retrieveMessage(data) { | ||
if (document.chatString != data) { | ||
console.log(data); | ||
document.chatString = data; | ||
|
||
// Initialize values for encryption | ||
const useRandomIv = true; | ||
const useRandomSalt = true; | ||
var jsonData = JSON.parse(document.chatString); | ||
|
||
const hint = "cool-chat(devoloped by cool-dev-guy)(encription used from github.com/jstrieb/link-lock)"; | ||
if (jsonData && jsonData.dreamlo && jsonData.dreamlo.leaderboard && jsonData.dreamlo.leaderboard.entry) { | ||
var messages = jsonData.dreamlo.leaderboard.entry; | ||
|
||
const encrypted = await generateFragment(url, password, hint, useRandomSalt,useRandomIv); | ||
const output = `https://example.com/?text=${encrypted}`; | ||
return output; | ||
} | ||
function createMessageURI(text) { | ||
return new Promise((resolve, reject) => { | ||
const url = `${short_api.post}${text}`; | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, true); | ||
xhr.onload = function () { | ||
if (xhr.status === 201) { | ||
const responseData = JSON.parse(xhr.responseText); | ||
// console.log('Request was successful.'); | ||
// console.log('Response content:', responseData); | ||
resolve(responseData.result.code); | ||
} else { | ||
console.log('Request failed:', xhr.statusText); | ||
reject(xhr.statusText); | ||
for (var i = 0; i < messages.length; i++) { | ||
var entry = messages[i]; | ||
var _temp_name = entry.name.split(';'); | ||
if (i > document.loadedIndex) { | ||
chatAppCreateMessageBox(_temp_name[0], entry.text); | ||
document.loadedIndex = i; | ||
} | ||
} | ||
}; | ||
xhr.onerror = function () { | ||
console.log('Network error occurred.'); | ||
reject('Network error'); | ||
}; | ||
xhr.send(); | ||
}); | ||
} | ||
function fetchMessageURI(code) { | ||
return new Promise((resolve, reject) => { | ||
const url = `${short_api.fetch}${code}`; | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, true); | ||
xhr.onload = function () { | ||
if (xhr.status === 200) { | ||
const responseData = JSON.parse(xhr.responseText); | ||
// console.log('Response content:', responseData); | ||
const encryptedMessage = responseData.result.url; | ||
resolve(encryptedMessage); | ||
} else { | ||
console.log('Request failed with status:', xhr.status); | ||
reject(xhr.statusText); | ||
}; | ||
}; | ||
xhr.onerror = function () { | ||
console.log('Network error occurred.'); | ||
reject('Network error'); | ||
}; | ||
xhr.send(); | ||
}); | ||
}; | ||
async function decryptMessage(urlx) { | ||
if (!("b64" in window)) { | ||
console.error("Base64 library not loaded."); | ||
return; | ||
} | ||
if (!("apiVersions" in window)) { | ||
console.error("API library not loaded."); | ||
return; | ||
} | ||
const urlObject = new URL(urlx); | ||
const searchParams = urlObject.searchParams; | ||
const textParameterValue = searchParams.get("text"); | ||
if (textParameterValue !== null) { | ||
// console.log("Encripted message:", textParameterValue); | ||
} else { | ||
console.error("The message is corrupted."); | ||
} | ||
const hash = `${textParameterValue}`; | ||
let params; | ||
try { | ||
params = JSON.parse(b64.decode(hash)); | ||
} catch { | ||
return "err"; | ||
} | ||
} | ||
|
||
function createWebWorker(url){ | ||
var worker; | ||
if(typeof(Worker) !== "undefined") { | ||
if(typeof(worker) == "undefined") { | ||
console.log("starting Web worker!"); | ||
worker = new Worker("worker/message_fetcher.js"); | ||
} | ||
const api = apiVersions[params["v"]]; | ||
const encrypted = b64.base64ToBinary(params["e"]); | ||
const salt = "s" in params ? b64.base64ToBinary(params["s"]) : null; | ||
const iv = "i" in params ? b64.base64ToBinary(params["i"]) : null; | ||
password = "default"; | ||
let url; | ||
try { | ||
url = await api.decrypt(encrypted, password, salt, iv); | ||
return url; | ||
} catch { | ||
return "err"; | ||
} | ||
console.log(worker,"started Web worker"); | ||
worker.postMessage(`${proxy_get}/${url}/json`); | ||
worker.onmessage = (evt) => retrieveMessage(evt.data); | ||
} else { | ||
console.log("Sorry, your browser does not support Web Workers..."); | ||
} | ||
|
||
// custom | ||
// const cross_origin_script_url = "https://cool-dev-guy.github.io/cool-chat/worker/message_fetcher.js"; | ||
// const worker_url = getWorkerURL( cross_origin_script_url ); | ||
// const worker = new Worker( worker_url ); | ||
|
||
// URL.revokeObjectURL( worker_url ); | ||
|
||
// Returns a blob:// URL which points | ||
// to a javascript file which will call | ||
// importScripts with the given URL | ||
// function getWorkerURL( url ) { | ||
// const content = `importScripts( "${ url }" );`; | ||
// return URL.createObjectURL( new Blob( [ content ], { type: "text/javascript" } ) ); | ||
// } | ||
} | ||
// MessageObject (aka message code) its a set of approx 6 chars representing a message. | ||
async function createMessageObject(message) { | ||
try { | ||
const _Encrypted_msg = await encryptMessage('default', `https://cool-dev-guy.github.io/link-encrypted-text/?text=${message}`); | ||
// console.log(_Encrypted_msg) | ||
const code = await createMessageURI(_Encrypted_msg); | ||
return code; | ||
} catch (error) { | ||
console.error('Error:', error); | ||
function chatAppCreateMessageBox(user,text){ | ||
var outerDiv = document.createElement('div'); | ||
var innerDiv = document.createElement('div'); | ||
if (user == document.chatUsername){ | ||
outerDiv.className = 'message-container user'; | ||
}else{ | ||
outerDiv.className = 'message-container other'; | ||
} | ||
|
||
|
||
innerDiv.className = 'messagebox'; | ||
var messager = document.createElement('span'); | ||
messager.textContent = user; | ||
messager.className = 'messager'; | ||
var message = document.createElement('span'); | ||
message.textContent = text; | ||
message.className = 'content'; | ||
innerDiv.appendChild(messager) | ||
innerDiv.appendChild(message) | ||
outerDiv.appendChild(innerDiv) | ||
document.getElementById('chat').appendChild(outerDiv); | ||
} | ||
async function readMessageObject(code) { | ||
try { | ||
const _Encrypted_msg = await fetchMessageURI(code); | ||
const _msg = await decryptMessage(_Encrypted_msg); | ||
console.log(_msg); | ||
console.log('Message:',new URL(_msg).searchParams.get('text')); | ||
} catch (error) { | ||
console.error('Error:', error); | ||
function sendMessage(){ | ||
_sendMessage = document.getElementById("MessageBoxInput").value; | ||
document.getElementById("MessageBoxInput").value = ''; | ||
if (_sendMessage!='' && document.chatUsername!=null && document.chatUri!=null){ | ||
_passableMessage = _sendMessage.substring(0, 185); | ||
console.log(document.chatUsername); | ||
//chatAppCreateMessageBox(document.chatUsername,_passableMessage); | ||
document.chatInterface.scrollTop = document.chatInterface.scrollHeight; | ||
sendMessageToServer(_passableMessage) | ||
} | ||
} | ||
async function sendMessageToServer(text) { | ||
var Mcount = parseInt(getStorage('userMessageCount'), 10); | ||
var message = await createMessageObject(text) | ||
const url = `${proxy}/${document.chatUri}/add/${document.chatUsername};${Mcount}/0/0/${message}`; | ||
sendStorage('userMessageCount',Mcount+1); | ||
console.log(url); | ||
|
||
// Create a new XHR object | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open("GET",url, true); | ||
xhr.onload = function () { | ||
if (xhr.status === 200) { | ||
console.log("Response:\n" + xhr.responseText) | ||
} else { | ||
console.log("Error: " + xhr.status) | ||
} | ||
}; | ||
xhr.onerror = function () { | ||
console.log("Network error occurred.") | ||
}; | ||
xhr.send(); | ||
} | ||
function openApp(){ | ||
openTab((document.setup === true) ? 'chatBox' : 'chatBin'); | ||
} |