Skip to content

Commit

Permalink
Add support for crawl.akrasiac.org
Browse files Browse the repository at this point in the history
  • Loading branch information
hax0kartik committed Nov 27, 2019
1 parent ce06b07 commit 561d804
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 79 deletions.
24 changes: 19 additions & 5 deletions source/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void keyboard::print(C2D_Image layout, C2D_Image keypress, LightLock *lock, std:
int keyboard::append_to_input(keyboard *keyb, int index)
{
key_s key = keyb->keys[index];
std::string character = key.normal;
std::string character = keyb->do_capslock ? key.special : key.normal;

if(character == "Bsp")
{
Expand All @@ -38,8 +38,14 @@ int keyboard::append_to_input(keyboard *keyb, int index)
{
keyb->callback('\t');
}

else if(character == "Entr")
{

if(!keyb->echo)
keyb->input.append("\n");
return 1;
}

else if(character == "Space")
{
Expand All @@ -51,7 +57,10 @@ int keyboard::append_to_input(keyboard *keyb, int index)

else
{
keyb->callback(character[0]);

if(keyb->echo)
keyb->callback(character[0]);

keyb->input.append(character);
}
return 0;
Expand Down Expand Up @@ -169,18 +178,23 @@ std::string keyboard::get_input()
return std::move(input);
}

keyboard::keyboard(std::function<void()> &func, std::function<void( char )> cb)
std::string keyboard::get_input_async()
{
done = false;
return std::move(input);
}

keyboard::keyboard(std::function<void()> &func, std::function<void( char )> cb, bool de)
{
LightLock_Init(&lock);
C2D_SpriteSheet spriteSheet = C2D_SpriteSheetLoad("romfs:/gfx/sprites.t3x");
if(!spriteSheet) printf("Couldn't locate sprites\n");
gen_key_table(keys);
C2D_Image layout = C2D_SpriteSheetGetImage(spriteSheet, 4);
C2D_Image keypress = C2D_SpriteSheetGetImage(spriteSheet, 5);

func = std::bind(keyboard::print, layout, keypress, &lock, keys, &do_capslock, &selected_key);
callback = cb;
thread = threadCreate((ThreadFunc)&keyboard::hid_input_thread, this, 0x1000, 0x29, 1, true);
thread = threadCreate((ThreadFunc)&keyboard::hid_input_thread, this, 0x1000, 0x29, 1, true);
}

keyboard::~keyboard()
Expand Down
16 changes: 14 additions & 2 deletions source/keyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ constexpr int NUM_KEYS = 56;
class keyboard
{
public:
keyboard(std::function<void()> &func, std::function<void(char)> cb);
keyboard(std::function<void()> &func, std::function<void(char)> cb, bool de);
~keyboard();
std::string get_input();
bool has_data() { return done; };
std::string get_input_async();
bool has_data() { return !input.empty(); };
void disable_local_echo() { echo = false; };
bool has_enter_been_pressed()
{
bool value = was_enter_clicked;
was_enter_clicked = !was_enter_clicked;
return value;
};

private:
// Normal Keyboard
Thread thread;
LightLock lock;
std::array<key_s, NUM_KEYS> keys;
Expand All @@ -36,6 +46,8 @@ class keyboard
bool do_capslock;
bool do_special;
bool done;
bool echo = true;
bool was_enter_clicked;
int selected_key = -1, old_key;

static void print(C2D_Image layout, C2D_Image keypress, LightLock *lock, std::array<key_s, NUM_KEYS> keys, bool *capslock, int *selected);
Expand Down
8 changes: 6 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ uiFuncs_s ui;
/* Main function */
int main(int argc, char *argv[])
{
//ui.debug = true;
// ui.debug = true;
APT_SetAppCpuTimeLimit(30);
threadCreate((ThreadFunc)&uiThread, nullptr, 0x1000, 0x28, 1, true);
svcSleepThread(1e+9);
ssh();
ssh ssho;
ssho.init();
ssho.mainLoop();
ssho.deinit();

while(aptMainLoop())
{
if(keysDown() & KEY_START) break;
Expand Down
103 changes: 43 additions & 60 deletions source/ssh.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,53 @@
#include <libssh2.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <poll.h>
#include <fcntl.h>
#include <malloc.h>
#include <algorithm>
#include <3ds.h>
#include "util.hpp"
#include "daisywheel.hpp"
#include "keyboard.hpp"
#include "ui.hpp"

#include "ssh.hpp"
#define EXIT_COMMAND "exit"

int ssh()
int ssh::init()
{
std::string hostname = "";
std::string username = "username";
std::string password = "password";
std::string port = "22";
int rc, sock, written, auth_pw = 0;
struct addrinfo hints, *res = nullptr;
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel;
std::string commandbuf;
char inputbuf[1920];
util utils;

utils.set_print_func(ui.top_func);
utils.print("Console test\n");
auto input_cb = std::bind(utils.put_char, utils.lock, utils.vt, std::placeholders::_1);
keyboard kbd(ui.bot_func, input_cb);

utils.print("Enter Host Name/Addr:");
hostname = kbd.get_input();
utils.print("\n");

utils.print("Enter Username or leave blank for default:");
username = kbd.get_input();
if(username == "") username = "username";
utils.print("\nUsername: " + username + "\n");

utils.print("Enter port or leave blank to connect to port 22:");
port = kbd.get_input();
if(port == "") port = "22";
utils.print("\nPort: " + port + "\n");

u32 *SOC_buffer = (u32*)memalign(0x1000, 0x100000);

if(SOC_buffer == NULL) {
if(SOC_buffer == NULL)
{
utils.print("memalign: failed to allocate\n");
return -1;
}

// Now intialise soc:u service
Result ret = 0;
if ((ret = socInit(SOC_buffer, 0x100000)) != 0) {
if ((ret = socInit(SOC_buffer, 0x100000)) != 0)
{
utils.print("socInit:" + std::to_string(ret));
return -1;
}

rc = libssh2_init(0);
if (rc) {
if (rc)
{
utils.print("Error: libssh_init()\n");
return -1;
//return (EXIT_FAILURE);
}
}

int ssh::mainLoop()
{
auto input_cb = std::bind(utils.put_char, utils.lock, utils.vt, std::placeholders::_1);
keyboard kbd (ui.bot_func, input_cb, false);

utils.print("Enter Host Name/Addr:");
hostname = kbd.get_input();
utils.print("\n");

utils.print("Enter Username or leave blank for default:");
username = kbd.get_input();
if(username == "") username = "username";
utils.print("\nUsername: " + username + "\n");

utils.print("Enter port or leave blank to connect to port 22:");
port = kbd.get_input();
if(port == "") port = "22";
utils.print("\nPort: " + port + "\n");

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG;
Expand Down Expand Up @@ -192,14 +172,15 @@ int ssh()
/* Main loop starts here.
* In it you will be requested to input a command
* command will be executed at remote side
* an you will get output from it //*/
* an you will get output from it /*/

utils.print("Executed till main loop\n");
std::string buf;
libssh2_channel_flush(channel);
fd_set rfds, wfds;
struct timeval tv;

utils.print("Executed till main loop\n");
kbd.disable_local_echo();
do
{
FD_ZERO(&rfds);
Expand All @@ -208,11 +189,9 @@ int ssh()
tv.tv_sec = 2;
tv.tv_usec = 30;
int retval = select(sock + 1, &rfds, NULL, NULL, &tv);
if(kbd.has_data() == false)
if(retval == 1 && !kbd.has_data())
{
if(retval == 1)
{
do
do
{
rc = libssh2_channel_read(channel, inputbuf, 1920);
buf.append(inputbuf);
Expand All @@ -230,25 +209,25 @@ int ssh()
utils.print(buf);
buf.clear();
}
}
}

else
{
// utils.print("Output\n");
commandbuf = kbd.get_input();
commandbuf = kbd.get_input_async(); // Get every character stroke

if (commandbuf == EXIT_COMMAND)
break;
commandbuf += '\n';

written = 0;
do
{
rc = libssh2_channel_write(channel, commandbuf.c_str(), commandbuf.length());
written += rc;
} while (LIBSSH2_ERROR_EAGAIN != rc && rc > 0 && written != commandbuf.length());

for(int i = 0; i < commandbuf.size(); i++)
input_cb('\b');
//for(int i = 0; i < commandbuf.size(); i++)
// input_cb('\b');
commandbuf.clear();
if (rc < 0 && LIBSSH2_ERROR_EAGAIN != rc)
{
Expand All @@ -258,5 +237,9 @@ int ssh()
}
}
} while (aptMainLoop());
}

int ssh::deinit()
{
return 0;
}
39 changes: 38 additions & 1 deletion source/ssh.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
int ssh();
#pragma once

#include <libssh2.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <poll.h>
#include <fcntl.h>
#include <malloc.h>
#include <algorithm>
#include <3ds.h>
#include "util.hpp"
#include "daisywheel.hpp"
#include "keyboard.hpp"

class ssh
{
public:
int init();
int mainLoop();
int deinit();

private:
std::string hostname = "";
std::string username = "username";
std::string password = "password";
std::string port = "22";

int rc, sock, written, auth_pw = 0;
struct addrinfo hints, *res = nullptr;
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel;

std::string commandbuf;
char inputbuf[1920];
util utils;
};
19 changes: 10 additions & 9 deletions source/tmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ HANDLER(ed)
HANDLER(ich)
size_t n = P1(0); /* XXX use MAX */
if (n > s->ncol - c->c - 1) n = s->ncol - c->c - 1;
else if(n == 0) return;

memmove(l->chars + c->c + n, l->chars + c->c,
MIN(s->ncol - 1 - c->c,
Expand All @@ -202,7 +203,7 @@ HANDLER(dch)
memmove(l->chars + c->c, l->chars + c->c + n,
(s->ncol - c->c - n) * sizeof(TMTCHAR));

clearline(vt, l, s->ncol - c->c - n, s->ncol);
clearline(vt, l, s->ncol - n, s->ncol);
}

HANDLER(el)
Expand All @@ -217,13 +218,13 @@ HANDLER(sgr)
#define FGBG(c) *(P0(i) < 40? &vt->attrs.fg : &vt->attrs.bg) = c
for (size_t i = 0; i < vt->npar; i++) switch (P0(i)){
case 0: vt->attrs = defattrs; break;
case 1: case 22: vt->attrs.bold = P0(0) < 20; break;
case 2: case 23: vt->attrs.dim = P0(0) < 20; break;
case 4: case 24: vt->attrs.underline = P0(0) < 20; break;
case 5: case 25: vt->attrs.blink = P0(0) < 20; break;
case 7: case 27: vt->attrs.reverse = P0(0) < 20; break;
case 8: case 28: vt->attrs.invisible = P0(0) < 20; break;
case 10: case 11: vt->acs = P0(0) > 10; break;
case 1: case 22: vt->attrs.bold = P0(i) < 20; break;
case 2: case 23: vt->attrs.dim = P0(i) < 20; break;
case 4: case 24: vt->attrs.underline = P0(i) < 20; break;
case 5: case 25: vt->attrs.blink = P0(i) < 20; break;
case 7: case 27: vt->attrs.reverse = P0(i) < 20; break;
case 8: case 28: vt->attrs.invisible = P0(i) < 20; break;
case 10: case 11: vt->acs = P0(i) > 10; break;
case 30: case 40: FGBG(TMT_COLOR_BLACK); break;
case 31: case 41: FGBG(TMT_COLOR_RED); break;
case 32: case 42: FGBG(TMT_COLOR_GREEN); break;
Expand All @@ -250,7 +251,7 @@ HANDLER(rep)

HANDLER(dsr)
char r[BUF_MAX + 1] = {0};
snprintf(r, BUF_MAX, "\033[%zd;%zdR", c->r, c->c);
snprintf(r, BUF_MAX, "\033[%zd;%zdR", c->r + 1, c->c + 1);
CB(vt, TMT_MSG_ANSWER, (const char *)r);
}

Expand Down
1 change: 1 addition & 0 deletions source/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void uiThread(void *arg)
printf("GUI thread created");
while(aptMainLoop())
{
// svcWaitSynchronization(ui.event, U64_MAX);
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);

if(ui.debug == false)
Expand Down
1 change: 1 addition & 0 deletions source/ui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct
bool debug;
C3D_RenderTarget *top;
C3D_RenderTarget *bottom;
Handle event;
}uiFuncs_s;

void uiThread(void *arg);
Expand Down

0 comments on commit 561d804

Please sign in to comment.