there is now a player character and the world renders using a cammera
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include <libssh/server.h>
|
||||
#include <libssh/callbacks.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <threads.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
@@ -28,9 +29,15 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef DOCKER
|
||||
#define RSA_LOCATION "./id_rsa"
|
||||
#else
|
||||
#define RSA_LOCATION "/root/.ssh/id_rsa"
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef BUF_SIZE
|
||||
#define BUF_SIZE 2049
|
||||
#define BUF_SIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef KEYS_FOLDER
|
||||
@@ -44,6 +51,8 @@
|
||||
|
||||
|
||||
|
||||
int client_stop_thread = 0;
|
||||
|
||||
static int authenticated=0;
|
||||
static int tries = 0;
|
||||
static int error = 0;
|
||||
@@ -52,6 +61,7 @@ static ssh_channel chan[30] ={NULL};
|
||||
typedef struct {
|
||||
ssh_session session;
|
||||
ServerConfig* config;
|
||||
|
||||
} ud;
|
||||
|
||||
static int auth_none(ssh_session session,
|
||||
@@ -110,6 +120,20 @@ static int auth_gssapi_mic(ssh_session session, const char *user, const char *pr
|
||||
}
|
||||
#endif
|
||||
|
||||
static int shell_resize(ssh_session s, ssh_channel ch, int width, int height,
|
||||
int pxwidth, int pwheight, void* userdata) {
|
||||
|
||||
client* cli = (client*)userdata;
|
||||
cli->resize = 1;
|
||||
cli->newHeight = height;
|
||||
cli->newWidth = width;
|
||||
|
||||
|
||||
printf("setting Size to %d,%d\n", cli->newWidth, cli->newHeight);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pty_request(ssh_session session, ssh_channel channel, const char *term,
|
||||
int x,int y, int px, int py, void *userdata){
|
||||
(void) session;
|
||||
@@ -120,6 +144,14 @@ static int pty_request(ssh_session session, ssh_channel channel, const char *ter
|
||||
(void) px;
|
||||
(void) py;
|
||||
(void) userdata;
|
||||
client* cli = (client*)userdata;
|
||||
cli->resize = 1;
|
||||
cli->newHeight = y;
|
||||
cli->newWidth = x;
|
||||
|
||||
|
||||
printf("setting Size to %d,%d\n", cli->newWidth, cli->newHeight);
|
||||
printf("Terminal is %s\n", term);
|
||||
printf("Allocated terminal\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -131,19 +163,25 @@ static int shell_request(ssh_session session, ssh_channel channel, void *userdat
|
||||
printf("Allocated shell\n");
|
||||
return 0;
|
||||
}
|
||||
struct ssh_channel_callbacks_struct channel_cb = {
|
||||
.channel_pty_request_function = pty_request,
|
||||
.channel_shell_request_function = shell_request
|
||||
};
|
||||
//struct ssh_channel_callbacks_struct channel_cb = {
|
||||
// .channel_pty_request_function = pty_request,
|
||||
// .channel_shell_request_function = shell_request,
|
||||
// .channel_pty_window_change_function = shell_resize,
|
||||
//
|
||||
//};
|
||||
|
||||
static ssh_channel new_session_channel(ssh_session session, void *userdata){
|
||||
(void) session;
|
||||
(void) userdata;
|
||||
|
||||
|
||||
client* c = (client*)userdata;
|
||||
ssh_channel* cli = &c->channel;
|
||||
printf("Allocated session channel for id %d\n", *(int*)userdata);
|
||||
*(ssh_channel*)userdata = ssh_channel_new(session);
|
||||
ssh_callbacks_init(&channel_cb);
|
||||
ssh_set_channel_callbacks(*(ssh_channel*)userdata, &channel_cb);
|
||||
return *(ssh_channel*)userdata;
|
||||
*cli = ssh_channel_new(session);
|
||||
ssh_callbacks_init(&c->channel_cb);
|
||||
ssh_set_channel_callbacks(*cli, &c->channel_cb);
|
||||
return *cli;
|
||||
}
|
||||
|
||||
|
||||
@@ -155,17 +193,27 @@ void* Handel_Client(void* d) {
|
||||
int i;
|
||||
ssh_event mainloop;
|
||||
ssh_session session = data.session;
|
||||
ssh_channel c;
|
||||
client* cli = (client*)malloc(sizeof(client));
|
||||
|
||||
struct ssh_server_callbacks_struct cb = {
|
||||
.userdata = (void*)&c,
|
||||
|
||||
printf("og cliet addres %d\n", cli);
|
||||
|
||||
cli->server_cb = (struct ssh_server_callbacks_struct){
|
||||
.userdata = cli,
|
||||
.auth_none_function = auth_none,
|
||||
.auth_password_function = auth_password,
|
||||
.channel_open_request_session_function = new_session_channel,
|
||||
};
|
||||
|
||||
ssh_callbacks_init(&cb);
|
||||
ssh_set_server_callbacks(session, &cb);
|
||||
cli->channel_cb = (struct ssh_channel_callbacks_struct){
|
||||
.userdata = cli,
|
||||
.channel_pty_request_function = pty_request,
|
||||
.channel_shell_request_function = shell_request,
|
||||
.channel_pty_window_change_function = shell_resize,
|
||||
};
|
||||
|
||||
ssh_callbacks_init(&cli->server_cb);
|
||||
ssh_set_server_callbacks(session, &cli->server_cb);
|
||||
|
||||
if (ssh_handle_key_exchange(session)) {
|
||||
printf("ssh_handle_key_exchange: %s\n", ssh_get_error(session));
|
||||
@@ -177,7 +225,7 @@ void* Handel_Client(void* d) {
|
||||
mainloop = ssh_event_new();
|
||||
ssh_event_add_session(mainloop, session);
|
||||
|
||||
while (!(authenticated && c != NULL)){
|
||||
while (!(authenticated && cli->channel != NULL)){
|
||||
if(error)
|
||||
break;
|
||||
r = ssh_event_dopoll(mainloop, -1);
|
||||
@@ -188,80 +236,102 @@ void* Handel_Client(void* d) {
|
||||
}
|
||||
}
|
||||
|
||||
char sendBuf[1024] = {0};
|
||||
int sendBuffSize = 1024;
|
||||
char* sendBuf = (char*)malloc(30);
|
||||
int sendBuffSize = 30;
|
||||
int counter = 0;
|
||||
if(error){
|
||||
printf("Error, exiting loop\n");
|
||||
} else
|
||||
printf("Authenticated and got a channel\n");
|
||||
client* cli = NULL;
|
||||
|
||||
// Data Init
|
||||
data.config->cbs.init_var = cli;
|
||||
data.config->cbs.run_var = cli;
|
||||
data.config->cbs.stop_var = cli;
|
||||
|
||||
data.config->cbs.ssh_init(data.config->cbs.init_var);
|
||||
|
||||
do{ // Update
|
||||
i = 1;
|
||||
//snprintf(sendBuf, 1024, "Counter: %d, id: %d\r", counter, data.config->id);
|
||||
//char kittyBuffer[30] = {0};
|
||||
//sprintf(kittyBuffer, "\e[>%d", 0b1000);
|
||||
//if (ssh_channel_write(c, kittyBuffer, 30) == SSH_ERROR) {
|
||||
// printf("error writing to channel\n");
|
||||
// return NULL;
|
||||
//}
|
||||
i = ssh_channel_read_nonblocking(c, buf, sizeof(buf)-1, 0);
|
||||
counter++;
|
||||
// code go here
|
||||
ssh_terminal_data tb = {
|
||||
data.config->cbs.ssh_init(cli);
|
||||
|
||||
cli->term = (ssh_terminal_data) {
|
||||
.inputBuffer = buf,
|
||||
.outputBuffer = sendBuf,
|
||||
.inputSize = 1024,
|
||||
.inputSize = sizeof(buf),
|
||||
.outputSize = &sendBuffSize,
|
||||
.channel = cli->channel,
|
||||
};
|
||||
data.config->cbs.ssh_run(&tb,data.config->cbs.run_var);
|
||||
|
||||
|
||||
do{ // Update
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
i = 1;
|
||||
i = ssh_channel_read_nonblocking(cli->channel, buf, sizeof(buf)-1, 0);
|
||||
// code go here
|
||||
if (ssh_channel_write(c, tb.outputBuffer, 1024) == SSH_ERROR) {
|
||||
printf("error writing to channel\n");
|
||||
return NULL;
|
||||
|
||||
|
||||
if(data.config->cbs.ssh_run(&cli->term,cli) == 0) {
|
||||
// code go here
|
||||
//if (ssh_channel_write(c, tb.outputBuffer, *tb.outputSize) == SSH_ERROR) {
|
||||
// printf("error writing to channel\n");
|
||||
// return NULL;
|
||||
//}
|
||||
}
|
||||
|
||||
if(i>0) {
|
||||
|
||||
buf[i] = '\0';
|
||||
printf("%s", buf);
|
||||
fflush(stdout);
|
||||
//printf("%s", buf);
|
||||
//fflush(stdout);
|
||||
if(buf[0] == '\x03') {
|
||||
ssh_disconnect(data.session);
|
||||
ssh_free(data.session);
|
||||
authenticated--;
|
||||
//ssh_channel_close(c);
|
||||
//ssh_channel_free(c);
|
||||
data.config->cbs.ssh_stop(cli);
|
||||
ssh_channel_close(cli->channel);
|
||||
ssh_channel_free(cli->channel);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} while (true);
|
||||
if(cli->resize > 0) {
|
||||
printf("running resize %d,%d\n", cli->newWidth, cli->newHeight);
|
||||
client_resize(cli->newWidth, cli->newHeight, cli);
|
||||
cli->resize = 0;
|
||||
}
|
||||
//memset(buf,0,BUF_SIZE);
|
||||
} while (!client_stop_thread);
|
||||
|
||||
data.config->cbs.ssh_stop(cli);
|
||||
ssh_channel_close(cli->channel);
|
||||
ssh_channel_free(cli->channel);
|
||||
|
||||
data.config->cbs.ssh_stop(data.config->cbs.stop_var);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stop_server(int arg) {
|
||||
printf("\nstopping server\n");
|
||||
client_stop_thread = 1;
|
||||
sleep(1);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int ssh_start(ServerConfig* conf){
|
||||
ssh_session session;
|
||||
ssh_bind sshbind;
|
||||
signal(SIGSTOP, stop_server);
|
||||
signal(SIGINT, stop_server);
|
||||
signal(SIGTERM, stop_server);
|
||||
|
||||
int port = 2222;// conf->port;
|
||||
int r =0;
|
||||
|
||||
sshbind=ssh_bind_new();
|
||||
|
||||
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, "/home/preacher/.ssh/id_rsa");
|
||||
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, RSA_LOCATION);
|
||||
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port);
|
||||
|
||||
while(sshbind) {
|
||||
while(sshbind && !client_stop_thread){ //&& !client_stop_thread) {
|
||||
|
||||
if(ssh_bind_listen(sshbind)<0){
|
||||
printf("Error listening to socket: %s\n",ssh_get_error(sshbind));
|
||||
@@ -274,15 +344,31 @@ int ssh_start(ServerConfig* conf){
|
||||
return 1;
|
||||
}
|
||||
ud d = {session, conf};
|
||||
d.config->stop = &client_stop_thread;
|
||||
|
||||
pthread_t thread_id;
|
||||
if(pthread_create(&thread_id, NULL, Handel_Client, (void*)&d) < 0) {
|
||||
ssh_disconnect(session);
|
||||
for(int i = 0; i < 12; i++) {
|
||||
if(conf->clients[i] == 0) {
|
||||
if(pthread_create(&conf->clients[i], NULL, Handel_Client, (void*)&d) < 0) {
|
||||
ssh_disconnect(session);
|
||||
}
|
||||
conf->threadCount++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//ssh_disconnect(session);
|
||||
//ssh_bind_free(sshbind);
|
||||
//ssh_finalize();
|
||||
|
||||
//for(int i = 0; i < 12; i++) {
|
||||
// if(conf->clients[i] != 0) {
|
||||
// printf("working on thread %d\n", i);
|
||||
// sleep(1);
|
||||
// pthread_cancel(conf->clients[i]);
|
||||
// }
|
||||
//}
|
||||
printf("Done\n");
|
||||
ssh_disconnect(session);
|
||||
ssh_bind_free(sshbind);
|
||||
ssh_finalize();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user