-
Notifications
You must be signed in to change notification settings - Fork 77
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
Improve checking entries in the hostfile #38
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, it's a really great bugfix. It's ready to merge as-is, but I have one recommendation about making it even more solid. Let me know!
export function isDomainInHostFile(hostFileContents: string, domain: string): boolean { | ||
// Do a check for a full match since a string includes can be fooled by | ||
// a subdomain being present in the host file. | ||
const isPresent = hostFileContents | ||
.replace(/\s+/g, " ") | ||
.split(" ") | ||
.filter(item => item === domain).length > 0; | ||
return isPresent; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like pretty solid code, but I'm not 100% sure it'll work for all possible hostfile contents, such as tab characters, different encodings, or comments. Maybe it would be easier to bring in a tool like https://www.npmjs.com/package/hostile which has a well-tested hostfile parser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a pretty quick and dirty way of improving the checking. Your are right that can be fooled by domains in comments, which the previous code had a problem with as well. I'll take a look hostile and see what that looks like to bring in.
Bumps [@types/rimraf](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/rimraf) from 2.0.4 to 3.0.0. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/rimraf) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Currently the host file check will not add a domain if a subdomain of that domain already exists.
For example if the host file contains
127.0.0.1 sub.example.com
and the user is setting upexample.com
it will be skipped and not added to the hostfile.