/** File is: ZombieExample.java 2010-03-01 Version 1.0 **/
/*-----------------------------------------------------------------------
Start server listening for input at 36000
Start ZombieA at 36005
Start AgentB at 36006
When a connection is made to 36000, send back a user input screen to
simulate the user having just typed it in.
Send the data to a zombie.
The zombie captures the data, rebuilds the input form with all the
data as dynamic html, and sends it back to the user, except now
pointing to the new location of the agent in the action statement.
The agent accepts the data.
Note: The hidden data in the HTML forms is just to make parsing the
data easier, it is not really necesasry.
-----------------------------------------------------------------------*/
import java.io.*; // Get the Input Output libraries
import java.net.*; // Get the Java networking libraries
class ZombieA implements Runnable { // New thread will be RUN
Socket zsocket; // Class member, socket, local to the Worker method
public void run() {
//Get I/O streams from the socket:
PrintStream out = null; // clear out the output queue
BufferedReader in = null; // clear out the input buffer
try{
ServerSocket ss = new ServerSocket(36005);
Socket zsocket = ss.accept();
out = new PrintStream(zsocket.getOutputStream());
in = new BufferedReader
(new InputStreamReader(zsocket.getInputStream()));
String inLine = in.readLine();
System.out.println("Now in ZombieA\n");
/* Parse the data from the input stream, then clean it up: */
int i1 = inLine.indexOf("label=") + 6;
int i2 = inLine.indexOf("&", i1);
int i3 = inLine.indexOf("matching-data=", i2) + 15;
int i4 = inLine.indexOf("xyz-terminator=", i3) - 1;
String newLabel = new String(inLine.substring(i1, i2));
newLabel = newLabel.replace('+', ' ');
newLabel = newLabel.replace("%3F", "?");
String newMatchingData = new String(inLine.substring(i3, i4));
newMatchingData = newMatchingData.replace('+', ' ');
newMatchingData = newMatchingData.replace("%3A", ":");
newMatchingData = newMatchingData.replace("%2C", ",");
/* System.out.println("Label is: " + newLabel);
System.out.println("MatchingData is: " + newMatchingData); */
sendHTML(out, newLabel, newMatchingData);
zsocket.close(); // Would not close Zombie if also has admin client
ss.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
/* Send back the resubmission screen with all the data in it. Note the new
endpoint pointing the new location of the migrated agent. */
static void sendHTML(PrintStream out, String Label, String MatchingData ){
out.println
("
Zombie Sample A " +
"
Your Agent has migrated -- Resubmit!
" +
" " +
" " +
"" + "\n\n");
}
}
/* The agent is listening in a new location. The data is sent to the agent from the screen
prepared by the zombie. */
class AgentB implements Runnable { // New thread will be RUN
Socket zsocket; // Class member, socket, local to the Worker method
public void run() {
//Get I/O streams from the socket:
PrintStream out = null; // clear out the output queue
BufferedReader in = null; // clear out the input buffer
try{
ServerSocket ss = new ServerSocket(36006);
Socket zsocket = ss.accept();
out = new PrintStream(zsocket.getOutputStream());
in = new BufferedReader
(new InputStreamReader(zsocket.getInputStream()));
String inLine = in.readLine();
System.out.println("Now in AgentB\n");
/* System.out.println(inLine); */
/* Parse the data from the input stream, then clean it up: */
int i1 = inLine.indexOf("label=") + 6;
int i2 = inLine.indexOf("&", i1);
int i3 = inLine.indexOf("matching-data=", i2) + 15;
int i4 = inLine.indexOf("xyz-terminator=", i3) - 1;
String newLabel = new String(inLine.substring(i1, i2));
newLabel = newLabel.replace('+', ' ');
newLabel = newLabel.replace("%3F", "?");
String newMatchingData = new String(inLine.substring(i3, i4));
newMatchingData = newMatchingData.replace('+', ' ');
newMatchingData = newMatchingData.replace("%3A", ":");
newMatchingData = newMatchingData.replace("%2C", ",");
/* System.out.println("Label is: " + newLabel);
System.out.println("MatchingData is: " + newMatchingData); */
out.println
("HTTP/1.1 200 OK" +
"Content-Length: 1500" + // who knows how long, faking it.
"Content-Type: text/html\n\n" +
" Zombie Sample B " +
"
Thank you for your submission!
" +
"Your migrated agent received your re-submission of:
" +
"
" +
newLabel +
"" +
newMatchingData + " \n\n");
zsocket.close(); // Would not close Zombie if also has admin client
ss.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
public class ZombieExample {
static void sendInitialHTML(PrintStream out){
out.println
("HTTP/1.1 200 OK" +
"Content-Length: 1500" + // who knows how long, faking it.
"Content-Type: text/html\n\n" +
" Zombie Sample A " +
"
The user sends this data to a zombie!
" +
" " +
" " +
"" + "\n\n");
}
public static void main(String a[]) throws IOException {
int q_len = 6; /* Number of requests for OpSys to queue */
int port = 36000; /* Send request to this port to start */
Socket sock;
boolean controlSwitch = true;
ZombieA ZA = new ZombieA(); // Start the zombie in its own thread
Thread t = new Thread(ZA);
t.start();
AgentB AB = new AgentB(); // Start the migrated agent in its own thread
Thread t2 = new Thread(AB);
t2.start();
System.out.println("\nClark Elliott's Zombie Example started.\n");
System.out.println("Zombie running at 36005; agent running at 36006\n");
System.out.println("\nIssue http://localhost:" + port + " from browser to run example.\n");
try{
ServerSocket ss = new ServerSocket(port, q_len);
PrintStream out = null; // clear out the output queue
BufferedReader in = null; // clear out the input buffer
Socket zsocket = ss.accept(); // accept initial connection
out = new PrintStream(zsocket.getOutputStream());
System.out.println("Sending back initial zombie screen\n");
sendInitialHTML(out);
out.flush();
/* zsocket.close(); */ /* Does not like this for some reason */
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}