Skip to content

Commit

Permalink
Merge pull request #6 from DFanso/rebase/dev
Browse files Browse the repository at this point in the history
Rebase/dev
  • Loading branch information
DFanso authored Dec 8, 2024
2 parents 92b42e9 + 58ffb89 commit be5f6a3
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 4 deletions.
119 changes: 119 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Deploy to Production

on:
push:
branches: [ rebase/dev ]
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Install sshpass
run: sudo apt-get install sshpass

- name: Deploy to VM
env:
VM_HOST: ${{ secrets.VM_HOST }}
VM_PASSWORD: ${{ secrets.VM_PASSWORD }}
DEPLOY_PATH: "/root/dfanso-tunnel-server"
SSHPASS: ${{ secrets.VM_PASSWORD }}
run: |
echo "Starting deployment..."
echo "Creating deploy directory..."
sshpass -e ssh -o StrictHostKeyChecking=no root@${{ secrets.VM_HOST }} "mkdir -p $DEPLOY_PATH"
echo "Copying files to VM..."
sshpass -e rsync -azP --delete \
-e "ssh -o StrictHostKeyChecking=no" \
--exclude '.git' \
--exclude 'node_modules' \
./ root@${{ secrets.VM_HOST }}:$DEPLOY_PATH/
echo "Setting up application..."
sshpass -e ssh -o StrictHostKeyChecking=no root@${{ secrets.VM_HOST }} "cd $DEPLOY_PATH && \
echo '=== Checking Node.js installation ===' && \
if ! command -v node &> /dev/null; then
echo 'Installing Node.js...' && \
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y nodejs && \
echo 'Node.js installation complete:' && \
node --version && \
npm --version
else
echo 'Node.js is already installed:' && \
node --version && \
npm --version
fi && \
echo '=== Installing PM2 ===' && \
npm install -g pm2@latest && \
echo '=== Installing dependencies ===' && \
npm ci --production && \
echo '=== Creating ecosystem config ===' && \
echo 'module.exports = {
apps: [{
name: \"dfanso-tunnel\",
script: \"./dist/index.js\",
instances: 1,
autorestart: true,
watch: true,
max_memory_restart: \"1G\",
env: {
NODE_ENV: \"${{ secrets.NODE_ENV }}\",
DOMAIN: \"${{ secrets.DOMAIN }}\",
WS_PORT: \"${{ secrets.WS_PORT }}\",
HTTP_PORT: \"${{ secrets.HTTP_PORT }}\",
HTTPS_PORT: \"${{ secrets.HTTPS_PORT }}\",
SSL_DIR: \"${{ secrets.SSL_DIR }}\"
}
}]
};' > ecosystem.config.js && \
echo '=== Setting up PM2 ===' && \
export PATH=$PATH:/usr/local/bin:/root/.npm-global/bin && \
echo 'Stopping any existing processes...' && \
pm2 kill || true && \
echo 'Starting application...' && \
if ! pm2 start ecosystem.config.js; then
echo 'Failed to start PM2 process' && \
pm2 logs dfanso-tunnel --lines 50 && \
exit 1
fi && \
echo 'Setting up PM2 startup...' && \
pm2 save && \
env PATH=$PATH:/usr/local/bin:/root/.npm-global/bin pm2 startup systemd -u root --hp /root && \
systemctl enable pm2-root && \
echo 'Checking process status...' && \
if ! pm2 pid dfanso-tunnel > /dev/null; then
echo 'PM2 process is not running' && \
pm2 logs dfanso-tunnel --lines 50 && \
exit 1
fi && \
echo '=== Final Status ===' && \
echo 'PM2 process list:' && \
pm2 list \
"
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Internet (HTTPS) -> Tunnel Server (dfanso.dev) -> WebSocket -> Local Server
- Node.js 16+
- Let's Encrypt SSL certificate
- Domain with wildcard DNS (*.dfanso.dev)
- Add a CNAME record for your domain to point to the tunnel server

### Installation

Expand Down Expand Up @@ -58,6 +59,26 @@ sudo apt-get install certbot
sudo certbot certonly --manual --preferred-challenges dns -d *.dfanso.dev -d dfanso.dev
```


```bash
# First install the Cloudflare certbot plugin
sudo apt install python3-certbot-dns-cloudflare # for Ubuntu/Debian

# Create a Cloudflare API token configuration file
sudo mkdir -p /etc/cloudflare
sudo nano /etc/cloudflare/cloudflare.ini

# Add these lines to cloudflare.ini:
# dns_cloudflare_email = [email protected]
# dns_cloudflare_api_key = your-global-api-key

# Secure the file
sudo chmod 600 /etc/cloudflare/cloudflare.ini

# Then run certbot with the Cloudflare plugin
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials /etc/cloudflare/cloudflare.ini -d *.dfanso.dev -d dfanso.dev
```

3. Follow certbot instructions to add DNS TXT records

### Running the Server
Expand All @@ -81,7 +102,7 @@ The recommended way to connect to this tunnel server is using our official npm p

1. Install the package:
```bash
npm install @dfanso/tunnel-client
npm install @dfanso/tunnel-client@1.0.2
```

2. Basic usage:
Expand Down
11 changes: 8 additions & 3 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ module.exports = {
apps: [{
name: 'dfanso-tunnel',
script: './dist/index.js',
instances: 1,
instances: 2,
autorestart: true,
watch: false,
watch: true,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production'
NODE_ENV: 'production',
DOMAIN: 'dfanso.dev',
WS_PORT: '8080',
HTTP_PORT: '80',
HTTPS_PORT: '443',
SSL_DIR: '/etc/letsencrypt/live/dfanso.dev'
}
}]
};

0 comments on commit be5f6a3

Please sign in to comment.