Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from gluesys/dohyunkim
Browse files Browse the repository at this point in the history
Dohyunkim
  • Loading branch information
dohyunKim12 authored Mar 2, 2021
2 parents 0949854 + 273cb87 commit c578e73
Show file tree
Hide file tree
Showing 19 changed files with 1,256 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sshell2
69 changes: 69 additions & 0 deletions docs/RestAPI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## Functional Specification

| Category | Name | Function |
|-----------|-------|-----------|
|Sign in SSHELL | Sign in| Login with id and password|
| | Sign up | Sign up with id and password |
|Connect To PC | Load session | Load saved session|
| | Save session | Save session information |
| | Delete session | Delete selected session |
| | Connect ssh | Connect to remote PC |
| Use terminal | Terminal shell | Use CLI |
| | Customize | customize font, background color |
| Customize themes | Customize preview | Show preview based on present font, </br>background color |
| | Save customization | Save present font, background color |
<br/>

## REST API
```
{
signin :
- id,
password
signup : [
id,
password
],
session :{
load : [
sessionID
],
save : [
IP
],
delete : [
sessionID
]
},
open : [
IP :
],
customize : {
preview : [
font,
fontColor,
fontSize,
backgroundColor
],
save : [
font,
fontColor,
fontSize,
backgroundColor
]
},
}
```

| URI | Input | Function |
|-------|-------|----------|
|signin|{ id: "",<br/> password: "" }| login|
|signup|{ id: "",<br/> password: "" }| signup|
|session/load | { sessionID: "" } | load session |
|session/save | { IP: "" } | save session |
|session/delete | { sessionID: "" } | delete session |
|open | { ip: "" } | connect |
|customize | | customize|
|customize/preview | { font: "", <br/>fontcolor: "", <br/>fontsize: "",<br/>backgroundColor: ""} | preview customization|
|customize/save | { font: "", <br/>fontcolor: "", <br/>fontsize: "",<br/>backgroundColor: ""} | save customization |

89 changes: 89 additions & 0 deletions sshell/lib/Handler.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package Handler;

use strict;
use warnings;

use Terminal;
use Terminal::Ascii2Html;
use JSON ();

sub new {
my $class = shift;

my $self = {@_};
bless $self, $class;

$self->{ascii2html} = Terminal::Ascii2Html->new;

return $self;
}

sub run {
my $handler = shift;

my $cmd = $handler->{cmd};

return sub {
my $self = shift;

my $terminal = Terminal->new(
cmd => $cmd,
on_row_changed => sub {
my ($terminal, $row, $text) = @_;

$text = $handler->{ascii2html}->htmlify($text);

my $message = JSON->new->encode(
{type => 'row', row => $row, text => $text});
$self->send($message);
},
on_cursor_move => sub {
my ($terminal, $x, $y) = @_;

my $message =
JSON->new->encode({type => 'cursor', x => $x, y => $y});
$self->send($message);
},
on_finished => sub {
my $terminal = shift;

$self->close;
}
);

$self->on(
message => sub {
my ($self, $message) = @_;

my $json = JSON->new;

eval { $message = $json->decode($message); };
return if !$message || $@;

my $type = $message->{type};
if ($type eq 'key') {
my $buffer;

my $code = $message->{code};

$terminal->key($code);
}
elsif ($type eq 'action') {
$terminal->move($message->{action});
}
else {
warn "Unknown type '$type'";
}
}
);

$self->on(
disconnect => sub {
}
);

$terminal->start;
};
}

1;
20 changes: 20 additions & 0 deletions sshell/lib/Sshell.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Sshell;
use Mojo::Base 'Mojolicious', -signatures;

# This method will run once at server start
sub startup ($self) {

# Load configuration from config file
my $config = $self->plugin('NotYAMLConfig');

# Configure the application
$self->secrets($config->{secrets});

# Router
my $r = $self->routes;

# Normal route to controller
$r->get('/')->to('example#index');
}

1;
82 changes: 82 additions & 0 deletions sshell/lib/Sshell/Controller/Example.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package Sshell::Controller::Example;
use Mojo::Base 'Mojolicious::Controller', -signatures;

use Terminal;
use Terminal::Ascii2Html;
use JSON ();


# This action will render a template
sub index ($self) {

$self->{ascii2html} = Terminal::Ascii2Html->new;

my $log = $self->app->log;


my $cmd = "/usr/bin/zsh";
my $terminal = Terminal->new(
cmd => $cmd,
on_row_changed => sub {
my ($terminal, $row, $text) = @_;

$text = $self->{ascii2html}->htmlify($text);

my $message = JSON->new->encode(
{type => 'row', row => $row, text => $text});
$self->send($message);
},
on_cursor_move => sub {
my ($terminal, $x, $y) = @_;

my $message =
JSON->new->encode({type => 'cursor', x => $x, y => $y});
$self->send($message);
},
on_finished => sub {
my $terminal = shift;

$self->close;
}
);
$self->on(
message => sub {
my ($self, $message) = @_;

my $json = JSON->new;

eval { $message = $json->decode($message); };
return if !$message || $@;

my $type = $message->{type};
if ($type eq 'key') {
my $buffer;

my $code = $message->{code};

$terminal->key($code);
}
elsif ($type eq 'action') {
$terminal->move($message->{action});
}
else {
warn "Unknown type '$type'";
}
}
);

$self->on(
disconnect => sub {
}
);

$terminal->start;



# Render template "example/welcome.html.ep" with message
$self->render(msg => 'Welcome to the Mojolicious real-time web framework!');
}


1;
Loading

0 comments on commit c578e73

Please sign in to comment.