Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): Listen only to 127.0.0.1 by default #2977

Merged
merged 12 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(server): Listen only to 127.0.0.1 by default
  • Loading branch information
eirslett committed May 2, 2021
commit c61214c3d2595c76a5f44742b52ea981c8e905a8
46 changes: 27 additions & 19 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,41 @@ export async function preview(
)

const options = config.server || {}
const hostname = options.host || 'localhost'
const hostname = options.host || '127.0.0.1'
const protocol = options.https ? 'https' : 'http'
const logger = config.logger
const base = config.base

httpServer.listen(port, () => {
httpServer.listen(port, hostname, () => {
logger.info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` build preview server running at:\n`)
)
const interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach((key) =>
(interfaces[key] || [])
.filter((details) => details.family === 'IPv4')
.map((detail) => {
return {
type: detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: ',
host: detail.address.replace('127.0.0.1', hostname)
}
})
.forEach(({ type, host }) => {
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
logger.info(` > ${type} ${chalk.cyan(url)}`)
})
)
if (hostname === '127.0.0.1') {
const url = `${protocol}://localhost:${chalk.bold(port)}${base}`
logger.info(` > ${chalk.cyan(url)}`)
logger.info(
` (run Vite with paramater --host 0.0.0.0 to listen to all network interfaces)`
)
} else {
const interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach((key) =>
(interfaces[key] || [])
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
.filter((details) => details.family === 'IPv4')
.map((detail) => {
return {
type: detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: ',
host: detail.address
}
})
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
.forEach(({ type, host }) => {
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
logger.info(` > ${type} ${chalk.cyan(url)}`)
})
)
}

if (options.open) {
const path = typeof options.open === 'string' ? options.open : base
Expand Down
50 changes: 29 additions & 21 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,7 @@ async function startServer(

const options = server.config.server || {}
let port = inlinePort || options.port || 3000
let hostname = options.host || 'localhost'
if (hostname === '0.0.0.0') hostname = 'localhost'
const hostname = options.host || '127.0.0.1'
const protocol = options.https ? 'https' : 'http'
const info = server.config.logger.info
const base = server.config.base
Expand All @@ -545,7 +544,7 @@ async function startServer(
reject(new Error(`Port ${port} is already in use`))
} else {
info(`Port ${port} is in use, trying another one...`)
httpServer.listen(++port)
httpServer.listen(++port, hostname)
}
} else {
httpServer.removeListener('error', onError)
Expand All @@ -555,7 +554,7 @@ async function startServer(

httpServer.on('error', onError)

httpServer.listen(port, options.host, () => {
httpServer.listen(port, hostname, () => {
httpServer.removeListener('error', onError)

info(
Expand All @@ -565,23 +564,32 @@ async function startServer(
clear: !server.config.logger.hasWarned
}
)
const interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach((key) =>
(interfaces[key] || [])
.filter((details) => details.family === 'IPv4')
.map((detail) => {
return {
type: detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: ',
host: detail.address.replace('127.0.0.1', hostname)
}
})
.forEach(({ type, host }) => {
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
info(` > ${type} ${chalk.cyan(url)}`)
})
)

if (hostname === '127.0.0.1') {
const url = `${protocol}://localhost:${chalk.bold(port)}${base}`
info(` > ${chalk.cyan(url)}`)
info(
` (run Vite with paramater --host 0.0.0.0 to listen to all network interfaces)`
)
} else {
const interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach((key) =>
(interfaces[key] || [])
.filter((details) => details.family === 'IPv4')
.map((detail) => {
return {
type: detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: ',
host: detail.address
}
})
.forEach(({ type, host }) => {
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
info(` > ${type} ${chalk.cyan(url)}`)
})
)
}

// @ts-ignore
if (global.__vite_start_time) {
Expand Down