experiments

This commit is contained in:
fbsanchez 2019-10-14 07:59:29 +02:00
parent e4775a3e6d
commit 86170b3ad8
10 changed files with 3350 additions and 0 deletions

View File

@ -0,0 +1,76 @@
<?php
// ______ __ _______ _______ _______
// | __ \.---.-.-----.--| |.-----.----.---.-. | ___| | | __|
// | __/| _ | | _ || _ | _| _ | | ___| |__ |
// |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
//
// ============================================================================
// Copyright (c) 2007-2018 Artica Soluciones Tecnologicas, http://www.artica.es
// This code is NOT free software. This code is NOT licenced under GPL2 licence
// You cannnot redistribute it without written permission of copyright holder.
// ============================================================================
require_once 'include/functions.php';
require_once 'include/functions_groupview.php';
require_once 'include/auth/mysql.php';
check_login();
if (! check_acl($config['id_user'], 0, 'PM')) {
db_pandora_audit(
'ACL Violation',
'Trying to access Profile Management'
);
include 'general/noaccess.php';
return;
}
function mainWetty()
{
global $config;
if (!isset($config['wetty_ip'])) {
config_update_value('wetty_ip', $_SERVER['SERVER_ADDR']);
}
if ($config['wetty_ip'] == '127.0.0.1') {
config_update_value('wetty_ip', $_SERVER['SERVER_ADDR']);
}
if (!isset($config['wetty_port'])) {
config_update_value('wetty_port', '3000');
}
$buttons['maps'] = [
'active' => false,
'text' => '<a href="index.php?login=1&extension_in_menu=gextensions&sec=gextensions&sec2=extensions/wetty_conf">'.html_print_image('images/setup.png', true, ['title' => __('Wetty settings')]).'</a>',
];
ui_print_page_header(__('Wetty'), 'images/extensions.png', false, '', true, $buttons);
$table->width = '100%';
$table->class = 'databox data';
$table->data = [];
$table->head = [];
$table->align = [];
// $table->align[3] = 'left';
$table->style = [];
$table->size = [];
// $table->size[3] = '10%';
$table->style[0] = 'font-weight: bold';
$table->head[0] = __('Wetty');
// $data[0] = '<iframe scrolling="auto" frameborder="0" width="100%" height="600px" src="http://192.168.70.64:3000/"></iframe>';
$data[0] = '<iframe scrolling="auto" frameborder="0" width="100%" height="600px" src="http://'.$config['wetty_ip'].':'.$config['wetty_port'].'/"></iframe>';
// $data[0] .= '<div id="terminal" style="background-color:black;width:100%;height:600px;overflow: hidden;"><div>';
array_push($table->data, $data);
html_print_table($table);
}
extensions_add_godmode_menu_option(__('Wetty'), 'AW', 'gextensions', null, 'v1');
extensions_add_godmode_function('mainWetty');

View File

