Client builds
This commit is contained in:
parent
ee2b7f0b4c
commit
d6e73cf7ad
10 changed files with 297 additions and 356 deletions
|
|
@ -0,0 +1,216 @@
|
|||
package JobServ;
|
||||
|
||||
import io.grpc.ManagedChannel;
|
||||
import io.grpc.ManagedChannelBuilder;
|
||||
import io.grpc.StatusRuntimeException;
|
||||
|
||||
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 {
|
||||
|
||||
// RequestMessage types
|
||||
// TODO: refactor to enum?
|
||||
public static final int OUTPUT = 0;
|
||||
public static final int RETURN = 1;
|
||||
public static final int PAUSE = 2;
|
||||
public static final int RESUME = 3;
|
||||
public static final int STATUS = 4;
|
||||
|
||||
/* 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 RequestMessage, blocks until response
|
||||
// returns an integer representing process status
|
||||
private int makeProcessRequest(int pid, int requestType) {
|
||||
logger.info("[+] Sending request");
|
||||
|
||||
RequestMessage request = RequestMessage.newBuilder()
|
||||
.setPID(pid)
|
||||
.setRequestType(requestType)
|
||||
.build();
|
||||
JobStatusMessage response;
|
||||
|
||||
try {
|
||||
// blocking network operation
|
||||
response = blockingStub.getStatusDetail(request);
|
||||
} catch (StatusRuntimeException e) {
|
||||
/* if this was using an async stub it might be
|
||||
* worthwhile to include the PID and request type
|
||||
*/
|
||||
logger.log(Level.WARNING, "[-]Status Request Failed: %s", e.getStatus());
|
||||
return -1;
|
||||
}
|
||||
|
||||
// return process status
|
||||
return response.getProcessStatus();
|
||||
}
|
||||
|
||||
// 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");
|
||||
|
||||
RequestMessage request = RequestMessage.newBuilder()
|
||||
.setPID(pid)
|
||||
.setRequestType(OUTPUT)
|
||||
.build();
|
||||
OutputMessage response;
|
||||
|
||||
try {
|
||||
// blocking network operation
|
||||
response = blockingStub.getJobOutput(request);
|
||||
} catch (StatusRuntimeException e) {
|
||||
logger.log(Level.WARNING, "[-] Request for output failed: %s", e.getStatus());
|
||||
// TODO: refactor this out by throwing here and catching in the shell
|
||||
return "";
|
||||
}
|
||||
|
||||
return response.getProcessOutput();
|
||||
}
|
||||
|
||||
// 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();
|
||||
JobStatusMessage response;
|
||||
|
||||
try {
|
||||
// blocking network operation
|
||||
response = blockingStub.makeNewJob(request);
|
||||
} catch (StatusRuntimeException e) {
|
||||
logger.log(Level.WARNING, "[-] Request for new job failed!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(response.getPID() == -1) {
|
||||
logger.log(Level.WARNING, "New job creation failed server side!");
|
||||
}
|
||||
|
||||
return response.getPID();
|
||||
}
|
||||
|
||||
// Client entrypoint
|
||||
public static void main(String[] args) throws Exception {
|
||||
// check args
|
||||
if(args.length != 3) {
|
||||
System.out.println("Usage: $ jobservclient host port");
|
||||
return;
|
||||
}
|
||||
|
||||
// start client (or fail if port is improperly formatted)
|
||||
JobServClient client;
|
||||
try {
|
||||
client = new JobServClient(args[1], Integer.parseInt(args[2]));
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid Port");
|
||||
return;
|
||||
}
|
||||
|
||||
Scanner reader = new Scanner(System.in);
|
||||
while(true) {
|
||||
System.out.print("> ");
|
||||
int pid, status;
|
||||
String input = reader.next();
|
||||
switch (input) {
|
||||
|
||||
case "pause":
|
||||
System.out.println("Enter a PID");
|
||||
pid = reader.nextInt();
|
||||
status = client.makeProcessRequest(pid, PAUSE);
|
||||
// TODO: parse status return
|
||||
break;
|
||||
|
||||
case "resume":
|
||||
System.out.println("Enter a PID");
|
||||
pid = reader.nextInt();
|
||||
status = client.makeProcessRequest(pid, RESUME);
|
||||
// TODO: parse status return
|
||||
break;
|
||||
|
||||
case "new":
|
||||
System.out.println("Enter a command");
|
||||
String command = reader.next();
|
||||
pid = client.sendNewJobMessage(command);
|
||||
System.out.println(String.format("New process on server: %d", pid));
|
||||
break;
|
||||
|
||||
case "output":
|
||||
System.out.println("Enter a PID");
|
||||
pid = reader.nextInt();
|
||||
String out = client.getProcessOutput(pid);
|
||||
System.out.println(out);
|
||||
break;
|
||||
|
||||
case "status":
|
||||
System.out.println("Enter a PID");
|
||||
pid = reader.nextInt();
|
||||
status = client.makeProcessRequest(pid, STATUS);
|
||||
System.out.println(String.format("Current status of program is: %d", status));
|
||||
break;
|
||||
|
||||
case "return":
|
||||
System.out.println("Enter a PID");
|
||||
pid = reader.nextInt();
|
||||
status = client.makeProcessRequest(pid, RETURN);
|
||||
System.out.println(String.format("Exit code of process is: %d", status));
|
||||
break;
|
||||
|
||||
case "quit":
|
||||
reader.close();
|
||||
return;
|
||||
|
||||
case "help":
|
||||
System.out.println("pause: pauses a process on the server");
|
||||
System.out.println("resume: resumes a process on the server");
|
||||
System.out.println("new: starts a new process on the server");
|
||||
System.out.println("output: garners (new) output from a process on the server");
|
||||
System.out.println("status: outputs the current status of a program on the server");
|
||||
System.out.println("return: outputs exit code of a process on the sercer");
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Improper output, try 'help'");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -14,81 +14,80 @@ public class JobServServer {
|
|||
|
||||
// starts the GRPC API Server
|
||||
private void start() throws IOException {
|
||||
// TODO: this should be passed in from a configuration manager
|
||||
int port = 8448;
|
||||
server = ServerBUilder.forPort(port)
|
||||
.addService(new JobServImpl())
|
||||
.build()
|
||||
.start();
|
||||
logger.info("Server initialized without tls");
|
||||
Runtime.getRuntime().addShutdownHook(new thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
// JVM shutdown might break logger functionality
|
||||
// so investigate this....
|
||||
logger.info("Shutting down server");
|
||||
JobServServer.this.stop();
|
||||
}
|
||||
});
|
||||
// TODO: this should be passed in from a configuration manager
|
||||
int port = 8448;
|
||||
server = ServerBUilder.forPort(port)
|
||||
.addService(new JobServImpl())
|
||||
.build()
|
||||
.start();
|
||||
logger.info("Server initialized without tls");
|
||||
Runtime.getRuntime().addShutdownHook(new thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
// JVM shutdown might break logger functionality
|
||||
// so investigate this....
|
||||
logger.info("Shutting down server");
|
||||
JobServServer.this.stop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void stop() {
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
}
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// spinlock for main()
|
||||
private void blockUntilShutdown() throws InterruptedException {
|
||||
if (server != null) {
|
||||
server.awaitTermination();
|
||||
}
|
||||
if (server != null) {
|
||||
server.awaitTermination();
|
||||
}
|
||||
}
|
||||
|
||||
// Main funciton. starts GRPC server and spins until server is shutdown
|
||||
// Main function. starts GRPC server and spins until server is shutdown
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
final JobServServer server = new JobServServer();
|
||||
server.start();
|
||||
server.blockUntilShutdown();
|
||||
final JobServServer server = new JobServServer();
|
||||
server.start();
|
||||
server.blockUntilShutdown();
|
||||
}
|
||||
|
||||
// wrap around stub code generated by GRPC
|
||||
static class JobServImpl extends JobServGrpc.JobServImplBase {
|
||||
static class JobServImpl extends JobServ.JobServImplBase {
|
||||
|
||||
@Override
|
||||
public void getStatusDetail(RequestMessage request,
|
||||
StreamObserver<JobStatusMessage> responder) {
|
||||
// TODO: Implement job controller, check request type, do the thing
|
||||
JobStatusMessage reply = JobStatusMessage.newBuilder()
|
||||
.setPID(-1)
|
||||
.setProcessStatus(-1)
|
||||
.build();
|
||||
responder.onNext(reply);
|
||||
responder.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getJobOutput(RequestMessage request,
|
||||
StreamObserver<OutputMessage> responder) {
|
||||
// TODO: Implement job controller, get output, do the thing
|
||||
OutputMessage reply = OutputMessage.newBuilder()
|
||||
.setPID(-1)
|
||||
.setProcessOutput("Hello World!")
|
||||
.build();
|
||||
responder.onNext(reply);
|
||||
responder.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeNewJob(NewJobMessage request,
|
||||
StreamObserver<JobStatusMessage> responder) {
|
||||
// TODO: Implement job controller, Start Job, do the thing
|
||||
JobStatusMessage reply = JobStatusMessage.newBuilder()
|
||||
.setPID(-1)
|
||||
.setProcessStatus(-1)
|
||||
.build():
|
||||
responder.onNext(reply);
|
||||
responder.onCompleted();
|
||||
}
|
||||
@Override
|
||||
public void getStatusDetail(RequestMessage request,
|
||||
StreamObserver<JobStatusMessage> responder) {
|
||||
// TODO: Implement job controller, check request type, do the thing
|
||||
JobStatusMessage reply = JobStatusMessage.newBuilder()
|
||||
.setPID(-1)
|
||||
.setProcessStatus(-1)
|
||||
.build();
|
||||
responder.onNext(reply);
|
||||
responder.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getJobOutput(RequestMessage request,
|
||||
StreamObserver<OutputMessage> responder) {
|
||||
// TODO: Implement job controller, get output, do the thing
|
||||
OutputMessage reply = OutputMessage.newBuilder()
|
||||
.setPID(-1)
|
||||
.build();
|
||||
responder.onNext(reply);
|
||||
responder.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeNewJob(NewJobMessage request,
|
||||
StreamObserver<JobStatusMessage> responder) {
|
||||
// TODO: Implement job controller, Start Job, do the thing
|
||||
JobStatusMessage reply = JobStatusMessage.newBuilder()
|
||||
.setPID(-1)
|
||||
.setJobStatus(-1)
|
||||
.build();
|
||||
responder.onNext(reply);
|
||||
responder.onCompleted();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
syntax = "proto3";
|
||||
|
||||
option java_multiple_files = true;
|
||||
option java_package = "com.ajhahn.jobserv";
|
||||
option java_outer_classname = "JobServProto";
|
||||
option java_package = "JobServ";
|
||||
option java_outer_classname = "JobServGrpc";
|
||||
option objc_class_prefix = "JSV";
|
||||
|
||||
package jobserv;
|
||||
package JobServ;
|
||||
|
||||
service jobserv {
|
||||
service ShellServer {
|
||||
rpc getStatusDetail (RequestMessage) returns (JobStatusMessage) {}
|
||||
rpc getJobOutput (RequestMessage) returns (OutputMessage) {}
|
||||
rpc makeNewJob (NewJobMessage) returns (JobStatusMessage) {}
|
||||
|
|
@ -28,6 +28,5 @@ message JobStatusMessage {
|
|||
}
|
||||
|
||||
message OutputMessage {
|
||||
int32 PID = 1;
|
||||
string ProcessOutput = 2;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue