├── .gitignore ├── ui.jpg ├── baidut.sinaapp.com ├── hosts.php └── gohosts.txt ├── main.cpp ├── gohosts.pro ├── gohosts.h ├── LICENSE ├── README.md ├── gohosts.ui └── gohosts.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.user -------------------------------------------------------------------------------- /ui.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baidut/GoHosts/HEAD/ui.jpg -------------------------------------------------------------------------------- /baidut.sinaapp.com/hosts.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baidut/GoHosts/HEAD/baidut.sinaapp.com/hosts.php -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "gohosts.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | 8 | GoHosts g; 9 | g.show(); 10 | 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /baidut.sinaapp.com/gohosts.txt: -------------------------------------------------------------------------------- 1 |

Gohosts

2 | 以管理员方式打开本程序,点击下面的按钮,等待几秒即可
3 | 访问在线hosts 4 | 项目网址[Github] 5 | 反馈建议
6 | 本程序仅可以用于学术研究,禁止传播 7 | 【更新日志】 8 | 1.0.1 修复502 Bad Gateway问题 9 | 1.0.2 更改hosts.txt源 -------------------------------------------------------------------------------- /gohosts.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-09-06T11:05:46 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | QT += network 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = gohosts 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | gohosts.cpp 17 | 18 | HEADERS += gohosts.h 19 | 20 | FORMS += gohosts.ui 21 | -------------------------------------------------------------------------------- /gohosts.h: -------------------------------------------------------------------------------- 1 | #ifndef GOHOSTS_H 2 | #define GOHOSTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace Ui { 14 | class GoHosts; 15 | } 16 | 17 | class GoHosts : public QMainWindow 18 | { 19 | Q_OBJECT 20 | 21 | public: 22 | explicit GoHosts(QWidget *parent = 0); 23 | ~GoHosts(); 24 | 25 | private slots: 26 | void getSource(); 27 | void updateHosts(); 28 | void on_pushButton_clicked(); 29 | void on_anchor_clicked(const QUrl &); 30 | 31 | private: 32 | Ui::GoHosts *ui; 33 | 34 | QNetworkAccessManager *manager; 35 | QNetworkReply *reply_hosts; 36 | QNetworkReply *reply_doc; 37 | QString url_hosts; 38 | }; 39 | 40 | #endif // GOHOSTS_H 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Zhenqiang.Ying 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GoHosts - Google Hosts 2 | 3 | [最新可用 hosts](https://raw.githubusercontent.com/racaljk/hosts/master/hosts) 4 | 5 | 通过修改hosts文件进行科学上网,相当简单! 6 | 7 | * 本hosts仅限用于学术科研等正规用途,禁止传播 8 | * hosts源虽然在更新,但用的人多了,也就不好用了,因此禁止传播 9 | * 网站部署在SAE下,流量需要付费,所以还是禁止传播 10 | 11 | ## Qt制作的一键式程序 12 | 13 | 下载 https://github.com/baidut/GoHosts/releases 14 | 15 | ![image](./ui.jpg) 16 | 17 | ## 不想下载?访问在线hosts 18 | 19 | 1. 访问[在线hosts](https://raw.githubusercontent.com/baidut/GoHosts/master/hosts.txt) 复制内容 20 | 1. 替换系统的hosts文件 21 | 22 | hosts文件位置 23 | - Widonws系统 `系统盘:\windows\system32\drivers\etc\hosts` 24 | - Linux/MAC系统 `/etc/hosts` 25 | - Android系统 同Linux,需要ROOT 26 | 27 | ## 【Windows】找系统hosts文件麻烦? 28 | 29 | 1. 任意位置新建记事本,将下面的内容复制到记事本中 30 | 1. 把记事本后缀名改为bat 31 | 1. 以后以管理员账户打开该文件即可弹出系统hosts文件 32 | 33 | ```bat 34 | notepad "%SystemRoot%\system32\drivers\etc\hosts" 35 | ipconfig /flushdns 36 | exit 37 | ``` 38 | 39 | ## 【Windows】右键管理员身份运行比较麻烦? 40 | 41 | 1. 创建具有管理员权限的快捷方式,具体方法自行百度 42 | 1. 以后每次双击该快捷方式即可打开 43 | 44 | ## 注 45 | 46 | - hosts资源来源 47 | - 谷友部落格 http://igge.gq/ 48 | - https://github.com/racaljk/hosts 49 | - Qt程序参考[/racaljk/hosts](https://github.com/racaljk/hosts)编写 50 | -------------------------------------------------------------------------------- /gohosts.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | GoHosts 4 | 5 | 6 | 7 | 0 8 | 0 9 | 311 10 | 322 11 | 12 | 13 | 14 | GoHosts 15 | 16 | 17 | 18 | 19 | 20 | 10 21 | 170 22 | 291 23 | 111 24 | 25 | 26 | 27 | 更新hosts 28 | 29 | 30 | 31 | 32 | 33 | 10 34 | 0 35 | 291 36 | 161 37 | 38 | 39 | 40 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> 41 | <html><head><meta name="qrichtext" content="1" /><style type="text/css"> 42 | p, li { white-space: pre-wrap; } 43 | </style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;"> 44 | <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">欢迎使用GoHosts!当前版本1.0.2</p></body></html> 45 | 46 | 47 | 48 | 49 | 50 | 51 | 0 52 | 0 53 | 311 54 | 21 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /gohosts.cpp: -------------------------------------------------------------------------------- 1 | #include "gohosts.h" 2 | #include "ui_gohosts.h" 3 | #include 4 | 5 | #define URL_GOHOSTS "http://baidut.sinaapp.com/gohosts.txt" 6 | 7 | #if defined(Q_OS_WIN32) 8 | #define PATH_LOCAL_HOSTS "C:\\Windows\\System32\\drivers\\etc\\hosts" 9 | //"%SYSTEMROOT%/System32/drivers/etc/hosts" 10 | #elif defined(Q_OS_LINUX) 11 | #define PATH_LOCAL_HOSTS "/etc/hosts" 12 | #endif 13 | 14 | GoHosts::GoHosts(QWidget *parent) : 15 | QMainWindow(parent), 16 | ui(new Ui::GoHosts) 17 | { 18 | ui->setupUi(this); 19 | 20 | manager =new QNetworkAccessManager(this); 21 | QNetworkRequest request ; 22 | request.setUrl(QUrl(URL_GOHOSTS)); 23 | reply_doc = manager->get(request); 24 | connect(reply_doc, SIGNAL(finished()), this, SLOT(getSource())); 25 | } 26 | 27 | GoHosts::~GoHosts() 28 | { 29 | delete ui; 30 | } 31 | 32 | void GoHosts::getSource(){ 33 | QString get = QString::fromUtf8( reply_doc->readAll()); 34 | if (get.isEmpty()) { 35 | QMessageBox::critical(this, "加载软件文档-失败", "请确保网络连接后重试
" 36 | "如网络连接正常时浏览器无法访问软件文档" 37 | "则说明软件已经下架,抱歉!"); 38 | } 39 | else { 40 | ui->textBrowser->append(get); 41 | 42 | // 调用系统浏览器打开文档里的链接 43 | ui->textBrowser->setOpenLinks(false); 44 | connect(ui->textBrowser, SIGNAL(anchorClicked(const QUrl&)),this, SLOT(on_anchor_clicked(const QUrl&))); 45 | 46 | // 解析 47 | QString pattern("http(.*)txt"); 48 | QRegExp rx(pattern); 49 | if (get.indexOf(rx) > -1){ 50 | url_hosts = rx.cap(0); 51 | ui->statusbar->showMessage("解析hosts资源地址-成功"); 52 | } 53 | else { 54 | QMessageBox::critical(this,"错误", "解析hosts资源地址-失败"); 55 | } 56 | } 57 | } 58 | 59 | void GoHosts::updateHosts(){ 60 | QString get = QString::fromUtf8( reply_hosts->readAll()); 61 | if (get.isEmpty()) { 62 | QMessageBox::critical(this, "Error", "获取hosts失败,请点击重试"); 63 | return; 64 | } 65 | 66 | // 502 Bad Gateway 67 | QString pattern("502 Bad Gateway"); 68 | QRegExp rx(pattern); 69 | if (get.indexOf(rx) > -1){ 70 | QMessageBox::critical(this, "Error", "服务器忙,请点击重试"); 71 | return; 72 | } 73 | 74 | // 删除已经存在的hosts文件 75 | QFile hosts_old(PATH_LOCAL_HOSTS); 76 | if ( hosts_old.exists()) { 77 | if(hosts_old.setPermissions(QFile::WriteGroup) 78 | &&hosts_old.setPermissions(QFile::WriteOwner) 79 | &&hosts_old.setPermissions(QFile::WriteUser)) { 80 | hosts_old.remove(); 81 | } 82 | else { 83 | QMessageBox::critical(this, "错误", "没有权限,请右键管理员身份打开本程序"); 84 | return; 85 | } 86 | } 87 | // 添加新的hosts文件 创建也需要权限 88 | QFile hosts_new(PATH_LOCAL_HOSTS); 89 | if (hosts_new.open( QIODevice::WriteOnly|QFile::Text)) { 90 | QTextStream stream(&hosts_new); 91 | stream << get; 92 | hosts_new.close(); 93 | QMessageBox::information(this, "完成", "更新hosts文件-成功"); 94 | } 95 | else { 96 | QMessageBox::critical(this, "错误", "没有权限,请右键管理员身份打开本程序"); 97 | } 98 | } 99 | 100 | void GoHosts::on_pushButton_clicked() 101 | { 102 | QNetworkRequest request ; 103 | request.setUrl(QUrl(url_hosts)); 104 | reply_hosts = manager->get(request); 105 | connect(reply_hosts, SIGNAL(finished()), this, SLOT(updateHosts())); 106 | } 107 | 108 | void GoHosts::on_anchor_clicked(const QUrl&url) 109 | { 110 | QDesktopServices::openUrl(QUrl(url)); 111 | } 112 | --------------------------------------------------------------------------------