Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Dec 24, 2024
1 parent dc596fa commit bcf1457
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 10 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Deploy to question.sh

on:
push:
branches:
- main # Adjust if necessary

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Setup SSH key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_KEY }}

- name: Deploy code to VPS
run: |
# Define server connection, user, and path to the app
SERVER_USER="root"
SERVER_HOST="question.sh"
APP_DIR="/opt/question-sh"
# SSH into the VPS, pull the latest code, and restart
ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST "
cd $APP_DIR && git pull && /root/.bun/bin/bun install && systemctl restart question-sh
"
55 changes: 45 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,14 +707,18 @@ Available Commands:
}
}
}
const HOST_KEY_PATH =
process.env.NODE_ENV === "production"
? "/app/storage/host.key"
: "./host.key";
const HOST_KEY_PATH = "./host.key";
// Modify the server startup code
const PORT = process.env.PORT ?? 2222;

const server = new SSH2.Server(
{
hostKeys: [readFileSync(HOST_KEY_PATH)],
bind: {
port: PORT,
host: "0.0.0.0",
family: 4, // Force IPv4
},
},
async (client) => {
console.log("New client connection established");
Expand Down Expand Up @@ -909,18 +913,49 @@ async function testDatabaseConnection() {
}
}

// Modify the server startup code
const PORT = process.env.PORT ?? 2222;

// Wrap the server startup in an async function
async function startServer() {
await testDatabaseConnection();

server.listen(PORT, "0.0.0.0", () => {
console.log(`SSH server running on port ${PORT}`);
return new Promise((resolve, reject) => {
console.log(`Attempting to bind to port ${PORT}...`);

try {
// Add error event listener before calling listen
server.on("error", (err) => {
console.error("Server error:", {
code: err.code,
message: err.message,
stack: err.stack,
});
reject(err);
});

server.on("listening", () => {
const address = server.address();
console.log("Server listening event triggered", address);
});

console.log("Calling server.listen...");
server.listen(PORT, "0.0.0.0", () => {
const address = server.address();
console.log("Listen callback triggered");

if (!address) {
console.error("Server failed to bind to an address");
process.exit(1);
}

console.log(`SSH server running on port ${PORT}`);
console.log(`Full address info:`, address);
resolve(true);
});
} catch (error) {
console.error("Caught error during server start:", error);
reject(error);
}
});
}

// Call the startServer function
startServer().catch((error) => {
console.error("Failed to start server:", error);
Expand Down
160 changes: 160 additions & 0 deletions scripts/setup-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/usr/bin/env bash
#
# Setup script for a brand new Ubuntu 24 VPS to run question.sh on port 22
# while keeping the main system SSH daemon on port 2345.
#
# USAGE:
# 1) Upload this script to your VPS (e.g., scp setup-question-sh.sh user@vps:/tmp/)
# 2) Run it as root: sudo bash /tmp/setup-question-sh.sh
# 3) Adjust firewall settings as needed.
#
# NOTES:
# - This script modifies the default OpenSSH config to listen on port 2345 (for admin).
# - question.sh will replace port 22 for the public "ssh question.sh".
# - Installs PostgreSQL and creates a DB + user for question.sh.
# - Please adjust DB credentials (username/password) as you prefer.

set -e # Exit immediately on error

#######################################
# 1. System Updates & Install Packages
#######################################
echo "Updating system packages..."
apt-get update -y
apt-get upgrade -y

echo "Installing required packages (git, cron, openssh-server, postgresql)..."
apt-get install -y git cron openssh-server unzip postgresql postgresql-contrib

#######################################
# 2. Configure Main SSH Server on Port 2345
#######################################
echo "Configuring existing OpenSSH to run on port 2345 for admin use..."
sed -i 's/^#*Port .*/Port 2345/' /etc/ssh/sshd_config
systemctl restart ssh
echo "Main SSH server is now on port 2345. You can connect via: ssh -p 2345 your-admin-user@your-server"

#######################################
# 3. Install Bun (for running question.sh)
#######################################
echo "Installing Bun..."
curl -fsSL https://bun.sh/install | bash
echo 'export BUN_INSTALL="$HOME/.bun"' >> ~/.bashrc
echo 'export PATH="$BUN_INSTALL/bin:$PATH"' >> ~/.bashrc
source /root/.bashrc

#######################################
# 4. Setup PostgreSQL (user + database)
#######################################
DB_USER="questionsh_user"
DB_PASSWORD="questionsh_password"
DB_NAME="questionsh_db"

echo "Creating PostgreSQL user and database..."
sudo -u postgres psql <<EOF
CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
CREATE DATABASE $DB_NAME;
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF

#######################################
# 5. Clone question.sh Repository
#######################################
REPO_URL="https://github.com/vincelwt/questionsh.git"
APP_DIR="/opt/question-sh"

echo "Fetching question.sh from $REPO_URL into $APP_DIR..."
if [ -d "$APP_DIR" ]; then
echo "Directory $APP_DIR already exists, pulling latest..."
cd "$APP_DIR"
git pull
else
git clone "$REPO_URL" "$APP_DIR"
cd "$APP_DIR"
fi

#######################################
# 6. Create .env File for DB Access
#######################################
echo "Creating .env file with DATABASE_URL..."
cat <<EOF > .env
DATABASE_URL=postgres://$DB_USER:$DB_PASSWORD@localhost:5432/$DB_NAME
NODE_ENV=production
PORT=22
EOF

#######################################
# 7. Generate SSH Host Keys
#######################################
echo "Generating SSH host keys..."
mkdir -p $APP_DIR/storage
ssh-keygen -t rsa -f $APP_DIR/storage/host.key -N ""
chmod 600 $APP_DIR/storage/host.key

#######################################
# 8. Install Dependencies
#######################################
echo "Installing dependencies with Bun..."
bun install

#######################################
# 9. Create a Systemd Service for question.sh on Port 22
#######################################
SERVICE_FILE="/etc/systemd/system/question-sh.service"
echo "Creating systemd service file at $SERVICE_FILE..."
cat <<EOF > "$SERVICE_FILE"
[Unit]
Description=Question.sh SSH server on port 22
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=$APP_DIR
EnvironmentFile=$APP_DIR/.env
ExecStart=/root/.bun/bin/bun run index.ts
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

echo "Reloading systemd, enabling, and starting question-sh service..."
systemctl daemon-reload
systemctl enable question-sh
systemctl start question-sh

#######################################
# 10. Open Firewall (UFW) for ports 22 & 2345
#######################################
if command -v ufw >/dev/null 2>&1; then
echo "Configuring UFW..."
ufw allow 22
ufw allow 2345
ufw --force enable
else
echo "UFW not found or not installed. Skipping firewall setup..."
fi

#######################################
# 11. Final Status
#######################################
echo "
Setup complete!
Main SSH for admin is listening on port 2345.
question.sh is now running on port 22.
You should be able to connect publicly with:
ssh question.sh
Admin access:
ssh -p 2345 root@your-server-domain
To see question.sh logs:
journalctl -u question-sh -f
To stop:
systemctl stop question-sh
To restart:
systemctl restart question-sh
"

0 comments on commit bcf1457

Please sign in to comment.