Kaydet (Commit) a9f16b0a authored tarafından Mert ÇELEN's avatar Mert ÇELEN

Merge branch 'master' of ssh://10.150.0.134:7999/lmys/core into 1.1-dev

......@@ -7,6 +7,7 @@ APP_NOTIFICATION_EMAIL=admin@liman.dev
LOG_LEVEL=debug
LOG_PATH=/liman/logs/liman.log
LOG_EXTENSION_PATH=/liman/logs/extension.log
EXTENSION_DEVELOPER_MODE=false
QUEUE_DRIVER=database
NAV_EXTENSION_HIDE_COUNT=10
......
MIT License
Copyright (c) 2019 liman
Copyright (c) 2020 liman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
{
"name": "Liman MYS",
"version": "0.1.0",
"version": "1.0",
"description": "Liman MYS Api Documentation",
"title": "Liman",
"url" : "https://liman"
"url" : "https://liman.dev"
}
\ No newline at end of file
......@@ -11,20 +11,26 @@ class PHPSandbox implements Sandbox
{
private $path = "/liman/sandbox/php/index.php";
private $fileExtension = ".blade.php";
private $server,$extension,$user,$request;
public function __construct($server = null, $extension = null, $user = null,$request = null)
{
$this->server = ($server) ? $server : server();
$this->extension = ($extension) ? $extension : extension();
$this->user = ($user) ? $user : user();
$this->request = ($request) ? $request : request()->except([
"permissions",
"extension",
"server",
"script",
"server_id",
]);
private $server, $extension, $user, $request, $logId;
public function __construct(
$server = null,
$extension = null,
$user = null,
$request = null
) {
$this->server = $server ? $server : server();
$this->extension = $extension ? $extension : extension();
$this->user = $user ? $user : user();
$this->request = $request
? $request
: request()->except([
"permissions",
"extension",
"server",
"script",
"server_id",
]);
}
public function getPath()
......@@ -32,6 +38,11 @@ class PHPSandbox implements Sandbox
return $this->path;
}
public function setLogId($logId)
{
$this->logId = $logId;
}
public function getFileExtension()
{
return $this->fileExtension;
......@@ -145,6 +156,7 @@ class PHPSandbox implements Sandbox
json_encode($userData),
$publicPath,
$isAjax,
$this->logId,
];
$encrypted = openssl_encrypt(
......@@ -162,9 +174,12 @@ class PHPSandbox implements Sandbox
$keyPath = '/liman/keys' . DIRECTORY_SEPARATOR . $this->extension->id;
$soPath = "/liman/extensions/" . strtolower($this->extension->name) . "/liman.so";
$soPath =
"/liman/extensions/" .
strtolower($this->extension->name) .
"/liman.so";
$extra = is_file($soPath) ? "-dextension=$soPath ": "";
$extra = is_file($soPath) ? "-dextension=$soPath " : "";
return "sudo runuser " .
cleanDash($this->extension->id) .
" -c 'timeout 30 /usr/bin/php $extra-d display_errors=on $combinerFile $keyPath $encrypted'";
......
......@@ -11,20 +11,26 @@ class PythonSandbox implements Sandbox
{
private $path = "/liman/sandbox/python/index.py";
private $fileExtension = ".html.ninja";
private $server,$extension,$user,$request;
public function __construct($server = null, $extension = null, $user = null,$request = null)
{
$this->server = ($server) ? $server : server();
$this->extension = ($extension) ? $extension : extension();
$this->user = ($user) ? $user : user();
$this->request = ($request) ? $request : request()->except([
"permissions",
"extension",
"server",
"script",
"server_id",
]);
private $server, $extension, $user, $request, $logId;
public function __construct(
$server = null,
$extension = null,
$user = null,
$request = null
) {
$this->server = $server ? $server : server();
$this->extension = $extension ? $extension : extension();
$this->user = $user ? $user : user();
$this->request = $request
? $request
: request()->except([
"permissions",
"extension",
"server",
"script",
"server_id",
]);
}
public function getPath()
......@@ -32,6 +38,11 @@ class PythonSandbox implements Sandbox
return $this->path;
}
public function setLogId($logId)
{
$this->logId = $logId;
}
public function getFileExtension()
{
return $this->fileExtension;
......@@ -48,14 +59,18 @@ class PythonSandbox implements Sandbox
$extensionDb = [];
foreach ($settings->get() as $setting) {
$key = env('APP_KEY') . $this->user->id . $this->extension->id . $this->server->id;
$key =
env('APP_KEY') .
$this->user->id .
$this->extension->id .
$this->server->id;
$decrypted = openssl_decrypt($setting->value, 'aes-256-cfb8', $key);
$stringToDecode = substr($decrypted, 16);
$extensionDb[$setting->name] = base64_decode($stringToDecode);
}
$extensionDb = json_encode($extensionDb);
$request = json_encode($this->request);
$apiRoute = route('extension_server', [
......@@ -136,6 +151,7 @@ class PythonSandbox implements Sandbox
json_encode($userData),
$publicPath,
$isAjax,
$this->logId,
];
$keyPath = '/liman/keys' . DIRECTORY_SEPARATOR . $this->extension->id;
......
......@@ -3,8 +3,13 @@
namespace App\Classes\Sandbox;
interface Sandbox
{
public function __construct($server = null, $extension = null, $user = null,$request = null);
{
public function __construct(
$server = null,
$extension = null,
$user = null,
$request = null
);
public function getPath();
......@@ -13,4 +18,6 @@ interface Sandbox
public function command($function, $extensiondb = null);
public function getInitialFiles();
public function setLogId($logId);
}
......@@ -20,7 +20,12 @@ class Extension extends Model
"sslPorts",
"issuer",
"language",
"support"
"support",
"displays",
];
protected $casts = [
'displays' => 'array',
];
/**
......@@ -86,6 +91,6 @@ class Extension extends Model
public function getDisplayNameAttribute($value)
{
return Str::title(str_replace("-"," ",$this->name));
return Str::title(str_replace("-", " ", $this->name));
}
}
......@@ -10,9 +10,9 @@ use App\Jobs\ExtensionJob;
use App\Notification;
use App\Permission;
use App\Server;
use App\ServerLog;
use App\Token;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
......@@ -312,6 +312,17 @@ class InternalController extends Controller
);
}
public function sendLog()
{
Log::channel('extension')->info(
json_encode([
"log_id" => request('log_id'),
"message" => base64_encode(request('message')),
"title" => base64_encode(request('title')),
])
);
}
/**
* @api {post} /lmn/private/reverseProxyRequest Add Vnc Proxy Config
* @apiName SandboxAddVncProxyConfig
......
......@@ -7,10 +7,10 @@ use Illuminate\Http\Request;
use App\UserSettings;
use App\Permission;
use App\Server;
use App\ServerLog;
use App\Classes\Sandbox\PHPSandbox;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class MainController extends Controller
{
......@@ -24,7 +24,7 @@ class MainController extends Controller
});
}
private function initializeClass()
public function initializeClass()
{
$this->extension = json_decode(
file_get_contents(
......@@ -51,14 +51,41 @@ class MainController extends Controller
? request('target_function')
: 'index';
ServerLog::new(extension()->name, $page);
$logId = (string) Str::uuid();
$this->sandbox->setLogId($logId);
list($output, $timestamp) = $this->executeSandbox($page);
// Find the function in file. TODO find better solution here.
$extension = json_decode(
file_get_contents(
"/liman/extensions/" .
strtolower(extension()->name) .
DIRECTORY_SEPARATOR .
"db.json"
),
true
);
$display = false;
if (array_key_exists("functions", $extension)) {
foreach ($extension["functions"] as $function) {
if ($function["name"] == $page) {
$display = array_key_exists("displayLog", $function)
? $function["displayLog"]
: false;
break;
}
}
}
system_log(7, "EXTENSION_RENDER_PAGE", [
"extension_id" => extension()->id,
"server_id" => server()->id,
"view" => $page,
"log_id" => $logId,
"display" => $display,
]);
if (trim($output) == "") {
abort(504, "İstek zaman aşımına uğradı!");
......@@ -93,10 +120,7 @@ class MainController extends Controller
]);
}
if (
env('LIMAN_RESTRICTED') == true &&
!user()->isAdmin()
) {
if (env('LIMAN_RESTRICTED') == true && !user()->isAdmin()) {
return view('extension_pages.server_restricted', [
"view" => $output,
]);
......
......@@ -20,6 +20,11 @@ Route::post(
'Extension\Sandbox\InternalController@sendNotification'
)->name('SandboxSendNotification');
Route::post(
'/lmn/private/sendLog',
'Extension\Sandbox\InternalController@sendLog'
)->name('SandboxSendLog');
Route::post(
'/lmn/private/runCommandApi',
'Extension\Sandbox\InternalController@runCommand'
......
......@@ -441,8 +441,24 @@ class SettingsController extends Controller
"name" => request("name"),
"description" => request("description"),
"isActive" => request()->has("isActive") ? "true" : "false",
"displayLog" => request()->has("displayLog") ? "true" : "false",
]);
$extensionSQL = extension();
if (request()->has("displayLog")) {
if ($extensionSQL->displays == null) {
$extensionSQL->update([
"displays" => [request('name')],
]);
} else {
$current = $extensionSQL->displays;
array_push($current, request('name'));
$extensionSQL->update([
"displays" => $current,
]);
}
}
$extension["functions"] = $functions;
if (array_key_exists("version_code", $extension)) {
$extension["version_code"] = intval($extension["version_code"]) + 1;
......@@ -493,10 +509,33 @@ class SettingsController extends Controller
"name" => request("name"),
"description" => request("description"),
"isActive" => request()->has("isActive") ? "true" : "false",
"displayLog" => request()->has("displayLog")
? "true"
: "false",
];
}
}
$extensionSQL = extension();
if ($extensionSQL->displays != null) {
$current = $extensionSQL->displays;
if (request()->has('displayLog')) {
if (!in_array(request('name'), $current)) {
array_push($current, request('name'));
}
} else {
if (in_array(request('name'), $current)) {
unset($current[array_search(request('name'), $current)]);
}
}
if (empty($current)) {
$current = null;
}
extension()->update([
"displays" => $current,
]);
}
$extension["functions"] = $functions;
if (array_key_exists("version_code", $extension)) {
$extension["version_code"] = intval($extension["version_code"]) + 1;
......
......@@ -8,7 +8,7 @@ use App\ConnectorToken;
use App\Extension;
use App\Http\Controllers\Controller;
use App\Notification;
use App\ServerLog;
use App\User;
use App\Permission;
use Carbon\Carbon;
use Exception;
......@@ -215,10 +215,8 @@ class OneController extends Controller
// Respond according to the flag.
if ($flag == "1") {
// ServerLog::new("Dosya Yükleme " . request('path'), "Sunucuya dosya yüklendi\n" . request('path') . " ", server()->id, auth()->id());
return respond("Dosya başarıyla yüklendi.");
}
// ServerLog::new("Dosya Yükleme " . request('path'), "Sunucuya dosya yüklenemedi\n" . request('path') . " ", server()->id, auth()->id());
return respond('Dosya yüklenemedi.', 201);
}
......@@ -295,7 +293,14 @@ class OneController extends Controller
"free -t | awk 'NR == 2 {printf($3/$2*100)}'",
false
);
$cpu = substr(server()->run("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}'",false),0,7);
$cpu = substr(
server()->run(
"grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}'",
false
),
0,
7
);
// $cpu = server()->run("vmstat 1 1|tail -1|awk '{print $15}'", false);
// $cpu = 100 - intval($cpu);
} elseif (server()->type == "windows_powershell") {
......@@ -572,8 +577,8 @@ class OneController extends Controller
public function serviceList()
{
if(!Permission::can(user()->id,'liman','id','server_services')){
return respond("Bu işlemi yapmak için yetkiniz yok!",201);
if (!Permission::can(user()->id, 'liman', 'id', 'server_services')) {
return respond("Bu işlemi yapmak için yetkiniz yok!", 201);
}
$services = [];
if (
......@@ -644,16 +649,122 @@ class OneController extends Controller
public function getLogs()
{
if(!Permission::can(user()->id,'liman','id','view_logs')){
return respond("Sunucu Günlük Kayıtlarını görüntülemek için yetkiniz yok",201);
if (!Permission::can(user()->id, 'liman', 'id', 'view_logs')) {
return respond(
"Sunucu Günlük Kayıtlarını görüntülemek için yetkiniz yok",
201
);
}
return view('l.table', [
"value" => ServerLog::retrieve(true),
"title" => ["Başlık", "Açıklama", "Kullanıcı","İşlem Tarihi"],
"display" => ["command", "output", "username","created_at"],
$page = request('page') * 10;
$query = request('query') ? request('query') : "";
$server_id = request('server_id');
$count = intval(
trim(
`grep --text EXTENSION_RENDER_PAGE /liman/logs/liman.log | grep '"display":"true"'| grep '$query' | grep $server_id | wc -l`
)
);
$head = $page > $count ? $count % 10 : 10;
$data = trim(
`grep --text EXTENSION_RENDER_PAGE /liman/logs/liman.log | grep '"display":"true"'| grep '$query' | grep $server_id | tail -$page | head -$head | tac`
);
$clean = [];
$knownUsers = [];
$knownExtensions = [];
if ($data == "") {
return respond([
"table" => "Bu aramaya göre bir sonuç bulunamadı.",
]);
}
foreach (explode("\n", $data) as $row) {
$dateEndPos = strposX($row, " ", 2);
$date = substr($row, 1, $dateEndPos - 2);
$json = substr($row, strpos($row, "{"));
$parsed = json_decode($json, true);
$parsed["date"] = $date;
if (!array_key_exists($parsed["extension_id"], $knownExtensions)) {
$extension = Extension::find($parsed["extension_id"]);
if ($extension) {
$knownExtensions[$parsed["extension_id"]] =
$extension->display_name;
} else {
$knownExtensions[$parsed["extension_id"]] =
$parsed["extension_id"];
}
}
$parsed["extension_id"] = $knownExtensions[$parsed["extension_id"]];
if (!array_key_exists("log_id", $parsed)) {
$parsed["log_id"] = null;
}
if (!array_key_exists($parsed["user_id"], $knownUsers)) {
$user = User::find($parsed["user_id"]);
if ($user) {
$knownUsers[$parsed["user_id"]] = $user->name;
} else {
$knownUsers[$parsed["user_id"]] = $parsed["user_id"];
}
}
$parsed["user_id"] = $knownUsers[$parsed["user_id"]];
array_push($clean, $parsed);
}
$table = view('table', [
"value" => (array) $clean,
"startingNumber" => (intval(request('page')) - 1) * 10,
"title" => [
"Eklenti",
"Fonksiyon",
"Kullanıcı",
"İşlem Tarihi",
"*hidden*",
],
"display" => [
"extension_id",
"view",
"user_id",
"date",
"log_id:id",
],
"onclick" => "getLogDetails",
])->render();
$pagination = view('pagination', [
"current" => request('page') ? intval(request('page')) : 1,
"count" => floor($count / 10) + 1,
"onclick" => "getLogs",
])->render();
return respond([
"table" => $table . "<br>" . $pagination,
]);
}
public function getLogDetails()
{
$query = request('log_id');
$data = trim(`grep '$query' /liman/logs/extension.log`);
if ($data == "") {
return respond("Bu loga ait detay bulunamadı", 201);
}
$logs = [];
foreach (explode("\n", $data) as $row) {
$dateEndPos = strposX($row, " ", 2);
$date = substr($row, 1, $dateEndPos - 2);
$json = substr($row, strpos($row, "{"));
$parsed = json_decode($json, true);
$parsed["title"] = base64_decode($parsed["title"]);
$parsed["message"] = base64_decode($parsed["message"]);
array_push($logs, $parsed);
}
return respond($logs);
}
public function installPackage()
{
if (
......@@ -667,14 +778,9 @@ class OneController extends Controller
basename($package) .
".txt\" 2>&1 & disown && echo \$!'"
);
ServerLog::new(
__('Paket Güncelleme: :package_name', [
'package_name' => request("package_name"),
]),
__(':package_name paketi için güncelleme isteği gönderildi.', [
'package_name' => request("package_name"),
])
);
system_log(7, "Paket Güncelleme", [
'package_name' => request("package_name"),
]);
} elseif (server()->type == "windows_powershell") {
$raw = "";
}
......@@ -730,14 +836,9 @@ class OneController extends Controller
($mode == "update" && $output == "0") ||
($mode == "install" && $output != "0")
) {
ServerLog::new(
__('Paket Güncelleme: :package_name', [
'package_name' => request("package_name"),
]),
__(':package_name paketi başarıyla kuruldu.', [
'package_name' => request("package_name"),
])
);
system_log(7, "Paket Güncelleme Başarılı", [
'package_name' => request("package_name"),
]);
return respond([
"status" => __(
":package_name paketi başarıyla kuruldu.",
......@@ -746,14 +847,9 @@ class OneController extends Controller
"output" => trim($command_output),
]);
} else {
ServerLog::new(
__('Paket Güncelleme: :package_name', [
'package_name' => request("package_name"),
]),
__(':package_name paketi kurulamadı.', [
'package_name' => request("package_name"),
])
);
system_log(7, "Paket Güncelleme Başarısız", [
'package_name' => request("package_name"),
]);
return respond([
"status" => __(":package_name paketi kurulamadı.", [
'package_name' => request("package_name"),
......
......@@ -169,6 +169,11 @@ Route::group(['middleware' => ['server']], function () {
'Server\OneController@getLogs'
)->name('server_get_logs');
Route::post(
'/sunucu/gunluk_kayitlari_detay',
'Server\OneController@getLogDetails'
)->name('server_get_log_details');
Route::post('/sunucu/paketler', 'Server\OneController@packageList')->name(
'server_package_list'
);
......
......@@ -67,6 +67,7 @@ class OneController extends Controller
]);
$sandboxController = new MainController();
$sandboxController->initializeClass();
$output = $sandboxController->API()->content();
if (!$output) {
......
......@@ -47,6 +47,25 @@ if (!function_exists('respond')) {
}
}
if (!function_exists('strposX')) {
function strposX($haystack, $needle, $number)
{
if ($number == '1') {
return strpos($haystack, $needle);
} elseif ($number > '1') {
return strpos(
$haystack,
$needle,
strposX($haystack, $needle, $number - 1) + strlen($needle)
);
} else {
return error_log(
'Error: Value for parameter $number is out of range'
);
}
}
}
if (!function_exists('registerModuleRoutes')) {
function registerModuleRoutes()
{
......@@ -102,7 +121,7 @@ if (!function_exists('getLimanPermissions')) {
"type" => "liman",
"key" => "id",
])->get();
$permissions = $permissions->map(function ($permission) use(&$map) {
$permissions = $permissions->map(function ($permission) use (&$map) {
$permission->name = __($map[$permission->value]);
$permission->id = $permission->value;
return $permission;
......@@ -325,10 +344,11 @@ if (!function_exists('addCertificate')) {
$file = "liman-" . $hostname . "_" . $port . ".crt";
$cert = file_get_contents('/tmp/' . $path);
shell_exec(
"echo '$cert'| sudo tee /usr/local/share/ca-certificates/" . strtolower($file)
"echo '$cert'| sudo tee /usr/local/share/ca-certificates/" .
strtolower($file)
);
shell_exec("sudo update-ca-certificates");
// Create Certificate Object.
return Certificate::create([
"server_hostname" => strtolower($hostname),