-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (127 loc) · 4.12 KB
/
index.js
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
#!/usr/bin/env node
const fs = require('fs')
const os = require('os')
const path = require('path')
const sudo = require('sudo-prompt')
const { exec } = require('child_process')
const args = process.argv.slice(2)
// Regular expression for validating IP address
const ipRegex =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
const ip = args[0]
if (!ipRegex.test(ip)) {
console.error(
'Invalid IP address format. Please provide a valid IPv4 address.'
)
process.exit(1)
}
// Regular expression for validating domain name
const domainRegex =
/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/
const domain = args[1]
if (!domainRegex.test(domain)) {
console.error(
'Invalid domain name format. Please provide a valid domain name.'
)
process.exit(1)
}
const platform = os.platform()
const run = (command) =>
new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error)
return
}
resolve(stdout ? stdout : stderr)
})
})
async function isMkcertInstalled() {
try {
await run('mkcert -version')
return true
} catch {
return false
}
}
async function installMkcert() {
try {
if (await isMkcertInstalled()) {
console.log('mkcert is already installed.')
return
}
let installCmd
if (platform === 'darwin' || platform === 'linux')
installCmd = 'brew install mkcert' // macOS and Linux ( Homebrew )
else if (platform === 'win32')
installCmd = 'choco install mkcert -y' // windows ( Chocolatey )
else throw new Error(`Unsupported platform: ${platform}`)
console.log('Installing mkcert...')
console.log(installCmd)
await run(installCmd)
console.log('mkcert installed successfully.')
} catch (error) {
console.error('Error installing mkcert:', error)
}
}
async function setupCertificates() {
try {
console.log('Installing local CA...')
await run('mkcert -install')
// Create a directory for certificates if it doesn't exist
const certDir = path.join('.', '.cert')
if (!fs.existsSync(certDir)) {
fs.mkdirSync(certDir, { recursive: true })
}
console.log('Generating SSL certificates...')
await run(
`mkcert -key-file "./.cert/key.pem" -cert-file "./.cert/cert.pem" "${domain}"`
)
console.log('Certificates generated successfully.')
} catch (error) {
console.error('Error setting up certificates:', error)
}
}
async function flushDNS() {
try {
let command
if (platform === 'win32') command = 'ipconfig /flushdns' // Windows
else if (platform === 'darwin')
command = 'sudo killall -HUP mDNSResponder' // macOS
else command = 'sudo systemd-resolve --flush-caches' // Linux
await run(command)
console.log('DNS cache flushed successfully.')
} catch (error) {
console.error('Error flushing DNS:', error.message)
}
}
function appendDomainToHostsFile() {
const entry = `${ip} ${domain}`
const filePath =
platform === 'win32'
? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
: '/etc/hosts'
const content = fs.readFileSync(filePath, 'utf-8')
if (content.includes(entry)) {
console.log('Entry already exists in the hosts file.')
return
}
sudo.exec(
`echo ${entry} >> ${filePath}`,
{ name: 'Hosts File Modification' },
(error) => {
if (error) {
console.error('Error modifying the hosts file:', error.message)
} else {
console.log('Entry added to the hosts file successfully.')
flushDNS()
}
}
)
}
async function setup() {
appendDomainToHostsFile()
await installMkcert()
await setupCertificates()
}
setup()