-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCamServer.java
473 lines (406 loc) · 18.3 KB
/
CamServer.java
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
package jcam;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JCamara {
private static ArrayList<Camera> cameras = new ArrayList<>();
private static Map<String, Boolean> desiredMacAddress = new HashMap<>();
private static byte[] IMAGE_STARTS = "ffd8".getBytes();
private static byte[] IMAGE_ENDS = "ffd9".getBytes();
private static String StrimHexCad(String cad) {
return cad.replaceAll(" ", "").trim();
}
public static void main(String[] args) {
// Desired MAC addresses: Ever Sparkle Technologies Ltd
desiredMacAddress.put("00:1e:b5:84:8f:01", Boolean.FALSE);
desiredMacAddress.put("f8:da:c:7d:e9:2f", Boolean.FALSE);
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("--- MENU ---");
System.out.println("1. Set Station Mode");
System.out.println("2. Connect to Camera");
System.out.println("0. Exit the program");
int opc = in.nextInt();
switch (opc) {
case 0:
System.exit(0);
break;
case 1:
System.out.println("Type the ESSID");
String essid = in.next();
System.out.println("Type the Pass");
String pass = in.next();
System.out.println("Waiting for device");
if (setStationMode(essid, pass)) {
System.out.println("Station Mode Activated");
} else {
System.out.println("Waiting...");
}
break;
case 2:
discoverCameras();
if (cameras.isEmpty()) {
System.out.println("No cameras detected!");
System.out.println("Do you want exit? Y o N");
if (in.next().equalsIgnoreCase("y")) {
System.out.println("Exiting the program!");
System.exit(0);
} else {
System.out.println("If you know there is a camera online, enter the IP address:");
String ipCam = in.next();
if (isAnIP(ipCam)) {
System.out.println("Enter the name of the camera, please:");
String nCam = in.next();
System.out.println("Trying to connect to " + nCam + " on: " + ipCam);
try {
initiateCameraSTMode(nCam, ipCam);
} catch (IOException ex) {
System.out.println("IOE: " + ex.getMessage());
}
} else {
System.out.println("This app is not a chatbot, I'm not programmed to know what you think.\nThe program will exit now!");
System.exit(0);
}
}
} else {
System.out.println("Please select the camera to work with:");
for (Camera cam : cameras) {
System.out.println(cameras.indexOf(cam) + ". " + cam.toString());
}
String selectedCam = in.next();
try {
int option = Integer.parseInt(selectedCam);
Camera myCam = cameras.get(option);
if (myCam.ip.equals("192.168.4.153")) {
System.out.println("This camera is on AP mode");
initiateCameraAPMode(myCam.getName(), myCam.ip);
} else {
System.out.println("Trying to connect with " + myCam.getName());
initiateCameraSTMode(myCam.getName(), myCam.ip);
}
} catch (NumberFormatException ex) {
System.out.println("NFE: " + ex.getMessage());
} finally {
continue;
}
}
System.out.println("Camera available!");
for (Camera cam : cameras) {
System.out.println("Camera: " + cam.getName() + " on: " + cam.ip);
}
try {
ServerSocket serverSocket = new ServerSocket(8081);
System.out.println("Server started at http://127.0.0.1:8081/cam.html");
while (true) {
Socket clientSocket = serverSocket.accept();
Thread clientThread = new Thread(new ClientHandler(clientSocket));
clientThread.start();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
break;
default:
System.out.println("Command not available!");
}
}
}
private static void initiateCameraAPMode(String name, String ip) throws IOException {
DatagramSocket socket = new DatagramSocket();
byte[] buffer = new byte[2];
InetAddress address = InetAddress.getByName(ip);
int port = 8070;
buffer[0] = (byte) 0x30;
buffer[1] = (byte) 0x67;
socket.send(new DatagramPacket(buffer, buffer.length, address, port));
buffer[0] = (byte) 0x30;
buffer[1] = (byte) 0x66;
socket.send(new DatagramPacket(buffer, buffer.length, address, port));
port = 8080;
buffer[0] = (byte) 0x42;
buffer[1] = (byte) 0x76;
socket.send(new DatagramPacket(buffer, buffer.length, address, port));
Camera camera = new Camera(name, ip, socket);
cameras.add(camera);
}
private static void initiateCameraSTMode(String nCam, String ip) throws IOException {
DatagramSocket socket = new DatagramSocket();
byte[] buffer = new byte[2];
InetAddress address = InetAddress.getByName(ip);
int port = 12476;
buffer[0] = (byte) 0x30;
buffer[1] = (byte) 0x67;
socket.send(new DatagramPacket(buffer, buffer.length, address, port));
buffer[0] = (byte) 0x30;
buffer[1] = (byte) 0x66;
socket.send(new DatagramPacket(buffer, buffer.length, address, port));
port = 32108;
buffer[0] = (byte) 0x42;
buffer[1] = (byte) 0x76;
socket.send(new DatagramPacket(buffer, buffer.length, address, port));
buffer = hexToAscii("5d782818").getBytes();
socket.send(new DatagramPacket(buffer, buffer.length, address, port));
}
private static boolean setStationMode(String essid, String pass) {
try (DatagramSocket socket = new DatagramSocket()) {
socket.setReuseAddress(true);
int port = 8090;
String data = "f" + essid + "&&&" + essid + "###" + pass + "";
byte[] buffer = data.getBytes();
System.out.println("Sending data and waiting for response...");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("192.168.4.153"), port);
socket.send(packet);
byte[] responseBuffer = new byte[Byte.MAX_VALUE];
DatagramPacket responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length);
socket.receive(responsePacket);
String response = new String(responsePacket.getData(), 0, responsePacket.getLength());
if (!response.isEmpty()) {
System.out.println("Data acquired: ");
System.out.println("Response: " + response);
System.out.println("ASCII response: " + asciiToHex(response));
if (response.equals("f�����")) {
return true;
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
return false;
}
public static String hexToAscii(String hexString) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < hexString.length(); i += 2) {
String hex = hexString.substring(i, i + 2);
int decimal = Integer.parseInt(hex, 16);
output.append((char) decimal);
}
return output.toString();
}
public static String asciiToHex(String asciiString) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < asciiString.length(); i++) {
char c = asciiString.charAt(i);
String hex = Integer.toHexString((int) c);
if (hex.length() < 2) {
hex = "0" + hex; // Agregar cero a la izquierda si es necesario
}
output.append(hex);
}
return output.toString().toUpperCase();
}
private static void sendData(String data, String hostname, int rport) {
try {
DatagramSocket sock = new DatagramSocket();
sock.setSoTimeout(500);
byte[] sdata = data.getBytes();
sock.send(new DatagramPacket(sdata, sdata.length, new InetSocketAddress(hostname, rport)));
byte[] buf = new byte[Byte.MAX_VALUE];
DatagramPacket rdp = new DatagramPacket(buf, buf.length);
sock.receive(rdp);
String response = new String(rdp.getData(), 0, rdp.getLength());
if (!response.isEmpty()) {
System.out.println("Response data: " + response);
System.out.println("Hex data: " + asciiToHex(response));
}
} catch (IOException ex) {
//System.out.println(ex.getMessage());
}
}
// validating if are correct
private static boolean isAnIP(String ip) {
if (ip.contains(".") && ip.split("\\.").length == 4) {
String[] parts = ip.split("\\.");
for (String part : parts) {
try {
int value = Integer.parseInt(part);
if (value < 0 || value > 255) {
return false;
}
} catch (NumberFormatException e) {
return false;
}
}
return true;
}
return false;
}
private static String checkIP(String ip) {
return ip.replace('(', ' ').replace(')', ' ').trim();
}
private static String checkMac(String mac) {
String[] parts = mac.split(":");
String nMac = "";
for (String part : parts) {
if (part.length() == 2) {
nMac += part + ":";
} else {
nMac += "0" + part + ":";
}
}
nMac = nMac.substring(0, nMac.length() - 1);
return nMac;
}
private static void discoverCameras() {
int intervalSeconds = 10; // Intervalo de detección en segundos
boolean running = true;
while (running) {
try {
Process process = Runtime.getRuntime().exec("arp -a");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
System.out.println("--- ARP Table ---\n");
while ((line = reader.readLine()) != null) {
// Filtrar las líneas que contienen información de la tabla ARP
if (line.contains("dynamic") || line.contains("ifscope")) {
String[] parts = line.split("\\s+");
String ipAddress = checkIP(parts[1]);
String macAddress = checkMac(parts[3]);
for (Map.Entry<String, Boolean> entry : desiredMacAddress.entrySet()) {
String mac = entry.getKey();
if (macAddress.equalsIgnoreCase(mac)) {
System.out.println("¡Se ha detectado el equipo con la dirección MAC deseada!");
System.out.println("Dirección IP: " + ipAddress);
System.out.println("Dirección MAC: " + macAddress);
// Realiza las acciones necesarias cuando se detecta el equipo
desiredMacAddress.replace(mac, Boolean.TRUE);
Camera camera = new Camera(macAddress, ipAddress, new DatagramSocket());
cameras.add(camera);
}
}
}
}
reader.close();
// Realiza las acciones necesarias cuando el equipo no está conectado
if (desiredMacAddress.containsValue(Boolean.FALSE)) {
running = false;
}
/*for (Map.Entry<String, Boolean> e : desiredMacAddress.entrySet()) {
if (!e.getValue()) {
System.out.println("El equipo con la dirección MAC: " + e.getKey() + " no está conectado.");
}
}*/
Thread.sleep(intervalSeconds * 1000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
private static boolean isImageStart(byte[] buffer) {
for (int i = 0; i < buffer.length - 4; i++) {
//102 102 100 56 = 0xff 0xd8
if (buffer[i] == 102 && buffer[i + 1] == 102 && buffer[i + 2] == 100 && buffer[i + 3] == 56) {
return true;
}
/*if (buffer[i] == (byte) 0xff && buffer[i + 1] == (byte) 0xd8) {
return true;
}*/
}
return false;
}
private static boolean isImageEnd(byte[] buffer) {
for (int i = 0; i < buffer.length - 2; i++) {
//EOF - 0xff 0xd9
if (buffer[i] == 102 && buffer[i + 1] == 102 && buffer[i + 2] == 100 && buffer[i + 3] == 57) {
return true;
}
/*if (buffer[i] == (byte) 0xff && buffer[i + 1] == (byte) 0xd9) {
return true;
}*/
}
return false;
}
private static void sendImage(Socket clientSocket, byte[] frame) throws IOException {
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write(("HTTP/1.1 200 OK\r\n"
+ "Content-type: image/jpeg\r\n"
+ "Content-length: " + frame.length + "\r\n"
+ "\r\n").getBytes());
outputStream.write(frame);
outputStream.write("\r\n--jpgboundary\r\n".getBytes());
outputStream.flush();
}
static class ClientHandler implements Runnable {
private Socket clientSocket;
public ClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
try {
String clientAddress = clientSocket.getInetAddress().getHostAddress();
String cameraName = "";//"ACCQ495869RFVSV";//getCameraByName(cameraName);
for (Camera camera : cameras) {
if (camera != null) {
InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream();
System.out.println("Data streaming...");
outputStream.write(("HTTP/1.1 200 OK\r\n"
+ "Content-type: multipart/x-mixed-replace; boundary=--jpgboundary\r\n"
+ "\r\n").getBytes());
while (true) {
byte[] buffer = new byte[4096];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
camera.getSocket().receive(packet);
buffer = packet.getData();
if (isImageStart(buffer)) {
camera.setFrame(Arrays.copyOfRange(buffer, 8, buffer.length));
} else {
byte[] newFrame = new byte[camera.getFrame().length + buffer.length];
System.arraycopy(camera.getFrame(), 0, newFrame, 0, camera.getFrame().length);
System.arraycopy(buffer, 0, newFrame, camera.getFrame().length, buffer.length);
camera.setFrame(newFrame);
}
if (isImageEnd(buffer)) {
sendImage(clientSocket, camera.getFrame());
}
}
} else {
clientSocket.close();
System.out.println("Data streaming closed");
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private Camera getCameraByName(String name) {
for (Camera camera : cameras) {
if (camera.getName().equals(name)) {
return camera;
}
}
return null;
}
}
static class Camera {
private String name;
private String ip;
private DatagramSocket socket;
private byte[] frame;
public Camera(String name, String ip, DatagramSocket socket) {
this.name = name;
this.ip = ip;
this.socket = socket;
this.frame = new byte[0];
}
public String getName() {
return name;
}
public DatagramSocket getSocket() {
return socket;
}
public byte[] getFrame() {
return frame;
}
public void setFrame(byte[] frame) {
this.frame = frame;
}
}
}