Skip to content

Commit

Permalink
fix(console): add safety to check job id before output to console
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Dec 16, 2017
1 parent 46239f2 commit 92a393b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
13 changes: 7 additions & 6 deletions src/api/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export function dockerExec(id: string, cmd: any, env: string[] = []): Observable
.then(exec => exec.start())
.then(stream => {
const ws = new Writable();
ws.setDefaultEncoding('utf8');

ws.on('finish', () => {
const duration = new Date().getTime() - startTime;
Expand All @@ -107,27 +108,27 @@ export function dockerExec(id: string, cmd: any, env: string[] = []): Observable
});

ws._write = (chunk, enc, next) => {
let str = chunk.toString();
let str = chunk.toString('utf8');

if (str.includes('[abstruse_error]')) {
if (str.includes('[error]')) {
const splitted = str.split(' ');
exitCode = splitted[splitted.length - 1] || 1;
exitCode = Number(splitted[splitted.length - 1]) || 1;
ws.end();
} else if (str.includes('[abstruse_success]')) {
} else if (str.includes('[success]')) {
exitCode = 0;
ws.end();
} else if (!str.startsWith('>') && !str.startsWith('abstruse@')) {
} else {
if (str.includes('//') && str.includes('@')) {
str = str.replace(/\/\/(.*)@/, '//');
}

observer.next({ type: 'data', data: str });
}

next();
};

container.modem.demuxStream(stream.output, ws, ws);
stream.output.on('end', () => ws.end());
})
.catch(err => observer.error(err));
});
Expand Down
20 changes: 14 additions & 6 deletions src/api/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { IMemoryData, memory } from './stats/memory';
import { ICpuData, cpu } from './stats/cpu';
import { decodeJwt } from './security';
import { getLastBuild } from './db/build';
import { Subscribable } from 'rxjs/Observable';

