File size: 3,432 Bytes
24bf580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash

# Start tmate in a socket
echo "Starting tmate..."
tmate -S /tmp/tmate.sock new-session -d

# Wait for tmate to connect and get the SSH connection string
echo "Waiting for tmate to generate SSH URL..."
for i in {1..30}; do
  SSH_CONN=$(tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}' 2>/dev/null)
  if [ -n "$SSH_CONN" ]; then
    echo "============================================="
    echo "SSH connection string: $SSH_CONN"
    echo "============================================="
    break
  fi
  sleep 1
done

# If SSH_CONN is empty, print error
if [ -z "$SSH_CONN" ]; then
  echo "Failed to get tmate SSH connection string."
  SSH_CONN="Error: Could not retrieve tmate SSH connection string."
fi

# Create a simple web page to display the SSH connection details
cat <<EOF > index.html
<!DOCTYPE html>
<html>
<head>
    <title>SSH Tunnel Connection Details</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #0f172a;
            color: #f8fafc;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #1e293b;
            padding: 2.5rem;
            border-radius: 12px;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
            text-align: center;
            max-width: 600px;
            width: 90%;
            border: 1px solid #334155;
        }
        h1 {
            color: #38bdf8;
            margin-top: 0;
            font-size: 1.8rem;
        }
        p {
            font-size: 1rem;
            color: #94a3b8;
            line-height: 1.5;
        }
        .code-block {
            background-color: #090d16;
            color: #34d399;
            padding: 1rem;
            border-radius: 6px;
            font-family: monospace;
            font-size: 1.1rem;
            word-break: break-all;
            margin: 1.5rem 0;
            border: 1px solid #1e293b;
            user-select: all;
            cursor: pointer;
        }
        .instructions {
            text-align: left;
            margin-top: 2rem;
            border-top: 1px solid #334155;
            padding-top: 1.5rem;
        }
        .instructions h3 {
            color: #cbd5e1;
            margin-bottom: 0.5rem;
        }
        .instructions ol {
            color: #94a3b8;
            padding-left: 1.2rem;
            margin-top: 0.5rem;
        }
        .instructions li {
            margin-bottom: 0.5rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>SSH Access Enabled</h1>
        <p>Your Hugging Face Space Docker container is running and has established a secure tunnel via tmate.</p>
        <p>Click below to select and copy the SSH command:</p>
        <div class="code-block">${SSH_CONN}</div>
        <div class="instructions">
            <h3>How to connect:</h3>
            <ol>
                <li>Copy the command above.</li>
                <li>Paste it in your terminal and press Enter.</li>
                <li>You will have direct shell access to the Hugging Face Space container.</li>
            </ol>
        </div>
    </div>
</body>
</html>
EOF

# Start a simple Python web server to serve the page on port 7860
echo "Starting web server on port 7860..."
python3 -m http.server 7860