-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoRun.java
More file actions
646 lines (540 loc) · 19.7 KB
/
EchoRun.java
File metadata and controls
646 lines (540 loc) · 19.7 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package echoclient;
/*
This is the Cleint Side
Travis Haycock
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class EchoRun extends JFrame implements ActionListener, KeyListener, Runnable {
File newTextFile;
JScrollPane scroll;
/*
VARS for send/receive messages
*/
InetAddress ip;
String hostName;
String sessionKey;
/*
Time and Date
*/
Date date;
DateFormat dateFormat;
String localTime;
/*
Audio Vars
*/
String voiceOutput = "output.wav";
TargetDataLine line;
double duration, seconds;
AudioInputStream audioInputStream;
/*
Send File VARS
*/
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
/*
App VARS main
*/
private JButton file, mic;
String errStr;
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket socket;
private JTextField userText;
private JTextArea chatWindow;
private ServerSocket server;
private Socket connection;
private Socket connection2;
private Socket connection3;
private Socket sessionSocketKey;
private String message = "";
private String serverIP;
Thread thread;
JToolBar toolbar;
String loco;
// encrypt key
byte[] key = "升力了山623423#&(@nds#826HLc".getBytes();
public EchoRun(String host) throws UnknownHostException {
super(" Echo Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLookAndFeel();
userText = new JTextField();
userText.setEditable(false);
/*
Time and date for messages
*/
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
date = new Date();
localTime = dateFormat.format(date);
chatWindow = new JTextArea();
chatWindow.setEditable(false);
chatWindow.setLineWrap(true);
chatWindow.setForeground(Color.GREEN);
chatWindow.setBackground(Color.BLACK);
scroll = new JScrollPane(chatWindow);
scroll.setPreferredSize(new Dimension(100, 50));
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Font font = new Font("Consolas", Font.BOLD, 12);
chatWindow.setFont(font);
mic = new JButton("Microphone");
mic.setFont(new Font("Serif", Font.BOLD, 14));
mic.setForeground(Color.blue);
userText.addKeyListener(this);
ip = InetAddress.getLocalHost();
hostName = ip.getHostName();
file = new JButton("Send File");
file.addActionListener(this);
file.setFont(new Font("Serif", Font.BOLD, 14));
file.setForeground(Color.blue);
toolbar = new JToolBar();
toolbar.add(mic);
toolbar.addSeparator();
toolbar.add(file);
toolbar.addSeparator();
serverIP = host;
userText.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
sendMessage(e.getActionCommand());
} catch (IOException ex) {
Logger.getLogger(EchoClient.class.getName()).log(Level.SEVERE, null, ex);
}
userText.setText(" ");
}
});
add(userText, BorderLayout.SOUTH);
add(chatWindow, BorderLayout.CENTER);
add("North", toolbar);
chatWindow.add("Center", scroll);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("UIManager.getSystemLookAndFeelClassName");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
UnsupportedLookAndFeelException e) {
}
}
public void startRunning() throws IOException {
try {
connectToServer();
setUpStreams();
whileChatting();
} catch (EOFException eofException) {
showMessage("Client Terminated Connection");
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
closeAll();
}
}
/*
Connect to server
*/
/*
Will generate Random encryption key not in the program atall but possibly future implementation
*/
public void generateEncryptionKey() throws IOException {
// generate the random integer
Random randomGenerator = new Random();
for (int idx = 1; idx <= 1; ++idx) {
int randomInt = randomGenerator.nextInt(100);
byte[] array = new byte[256]; // length is bounded by 256
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));
// both the int and char togetehr in a string
sessionKey = generatedString + randomInt;
System.out.println("session key is:" + sessionKey);
newTextFile = new File("sessionKey.txt");
FileWriter fileWriter = new FileWriter(newTextFile);
fileWriter.write(sessionKey);
fileWriter.close();
// CARRY ON WITH LOGIC
System.out.println("New Session Key Created");
// socket to pass the encrytion key
sessionSocketKey = new Socket(InetAddress.getByName(serverIP),2000);
byte[] mybytearray = new byte[(int) newTextFile.length()];
FileInputStream fis = new FileInputStream(newTextFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sessionSocketKey.getOutputStream();
/*
This will send the header info text.doc, hello.pdf
*/
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(newTextFile.getName());
System.out.println("Sending Key...");
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
sessionSocketKey.close();
System.out.println("Key Sent Sucessfully!");
}
}
private void connectToServer() throws IOException {
// *********connect to text to text via. server and client
showMessage("\nAttempting To Connect To Server For Main Chat");
connection = new Socket(InetAddress.getByName(serverIP), 2014);
showMessage("\nconnected to:" + connection.getInetAddress().getHostName());
generateEncryptionKey();
}
private void setUpStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\nStreams Established");
}
private void whileChatting() throws IOException {
ableToType(true);
do {
try {
message = (String) input.readObject();
showMessage("\n " + message);
} catch (ClassNotFoundException classNotFoundException) {
showMessage("\n ERROR SENDING OBJECT");
}
} while (!message.equals("SERVER - END"));
}
/*
Close func
*/
private void closeAll() throws IOException {
showMessage("\n closing...");
ableToType(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
/*
Send the text from jtextarea
*/
private void sendMessage(String message) throws IOException {
try {
output.writeObject("\n" + localTime + "- " + hostName + ": \n\n" + message);
output.flush();
showMessage("\n " + localTime + "- " + hostName + ": \n\n" + message);
} catch (IOException ioException) {
chatWindow.append("\n ERROR Sending Message");
ioException.printStackTrace();
}
}
/*
Display MSG
*/
private void showMessage(final String m) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
chatWindow.append(m);
}
});
}
private void ableToType(final boolean tof) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
userText.setEditable(tof);
}
});
}
/*
Handling the microphone
*/
public void stop() {
thread = null;
}
public void shutDown(String message) {
if ((errStr = message) != null && thread != null) {
thread = null;
System.err.println(errStr);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 'q') {
System.out.println("Started Recording");
start();
}
}
public void start() {
errStr = null;
thread = new Thread((Runnable) this);
thread.setName("Capture");
thread.start();
}
@Override
public void keyReleased(KeyEvent e) {
Object source = e.getSource();
if (e.getKeyChar() == 'q') {
System.out.println("Stopped Recording");
stop();
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == file) {
try {
chooseFile();
} catch (IOException ex) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/*
Choose file method
*/
public void chooseFile() throws IOException, Exception {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Send File");
fc.setMultiSelectionEnabled(true);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int dialog = fc.showSaveDialog(this);
if (dialog == JFileChooser.APPROVE_OPTION) {
File inputFile = fc.getSelectedFile();
inputFile.getAbsoluteFile();
System.out.println("the extension type:" + fc.getTypeDescription(inputFile));
/*
will encrypt a file for you
*/
File savedFile = new File("encrypted" + inputFile.getName());
encryptFile(inputFile, (savedFile), key);
//
sendFile(savedFile);
}
}
public void sendFile(File inputFile) throws IOException {
// counting ms
long start = System.currentTimeMillis();
connection3 = new Socket(InetAddress.getByName(serverIP), 1995);
byte[] mybytearray = new byte[(int) inputFile.length()];
FileInputStream fis = new FileInputStream(inputFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = connection3.getOutputStream();
/*
This will send the header info text.doc, hello.pdf
*/
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(inputFile.getName());
System.out.println("Sending...");
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
connection3.close();
long end = System.currentTimeMillis();
chatWindow.append("File Name: " + inputFile.getName());
System.out.println("Took " + (end - start) + "ms");
JOptionPane.showMessageDialog(null, "Your File Transfer Took:" + (end - start) + "ms");
}
public void run() {
duration = 0;
audioInputStream = null;
// define the required attributes for our line,
// and make sure a compatible line is supported.
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
float rate = 44100.0f;
int channels = 2;
int frameSize = 4;
int sampleSize = 16;
boolean bigEndian = true;
AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
shutDown("Line matching " + info + "not supported.");
return;
}
// get and open the target data line for capture.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
} catch (LineUnavailableException ex) {
shutDown("Unable to open the line: " + ex);
return;
} catch (SecurityException ex) {
shutDown(ex.toString());
//JavaSound.showInfoDialog();
return;
} catch (Exception ex) {
shutDown(ex.toString());
return;
}
// play back the captured audio data
ByteArrayOutputStream out = new ByteArrayOutputStream();
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead;
line.start();
while (thread != null) {
if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
break;
}
out.write(data, 0, numBytesRead);
}
// we reached the end of the stream.
// stop and close the line.
line.stop();
line.close();
line = null;
// stop and close the output stream
try {
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
// load bytes into the audio input stream for playback
byte audioBytes[] = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
audioInputStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes);
long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format.getFrameRate());
duration = milliseconds / 1000.0;
try {
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(voiceOutput));
//encryptFile(voiceOutput, new File( voiceOutput),key);
System.out.println(voiceOutput);
sendSound();
} catch (IOException ex) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void sendSound() throws IOException, Exception {
connection2 = new Socket(InetAddress.getByName(serverIP), 1990);
System.out.println("send voice");
OutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
output2.flush();
InputStream input2 = new ObjectInputStream(connection2.getInputStream());
System.out.println(input2);
DataOutputStream dos = null;
try {
dos = new DataOutputStream(connection2.getOutputStream());
FileInputStream fis = null;
fis = new FileInputStream(voiceOutput);
int count = 0;
byte[] buffer = new byte[4096];
while ((count = fis.read(buffer)) > 0) {
dos.write(buffer, 0, count);
dos.flush();
System.out.println(fis);
}
fis.close();
dos.flush();
dos.close();
} catch (IOException ex1) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
System.out.println("or closing dos");
}
}
public void encryptFile(File in, File output, byte[] key) throws Exception {
Cipher cipher = getCipherEncrypt(key);
FileOutputStream fos = null;
CipherOutputStream cos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(in);
fos = new FileOutputStream(output);
cos = new CipherOutputStream(fos, cipher);
byte[] data = new byte[1024];
int read = fis.read(data);
while (read != -1) {
cos.write(data, 0, read);
read = fis.read(data);
System.out.println(new String(data, "UTF-8").trim());
chatWindow.append(new String(data, "UTF-8"));
}
cos.flush();
} finally {
System.out.println("performed encrypt method now closing streams:\n" + output.toString());
cos.close();
fos.close();
fis.close();
}
}
private byte[] getKeyBytes(final byte[] key) throws Exception {
byte[] keyBytes = new byte[16];
System.arraycopy(key, 0, keyBytes, 0, Math.min(key.length, keyBytes.length));
return keyBytes;
}
public Cipher getCipherEncrypt(final byte[] key) throws Exception {
byte[] keyBytes = getKeyBytes(key);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
return cipher;
}
}