Skip to content

Commit 8171092

Browse files
authored
Improve detection of port used (hestiacp#2187)
* Improve detection of port used Use instead of grep function over sshd_config the command sshd -T | grep '^port' | cut -d ' ' -f2 to see the ports used * Update v-list-sys-sshd-port
1 parent a2845d4 commit 8171092

File tree

2 files changed

+112
-29
lines changed

2 files changed

+112
-29
lines changed

bin/v-list-sys-sshd-port

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
# info: list sshd port
3+
# options: [FORMAT]
4+
# labels: panel
5+
#
6+
# example: v-list-sys-sshd-port
7+
#
8+
# The function for obtainings the port of sshd listens to
9+
10+
# Argument definition
11+
12+
# Argument definition
13+
format=${1-shell}
14+
15+
# Includes
16+
# shellcheck source=/usr/local/hestia/func/main.sh
17+
source $HESTIA/func/main.sh
18+
19+
json_list() {
20+
sh_counter=$(echo "$ports" | wc -l)
21+
i=1
22+
echo '['
23+
for port in $ports; do
24+
if [ "$i" -lt "$sh_counter" ]; then
25+
echo -e "\t\"$port\","
26+
else
27+
echo -e "\t\"$port\""
28+
fi
29+
(( ++i))
30+
done
31+
echo "]"
32+
}
33+
34+
# SHELL list function
35+
shell_list() {
36+
echo "PORT"
37+
echo "-----"
38+
for port in $ports; do
39+
echo "$port"
40+
done
41+
}
42+
43+
# PLAIN list function
44+
plain_list() {
45+
for port in $ports; do
46+
echo "$port"
47+
done
48+
}
49+
50+
# CSV list function
51+
csv_list() {
52+
echo "PORT"
53+
for port in $ports; do
54+
echo "$port"
55+
done
56+
}
57+
58+
#----------------------------------------------------------#
59+
# Variable&Function #
60+
#----------------------------------------------------------#
61+
62+
ports=$(sshd -T | grep '^port'| cut -d ' ' -f2);
63+
64+
# Listing data
65+
case $format in
66+
json) json_list ;;
67+
plain) plain_list ;;
68+
csv) csv_list ;;
69+
shell) shell_list ;;
70+
esac
71+
72+
#----------------------------------------------------------#
73+
# Action #
74+
#----------------------------------------------------------#
75+
76+
77+
78+
#----------------------------------------------------------#
79+
# Hestia #
80+
#----------------------------------------------------------#
81+
82+
exit

install/deb/filemanager/filegator/configuration.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,39 @@
1111
$dist_config['frontend_config']['upload_max_size'] = 1024 * 1024 * 1024;
1212

1313
$dist_config['services']['Filegator\Services\Storage\Filesystem']['config']['adapter'] = function () {
14+
if (isset($_SESSION['user'])) {
15+
$v_user = $_SESSION['user'];
16+
}
17+
if (isset($_SESSION['look']) && ($_SESSION['userContext'] === 'admin')) {
18+
$v_user = $_SESSION['look'];
19+
}
20+
if ((isset($_SESSION['look']) && ($_SESSION['look'] == 'admin') && ($_SESSION['POLICY_SYSTEM_PROTECTED_ADMIN'] == 'yes'))) {
21+
header('Location: /');
22+
}
23+
# Create filemanager sftp key if missing and trash it after 30 min
24+
if (! file_exists('/home/'.basename($v_user).'/.ssh/hst-filemanager-key')) {
25+
exec("sudo /usr/local/hestia/bin/v-add-user-sftp-key " . escapeshellarg(basename($v_user)) . " 30", $output, $return_var);
26+
}
1427

15-
if (isset($_SESSION['user'])) {
16-
$v_user = $_SESSION['user'];
17-
}
18-
if (isset($_SESSION['look']) && ($_SESSION['userContext'] === 'admin')) {
19-
$v_user = $_SESSION['look'];
20-
}
21-
if ((isset($_SESSION['look']) && ($_SESSION['look'] == 'admin') && ($_SESSION['POLICY_SYSTEM_PROTECTED_ADMIN'] == 'yes') )) {
22-
header('Location: /');
23-
}
24-
# Create filemanager sftp key if missing and trash it after 30 min
25-
if (! file_exists('/home/'.basename($v_user).'/.ssh/hst-filemanager-key')) {
26-
exec ("sudo /usr/local/hestia/bin/v-add-user-sftp-key " . escapeshellarg(basename($v_user)) . " 30", $output, $return_var);
28+
if (!isset($_SESSION['SFTP_PORT'])) {
29+
exec("sudo /usr/local/hestia/bin/v-list-sys-sshd-port json", $output, $result);
30+
if ($output) {
31+
$port=json_decode(implode('', $output));
32+
$_SESSION['SFTP_PORT'] = $port[0];
33+
} else {
34+
$_SESSION['SFTP_PORT'] = 22;
2735
}
36+
}
2837

29-
if ( !isset($_SESSION['SFTP_PORT']) ) {
30-
if( preg_match('/^\s*Port\s+(\d+)$/im', file_get_contents('/etc/ssh/sshd_config'), $matches) ) {
31-
$_SESSION['SFTP_PORT'] = $matches[1] ?? 22;
32-
} else {
33-
$_SESSION['SFTP_PORT'] = 22;
34-
}
35-
}
38+
preg_match('/(Hestia SFTP Chroot\nMatch User)(.*)/i', file_get_contents('/etc/ssh/sshd_config'), $matches);
39+
$user_list = explode(',', $matches[2]);
40+
if (in_array($v_user, $user_list)) {
41+
$root = '/';
42+
} else {
43+
$root = '/home/'.$v_user;
44+
}
3645

37-
preg_match('/(Hestia SFTP Chroot\nMatch User)(.*)/i', file_get_contents('/etc/ssh/sshd_config'), $matches);
38-
$user_list = explode(',', $matches[2]);
39-
if(in_array($v_user,$user_list)){
40-
$root = '/';
41-
}else{
42-
$root = '/home/'.$v_user;
43-
}
44-
45-
return new \League\Flysystem\Sftp\SftpAdapter([
46+
return new \League\Flysystem\Sftp\SftpAdapter([
4647
'host' => '127.0.0.1',
4748
'port' => intval($_SESSION['SFTP_PORT']),
4849
'username' => basename($v_user),
@@ -51,7 +52,7 @@
5152
'timeout' => 10,
5253
'directoryPerm' => 0755,
5354
]);
54-
};
55+
};
5556

5657
$dist_config['services']['Filegator\Services\Archiver\ArchiverInterface'] = [
5758
'handler' => '\Filegator\Services\Archiver\Adapters\HestiaZipArchiver',

0 commit comments

Comments
 (0)