1 /* |
1 /* |
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
133 * |
133 * |
134 * @param triggerBytes array of bytes to use as a trigger, may be zero length |
134 * @param triggerBytes array of bytes to use as a trigger, may be zero length |
135 * @param matchBytes bytes to match after the trigger has been seen |
135 * @param matchBytes bytes to match after the trigger has been seen |
136 * @param replaceBytes bytes to replace the matched bytes |
136 * @param replaceBytes bytes to replace the matched bytes |
137 */ |
137 */ |
138 public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, |
138 public synchronized void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, |
139 byte[] replaceBytes) { |
139 byte[] replaceBytes) { |
140 this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes"); |
140 this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes"); |
141 this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes"); |
141 this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes"); |
142 this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes"); |
142 this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes"); |
143 sockets.forEach( s -> s.setMatchReplaceBytes(triggerBytes, matchBytes, |
143 sockets.forEach( s -> s.setMatchReplaceBytes(triggerBytes, matchBytes, |
145 serverSockets.forEach( s -> s.setMatchReplaceBytes(triggerBytes, matchBytes, |
145 serverSockets.forEach( s -> s.setMatchReplaceBytes(triggerBytes, matchBytes, |
146 replaceBytes)); |
146 replaceBytes)); |
147 } |
147 } |
148 |
148 |
149 @Override |
149 @Override |
150 public Socket createSocket(String host, int port) throws IOException { |
150 public synchronized Socket createSocket(String host, int port) throws IOException { |
151 Socket socket = RMISocketFactory.getDefaultSocketFactory() |
151 Socket socket = RMISocketFactory.getDefaultSocketFactory() |
152 .createSocket(host, port); |
152 .createSocket(host, port); |
153 InterposeSocket s = new InterposeSocket(socket, |
153 InterposeSocket s = new InterposeSocket(socket, |
154 triggerBytes, matchBytes, replaceBytes); |
154 triggerBytes, matchBytes, replaceBytes); |
155 sockets.add(s); |
155 sockets.add(s); |
158 |
158 |
159 /** |
159 /** |
160 * Return the current list of sockets. |
160 * Return the current list of sockets. |
161 * @return Return a snapshot of the current list of sockets |
161 * @return Return a snapshot of the current list of sockets |
162 */ |
162 */ |
163 public List<InterposeSocket> getSockets() { |
163 public synchronized List<InterposeSocket> getSockets() { |
164 List<InterposeSocket> snap = new ArrayList<>(sockets); |
164 List<InterposeSocket> snap = new ArrayList<>(sockets); |
165 return snap; |
165 return snap; |
166 } |
166 } |
167 |
167 |
168 @Override |
168 @Override |
169 public ServerSocket createServerSocket(int port) throws IOException { |
169 public synchronized ServerSocket createServerSocket(int port) throws IOException { |
170 |
170 |
171 ServerSocket serverSocket = RMISocketFactory.getDefaultSocketFactory() |
171 ServerSocket serverSocket = RMISocketFactory.getDefaultSocketFactory() |
172 .createServerSocket(port); |
172 .createServerSocket(port); |
173 InterposeServerSocket ss = new InterposeServerSocket(serverSocket, |
173 InterposeServerSocket ss = new InterposeServerSocket(serverSocket, |
174 triggerBytes, matchBytes, replaceBytes); |
174 triggerBytes, matchBytes, replaceBytes); |
200 private volatile byte[] matchBytes; |
200 private volatile byte[] matchBytes; |
201 private volatile byte[] replaceBytes; |
201 private volatile byte[] replaceBytes; |
202 private final ByteArrayOutputStream inLogStream; |
202 private final ByteArrayOutputStream inLogStream; |
203 private final ByteArrayOutputStream outLogStream; |
203 private final ByteArrayOutputStream outLogStream; |
204 private final String name; |
204 private final String name; |
205 private static volatile int num = 0; // index for created InterposeSockets |
205 private static volatile int num = 0; // index for created Interpose509s |
206 |
206 |
207 /** |
207 /** |
208 * Construct a socket that interposes on a socket to match and replace. |
208 * Construct a socket that interposes on a socket to match and replace. |
209 * The trigger is empty. |
209 * The trigger is empty. |
210 * @param socket the underlying socket |
210 * @param socket the underlying socket |
455 * |
455 * |
456 * @param triggerBytes array of bytes to use as a trigger, may be zero length |
456 * @param triggerBytes array of bytes to use as a trigger, may be zero length |
457 * @param matchBytes bytes to match after the trigger has been seen |
457 * @param matchBytes bytes to match after the trigger has been seen |
458 * @param replaceBytes bytes to replace the matched bytes |
458 * @param replaceBytes bytes to replace the matched bytes |
459 */ |
459 */ |
460 public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, |
460 public synchronized void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, |
461 byte[] replaceBytes) { |
461 byte[] replaceBytes) { |
462 this.triggerBytes = triggerBytes; |
462 this.triggerBytes = triggerBytes; |
463 this.matchBytes = matchBytes; |
463 this.matchBytes = matchBytes; |
464 this.replaceBytes = replaceBytes; |
464 this.replaceBytes = replaceBytes; |
465 sockets.forEach(s -> s.setMatchReplaceBytes(triggerBytes, matchBytes, replaceBytes)); |
465 sockets.forEach(s -> s.setMatchReplaceBytes(triggerBytes, matchBytes, replaceBytes)); |
466 } |
466 } |
467 /** |
467 /** |
468 * Return a snapshot of the current list of sockets created from this server socket. |
468 * Return a snapshot of the current list of sockets created from this server socket. |
469 * @return Return a snapshot of the current list of sockets |
469 * @return Return a snapshot of the current list of sockets |
470 */ |
470 */ |
471 public List<InterposeSocket> getSockets() { |
471 public synchronized List<InterposeSocket> getSockets() { |
472 List<InterposeSocket> snap = new ArrayList<>(sockets); |
472 List<InterposeSocket> snap = new ArrayList<>(sockets); |
473 return snap; |
473 return snap; |
474 } |
474 } |
475 |
475 |
476 @Override |
476 @Override |
499 } |
499 } |
500 |
500 |
501 @Override |
501 @Override |
502 public Socket accept() throws IOException { |
502 public Socket accept() throws IOException { |
503 Socket s = socket.accept(); |
503 Socket s = socket.accept(); |
504 InterposeSocket socket = new InterposeSocket(s, matchBytes, replaceBytes); |
504 synchronized(this) { |
505 sockets.add(socket); |
505 InterposeSocket aSocket = new InterposeSocket(s, matchBytes, |
506 return socket; |
506 replaceBytes); |
|
507 sockets.add(aSocket); |
|
508 return aSocket; |
|
509 } |
507 } |
510 } |
508 |
511 |
509 @Override |
512 @Override |
510 public void close() throws IOException { |
513 public void close() throws IOException { |
511 socket.close(); |
514 socket.close(); |