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

change server domain to 0.0.0.0 #163

Merged
merged 1 commit into from
Jun 5, 2024

Conversation

tiezhuli001
Copy link
Contributor

change server domain to 0.0.0.0. allow remote ssh connections to access the http address via the ip:port format.

when the host is set to 0.0.0.0 in net.Listen(), it supports both IPv4 and IPv6. Refer to: golang/go#22811

@tiezhuli001 tiezhuli001 force-pushed the enable-ip-port-access branch from dec7e98 to 97b1f7c Compare May 29, 2024 07:31
@ccoVeille
Copy link
Contributor

ccoVeille commented May 29, 2024

I might be wrong but this change may lead that anyone on the same local network will be able to access your port, while it was not available with localhost.

As I said, I might be wrong, but it's worth trying. And if so, maybe it would be interesting to make this available via a command-line parameters (like --public or whatever)

I'm reporting this because of security concern

@xhd2015
Copy link
Owner

xhd2015 commented May 30, 2024

The default behavior is to follow an npm package called http-server, which defaults to listen on public address.

Since this tool is primarily used in local development, it's better to listen on local address by default. And can be made public by specifying --public.

Good point.

@tiezhuli001
Copy link
Contributor Author

You're right. With regards to security, using the --public parameter certainly seems more prudent. The default is still set to the localhost:port configuration, but I'll make the appropriate changes. Thank you for your advice.

@ccoVeille
Copy link
Contributor

You're right. With regards to security, using the --public parameter certainly seems more prudent. Thank you for your advice.

I faced that issue when implementing something like that on a tool, that's why I raised the point.

Also, @xhd2015 @tiezhuli001 most people won't need the feature to listen on all network ever. So yes, it should be something to pass as argument

By the way, --public was a suggestion, a better name should be found I think.

@tiezhuli001
Copy link
Contributor Author

Yes, thanks for the suggestions. Similar to Python's http.server and Node.js's http-server, I've added --bind and --port as options to set IP and port. The default host remains as localhost.

@ccoVeille
Copy link
Contributor

Perfect, thanks

}
if _, ok := validHosts[bindStr]; ok {
host = bindStr
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So no warning, error reporting here that you will fallback to local host?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree that there's no need to return an error and terminate the process here.
Regardless of the situation, we should open to the default host and port - localhost should always be accessible, right?

}
}
}
fmt.Printf("no network interface found")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one may cause problem

If this method failed to find an IP, you will display an error message, but the user may have provided 0.0.0.0.

So you will use it, but an error will be displayed

It's unlikely to happen I think, but I would expect the local IPs to be tested after the valid one would be tested first

url := fmt.Sprintf("http://%s:%d", host, port)
fmt.Println("Server listen at:")
fmt.Printf("- Local: http://%s:%d \r\n", host, port)
if host == "0.0.0.0" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is valid hereif someone provides a bind to his public IP you won't display it.

You will have to create a function to test if the bind is purely local I think and use icon other method, or copy paste the code here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will refer to the nodejs http-server to print the corresponding URL, like this:

if (argv.a && host !== '0.0.0.0') {
logger.info( ${protocol}${host}:${chalk.green(port.toString())});
} else {
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
logger.info((' ' + protocol + details.address + ':' + chalk.green(port.toString())));
}
});
});
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it only displays something if not listening on everything

@xhd2015
Copy link
Owner

xhd2015 commented Jun 5, 2024

Need to rebase master and squash to one commit

@tiezhuli001 tiezhuli001 force-pushed the enable-ip-port-access branch from 504b81e to b2c3ae1 Compare June 5, 2024 02:44
@tiezhuli001
Copy link
Contributor Author

Yes, it has been merged into one commit, thank you.

@xhd2015 xhd2015 merged commit b2c3ae1 into xhd2015:master Jun 5, 2024
7 checks passed
return
}
// add default router
localIPs = append(localIPs, []string{"0.0.0.0", "::"}...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a real issue

But, I would have written this 😅

        localIPs = append(localIPs, "0.0.0.0", "::")

url := fmt.Sprintf("http://%s:%d", host, port)
fmt.Println("Server listen at:")
fmt.Printf("- Local: http://%s:%d \r\n", host, port)
if host == "0.0.0.0" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it only displays something if not listening on everything

@ccoVeille
Copy link
Contributor

Thanks for applying the suggestions I made

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants