Skip to content

Commit bcf1457

Browse files
committed
add
1 parent dc596fa commit bcf1457

File tree

3 files changed

+236
-10
lines changed

3 files changed

+236
-10
lines changed

.github/workflows/deploy.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Deploy to question.sh
2+
3+
on:
4+
push:
5+
branches:
6+
- main # Adjust if necessary
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Check out repository
14+
uses: actions/checkout@v3
15+
16+
- name: Setup SSH key
17+
uses: webfactory/ssh-agent@v0.5.4
18+
with:
19+
ssh-private-key: ${{ secrets.SSH_KEY }}
20+
21+
- name: Deploy code to VPS
22+
run: |
23+
# Define server connection, user, and path to the app
24+
SERVER_USER="root"
25+
SERVER_HOST="question.sh"
26+
APP_DIR="/opt/question-sh"
27+
28+
# SSH into the VPS, pull the latest code, and restart
29+
ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST "
30+
cd $APP_DIR && git pull && /root/.bun/bin/bun install && systemctl restart question-sh
31+
"

index.ts

+45-10
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,18 @@ Available Commands:
707707
}
708708
}
709709
}
710-
const HOST_KEY_PATH =
711-
process.env.NODE_ENV === "production"
712-
? "/app/storage/host.key"
713-
: "./host.key";
710+
const HOST_KEY_PATH = "./host.key";
711+
// Modify the server startup code
712+
const PORT = process.env.PORT ?? 2222;
714713

715714
const server = new SSH2.Server(
716715
{
717716
hostKeys: [readFileSync(HOST_KEY_PATH)],
717+
bind: {
718+
port: PORT,
719+
host: "0.0.0.0",
720+
family: 4, // Force IPv4
721+
},
718722
},
719723
async (client) => {
720724
console.log("New client connection established");
@@ -909,18 +913,49 @@ async function testDatabaseConnection() {
909913
}
910914
}
911915

912-
// Modify the server startup code
913-
const PORT = process.env.PORT ?? 2222;
914-
915916
// Wrap the server startup in an async function
916917
async function startServer() {
917918
await testDatabaseConnection();
918919

919-
server.listen(PORT, "0.0.0.0", () => {
920-
console.log(`SSH server running on port ${PORT}`);
920+
return new Promise((resolve, reject) => {
921+
console.log(`Attempting to bind to port ${PORT}...`);
922+
923+
try {
924+
// Add error event listener before calling listen
925+
server.on("error", (err) => {
926+
console.error("Server error:", {
927+
code: err.code,
928+
message: err.message,
929+
stack: err.stack,
930+
});
931+
reject(err);
932+
});
933+
934+
server.on("listening", () => {
935+
const address = server.address();
936+
console.log("Server listening event triggered", address);
937+
});
938+
939+
console.log("Calling server.listen...");
940+
server.listen(PORT, "0.0.0.0", () => {
941+
const address = server.address();
942+
console.log("Listen callback triggered");
943+
944+
if (!address) {
945+
console.error("Server failed to bind to an address");
946+
process.exit(1);
947+
}
948+
949+
console.log(`SSH server running on port ${PORT}`);
950+
console.log(`Full address info:`, address);
951+
resolve(true);
952+
});
953+
} catch (error) {
954+
console.error("Caught error during server start:", error);
955+
reject(error);
956+
}
921957
});
922958
}
923-
924959
// Call the startServer function
925960
startServer().catch((error) => {
926961
console.error("Failed to start server:", error);

scripts/setup-server.sh

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Setup script for a brand new Ubuntu 24 VPS to run question.sh on port 22
4+
# while keeping the main system SSH daemon on port 2345.
5+
#
6+
# USAGE:
7+
# 1) Upload this script to your VPS (e.g., scp setup-question-sh.sh user@vps:/tmp/)
8+
# 2) Run it as root: sudo bash /tmp/setup-question-sh.sh
9+
# 3) Adjust firewall settings as needed.
10+
#
11+
# NOTES:
12+
# - This script modifies the default OpenSSH config to listen on port 2345 (for admin).
13+
# - question.sh will replace port 22 for the public "ssh question.sh".
14+
# - Installs PostgreSQL and creates a DB + user for question.sh.
15+
# - Please adjust DB credentials (username/password) as you prefer.
16+
17+
set -e # Exit immediately on error
18+
19+
#######################################
20+
# 1. System Updates & Install Packages
21+
#######################################
22+
echo "Updating system packages..."
23+
apt-get update -y
24+
apt-get upgrade -y
25+
26+
echo "Installing required packages (git, cron, openssh-server, postgresql)..."
27+
apt-get install -y git cron openssh-server unzip postgresql postgresql-contrib
28+
29+
#######################################
30+
# 2. Configure Main SSH Server on Port 2345
31+
#######################################
32+
echo "Configuring existing OpenSSH to run on port 2345 for admin use..."
33+
sed -i 's/^#*Port .*/Port 2345/' /etc/ssh/sshd_config
34+
systemctl restart ssh
35+
echo "Main SSH server is now on port 2345. You can connect via: ssh -p 2345 your-admin-user@your-server"
36+
37+
#######################################
38+
# 3. Install Bun (for running question.sh)
39+
#######################################
40+
echo "Installing Bun..."
41+
curl -fsSL https://bun.sh/install | bash
42+
echo 'export BUN_INSTALL="$HOME/.bun"' >> ~/.bashrc
43+
echo 'export PATH="$BUN_INSTALL/bin:$PATH"' >> ~/.bashrc
44+
source /root/.bashrc
45+
46+
#######################################
47+
# 4. Setup PostgreSQL (user + database)
48+
#######################################
49+
DB_USER="questionsh_user"
50+
DB_PASSWORD="questionsh_password"
51+
DB_NAME="questionsh_db"
52+
53+
echo "Creating PostgreSQL user and database..."
54+
sudo -u postgres psql <<EOF
55+
CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
56+
CREATE DATABASE $DB_NAME;
57+
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
58+
EOF
59+
60+
#######################################
61+
# 5. Clone question.sh Repository
62+
#######################################
63+
REPO_URL="https://github.com/vincelwt/questionsh.git"
64+
APP_DIR="/opt/question-sh"
65+
66+
echo "Fetching question.sh from $REPO_URL into $APP_DIR..."
67+
if [ -d "$APP_DIR" ]; then
68+
echo "Directory $APP_DIR already exists, pulling latest..."
69+
cd "$APP_DIR"
70+
git pull
71+
else
72+
git clone "$REPO_URL" "$APP_DIR"
73+
cd "$APP_DIR"
74+
fi
75+
76+
#######################################
77+
# 6. Create .env File for DB Access
78+
#######################################
79+
echo "Creating .env file with DATABASE_URL..."
80+
cat <<EOF > .env
81+
DATABASE_URL=postgres://$DB_USER:$DB_PASSWORD@localhost:5432/$DB_NAME
82+
NODE_ENV=production
83+
PORT=22
84+
EOF
85+
86+
#######################################
87+
# 7. Generate SSH Host Keys
88+
#######################################
89+
echo "Generating SSH host keys..."
90+
mkdir -p $APP_DIR/storage
91+
ssh-keygen -t rsa -f $APP_DIR/storage/host.key -N ""
92+
chmod 600 $APP_DIR/storage/host.key
93+
94+
#######################################
95+
# 8. Install Dependencies
96+
#######################################
97+
echo "Installing dependencies with Bun..."
98+
bun install
99+
100+
#######################################
101+
# 9. Create a Systemd Service for question.sh on Port 22
102+
#######################################
103+
SERVICE_FILE="/etc/systemd/system/question-sh.service"
104+
echo "Creating systemd service file at $SERVICE_FILE..."
105+
cat <<EOF > "$SERVICE_FILE"
106+
[Unit]
107+
Description=Question.sh SSH server on port 22
108+
After=network.target
109+
110+
[Service]
111+
Type=simple
112+
User=root
113+
WorkingDirectory=$APP_DIR
114+
EnvironmentFile=$APP_DIR/.env
115+
ExecStart=/root/.bun/bin/bun run index.ts
116+
Restart=on-failure
117+
118+
[Install]
119+
WantedBy=multi-user.target
120+
EOF
121+
122+
echo "Reloading systemd, enabling, and starting question-sh service..."
123+
systemctl daemon-reload
124+
systemctl enable question-sh
125+
systemctl start question-sh
126+
127+
#######################################
128+
# 10. Open Firewall (UFW) for ports 22 & 2345
129+
#######################################
130+
if command -v ufw >/dev/null 2>&1; then
131+
echo "Configuring UFW..."
132+
ufw allow 22
133+
ufw allow 2345
134+
ufw --force enable
135+
else
136+
echo "UFW not found or not installed. Skipping firewall setup..."
137+
fi
138+
139+
#######################################
140+
# 11. Final Status
141+
#######################################
142+
echo "
143+
Setup complete!
144+
Main SSH for admin is listening on port 2345.
145+
question.sh is now running on port 22.
146+
147+
You should be able to connect publicly with:
148+
ssh question.sh
149+
150+
Admin access:
151+
ssh -p 2345 root@your-server-domain
152+
153+
To see question.sh logs:
154+
journalctl -u question-sh -f
155+
156+
To stop:
157+
systemctl stop question-sh
158+
To restart:
159+
systemctl restart question-sh
160+
"

0 commit comments

Comments
 (0)