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

A merge from dev to main (v1.3.0) #15

Merged
merged 7 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 lingbopro
Copyright (c) 2024 lingbopro, and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ npm install

确保你有执行权限:
```shell
#似乎这只是Linux的专属指令
Copy link
Owner

Choose a reason for hiding this comment

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

这个写在上面的文字那吧,别放在代码块里了
而且,这就是Linux的命令,Windows不用改权限

chmod +x main.js updateHosts.js ipFetcher.js
```
然后运行主程序:
Expand All @@ -48,12 +49,26 @@ node main.js


有 3 种可选的选项,详见[选项](#选项)。
如果写入失败,请尝试以管理员运行(Windows),或者以超级用户权限执行(`sudo`,Linux/Mac)
_(PS:测试的时候只测了 `Windows`,`Linux` 不保证完全可行)_
_(另一位开发只测试了Linux,其他的就交给大怨种了)_
如果写入失败,请尝试以管理员运行(Windows),或者以超级用户权限执行(`sudo`,Linux/Mac)
_(PS:测试的时候只测了 `Windows`,`Linux` 不保证完全可行)_ _(另一位开发只测试了llnux,其他的就交给大怨种了)_

### 5. 完事!

#### 什么?你想改回你的Hosts文件?
~hahaha,你别想改回去了~

恢复hosts文件

要从备份中恢复原始的`hosts`文件,请按以下步骤操作:

1. **运行恢复命令**:使用以下命令恢复您的`hosts`文件。

```sh
npm run restore
```

2. **完成**:程序将在原始`hosts`文件恢复完成后通知您。

## 选项

可以在运行时在命令中增加可选的选项(见下)
Expand Down
85 changes: 85 additions & 0 deletions docs/README-dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Easy GitHub Hosts - Development Guide

This document provides a detailed guide for developers who wish to understand, modify, or contribute to the Easy GitHub Hosts project.

## Table of Contents

1. [Project Structure](#project-structure)
2. [Installation](#installation)
3. [Usage](#usage)
4. [Scripts](#scripts)
5. [Development Workflow](#development-workflow)
6. [Code Overview](#code-overview)
- [ipFetcher.js](#ipfetcherjs)
- [updateHosts.js](#updatehostsjs)
- [restoreHosts.js](#restorehostsjs)
- [main.js](#mainjs)
7. [Contributing](#contributing)
8. [License](#license)

## Project Structure

The project has the following structure:

easy-github-hosts/
├── .github/
│ ├── bug_report.md
│ ├── feature_request.md
├── docs/
│ ├── README.md
│ ├── README-dev.md
├── ipFetcher.js
├── main.js
├── package.json
├── restoreHosts.js
├── updateHosts.js

- **.github/**: Contains the issue template files.
- **bug_report.md**: Yes! It for bug report issue.
- **feature_request.md**: Yes! It for feature request issue.
- **docs/**: Contains the documentation files.
- **README.md**: Provides general information and usage instructions.
- **README-dev.md**: This development guide.
- **ipFetcher.js**: Contains functions to fetch the IP addresses for GitHub-related domains.
- **main.js**: The main entry point for the program.
- **package.json**: Manages project dependencies and scripts.
- **restoreHosts.js**: Contains functions to restore the original `hosts` file.
- **updateHosts.js**: Contains functions to update the `hosts` file with new IP addresses.

## Code Overview

### ipFetcher.js

This file contains the logic to fetch IP addresses for a list of GitHub-related domains using axios and cheerio:

**getIP(host)**: Fetches the IP address for a given host.
**getIPs()**: Fetches IP addresses for all predefined GitHub-related domains.


### updateHosts.js
This file contains the logic to update the hosts file:

**checkIPv4(IP)**: Checks if a string is a valid IPv4 address.
**parseHostsRecord(record)**: Parses a single record from the hosts file.
**getHostsRecords(content)**: Parses all records from the hosts file.
**getHostsRecordIndexByHost(records, host)**: Finds the index of a record by host.
**genHosts(records)**: Generates the content for the hosts file from records.
**createBackup(hostsPath)**: Creates a backup of the hosts file.
**updateHosts()**: Main function to update the hosts file.

### restoreHosts.js
This file contains the logic to restore the original hosts file from a backup:

**restoreHosts()**: Main function to restore the hosts file from a backup.

### main.js
This file serves as the entry point for the program. It imports and runs the updateHosts function from updateHosts.js.

## Contributing
Contributions are welcome! Please follow these guidelines:

Fork the repository.
Create a new branch for your feature or bug fix.
Commit your changes with a descriptive message.
Push your branch to your fork.
Submit a pull request to the main repository **(dev branch)**.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "easy-github-hosts",
"version": "1.2.0",
"version": "1.3.0",
"description": "Easy to add IP of GitHub into your HOSTS file.",
"main": "main.js",
"scripts": {
"test": "node main.js --debug",
"start": "node main.js"
"start": "node main.js",
"restore": "node restoreHosts.js"
},
"author": "lingbopro,Minemetero",
"license": "MIT",
Expand Down
38 changes: 38 additions & 0 deletions restoreHosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node

"use strict";

const fs = require("fs");
const os = require("os");

const appName = "Easy GitHub Hosts";

/**
* 恢复 HOSTS 文件的备份
*/
function restoreHosts() {
console.log(`${appName}: Starting restoration`);

const hostsPath = os.type().includes("Windows") ? "C:\\Windows\\System32\\drivers\\etc\\hosts" : "/etc/hosts";
const backupPath = `${hostsPath}.backup`;

if (!fs.existsSync(backupPath)) {
console.error(`${appName}: ERROR - Backup file not found: ${backupPath}`);
process.exit(1);
}

try {
fs.copyFileSync(backupPath, hostsPath);
console.log(`${appName}: Successfully restored HOSTS file from backup`);
process.exit(0);
} catch (err) {
console.error(`${appName}: ERROR - An unexpected error occurred while restoring:`, err);
process.exit(1);
}
}

module.exports = { restoreHosts };

if (require.main === module) {
restoreHosts();
}
Loading