@ -0,0 +1,74 @@
<?php
/**
* Quick shell extra tab (agent view). Gotty based.
*
* @category Extensions
* @package Pandora FMS
* @subpackage Quick Shell (gotty)
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2019 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
// Begin.
global $config;
/**
* QuickShell.
*/
class QuickShell
{
/**
* Initializes a QuickShell environment.
*/
public function __construct()
{
}
/**
* Install QuickShell extension into PandoraFMS
*
* Registers class into system.
*
* @return void
*/
public function install()
{
return true;
}
/**
* Launch an interactive bash session
*
* @return void
*/
public function interactiveShell()
{
file_get_Str
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,68 @@
<?php
/**
* PHP WebocketServer from:
*
* Copyright (c) 2012, Adam Alexander
* All rights reserved.
*
* Adapted to PandoraFMS by Fco de Borja Sanchez <fborja.sanchez@artica.es>
* Compatible with PHP >= 7.0
*
* @category External library
* @package Pandora FMS
* @subpackage WebSocketServer
* @version 1.0.0
* @license See below
* @filesource https://github.com/ghedipunk/PHP-Websockets
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of PHP WebSockets nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// Begin.
namespace \PandoraFMS\WebSocketServer;
class WebSocketUser
{
public $socket;
public $id;
public $headers = array();
public $handshake = false;
public $handlingPartialPacket = false;
public $partialBuffer = "";
public $sendingContinuous = false;
public $partialMessage = "";
public $hasSentClose = false;
function __construct($id, $socket)
{
$this->id = $id;
$this->socket = $socket;
}
}

View File

@ -0,0 +1,162 @@
<?php
/**
* PHP WebSocketUser from:
*
* Copyright (c) 2012, Adam Alexander
* All rights reserved.
*
* Adapted to PandoraFMS by Fco de Borja Sanchez <fborja.sanchez@artica.es>
* Compatible with PHP >= 7.0
*
* @category External library
* @package Pandora FMS
* @subpackage WebSocketUser
* @version 1.0.0
* @license See below
* @filesource https://github.com/ghedipunk/PHP-Websockets
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of PHP WebSockets nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// Begin.
namespace PandoraFMS;
/**
* Parent class for WebSocket User.
*/
class WebSocketUser
{
/**
* Socket.
*
* @var Socket
*/
public $socket;
/**
* Id.
*
* @var string
*/
public $id;
/**
* Headers.
*
* @var array
*/
public $headers = [];
/**
* Handshake.
*
* @var boolean
*/
public $handshake = false;
/**
* HandlingPartialPacket.
*
* @var boolean
*/
public $handlingPartialPacket = false;
/**
* PartialBuffer.
*
* @var string
*/
public $partialBuffer = '';
/**
* SendingContinuous.
*
* @var boolean
*/
public $sendingContinuous = false;
/**
* PartialMessage.
*
* @var string
*/
public $partialMessage = '';
/**
* HasSentClose.
*
* @var boolean
*/
public $hasSentClose = false;
/**
* Received raw packet, to be redirected.
*
* @var string
*/
public $rawPacket;
/**
* Initializes a websocket user.
*
* @param string $id Id.
* @param Socket $socket Socket.
*/
public function __construct($id, $socket)
{
$this->id = $id;
$this->socket = $socket;
}
/**
* Retun last packet (raw) received
*
* @return string
*/
public function getRawPacket()
{
return $this->rawPacket;
}
/**
* Store raw packet.
*
* @param string $packet received.
*
* @return void
*/
public function setRawPacket($packet)
{
$this->rawPacket = $packet;
}
}

View File

@ -0,0 +1,100 @@
<?php
// ______ __ _______ _______ _______
// | __ \.---.-.-----.--| |.-----.----.---.-. | ___| | | __|
// | __/| _ | | _ || _ | _| _ | | ___| |__ |
// |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
//
// ============================================================================
// Copyright (c) 2007-2018 Artica Soluciones Tecnologicas, http://www.artica.es
// This code is NOT free software. This code is NOT licenced under GPL2 licence
// You cannnot redistribute it without written permission of copyright holder.
// ============================================================================
require_once 'include/functions.php';
require_once 'include/functions_config.php';
require_once 'include/functions_groupview.php';
require_once 'include/auth/mysql.php';
check_login();
if (! check_acl($config['id_user'], 0, 'PM')) {
db_pandora_audit(
'ACL Violation',
'Trying to access Profile Management'
);
include 'general/noaccess.php';
return;
}
function confWetty()
{
global $config;
if (get_parameter('wetty_ip')) {
config_update_value('wetty_ip', get_parameter('wetty_ip'));
} else {
if (!isset($config['wetty_ip'])) {
$config['wetty_ip'] = $_SERVER['SERVER_ADDR'];
}
}
if ($config['wetty_ip'] == '127.0.0.1') {
config_update_value('wetty_ip', $_SERVER['SERVER_ADDR']);
}
if (get_parameter('wetty_port')) {
config_update_value('wetty_port', get_parameter('wetty_port'));
} else {
if (!isset($config['wetty_port'])) {
$config['wetty_port'] = '3000';
}
}
$buttons['maps'] = [
'active' => false,
'text' => '<a href="index.php?login=1&extension_in_menu=gextensions&sec=gextensions&sec2=extensions/wetty">'.html_print_image('images/groups_small/application_osx_terminal.png', true, ['title' => __('Wetty')]).'</a>',
];
ui_print_page_header(__('Wetty'), 'images/extensions.png', false, '', true, $buttons);
$row = 0;
echo '<form id="form_setup" action="'.$_SERVER['REQUEST_URI'].'" method="post">';
$table->width = '100%';
$table->class = 'databox data';
$table->data = [];
$table->head = [];
$table->align = [];
// $table->align[3] = 'left';
$table->style = [];
$table->size = [];
// $table->size[3] = '10%';
$table->style[0] = 'font-weight: bold';
$table->head[0] = __('Wetty Configuration');
$table->head[1] = __('');
$table->data[$row][0] = __('Wetty ip address connection');
$table->data[$row][1] = html_print_input_text('wetty_ip', $config['wetty_ip'], '', 25, 25, true);
$row++;
$table->data[$row][0] = __('Wetty port connection');
$table->data[$row][1] = html_print_input_text('wetty_port', $config['wetty_port'], '', 25, 25, true);
$row++;
array_push($table->data, $data);
html_print_table($table);
echo '<div class="action-buttons" style="width: '.$table_other->width.'">';
html_print_submit_button(__('Update'), 'update_button', false, 'class="sub upd"');
echo '</div>';
echo '</form>';
}
extensions_add_godmode_function('confWetty');

View File

@ -0,0 +1,76 @@
<?php
// ______ __ _______ _______ _______
// | __ \.---.-.-----.--| |.-----.----.---.-. | ___| | | __|
// | __/| _ | | _ || _ | _| _ | | ___| |__ |
// |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
//
// ============================================================================
// Copyright (c) 2007-2018 Artica Soluciones Tecnologicas, http://www.artica.es
// This code is NOT free software. This code is NOT licenced under GPL2 licence
// You cannnot redistribute it without written permission of copyright holder.
// ============================================================================
require_once 'include/functions.php';
require_once 'include/functions_groupview.php';
require_once 'include/auth/mysql.php';
check_login();
if (! check_acl($config['id_user'], 0, 'PM')) {
db_pandora_audit(
'ACL Violation',
'Trying to access Profile Management'
);
include 'general/noaccess.php';
return;
}
function mainWetty()
{
global $config;
if (!isset($config['wetty_ip'])) {
config_update_value('wetty_ip', $_SERVER['SERVER_ADDR']);
}
if ($config['wetty_ip'] == '127.0.0.1') {
config_update_value('wetty_ip', $_SERVER['SERVER_ADDR']);
}
if (!isset($config['wetty_port'])) {
config_update_value('wetty_port', '3000');
}
$buttons['maps'] = [
'active' => false,
'text' => '<a href="index.php?login=1&extension_in_menu=gextensions&sec=gextensions&sec2=extensions/wetty_conf">'.html_print_image('images/setup.png', true, ['title' => __('Wetty settings')]).'</a>',
];
ui_print_page_header(__('Wetty'), 'images/extensions.png', false, '', true, $buttons);
$table->width = '100%';
$table->class = 'databox data';
$table->data = [];
$table->head = [];
$table->align = [];
// $table->align[3] = 'left';
$table->style = [];
$table->size = [];
// $table->size[3] = '10%';
$table->style[0] = 'font-weight: bold';
$table->head[0] = __('Wetty');
// $data[0] = '<iframe scrolling="auto" frameborder="0" width="100%" height="600px" src="http://192.168.70.64:3000/"></iframe>';
$data[0] = '<iframe scrolling="auto" frameborder="0" width="100%" height="600px" src="http://'.$config['wetty_ip'].':'.$config['wetty_port'].'/"></iframe>';
// $data[0] .= '<div id="terminal" style="background-color:black;width:100%;height:600px;overflow: hidden;"><div>';
array_push($table->data, $data);
html_print_table($table);
}
extensions_add_godmode_menu_option(__('Wetty'), 'AW', 'gextensions', null, 'v1');
extensions_add_godmode_function('mainWetty');

View File

@ -0,0 +1,77 @@
<?php
$host = '127.0.0.1';
$port = 8080;
$r = file_get_contents('http://'.$host.':'.$port.'/js/hterm.js');
$r .= file_get_contents('http://'.$host.':'.$port.'/hterms.js');
$gotty = file_get_contents('http://'.$host.':'.$port.'/js/gotty.js');
$url = "var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws';";
$new = "var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + ':8081' + window.location.pathname;";
$gotty = str_replace($url, $new, $gotty);
?>
<!doctype html>
<html>
<head>
<title>GoTTY</title>
<style>body, #terminal {position: absolute; height: 100%; width: 100%; margin: 0px;}</style>
<link rel="icon" type="image/png" href="favicon.png">
</head>
<body>
<div id="terminal"></div>
<script type="text/javascript">
<?php echo $r; ?>
</script>
<script type="text/javascript">
<?php echo $gotty; ?>
</script>
</body>
</html>
<?php
/*
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connect = socket_connect($socket, $host, $port);
if (!$connect) {
echo 'failed to open target socket redirection';
$this->disconnect($user->socket);
} else {
echo ">> me he conectado al otro lado \n";
echo "Enviando petición HTTP HEAD ...\n";
$out .= 'GET ws://'.$host.':'.$port.'/ws HTTP/1.1'."\r\n";
$out .= 'Host: '.$host.':'.$port."\r\n";
$out .= 'Connection: Upgrade'."\r\n";
$out .= 'Upgrade: websocket'."\r\n";
//$out .= 'Sec-WebSocket-Key: tqIu95AAeKFFlrLTsixBAA=='."\r\n";
$out .= 'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits'."\r\n";
$out .= 'Sec-WebSocket-Protocol: gotty'."\r\n";
$out .= '{"Arguments":"","AuthToken":""}';
socket_write($socket, $out);
echo "Leyendo respuesta:\n\n";
while ($out = socket_read($socket, 2048)) {
echo '['.$out.']';
}
}
// Disconnect.
$out = "GET / HTTP/1.1\r\n";
$out .= 'Host: '.$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
socket_write($socket, $out);
echo ">> Recibiendo respuesta de peticion de cierre:\n";
while ($out = socket_read($socket, 2048)) {
echo '['.$out.']';
}
socket_close($socket);
*/

View File

@ -0,0 +1,193 @@
#!/usr/bin/env php
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once __DIR__.'/WebSocketServer.php';
require_once __DIR__.'/WebSocketUser.php';
use \PandoraFMS\WebSocketServer;
use \PandoraFMS\WebSocketUser;
class MyUser extends WebSocketUser
{
public $myId;
function __construct($id, $socket)
{
parent::__construct($id, $socket);
$this->myId = $id;
}
}
class echoServer extends WebSocketServer
{
private $redirectedSocket;
private $redirectedHost = '127.0.0.1';
function __construct($addr, $port, $bufferLength=2048)
{
parent::__construct($addr, $port, $bufferLength);
$this->userClass = 'MyUser';
}
/**
* 1MB... overkill for an echo server, but potentially plausible for other
* applications.
*
* @var integer
*/
protected $maxBufferSize = 1048576;
protected function readSocket()
{
$buffer;
$numBytes = socket_recv(
$this->redirectedSocket,
$buffer,
$this->maxBufferSize,
0
);
if ($numBytes === false) {
return false;
}
return $buffer;
}
protected function parseGottyHeaders($response)
{
$headers = [];
$lines = explode('\n', $response);
foreach ($lines as $l) {
$c = explode(':', $l);
$headers[trim($c[0])] = trim($c[1]);
}
return $headers;
}
protected function translateGottyHeaders($headers)
{
// Redirect.
$h = $headers;
/*
$h['Sec-Websocket-Key'] = base64_encode($headers['sec-websocket-key']);
$h['Sec-Websocket-Extensions'] = $headers['sec-websocket-extensions'];
$h['Sec-Websocket-Protocol'] = $headers['sec-websocket-protocol'];
$h['Sec-Websocket-Version'] = $headers['sec-websocket-version'];
$h['Connection'] = $headers['connection'];
if ($headers['upgrade']) {
$h['Upgrade'] = $headers['upgrade'];
}
*/
$h['Host'] = '127.0.0.1:8081';
$h['Origin'] = 'http://127.0.0.1:8081';
// Cleanup.
unset($h['get']);
unset($h['user-agent']);
unset($h['cache-control']);
unset($h['pragma']);
// Build.
$out = "GET /ws HTTP/1.1\r\n";
foreach ($h as $key => $value) {
$out .= ucfirst($key).': '.$value."\r\n";
}
$out .= "\r\n";
return $out;
}
protected function process($user, $message)
{
// What to do with received message.
echo 'Received from client> ['.$message."]\n";
socket_write($this->redirectedSocket, $message);
$out = $this->readSocket();
$this->send($user, $out);
}
protected function processProtocol(string $protocol): string
{
return 'Sec-Websocket-Protocol: '.$protocol;
}
protected function connected($user)
{
$this->redirectedSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connect = socket_connect($this->redirectedSocket, $this->redirectedHost, 8080);
if (!$connect) {
echo 'failed to open target socket redirection';
$this->disconnect($user->socket);
} else {
var_dump($this->rawHeaders);
$out = $this->translateGottyHeaders($this->rawHeaders);
echo '????';
socket_write($this->redirectedSocket, $out);
$response = '';
$response = $this->readSocket();
// Upgrade $user headers.
$new_headers = $this->parseGottyHeaders($response);
$user->headers += $new_headers;
print ">> Reenviando [gotty] [$response]\n";
// $this->send($user, $response);
}
// Do nothing: This is just an echo server, there's no need to track the user.
// However, if we did care about the users, we would probably have a cookie to
// parse at this step, would be looking them up in permanent storage, etc.
}
protected function closed($user)
{
$out = "GET /ws HTTP/1.1\r\n";
$out .= 'Host: '.$this->redirectedHost."\r\n";
$out .= "Connection: Close\r\n\r\n";
socket_write($this->redirectedSocket, $out);
echo ">> Recibiendo respuesta de peticion de cierre:\n";
$out = $this->readSocket();
socket_close($this->redirectedSocket);
// Do nothing: This is where cleanup would go, in case the user had any sort of
// open files or other objects associated with them. This runs after the socket
// has been closed, so there is no need to clean up the socket itself here.
}
}
$echo = new echoServer('0.0.0.0', '8081');
try {
$echo->run();
} catch (Exception $e) {
$echo->stdout($e->getMessage());
}