Skip to content

Commit

Permalink
Fixed data-clipboard-text to copy the right values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar Ovcharov committed Aug 4, 2024
1 parent 2eb0994 commit 5d97d74
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 38 deletions.
61 changes: 52 additions & 9 deletions pterosync/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class PteroSyncInstance
public array $hooksData = [];
public bool $use_alias_ip = false;
public $service_id = 0;
/**
* @var false|mixed
*/
public $dedicated_ip = false;
private array|false $server = false;

public function __construct()
Expand Down Expand Up @@ -164,12 +168,12 @@ public function getPterodactylUser($params, array $client = [], $create = true)
if ($create === true && $userResult['meta']['pagination']['total'] === 0) {
$userResult = $this->createPteroUser($client, $params);
} else {
if ($userResult['meta']['pagination']['total'] === 0){
if ($userResult['meta']['pagination']['total'] === 0) {
$userResult['status_code'] = 404;
}
if ($userResult['status_code'] !== 404){
if ($userResult['status_code'] !== 404) {
foreach ($userResult['data'] as $key => $value) {
if ($value['attributes']['email'] == $client['email']) {
if (strtolower($value['attributes']['email']) == strtolower($client['email'])) {
$userResult = $value;
$userResult['status_code'] = 200;
//Let's update the external_id to match the actual client id.
Expand All @@ -183,7 +187,7 @@ public function getPterodactylUser($params, array $client = [], $create = true)
}
}

if ($userResult['status_code'] === 404){
if ($userResult['status_code'] === 404) {
$userResult = $this->createPteroUser($client, $params);
}

Expand Down Expand Up @@ -413,14 +417,29 @@ function pteroSyncConvertToMB($input)
return $input * 1024;
}

function pteroSyncGetNodeAllocations($params, $nodePath)
function pteroSyncGetNodeAllocations($params, $serverNode)
{

$node = [];
$allocations = [];
$nodes = pteroSyncApplicationApi($params, 'nodes?include=allocations');
if ($nodes) {
foreach ($nodes['data'] as $nodeData) {
$node = $nodeData['attributes'];
if ($node['id'] == $serverNode) {
$allocations = $node['relationships']['allocations']['data'];
break;
}
}
}
return [$node, $allocations];

die;
$perPage = 200;
$PteroSyncSettings = PteroSyncInstance::get();
$currentPage = $PteroSyncSettings->fetchingNextPage;
$path = sprintf(
'%s?per_page=%d&page=%d&filter[server_id]=0',
'%s?per_page=%d&page=%d',
$nodePath,
$perPage,
$currentPage
Expand Down Expand Up @@ -462,11 +481,35 @@ function pteroSync_calculatePortRange($serverPortRange, $offset)
function pteroSyncMakeIParray($allocations)
{
$ips = [];
if (PteroSyncInstance::get()->dedicated_ip) {
// Array to store IPs to be removed
$ipsToRemove = [];

// First pass to identify all IPs with assigned ports
foreach ($allocations as $allocation) {
$attr = $allocation['attributes'];
if ($attr['assigned'] == 1) {
$ipsToRemove[] = $attr['ip'];
}
}

// Second pass to filter out allocations with IPs that should be removed
$allocations = array_filter($allocations, function($allocation) use ($ipsToRemove) {
return !in_array($allocation['attributes']['ip'], $ipsToRemove);
});

// Optional: Reindex the array to have consecutive keys
$allocations = array_values($allocations);

// Now $allocations contains only the entries without any IPs that have assigned ports
}

foreach ($allocations as $allocation) {
$attr = $allocation['attributes'];
$ip = $attr['ip'];
$ips[$ip][] = [
'port' => $attr['port'],
'assigned' => $attr['assigned'],
'id' => $attr['id']
];
}
Expand Down Expand Up @@ -787,7 +830,7 @@ function pteroSyncGenerateServerStatusArray($server, $hide_server_status)
$allocations = $server['relationships']['allocations']['data'];
$address = '';
$port = '';
pteroSync_getServerIPAndPort($address, $port,$address, $allocations, $server['allocation']);
pteroSync_getServerIPAndPort($address, $port, $address, $allocations, $server['allocation']);
if ($useQueryPort) {
$port = $environment['QUERY_PORT'];
}
Expand All @@ -807,10 +850,10 @@ function pteroSyncGenerateServerStatusArray($server, $hide_server_status)
return [$gameEngine, $address, $port];
}

function pteroSync_updateServerDomain($serverIp, $serverPort,$serverAliasIp, $params)
function pteroSync_updateServerDomain($serverIp, $serverPort, $serverAliasIp, $params)
{
try {
if ($serverAliasIp !== false){
if ($serverAliasIp !== false) {
$serverIp = $serverAliasIp;
}
Capsule::table('tblhosting')
Expand Down
86 changes: 57 additions & 29 deletions pterosync/pterosync.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
//TODO handle when egg_id and nest_id is changed
//TODO make it optional to reinstall the server when egg_id or nest_id is changed or both.

if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
Expand Down Expand Up @@ -121,8 +124,9 @@ function pterosync_ConfigKeys()
"ports_ranges" => "Ports Ranges",
"default_variables" => "Default Variables",
'server_port_offset' => "Server Port Offset",
"split_limit" => "Split Limit",
"hide_server_status" => "Hide Server Status"
"feature_limits" => "Feature Limits",
"hide_server_status" => "Hide Server Status",
'threads' => "Enter the specific CPU cores that this process can run on, or leave blank to allow all cores. This can be a single number, or a comma seperated list. Example: 0, 0-1,3, or 0,1,3,4."
];

}
Expand Down Expand Up @@ -293,9 +297,9 @@ function pterosync_ConfigOptions()
"Size" => 25,
'SimpleMode' => true,
],
"split_limit" => [
'FriendlyName' => "Split Limit",
"Description" => pterosyncAddHelpTooltip("Adjust the number of allowed splits effortlessly. Just set the desired maximum number of server splits.", 'split_limit'),
"feature_limits" => [
'FriendlyName' => "Feature Limits",
"Description" => pterosyncAddHelpTooltip("Feature limits are ideal for overriding add-ons that are integrated into your Pterodactyl panel. Ensure that the input is valid JSON. For more information, please refer to our Wiki page.", 'feature_limits'),
"Type" => "text",
"default" => '0',
"Size" => 25,
Expand All @@ -307,6 +311,15 @@ function pterosync_ConfigOptions()
"Type" => "yesno",
"Size" => 25,
'SimpleMode' => true,
],
// 'threads' => "Enter the specific CPU cores that this process can run on, or leave blank to allow all cores. This can be a single number, or a comma seperated list. Example: 0, 0-1,3, or 0,1,3,4."
"threads" => [
'FriendlyName' => "CPU Pinning",
"Description" => pterosyncAddHelpTooltip("Enter the specific CPU cores that this process can run on, or leave blank to allow all cores. This can be a single number, or a comma seperated list. Example: 0, 0-1,3, or 0,1,3,4.", 'threads'),
"Type" => "text",
"Size" => 25,
"default" => null,
'SimpleMode' => true,
]
];
}
Expand Down Expand Up @@ -358,8 +371,7 @@ function pterosync_TestConnection(array $params)

if ($response['status_code'] !== 200) {
$status_code = $response['status_code'];
$err = "Invalid status_code received: " . $status_code . ". Possible solutions: "
. (isset($solutions[$status_code]) ? $solutions[$status_code] : "None.");
$err = "Invalid status_code received: " . $status_code . ". Possible solutions: " . $solutions[$status_code] ?? "None.";
} else {
if ($response['meta']['pagination']['count'] === 0) {
$err = "Authentication successful, but no nodes are available.";
Expand Down Expand Up @@ -459,6 +471,7 @@ function pterosync_CreateAccount(array $params)

$location_id = pteroSyncGetOption($params, 'location_id');
$dedicated_ip = (bool)pteroSyncGetOption($params, 'dedicated_ip');
PteroSyncInstance::get()->dedicated_ip = $dedicated_ip;

PteroSyncInstance::get()->server_port_offset = pteroSyncGetOption($params, 'server_port_offset');

Expand All @@ -474,8 +487,8 @@ function pterosync_CreateAccount(array $params)
$maximumAllocations = pteroSyncGetOption($params, 'allocations');
$backups = pteroSyncGetOption($params, 'backups');
$oom_disabled = (bool)pteroSyncGetOption($params, 'oom_disabled');
$split_limit = pteroSyncGetOption($params, 'split_limit');

$threads = pteroSyncGetOption($params,'threads');
$serverData = [
'name' => $name,
'user' => (int)$userId,
Expand All @@ -486,17 +499,16 @@ function pterosync_CreateAccount(array $params)
'oom_disabled' => $oom_disabled,
'limits' => [
'memory' => (int)$memory,
//'threads' => '0-1,3',
'swap' => (int)$swap,
'io' => (int)$io,
'cpu' => (int)$cpu,
'disk' => (int)$disk,
'threads' => (string)$threads,
],
'feature_limits' => [
'databases' => $databases ? (int)$databases : null,
'allocations' => (int)$maximumAllocations,
'backups' => (int)$backups,
'split_limit' => (int)$split_limit,
],
'deploy' => [
'locations' => [(int)$location_id],
Expand All @@ -508,6 +520,12 @@ function pterosync_CreateAccount(array $params)
'external_id' => (string)$params['serviceid'],
];

$feature_limits = pteroSyncGetOption($params, 'feature_limits');
$feature_limits = json_decode($feature_limits, true);
if ($feature_limits) {
$serverData['feature_limits'] = array_merge($serverData['feature_limits'], $feature_limits);
}

$server = pteroSyncApplicationApi($params, 'servers?include=allocations', $serverData, 'POST');

if ($server['status_code'] === 400) throw new Exception('Couldn\'t find any nodes satisfying the request.');
Expand All @@ -520,18 +538,19 @@ function pterosync_CreateAccount(array $params)
$_SERVER_ALIAS_IP = false;
$serverAllocations = $server['attributes']['relationships']['allocations']['data'];
$allocation = $server['attributes']['allocation'];
pteroSync_getServerIPAndPort($_SERVER_IP, $_SERVER_PORT,$_SERVER_ALIAS_IP, $serverAllocations, $allocation);
pteroSync_getServerIPAndPort($_SERVER_IP, $_SERVER_PORT, $_SERVER_ALIAS_IP, $serverAllocations, $allocation);
$_SERVER_PORT_ID = $serverAllocations[0]['attributes']['id'];

$serverNode = $server['attributes']['node'];
$node_path = 'nodes/' . $serverNode . '/allocations';
$foundPorts = [];
$variables = [];
$ips = [];
$nodeAllocations = [];
if ($portsArray) {
$nodeAllocations = pteroSyncGetNodeAllocations($params, $node_path);

[$node, $nodeAllocations] = pteroSyncGetNodeAllocations($params, $serverNode);
$variables = pteroSyncProcessAllocations($eggData, $portsArray, $_SERVER_PORT);

}

if (!$variables) {
Expand All @@ -542,18 +561,22 @@ function pterosync_CreateAccount(array $params)
pteroSyncLog('NODE ALLOCATIONS', 'Node allocations not found.', [$node_path]);
}

while ($variables && $nodeAllocations) {
if ($nodeAllocations) {
$ips = pteroSyncMakeIParray($nodeAllocations);
$foundPorts = pteroSyncfindPorts($portsArray, $_SERVER_PORT, $_SERVER_IP, $variables, $ips);
if ($foundPorts) {
break;
}
if (PteroSyncInstance::get()->fetching) {
$nodeAllocations = pteroSyncGetNodeAllocations($params, $node_path);
} else {
break;
}
}
// while ($variables && $nodeAllocations) {
// $ips = pteroSyncMakeIParray($nodeAllocations);
// $foundPorts = pteroSyncfindPorts($portsArray, $_SERVER_PORT, $_SERVER_IP, $variables, $ips, $dedicated_ip);
// if ($foundPorts) {
// break;
// }
// if (PteroSyncInstance::get()->fetching) {
// $nodeAllocations = pteroSyncGetNodeAllocations($params, $node_path);
// } else {
// break;
// }
// }

if (!$foundPorts && $variables && $nodeAllocations) {
pteroSyncLog('Ports not founds', 'Ports not founds.', [
Expand Down Expand Up @@ -602,7 +625,6 @@ function pterosync_CreateAccount(array $params)
'databases' => (int)$databases,
'allocations' => (int)$maximumAllocations,
'backups' => (int)$backups,
'split_limit' => (int)$split_limit,
],
], $allocationArray), 'PATCH');

Expand All @@ -611,7 +633,7 @@ function pterosync_CreateAccount(array $params)

$allocation = $updateResult['attributes']['allocation'];
$serverAllocations = $updateResult['attributes']['relationships']['allocations']['data'];
pteroSync_getServerIPAndPort($_SERVER_IP, $_SERVER_PORT,$_SERVER_ALIAS_IP, $serverAllocations, $allocation);
pteroSync_getServerIPAndPort($_SERVER_IP, $_SERVER_PORT, $_SERVER_ALIAS_IP, $serverAllocations, $allocation);
pteroSyncApplicationApi($params, 'servers/' . $serverId . '/startup', [
'startup' => $server['attributes']['container']['environment']['STARTUP'],
'egg' => $server['attributes']['egg'],
Expand All @@ -624,7 +646,7 @@ function pterosync_CreateAccount(array $params)


unset($params['password']);
pteroSync_updateServerDomain($_SERVER_IP, $_SERVER_PORT,$_SERVER_ALIAS_IP, $params);
pteroSync_updateServerDomain($_SERVER_IP, $_SERVER_PORT, $_SERVER_ALIAS_IP, $params);
pteroSyncUpdateCustomFiled($params, $customFieldId, $_SERVER_ID);
Capsule::table('tblhosting')->where('id', $params['serviceid'])->update([
'username' => '',
Expand Down Expand Up @@ -737,32 +759,38 @@ function pterosync_ChangePackage(array $params)
$allocations = pteroSyncGetOption($params, 'allocations');
$backups = pteroSyncGetOption($params, 'backups');
$oom_disabled = (bool)pteroSyncGetOption($params, 'oom_disabled');
$split_limit = pteroSyncGetOption($params, 'split_limit');

$threads = pteroSyncGetOption($params,'threads');
$updateData = [
'allocation' => $serverData['allocation'],
'memory' => (int)$memory,
'swap' => (int)$swap,
'io' => (int)$io,
'cpu' => (int)$cpu,
'disk' => (int)$disk,
'threads' => (string)$threads,
'oom_disabled' => $oom_disabled,
'feature_limits' => [
'databases' => (int)$databases,
'allocations' => (int)$allocations,
'backups' => (int)$backups,
'split_limit' => (int)$split_limit,
],
];

$feature_limits = pteroSyncGetOption($params, 'feature_limits');
$feature_limits = json_decode($feature_limits, true);
if ($feature_limits) {
$updateData['feature_limits'] = array_merge($updateData['feature_limits'], $feature_limits);
}

$updateResult = pteroSyncApplicationApi($params, 'servers/' . $serverId . '/build?include=allocations', $updateData, 'PATCH');
if ($updateResult['status_code'] !== 200) throw new Exception('Failed to update build of the server, received error code: ' . $updateResult['status_code'] . '. Enable module debug log for more info.');
$allocation = $updateResult['attributes']['allocation'];
$serverAllocations = $updateResult['attributes']['relationships']['allocations']['data'];
$_SERVER_IP = '';
$_SERVER_PORT = '';
$_SERVER_ALIAS_IP = false;
pteroSync_getServerIPAndPort($_SERVER_IP, $_SERVER_PORT,$_SERVER_ALIAS_IP, $serverAllocations, $allocation);
pteroSync_getServerIPAndPort($_SERVER_IP, $_SERVER_PORT, $_SERVER_ALIAS_IP, $serverAllocations, $allocation);

$nestId = pteroSyncGetOption($params, 'nest_id');
$eggId = pteroSyncGetOption($params, 'egg_id');
Expand Down Expand Up @@ -806,7 +834,7 @@ function pterosync_ChangePackage(array $params)
$_SERVER_ID = $serverData['uuid'];
$customFieldId = pteroSyncGetCustomFiledId($params);

pteroSync_updateServerDomain($_SERVER_IP, $_SERVER_PORT,$_SERVER_ALIAS_IP, $params);
pteroSync_updateServerDomain($_SERVER_IP, $_SERVER_PORT, $_SERVER_ALIAS_IP, $params);
pteroSyncUpdateCustomFiled($params, $customFieldId, $_SERVER_ID);
} catch (Exception $err) {
return $err->getMessage();
Expand Down

0 comments on commit 5d97d74

Please sign in to comment.