File size: 2,982 Bytes
b92ee48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/sh

# Print the current hostname
echo "* The hostname of this container is: $(cat /etc/hostname)"
echo "* The host of this container is: $(cat /etc/hosts)"
echo "* ID of the user running the script: $(id -u) * Group: $(id -g) * Status of Admin : $(id admin)"

# Check if SSH host keys are present, if not generate them in a writable location
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
    echo "* Generating SSH host keys *"
    mkdir -p /app/ssh
    ssh-keygen -t rsa -b 2048 -f /app/ssh/ssh_host_rsa_key -N ""
    ssh-keygen -t ecdsa -b 256 -f /app/ssh/ssh_host_ecdsa_key -N ""
    ssh-keygen -t ed25519 -f /app/ssh/ssh_host_ed25519_key -N ""
    mv /app/ssh/ssh_host_* /etc/ssh/
fi

# Check if SSH service is running
if ! pgrep -x "sshd" > /dev/null; then
    echo "* Starting SSH server on port : 2222 *"
    #/usr/sbin/sshd
    /usr/sbin/sshd -p 2222
else
    echo "* SSH server is already running. *"
fi

# Check if admin user exists
if ! id -u admin &>/dev/null; then
    adduser -D admin
    echo "admin:password" | chpasswd
fi

# Generate SSH keys for admin user if not already present
if [ ! -f /home/admin/.ssh/id_rsa ]; then
    echo "* Generating SSH key for admin user *"
    mkdir -p /home/admin/.ssh
    ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -q -N ""
    chown -R admin:admin /home/admin/.ssh
    chmod 700 /home/admin/.ssh
    chmod 600 /home/admin/.ssh/id_rsa
    chmod 644 /home/admin/.ssh/id_rsa.pub
fi

# Add public key to authorized_keys for admin user
if [ ! -f /home/admin/.ssh/authorized_keys ]; then
    cat /home/admin/.ssh/id_rsa.pub >> /home/admin/.ssh/authorized_keys
    chmod 600 /home/admin/.ssh/authorized_keys
    chown admin:admin /home/admin/.ssh/authorized_keys
fi

#echo "* Contents of id_rsa:"
#cat /home/admin/.ssh/id_rsa

# Test SSH login locally
#ssh -o StrictHostKeyChecking=no -i /home/admin/.ssh/id_rsa admin@127.0.0.1 -p 2222 exit

#if [ $? -eq 0 ]; then
#    echo "* Admin SSH key-based login successful."
#else
#    echo "* Admin SSH key-based login failed!" >&2
#fi

echo "* Status of SSH service:"
netstat -tuln 

# Test if admin's credentials are correct
#echo "* Testing admin's SSH login locally *"
#ssh -o StrictHostKeyChecking=no -i /home/admin/.ssh/id_rsa admin@127.0.0.1 -p 2222 exit

#if [ $? -eq 0 ]; then
#    echo "* Admin credentials are valid."
#else
#    echo "* Admin login failed! Check the password for 'admin' user." >&2
#fi

# Activate virtual environment
source /app/venv/bin/activate

# Set working directory for the application
cd /app

# Print list of the contents inside this directory
echo "* Contents of /app directory: *"
ls -la /app

python3 -u -m WebSSH

#wssh --address='0.0.0.0' --port=7860 --xsrf=False --debug=True --maxconn=4 --policy=autoadd
#wssh --address='0.0.0.0' --port=7860 --xsrf=False --debug=True --maxconn=4 --policy=reject
#wssh --address='0.0.0.0' --port=7860 --xsrf=False --debug=True --maxconn=4  --policy=accept
# Keep the container running
tail -f /dev/null