forked from nexhacks-proj/nexhacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-connection.ts
More file actions
160 lines (138 loc) Β· 5.7 KB
/
Copy pathtest-connection.ts
File metadata and controls
160 lines (138 loc) Β· 5.7 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env tsx
/**
* Diagnostic script to test MongoDB connection with detailed error info
*/
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://elgooseman321_db_user:VkOrkGwTYnu64tuz@maincluster.ndr3cps.mongodb.net/swipehire?retryWrites=true&w=majority&appName=MainCluster'
async function testConnection() {
console.log('π MongoDB Connection Diagnostics\n')
console.log(`Connection String: ${MONGODB_URI.replace(/:[^:@]+@/, ':***@')}\n`)
// Test 1: Basic connection
console.log('Test 1: Basic Connection')
console.log('βββββββββββββββββββββββββββββββββ')
try {
const conn = await mongoose.connect(MONGODB_URI, {
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
})
console.log('β
Connected successfully!')
console.log(` Database: ${conn.connection.db?.databaseName}`)
console.log(` Host: ${conn.connection.host}`)
console.log(` Port: ${conn.connection.port}`)
await mongoose.disconnect()
return true
} catch (error: any) {
console.log('β Connection failed\n')
console.log('Error details:')
console.log(` Message: ${error.message}`)
if (error.name === 'MongooseServerSelectionError') {
console.log('\nπ Possible Issues:')
console.log(' 1. IP not whitelisted (wait 2-3 min after adding)')
console.log(' 2. Network firewall blocking MongoDB ports (27017, 443)')
console.log(' 3. School/corporate WiFi blocking database connections')
console.log(' 4. VPN interfering with connection')
console.log(' 5. Incorrect username/password')
}
if (error.cause?.reason) {
const reason = error.cause.reason
console.log(`\n Server Status: ${reason.type}`)
if (reason.servers) {
console.log(` Servers attempted: ${reason.servers.size}`)
}
}
// Check if it's a network/firewall issue
const errorMsg = error.message.toLowerCase()
if (errorMsg.includes('timeout') || errorMsg.includes('enotfound') || errorMsg.includes('econnrefused')) {
console.log('\nπ« NETWORK/FIREWALL ISSUE DETECTED')
console.log(' This looks like a network blocking issue.')
console.log(' Solutions:')
console.log(' β’ Try a different network (mobile hotspot, home WiFi)')
console.log(' β’ Disable VPN if active')
console.log(' β’ Contact network admin about MongoDB Atlas (ports 443/27017)')
console.log(' β’ Use MongoDB Realm Functions as proxy (advanced)')
}
return false
}
}
// Test 2: Check if we can reach the domain
async function testDNS() {
console.log('\n\nTest 2: DNS Resolution')
console.log('βββββββββββββββββββββββββββββββββ')
const dns = await import('dns').then(m => m.promises)
try {
const hostname = 'maincluster.ndr3cps.mongodb.net'
// Try A record (IPv4)
try {
const addresses = await dns.resolve4(hostname)
console.log(`β
A record resolved: ${hostname}`)
console.log(` IP addresses: ${addresses.join(', ')}`)
} catch (e: any) {
console.log(`β A record lookup failed: ${e.message}`)
}
// Try SRV record (used by mongodb+srv://)
try {
const srv = await dns.resolveSrv(`_mongodb._tcp.${hostname}`)
console.log(`β
SRV record resolved:`)
srv.forEach(record => {
console.log(` ${record.name}:${record.port} (priority: ${record.priority})`)
})
return true
} catch (e: any) {
console.log(`β SRV record lookup failed: ${e.message}`)
console.log(` β οΈ This is critical - mongodb+srv:// requires SRV records!`)
}
return false
} catch (error: any) {
console.log(`β DNS lookup failed: ${error.message}`)
return false
}
}
// Test 3: Try with different connection options
async function testConnectionOptions() {
console.log('\n\nTest 3: Alternative Connection Options')
console.log('βββββββββββββββββββββββββββββββββ')
const testOptions = [
{ name: 'Standard', opts: {} },
{ name: 'Short timeout', opts: { serverSelectionTimeoutMS: 3000 } },
{ name: 'Long timeout', opts: { serverSelectionTimeoutMS: 15000 } },
]
for (const test of testOptions) {
try {
console.log(`Trying: ${test.name}...`)
await mongoose.connect(MONGODB_URI, {
...test.opts,
bufferCommands: false,
})
console.log(`β
${test.name} succeeded!`)
await mongoose.disconnect()
return true
} catch (error: any) {
console.log(`β ${test.name} failed: ${error.message.substring(0, 60)}...`)
}
}
return false
}
async function runDiagnostics() {
const dnsOk = await testDNS()
const basicOk = await testConnection()
if (!basicOk && dnsOk) {
console.log('\n\nπ‘ Since DNS works but connection fails, this is likely:')
console.log(' β’ Network firewall blocking MongoDB')
console.log(' β’ School/corporate WiFi restrictions')
console.log(' β’ Try mobile hotspot to confirm')
}
if (!dnsOk) {
console.log('\n\nπ‘ DNS resolution failed - check internet connection')
}
if (basicOk) {
console.log('\n\nπ Connection works! Your setup is correct.')
} else {
console.log('\n\nπ Quick Fixes to Try:')
console.log(' 1. Switch to mobile hotspot')
console.log(' 2. Disable VPN')
console.log(' 3. Try from home network')
console.log(' 4. Verify IP whitelist includes your current IP')
console.log(' 5. Double-check username/password in connection string')
}
}
runDiagnostics().catch(console.error)