Skip to content

Commit be5f6a3

Browse files
authored
Merge pull request #6 from DFanso/rebase/dev
Rebase/dev
2 parents 92b42e9 + 58ffb89 commit be5f6a3

File tree

3 files changed

+149
-4
lines changed

3 files changed

+149
-4
lines changed

.github/workflows/deploy.yml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
name: Deploy to Production
2+
3+
on:
4+
push:
5+
branches: [ rebase/dev ]
6+
workflow_dispatch:
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: '20'
19+
cache: 'npm'
20+
21+
- name: Install dependencies
22+
run: npm ci
23+
24+
- name: Build
25+
run: npm run build
26+
27+
- name: Install sshpass
28+
run: sudo apt-get install sshpass
29+
30+
- name: Deploy to VM
31+
env:
32+
VM_HOST: ${{ secrets.VM_HOST }}
33+
VM_PASSWORD: ${{ secrets.VM_PASSWORD }}
34+
DEPLOY_PATH: "/root/dfanso-tunnel-server"
35+
SSHPASS: ${{ secrets.VM_PASSWORD }}
36+
run: |
37+
echo "Starting deployment..."
38+
39+
echo "Creating deploy directory..."
40+
sshpass -e ssh -o StrictHostKeyChecking=no root@${{ secrets.VM_HOST }} "mkdir -p $DEPLOY_PATH"
41+
42+
echo "Copying files to VM..."
43+
sshpass -e rsync -azP --delete \
44+
-e "ssh -o StrictHostKeyChecking=no" \
45+
--exclude '.git' \
46+
--exclude 'node_modules' \
47+
./ root@${{ secrets.VM_HOST }}:$DEPLOY_PATH/
48+
49+
echo "Setting up application..."
50+
sshpass -e ssh -o StrictHostKeyChecking=no root@${{ secrets.VM_HOST }} "cd $DEPLOY_PATH && \
51+
echo '=== Checking Node.js installation ===' && \
52+
if ! command -v node &> /dev/null; then
53+
echo 'Installing Node.js...' && \
54+
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
55+
apt-get install -y nodejs && \
56+
echo 'Node.js installation complete:' && \
57+
node --version && \
58+
npm --version
59+
else
60+
echo 'Node.js is already installed:' && \
61+
node --version && \
62+
npm --version
63+
fi && \
64+
65+
echo '=== Installing PM2 ===' && \
66+
npm install -g pm2@latest && \
67+
68+
echo '=== Installing dependencies ===' && \
69+
npm ci --production && \
70+
71+
echo '=== Creating ecosystem config ===' && \
72+
echo 'module.exports = {
73+
apps: [{
74+
name: \"dfanso-tunnel\",
75+
script: \"./dist/index.js\",
76+
instances: 1,
77+
autorestart: true,
78+
watch: true,
79+
max_memory_restart: \"1G\",
80+
env: {
81+
NODE_ENV: \"${{ secrets.NODE_ENV }}\",
82+
DOMAIN: \"${{ secrets.DOMAIN }}\",
83+
WS_PORT: \"${{ secrets.WS_PORT }}\",
84+
HTTP_PORT: \"${{ secrets.HTTP_PORT }}\",
85+
HTTPS_PORT: \"${{ secrets.HTTPS_PORT }}\",
86+
SSL_DIR: \"${{ secrets.SSL_DIR }}\"
87+
}
88+
}]
89+
};' > ecosystem.config.js && \
90+
91+
echo '=== Setting up PM2 ===' && \
92+
export PATH=$PATH:/usr/local/bin:/root/.npm-global/bin && \
93+
94+
echo 'Stopping any existing processes...' && \
95+
pm2 kill || true && \
96+
97+
echo 'Starting application...' && \
98+
if ! pm2 start ecosystem.config.js; then
99+
echo 'Failed to start PM2 process' && \
100+
pm2 logs dfanso-tunnel --lines 50 && \
101+
exit 1
102+
fi && \
103+
104+
echo 'Setting up PM2 startup...' && \
105+
pm2 save && \
106+
env PATH=$PATH:/usr/local/bin:/root/.npm-global/bin pm2 startup systemd -u root --hp /root && \
107+
systemctl enable pm2-root && \
108+
109+
echo 'Checking process status...' && \
110+
if ! pm2 pid dfanso-tunnel > /dev/null; then
111+
echo 'PM2 process is not running' && \
112+
pm2 logs dfanso-tunnel --lines 50 && \
113+
exit 1
114+
fi && \
115+
116+
echo '=== Final Status ===' && \
117+
echo 'PM2 process list:' && \
118+
pm2 list \
119+
"

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Internet (HTTPS) -> Tunnel Server (dfanso.dev) -> WebSocket -> Local Server
2424
- Node.js 16+
2525
- Let's Encrypt SSL certificate
2626
- Domain with wildcard DNS (*.dfanso.dev)
27+
- Add a CNAME record for your domain to point to the tunnel server
2728

2829
### Installation
2930

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

62+
63+
```bash
64+
# First install the Cloudflare certbot plugin
65+
sudo apt install python3-certbot-dns-cloudflare # for Ubuntu/Debian
66+
67+
# Create a Cloudflare API token configuration file
68+
sudo mkdir -p /etc/cloudflare
69+
sudo nano /etc/cloudflare/cloudflare.ini
70+
71+
# Add these lines to cloudflare.ini:
72+
# dns_cloudflare_email = [email protected]
73+
# dns_cloudflare_api_key = your-global-api-key
74+
75+
# Secure the file
76+
sudo chmod 600 /etc/cloudflare/cloudflare.ini
77+
78+
# Then run certbot with the Cloudflare plugin
79+
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials /etc/cloudflare/cloudflare.ini -d *.dfanso.dev -d dfanso.dev
80+
```
81+
6182
3. Follow certbot instructions to add DNS TXT records
6283

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

82103
1. Install the package:
83104
```bash
84-
npm install @dfanso/tunnel-client
105+
npm install @dfanso/tunnel-client@1.0.2
85106
```
86107

87108
2. Basic usage:

ecosystem.config.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ module.exports = {
22
apps: [{
33
name: 'dfanso-tunnel',
44
script: './dist/index.js',
5-
instances: 1,
5+
instances: 2,
66
autorestart: true,
7-
watch: false,
7+
watch: true,
88
max_memory_restart: '1G',
99
env: {
10-
NODE_ENV: 'production'
10+
NODE_ENV: 'production',
11+
DOMAIN: 'dfanso.dev',
12+
WS_PORT: '8080',
13+
HTTP_PORT: '80',
14+
HTTPS_PORT: '443',
15+
SSL_DIR: '/etc/letsencrypt/live/dfanso.dev'
1116
}
1217
}]
1318
};

0 commit comments

Comments
 (0)