2019-05-17 01:28:26 -07:00
|
|
|
package JobServ;
|
|
|
|
|
|
|
|
|
|
import io.grpc.ManagedChannel;
|
|
|
|
|
import io.grpc.ManagedChannelBuilder;
|
|
|
|
|
import io.grpc.StatusRuntimeException;
|
2019-05-18 12:20:12 -07:00
|
|
|
import java.util.InputMismatchException;
|
2019-05-17 01:28:26 -07:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
|
|
// GRPC Client Class
|
|
|
|
|
public class JobServClient {
|
|
|
|
|
|
|
|
|
|
/* The client should not use the same logging module as the server.
|
|
|
|
|
* In a more robust product the server logging module will take advantage of system level
|
|
|
|
|
* log aggregators such as journalctl, which the client should not be writing to on the users system
|
|
|
|
|
*/
|
|
|
|
|
private static final Logger logger = Logger.getLogger(JobServClient.class.getName());
|
|
|
|
|
|
|
|
|
|
private final ManagedChannel channel;
|
|
|
|
|
|
|
|
|
|
/* blockingStub is used when the client needs to block until the server responds
|
|
|
|
|
* the client doesnt nessesarily need to support asynchronously firing off commands
|
|
|
|
|
* in this shell-like interface it would be disconcerting to get multiple returns out of order
|
|
|
|
|
*/
|
|
|
|
|
private final ShellServerGrpc.ShellServerBlockingStub blockingStub;
|
|
|
|
|
|
|
|
|
|
// Constructor connects to server
|
|
|
|
|
public JobServClient(String host, int port) {
|
|
|
|
|
this(ManagedChannelBuilder.forAddress(host, port)
|
|
|
|
|
// TODO: MTLS
|
|
|
|
|
.usePlaintext()
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private overload of constructor, used in the above constructor
|
|
|
|
|
JobServClient(ManagedChannel channel) {
|
|
|
|
|
this.channel = channel;
|
|
|
|
|
blockingStub = ShellServerGrpc.newBlockingStub(channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void shutdown() throws InterruptedException {
|
|
|
|
|
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sends the server a request for output from PID
|
|
|
|
|
// different from getProcessStatus in output expected from the server
|
|
|
|
|
// returns process output as string
|
|
|
|
|
public String getProcessOutput(int pid) {
|
|
|
|
|
logger.info("[+] requesting output");
|
|
|
|
|
|
2019-05-18 12:20:12 -07:00
|
|
|
PIDMessage request = PIDMessage.newBuilder()
|
2019-05-17 18:22:38 -07:00
|
|
|
.setPid(pid)
|
2019-05-17 01:28:26 -07:00
|
|
|
.build();
|
|
|
|
|
OutputMessage response;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// blocking network operation
|
2019-05-17 18:22:38 -07:00
|
|
|
response = blockingStub.getOutput(request);
|
2019-05-17 01:28:26 -07:00
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
|
|
logger.log(Level.WARNING, "[-] Request for output failed: %s", e.getStatus());
|
2019-05-18 12:20:12 -07:00
|
|
|
return "<Error connecting to API>";
|
2019-05-17 01:28:26 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-18 12:20:12 -07:00
|
|
|
return response.getOutput();
|
2019-05-17 01:28:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sends the server a command for a new job, blocks until response
|
|
|
|
|
// returns new pid of job
|
|
|
|
|
public int sendNewJobMessage(String command) {
|
|
|
|
|
// thought of escaping this, but the vulnerability is only client side, from client user input.
|
|
|
|
|
logger.info("[+] Sending command to server");
|
|
|
|
|
|
|
|
|
|
NewJobMessage request = NewJobMessage.newBuilder()
|
|
|
|
|
.setCommand(command)
|
|
|
|
|
.build();
|
2019-05-17 18:22:38 -07:00
|
|
|
PIDMessage response;
|
2019-05-17 01:28:26 -07:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// blocking network operation
|
2019-05-17 18:22:38 -07:00
|
|
|
response = blockingStub.newJob(request);
|
2019-05-17 01:28:26 -07:00
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
|
|
logger.log(Level.WARNING, "[-] Request for new job failed!");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-18 12:20:12 -07:00
|
|
|
if(response.getPid() == -1) {
|
2019-05-17 01:28:26 -07:00
|
|
|
logger.log(Level.WARNING, "New job creation failed server side!");
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
return response.getPid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// requests running status of job
|
|
|
|
|
// returns true if job still running else false
|
2019-05-18 12:20:12 -07:00
|
|
|
public Boolean getProcessStatus(int pid) {
|
2019-05-17 18:22:38 -07:00
|
|
|
logger.info("[+] Requesting status of a job");
|
|
|
|
|
|
|
|
|
|
PIDMessage request = PIDMessage.newBuilder()
|
|
|
|
|
.setPid(pid)
|
|
|
|
|
.build();
|
|
|
|
|
StatusMessage response;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// blocking network operation
|
|
|
|
|
response = blockingStub.getStatus(request);
|
|
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
|
|
logger.log(Level.WARNING, "[-] Request for status failed!");
|
2019-05-18 12:20:12 -07:00
|
|
|
return false;
|
2019-05-17 18:22:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.getIsRunning();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sends PID to server expecting the return cod eof a process
|
|
|
|
|
// function returns a 0-255 return code or 277 if still running
|
|
|
|
|
// or 278 if error in API
|
|
|
|
|
public int getProcessReturn(int pid) {
|
|
|
|
|
logger.info("[+] Requesting return code of a job");
|
|
|
|
|
|
|
|
|
|
PIDMessage request = PIDMessage.newBuilder()
|
|
|
|
|
.setPid(pid)
|
|
|
|
|
.build();
|
|
|
|
|
ReturnMessage response;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// blocking network operation
|
|
|
|
|
response = blockingStub.getReturn(request);
|
|
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
|
|
logger.log(Level.WARNING, "[-] Failed to get return code!");
|
|
|
|
|
return 278;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.getProcessReturnCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send a PID to be killed, returns nothing
|
|
|
|
|
// logs warning if job status comes back still running
|
|
|
|
|
public void killProcess(int pid) {
|
|
|
|
|
logger.info("[+] Killing a job");
|
|
|
|
|
|
|
|
|
|
PIDMessage request = PIDMessage.newBuilder()
|
|
|
|
|
.setPid(pid)
|
|
|
|
|
.build();
|
|
|
|
|
StatusMessage response;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// blocking network operation
|
|
|
|
|
response = blockingStub.killJob(request);
|
|
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
|
|
logger.log(Level.WARNING, "[-] Failed to send request!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.getIsRunning()) {
|
|
|
|
|
logger.log(Level.WARNING, "[-] Server failed to kill job!");
|
|
|
|
|
}
|
2019-05-17 01:28:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Client entrypoint
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2019-05-17 18:22:38 -07:00
|
|
|
if (args.length == 1 && args[0] == "--help"){
|
|
|
|
|
outputHelp();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 01:28:26 -07:00
|
|
|
// check args
|
2019-05-17 18:22:38 -07:00
|
|
|
if (args.length < 3) {
|
|
|
|
|
System.out.println("Usage: $ jobservclient host port command");
|
2019-05-18 12:20:12 -07:00
|
|
|
System.out.println("Or try client --help");
|
2019-05-17 01:28:26 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// start client (or fail if port is improperly formatted)
|
|
|
|
|
JobServClient client;
|
|
|
|
|
try {
|
2019-05-17 18:22:38 -07:00
|
|
|
client = new JobServClient(args[0], Integer.parseInt(args[1]));
|
|
|
|
|
|
2019-05-17 01:28:26 -07:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("Invalid Port");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
// declare up here so that multiple switch cases can use it
|
|
|
|
|
int candidatePid;
|
|
|
|
|
switch (args[2]) {
|
|
|
|
|
case "new":
|
|
|
|
|
if (args.length < 4) {
|
|
|
|
|
System.out.println("Improper formatting, try client --help");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-05-17 01:28:26 -07:00
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
String command = "";
|
2019-05-18 12:20:12 -07:00
|
|
|
for (int token = 3; token < args.length; token++) {
|
2019-05-17 18:22:38 -07:00
|
|
|
command += " " + args[token];
|
|
|
|
|
}
|
2019-05-17 01:28:26 -07:00
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
int newProcess = client.sendNewJobMessage(command);
|
2019-05-18 12:20:12 -07:00
|
|
|
System.out.printf("Process started, assigned pid is %d\n", newProcess);
|
2019-05-17 01:28:26 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "output":
|
2019-05-17 18:22:38 -07:00
|
|
|
if (args.length != 4) {
|
|
|
|
|
System.out.println("Improper formatting, try client --help");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
candidatePid = Integer.parseInt(args[3]);
|
|
|
|
|
} catch (InputMismatchException e) {
|
|
|
|
|
System.out.println(args[3] + " is not a valid int, much less a valid pid");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String processOutput = client.getProcessOutput(candidatePid);
|
|
|
|
|
System.out.println(processOutput);
|
2019-05-17 01:28:26 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "status":
|
2019-05-17 18:22:38 -07:00
|
|
|
if (args.length != 4) {
|
|
|
|
|
System.out.println("Improper formatting, try client --help");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-05-17 01:28:26 -07:00
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
try {
|
|
|
|
|
candidatePid = Integer.parseInt(args[3]);
|
2019-05-17 01:28:26 -07:00
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
} catch (InputMismatchException e) {
|
|
|
|
|
System.out.println(args[3] + " is not a valid int, much less a valid pid");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Boolean processStatus = client.getProcessStatus(candidatePid);
|
2019-05-18 12:20:12 -07:00
|
|
|
System.out.printf("Process is currently running? %b\n", processStatus);
|
2019-05-17 01:28:26 -07:00
|
|
|
break;
|
|
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
case "kill":
|
|
|
|
|
if (args.length != 4) {
|
|
|
|
|
System.out.println("Improper formatting, try client --help");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
candidatePid = Integer.parseInt(args[3]);
|
|
|
|
|
|
|
|
|
|
} catch (InputMismatchException e) {
|
|
|
|
|
System.out.println(args[3] + " is not a valid int, much less a valid pid");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-18 12:20:12 -07:00
|
|
|
client.killProcess(candidatePid);
|
2019-05-17 18:22:38 -07:00
|
|
|
System.out.println("End process request recieved!");
|
2019-05-17 01:28:26 -07:00
|
|
|
break;
|
2019-05-17 12:19:51 -07:00
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
case "return":
|
|
|
|
|
if (args.length != 4) {
|
|
|
|
|
System.out.println("Improper formatting, try client --help");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-05-17 12:19:51 -07:00
|
|
|
|
|
|
|
|
try {
|
2019-05-17 18:22:38 -07:00
|
|
|
candidatePid = Integer.parseInt(args[3]);
|
2019-05-17 12:19:51 -07:00
|
|
|
|
|
|
|
|
} catch (InputMismatchException e) {
|
2019-05-17 18:22:38 -07:00
|
|
|
System.out.println(args[3] + " is not a valid int, much less a valid pid");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-18 12:20:12 -07:00
|
|
|
int returnCode = client.getProcessReturn(candidatePid);
|
2019-05-17 18:22:38 -07:00
|
|
|
|
|
|
|
|
if (returnCode == 277) {
|
|
|
|
|
System.out.println("Process is still running");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
} else if (returnCode == 278) {
|
|
|
|
|
System.out.println("RPC Call error!");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
} else {
|
2019-05-18 12:20:12 -07:00
|
|
|
System.out.printf("Process Exit Code: %d\n", returnCode);
|
2019-05-17 12:19:51 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 18:22:38 -07:00
|
|
|
default:
|
|
|
|
|
System.out.println("Improper command, try 'help'");
|
2019-05-17 12:19:51 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-17 18:22:38 -07:00
|
|
|
|
|
|
|
|
public static void outputHelp() {
|
|
|
|
|
System.out.println("... new (command)");
|
2019-05-18 12:20:12 -07:00
|
|
|
System.out.println("Starts a new process on the server");
|
2019-05-17 18:22:38 -07:00
|
|
|
System.out.println("... output (pid)");
|
|
|
|
|
System.out.println("Garners output from process on server");
|
|
|
|
|
System.out.println("... status (pid)");
|
|
|
|
|
System.out.println("Returns whether process on server is running");
|
|
|
|
|
System.out.println("... return (pid)");
|
|
|
|
|
System.out.println("Collects return code from remote process");
|
|
|
|
|
System.out.println("... kill (pid)");
|
|
|
|
|
System.out.println("Immediately destroys remote process");
|
2019-05-17 01:28:26 -07:00
|
|
|
}
|
|
|
|
|
}
|