import React, { Component } from 'react'; import { withTranslation } from "react-i18next"; import {Button, IconButton, Badge, Tooltip, TextField, Grid, Container, Card, CardHeader, Paper, Avatar, Typography} from "@mui/material"; import ReconnectingWebSocket from 'reconnecting-websocket'; import { encryptMessage , decryptMessage} from "../utils/pgp"; import { getCookie } from "../utils/cookies"; import { saveAsJson } from "../utils/saveFile"; import { AuditPGPDialog } from "./Dialogs" // Icons import CheckIcon from '@mui/icons-material/Check'; import CloseIcon from '@mui/icons-material/Close'; import ContentCopy from "@mui/icons-material/ContentCopy"; import VisibilityIcon from '@mui/icons-material/Visibility'; import CircularProgress from '@mui/material/CircularProgress'; import KeyIcon from '@mui/icons-material/Key'; import { ExportIcon } from './Icons'; class Chat extends Component { constructor(props) { super(props); } state = { own_pub_key: getCookie('pub_key').split('\\').join('\n'), own_enc_priv_key: getCookie('enc_priv_key').split('\\').join('\n'), peer_pub_key: null, token: getCookie('robot_token'), messages: [], value:'', connected: false, peer_connected: false, audit: false, showPGP: new Array, waitingEcho: false, lastSent: '---BLANK---', }; rws = new ReconnectingWebSocket('ws://' + window.location.host + '/ws/chat/' + this.props.orderId + '/'); componentDidMount() { this.rws.addEventListener('open', () => { console.log('Connected!'); this.setState({connected: true}); if ( this.state.peer_pub_key == null){ this.rws.send(JSON.stringify({ type: "message", message: "----PLEASE SEND YOUR PUBKEY----", nick: this.props.ur_nick, })); } this.rws.send(JSON.stringify({ type: "message", message: this.state.own_pub_key, nick: this.props.ur_nick, })); }); this.rws.addEventListener('message', (message) => { const dataFromServer = JSON.parse(message.data); console.log('Got reply!', dataFromServer.type); if (dataFromServer){ console.log(dataFromServer) // If we receive our own key on a message if (dataFromServer.message == this.state.own_pub_key){console.log("ECHO OF OWN PUB KEY RECEIVED!!")} // If we receive a request to send our public key if (dataFromServer.message == `----PLEASE SEND YOUR PUBKEY----`) { this.rws.send(JSON.stringify({ type: "message", message: this.state.own_pub_key, nick: this.props.ur_nick, })); } else // If we receive a public key other than ours (our peer key!) if (dataFromServer.message.substring(0,36) == `-----BEGIN PGP PUBLIC KEY BLOCK-----` & dataFromServer.message != this.state.own_pub_key) { if (dataFromServer.message == this.state.peer_pub_key){ console.log("PEER HAS RECONNECTED USING HIS PREVIOUSLY KNOWN PUBKEY") } else if (dataFromServer.message != this.state.peer_pub_key & this.state.peer_pub_key != null){ console.log("PEER PUBKEY HAS CHANGED") } console.log("PEER KEY PUBKEY RECEIVED!!") this.setState({peer_pub_key:dataFromServer.message}) } else // If we receive an encrypted message if (dataFromServer.message.substring(0,27) == `-----BEGIN PGP MESSAGE-----`){ decryptMessage( dataFromServer.message.split('\\').join('\n'), dataFromServer.user_nick == this.props.ur_nick ? this.state.own_pub_key : this.state.peer_pub_key, this.state.own_enc_priv_key, this.state.token) .then((decryptedData) => this.setState((state) => ({ waitingEcho: this.state.waitingEcho == true ? (decryptedData.decryptedMessage == this.state.lastSent ? false: true ) : false, lastSent: decryptedData.decryptedMessage == this.state.lastSent ? '----BLANK----': this.state.lastSent, messages: [...state.messages, { encryptedMessage: dataFromServer.message.split('\\').join('\n'), plainTextMessage: decryptedData.decryptedMessage, validSignature: decryptedData.validSignature, userNick: dataFromServer.user_nick, time: dataFromServer.time }], }) )); } this.setState({peer_connected: dataFromServer.peer_connected}) } }); this.rws.addEventListener('close', () => { console.log('Socket is closed. Reconnect will be attempted'); this.setState({connected: false}); }); this.rws.addEventListener('error', () => { console.error('Socket encountered error: Closing socket'); }); } componentDidUpdate() { this.scrollToBottom(); } scrollToBottom = () => { this.messagesEnd.scrollIntoView({ behavior: "smooth" }); } onButtonClicked = (e) => { if(this.state.value!=''){ this.setState({waitingEcho:true, lastSent:this.state.value}); encryptMessage(this.state.value, this.state.own_pub_key, this.state.peer_pub_key, this.state.own_enc_priv_key, this.state.token) .then((encryptedMessage) => console.log("Sending Encrypted MESSAGE "+encryptedMessage) & this.rws.send(JSON.stringify({ type: "message", message: encryptedMessage.split('\n').join('\\'), nick: this.props.ur_nick, }) ) & this.setState({value: "", waitingEcho: false}) ); } e.preventDefault(); } createJsonFile = () => { return ({ "credentials": { "own_public_key": this.state.own_pub_key, "peer_public_key":this.state.peer_pub_key, "encrypted_private_key":this.state.own_enc_priv_key, "passphrase":this.state.token}, "messages": this.state.messages, }) } messageCard = (props) => { const { t } = this.props; return( } style={{backgroundColor: props.cardColor}} title={
{props.message.userNick} {props.message.validSignature ? : }
this.setState(prevState => { const newShowPGP = [...prevState.showPGP]; newShowPGP[props.index] = !newShowPGP[props.index]; return {showPGP: newShowPGP}; })}>
navigator.clipboard.writeText(this.state.showPGP[props.index] ? props.message.encryptedMessage : props.message.plainTextMessage)}>
} subheader={this.state.showPGP[props.index] ? {props.message.time}
{"Valid signature: " + props.message.validSignature}
{props.message.encryptedMessage}
: props.message.plainTextMessage} subheaderTypographyProps={{sx: {wordWrap: "break-word", width: '200px', color: '#444444', fontSize: this.state.showPGP[props.index]? 11 : null }}} />
) } render() { const { t } = this.props; return ( {t("You")+": "}{this.state.connected ? t("connected"): t("disconnected")} {t("Peer")+": "}{this.state.peer_connected ? t("connected"): t("disconnected")} {this.state.messages.map((message, index) =>
  • {message.userNick == this.props.ur_nick ? : }
  • )}
    { this.messagesEnd = el; }}>
    { this.setState({ value: e.target.value }); this.value = this.state.value; }} sx={{width: 214}} />
    this.setState({audit:false})} orderId={Number(this.props.orderId)} messages={this.state.messages} own_pub_key={this.state.own_pub_key} own_enc_priv_key={this.state.own_enc_priv_key} peer_pub_key={this.state.peer_pub_key ? this.state.peer_pub_key : "Not received yet"} passphrase={this.state.token} onClickBack={() => this.setState({audit:false})} /> ) } } export default withTranslation()(Chat);