Skip to content

Commit

Permalink
Merge pull request #90 from limanmys/1.4-dev
Browse files Browse the repository at this point in the history
Connector yapısının Sunucu objelerinden ayrılması
  • Loading branch information
mertcelen authored Sep 30, 2020
2 parents be1df29 + a3ea1c4 commit c84dab4
Show file tree
Hide file tree
Showing 75 changed files with 2,431 additions and 2,996 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ EXTENSION_DEVELOPER_MODE=false
QUEUE_DRIVER=database
NAV_EXTENSION_HIDE_COUNT=10
BRAND_NAME="Havelsan © 2020"

MAIL_ENABLED=false
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls

MARKET_URL=https://market.liman.dev
MARKET_CLIENT_ID=
MARKET_CLIENT_SECRET=
20 changes: 3 additions & 17 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
repository: limanmys/php-sandbox
path: package/liman/sandbox/php
token: ${{ secrets.CI_TOKEN }}
ref: "1.4-dev"

- uses: actions/cache@v2
with:
path: ~/.npm
Expand All @@ -39,22 +41,6 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Create Docs
run: |
sudo apt install npm -y
cd package/liman/server/
npm install
npm run docs
echo "api.liman.dev" > storage/docs/CNAME
- name: Push Docs to APIDOCS Repository
uses: cpina/github-action-push-to-another-repository@master
env:
API_TOKEN_GITHUB: ${{ secrets.CI_TOKEN }}
with:
source-directory: 'package/liman/server/storage/docs'
destination-github-username: 'limanmys'
destination-repository-name: 'apidocs'
user-email: mcelen94@gmail.com
- name: Set up Liman Environment
uses: shivammathur/setup-php@v2
with:
Expand Down Expand Up @@ -97,7 +83,7 @@ jobs:
Architecture: amd64
Priority: important
Description: Liman MYS
Depends: gpg, zip, unzip, dnsutils, nginx, redis, php-redis, php7.3-fpm, php7.3-curl, php7.3, php7.3-sqlite3, php7.3-ldap, php7.3-snmp, php7.3-mbstring, php7.3-xml, php7.3-zip, php7.3-ssh2, php7.3-posix, libnginx-mod-http-headers-more-filter, php7.3-smbclient, krb5-user, smbclient, libssl1.1, acl, novnc, supervisor, expect, php-mongodb, php7.3-gd, rsyslog, python3.7, python3-jinja2, python3-requests, python3-crypto, python3-paramiko, python3-tornado
Depends: gpg, zip, unzip, nginx, redis, php-redis, php7.3-fpm, php7.3-curl, php7.3, php7.3-sqlite3, php7.3-snmp, php7.3-mbstring, php7.3-xml, php7.3-zip, php7.3-posix, libnginx-mod-http-headers-more-filter, libssl1.1, supervisor
""" > DEBIAN/control
cd ../
dpkg-deb -Zgzip --build package
Expand Down
126 changes: 126 additions & 0 deletions app/Connectors/GenericConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Connectors;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use App\Models\Token;

class GenericConnector
{
public $server, $user;

public function __construct(\App\Models\Server $server = null, $user = null)
{
$this->server = $server;
$this->user = $user;
}

public function execute($command)
{
return trim(
self::request('runCommand', [
"command" => $command,
])
);
}

public function sendFile($localPath, $remotePath, $permissions = 0644)
{
return trim(
self::request('putFile', [
"localPath" => $localPath,
"remotePath" => $remotePath,
])
);
}

public function receiveFile($localPath, $remotePath)
{
return trim(
self::request('getFile', [
"localPath" => $localPath,
"remotePath" => $remotePath,
])
);
}

public function runScript($script, $parameters, $runAsRoot = false)
{
return trim(
self::request('getFile', [
"script" => $script,
"parameters" => $parameters,
"runAsRoot" => $runAsRoot,
])
);
$remotePath = "/tmp/" . Str::random();

$this->sendFile($script, $remotePath);
$output = $this->execute("[ -f '$remotePath' ] && echo 1 || echo 0");
if ($output != "1") {
abort(504, "Betik gönderilemedi");
}
$this->execute("chmod +x " . $remotePath);

// Run Part Of The Script
$query = $runAsRoot ? sudo() : '';
$query = $query . $remotePath . " " . $parameters . " 2>&1";
$output = $this->execute($query);

return $output;
}

public function verify($ip_address, $username, $password, $port, $type)
{
return trim(
self::request('verify', [
"ip_address" => $ip_address,
"username" => $username,
"password" => $password,
"port" => $port,
"keyType" => $type,
])
);
}

public function create(
\App\Models\Server $server,
$username,
$password,
$user_id,
$key,
$port = null
) {
}

public function request($url, $params, $retry = 3)
{
$client = new Client(['verify' => false]);

if ($this->server != null) {
$params["server_id"] = $this->server->id;
}

if ($this->user == null) {
$params["token"] = Token::create(user()->id);
} else {
$params["token"] = Token::create($this->user->id);
}

try {
$response = $client->request(
'POST',
"https://127.0.0.1:5454/$url",
[
"form_params" => $params,
]
);
return $response->getBody()->getContents();
} catch (GuzzleException $exception) {
return $exception->getMessage();
}
$json = json_decode((string) $res->getBody());
return $json->output;
}
}
59 changes: 26 additions & 33 deletions app/Connectors/SNMPConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ class SNMPConnector implements Connector
*/
public function __construct(\App\Models\Server $server, $user_id)
{
list($username, $password, $port) = self::retrieveCredentials();
$this->server = $server;
$this->username = $this->getCredential("username");
$this->securityLevel = $this->getCredential("SNMPsecurityLevel");
$this->authProtocol = $this->getCredential("SNMPauthProtocol");
$this->authPassword = $this->getCredential("SNMPauthPassword");
$this->privacyProtocol = $this->getCredential("SNMPprivacyProtocol");
$this->privacyPassword = $this->getCredential("SNMPprivacyPassword");
$this->username = $username;
$this->securityLevel = 'authPriv';
$this->authProtocol = 'SHA';
$this->authPassword = $password;
$this->privacyProtocol = 'AES';
$this->privacyPassword = $password;
}

public function execute($command, $flag = true)
Expand Down Expand Up @@ -102,25 +103,18 @@ public static function createSnmp()
return true;
}

public static function verifySnmp(
$ip_address,
$username,
$securityLevel,
$authProtocol,
$authPassword,
$privacyProtocol,
$privacyPassword
) {
public static function verifySnmp($ip_address, $username, $authPassword)
{
foreach (SNMPConnector::$verifyCommands as $command) {
try {
$flag = snmp3_get(
$ip_address,
$username,
$securityLevel,
$authProtocol,
'authPriv',
'SHA',
$authPassword,
'DES',
$authPassword,
$privacyProtocol,
$privacyPassword,
$command
);
} catch (\Exception $e) {
Expand All @@ -129,28 +123,27 @@ public static function verifySnmp(
}

if (isset($flag)) {
return respond("SNMP bağlantısı doğrulandı.", 200);
return "ok";
}
return respond(
"$username,$securityLevel,$authProtocol,$authPassword,$privacyProtocol,$privacyPassword,$ip_address",
201
);
return isset($flag);
return "nok";
}

private function getCredential($name)
public static function retrieveCredentials()
{
$object = UserSettings::where([
'user_id' => user()->id,
'server_id' => server()->id,
'name' => $name,
])->first();
if (!$object) {
if (server()->key() == null) {
abort(
504,
"Bu sunucu için SNMP anahtarınız yok. Kasa üzerinden bir anahtar ekleyebilirsiniz."
);
}
return lDecrypt($object["value"]);
$data = json_decode(server()->key()->data, true);

return [
lDecrypt($data["clientUsername"]),
lDecrypt($data["clientPassword"]),
array_key_exists("key_port", $data)
? intval($data["key_port"])
: 161,
];
}
}
Loading

0 comments on commit c84dab4

Please sign in to comment.