export interface ISocketServerOptions {
app: express.Application;
Expand All @@ -41,7 +42,7 @@ export interface Client {
session: { cookie: any, ip: string, userId: number, email: string, isAdmin: boolean };
socket: uws.Socket;
send: Function;
subscriptions: { stats: Subscription };
subscriptions: { stats: Subscription, jobOutput: Subscription, logs: Subscription };
}

export class SocketServer {
Expand Down Expand Up @@ -109,7 +110,7 @@ export class SocketServer {
session: socket.upgradeReq.session,
socket: socket,
send: (message: any) => client.socket.send(JSON.stringify(message)),
subscriptions: { stats: null }
subscriptions: { stats: null, jobOutput: null, logs: null }
};
this.addClient(client);

Expand Down Expand Up @@ -147,6 +148,13 @@ export class SocketServer {
private removeClient(socket: uws.Socket): void {
const index = this.clients.findIndex(c => c.socket === socket);
const client = this.clients[index];

Object.keys(client.subscriptions).forEach(sub => {
if (client.subscriptions[sub]) {
client.subscriptions[sub].unsubscribe();
}
});

const msg: LogMessageType = {
message: `[socket]: user ${client.session.email} from ${client.session.ip} disconnected`,
type: 'info',
Expand Down Expand Up @@ -276,13 +284,13 @@ export class SocketServer {
const idx = processes.findIndex(proc => Number(proc.job_id) === jobId);
if (idx !== -1) {
const proc = processes[idx];
client.send({ type: 'data', data: proc.log });
client.send({ type: 'jobLog', data: proc.log });
client.send({ type: 'exposed ports', data: proc.exposed_ports || null });
client.send({ type: 'debug', data: proc.debug || null });
}

terminalEvents
.filter(e => e.job_id === parseInt(event.data.jobId, 10))
client.subscriptions.jobOutput = terminalEvents
.filter(e => Number(e.job_id) === Number(event.data.jobId))
.subscribe(output => client.send(output));
break;

Expand All @@ -291,7 +299,7 @@ export class SocketServer {
client.send({ type: 'error', data: 'not authorized' });
} else {
client.send({ type: 'request_received' });
logger.subscribe(msg => client.send(msg));
client.subscriptions.logs = logger.subscribe(msg => client.send(msg));
}
break;

Expand Down
3 changes: 2 additions & 1 deletion src/app/components/app-images/app-images.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class AppImagesComponent implements OnInit, OnDestroy {
'',
'# your commands go below: ',
'# example; install Chromium',
'RUN sudo apt-get install chromium-browser libgconf2-dev -y',
'RUN sudo apt-get update && sudo apt-get install chromium-browser libgconf2-dev -y',
'',
'# example; install nvm (Node Version Manager)',
'RUN cd /home/abstruse \\',
Expand Down Expand Up @@ -194,6 +194,7 @@ export class AppImagesComponent implements OnInit, OnDestroy {
'FROM ubuntu:17.10',
'',
'ENV DEBIAN_FRONTEND=noninteractive',
'ENV DISPLAY=:99',
'',
'# please do not edit between lines or image on abstruse will not work properly',
'',
Expand Down
8 changes: 5 additions & 3 deletions src/app/components/app-job/app-job.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ export class AppJobComponent implements OnInit, OnDestroy {

this.termSub = this.socketService.outputEvents
.subscribe(event => {
if (event.type === 'data' || event.type === 'exit' || event.type === 'container') {
this.terminalInput = event.data;
if (event.type === 'data' || event.type === 'exit' || event.type === 'container' || event.type === 'jobLog') {
if (Number(event.job_id) === Number(this.id) || event.type === 'jobLog') {
this.terminalInput = event.data;
}
} else if (event.type === 'job stopped' && event.data === this.id) {
this.processing = false;
} else if (event.type === 'job restarted' && event.data === this.id) {
Expand Down Expand Up @@ -110,7 +112,7 @@ export class AppJobComponent implements OnInit, OnDestroy {
this.jobRun.status = 'success';
this.jobRun.end_time = event.additionalData;
this.previousRuntime = this.jobRun.end_time - this.jobRun.start_time;
} else if (event.data === 'job failed' || event.data === 'job stopped') {
} else if (event.data === 'job failed') {
this.jobRun.status = 'failed';
this.jobRun.end_time = event.additionalData;
this.previousRuntime = this.jobRun.end_time - this.jobRun.start_time;
Expand Down
Binary file modified src/files/docker-essential/abstruse-pty-amd64
Binary file not shown.
Binary file modified src/files/docker-essential/abstruse-pty-armv7
Binary file not shown.
4 changes: 2 additions & 2 deletions src/files/docker-essential/abstruse-pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ int main(int argc, char *argv[]) {
if (WIFEXITED(status)) {
const int code = WEXITSTATUS(status);
if (code == 0) {
printf("\n[abstruse_success]: 0\n");
printf("\n[success]: 0\n");
} else {
printf("\n[abstruse_error]: %d\n", code);
printf("\n[error]: %d\n", code);
}

return code;
Expand Down
3 changes: 1 addition & 2 deletions src/files/docker-essential/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
VNC_STORE_PWD_FILE=/home/abstruse/.vnc/passwd
if [ ! -e "${VNC_STORE_PWD_FILE}" -o -n "${VNC_PASSWORD}" ]; then
mkdir -vp /home/abstruse/.vnc > /dev/null 2>&1
x11vnc -storepasswd ${VNC_PASSWORD:-abstruse} ${VNC_STORE_PWD_FILE} > /dev/null 2>&1
x11vnc -storepasswd ${VNC_PASSWORD:-abstrusePass} ${VNC_STORE_PWD_FILE} > /dev/null 2>&1
fi

export CHROME=${CHROME:-/opt/google/chrome/google-chrome}
export DISPLAY=:99

{
Expand Down

0 comments on commit 92a393b

Please sign in to comment.