Skip to content

Commit 67401c8

Browse files
committedMay 19, 2020
init
1 parent a8a893a commit 67401c8

File tree

8 files changed

+294
-1
lines changed

8 files changed

+294
-1
lines changed
 

‎.github/workflows/GitHub520.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: GitHub520
5+
6+
on:
7+
push:
8+
schedule:
9+
- cron: '0 */2 * * *'
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
python-version: [3.7]
18+
19+
steps:
20+
- uses: actions/checkout@v2
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
29+
- name: update readme
30+
run: |
31+
# fetch new ip content and update readme file
32+
python fetch_ips.py
33+
- name: commit
34+
run: |
35+
git config --global user.email 595666367@qq.com
36+
git config --global user.name action_bot
37+
git add .
38+
git commit -m "update readme content"
39+
- name: Push changes
40+
uses: ad-m/github-push-action@master
41+
with:
42+
github_token: ${{ secrets.GITHUB_TOKEN }}

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
.idea/
131+
.DS_Store

‎README.md

+80-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,80 @@
1-
# GitHub520
1+
# GitHub520
2+
<p align="center">
3+
<img src="https://vip1.loli.net/2020/05/19/fLSBE29NxoFtOcd.gif"/>
4+
<br><strong>HelloGitHub</strong> 分享 GitHub 上有趣、入门级的开源项目。<br>兴趣是最好的老师,这里能够帮你找到编程的兴趣!
5+
</p>
6+
7+
## 一、介绍
8+
对 GitHub 说"爱"太难了:访问慢、图片加载不出来。
9+
10+
*注:* 本项目还处于测试阶段,仅在本机测试通过,如有问题欢迎提 [issues](https://github.com/521xueweihan/GitHub520/issues)
11+
12+
---
13+
14+
本项目无需安装任何程序,通过修改本地 hosts 文件,试图解决:
15+
- GitHub 访问速度慢的问题
16+
- GitHub 项目中的图片显示不出的问题
17+
18+
花 5 分钟时间,让你"爱"上 GitHub。
19+
20+
## 二、修改 hosts 文件方法
21+
### 2.1 复制下面的内容
22+
```bash
23+
# GitHub520 Host Start
24+
185.199.108.154 github.githubassets.com
25+
199.232.68.133 camo.githubusercontent.com
26+
199.232.68.133 github.map.fastly.net
27+
199.232.69.194 github.global.ssl.fastly.net
28+
140.82.113.4 github.com
29+
140.82.114.5 api.github.com
30+
199.232.68.133 raw.githubusercontent.com
31+
199.232.68.133 avatars5.githubusercontent.com
32+
199.232.68.133 avatars4.githubusercontent.com
33+
199.232.68.133 avatars3.githubusercontent.com
34+
199.232.68.133 avatars2.githubusercontent.com
35+
199.232.68.133 avatars1.githubusercontent.com
36+
199.232.68.133 avatars0.githubusercontent.com
37+
# GitHub520 Host End
38+
```
39+
40+
### 2.2 修改 hosts 文件
41+
hosts 文件在每个系统的位置不一,详情如下:
42+
- Windows 系统:`C:\Windows\System32\drivers\etc\hosts`
43+
- Linux 系统:`/etc/hosts`
44+
- Mac(苹果电脑)系统:`/etc/hosts`
45+
- Android(安卓)系统:`/system/etc/hosts`
46+
- iPhone(iOS)系统:`/etc/hosts`
47+
48+
修改方法,把第一步的内容复制到文本末尾:
49+
50+
1. Windows 使用记事本。
51+
2. Linux、Mac 使用 Root 权限:`sudo vi /etc/hosts`
52+
3. iPhone、iPad 须越狱、Android 必须要 root。
53+
54+
### 2.3 激活生效
55+
大部分情况下是直接生效,如未生效可尝试下面的办法,刷新 DNS:
56+
57+
1. Windows:在 CMD 窗口输入:`ipconfig /flushdns`
58+
59+
2. Linux 命令:`sudo rcnscd restart`
60+
61+
3. Mac 命令:`sudo killall -HUP mDNSResponder`
62+
63+
**Tips:** 上述方法无效可以尝试重启机器。
64+
65+
## 三、效果对比
66+
之前的样子:
67+
68+
![](./img/old.png)
69+
70+
修改完 hosts 的样子:
71+
72+
![](./img/new.png)
73+
74+
## TODO
75+
- [ ] 定时自动更新 hosts 内容
76+
- [ ] 寻到最有 ip 解析结果
77+
78+
79+
## 声明
80+
<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh"><img alt="知识共享许可协议" style="border-width: 0" src="https://licensebuttons.net/l/by-nc-nd/4.0/88x31.png"></a><br>本作品采用 <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh">署名-非商业性使用-禁止演绎 4.0 国际</a> 进行许可。

‎README_template.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# GitHub520
2+
<p align="center">
3+
<img src="https://vip1.loli.net/2020/05/19/fLSBE29NxoFtOcd.gif"/>
4+
<br><strong>HelloGitHub</strong> 分享 GitHub 上有趣、入门级的开源项目。<br>兴趣是最好的老师,这里能够帮你找到编程的兴趣!
5+
</p>
6+
7+
## 一、介绍
8+
对 GitHub 说"爱"太难了:访问慢、图片加载不出来。
9+
10+
*注:* 本项目还处于测试阶段,仅在本机测试通过,如有问题欢迎提 [issues](https://github.com/521xueweihan/GitHub520/issues)
11+
12+
---
13+
14+
本项目无需安装任何程序,通过修改本地 hosts 文件,试图解决:
15+
- GitHub 访问速度慢的问题
16+
- GitHub 项目中的图片显示不出的问题
17+
18+
花 5 分钟时间,让你"爱"上 GitHub。
19+
20+
## 二、修改 hosts 文件方法
21+
22+
### 2.1 复制下面的内容
23+
```bash
24+
{hosts_str}
25+
```
26+
27+
上面内容会自动定时更新,保证最新有效。数据时间:{update_time}
28+
29+
### 2.2 修改 hosts 文件
30+
hosts 文件在每个系统的位置不一,详情如下:
31+
- Windows 系统:`C:\Windows\System32\drivers\etc\hosts`
32+
- Linux 系统:`/etc/hosts`
33+
- Mac(苹果电脑)系统:`/etc/hosts`
34+
- Android(安卓)系统:`/system/etc/hosts`
35+
- iPhone(iOS)系统:`/etc/hosts`
36+
37+
修改方法,把第一步的内容复制到文本末尾:
38+
39+
1. Windows 使用记事本。
40+
2. Linux、Mac 使用 Root 权限:`sudo vi /etc/hosts`
41+
3. iPhone、iPad 须越狱、Android 必须要 root。
42+
43+
### 2.3 激活生效
44+
大部分情况下是直接生效,如未生效可尝试下面的办法,刷新 DNS:
45+
46+
1. Windows:在 CMD 窗口输入:`ipconfig /flushdns`
47+
48+
2. Linux 命令:`sudo rcnscd restart`
49+
50+
3. Mac 命令:`sudo killall -HUP mDNSResponder`
51+
52+
**Tips:** 上述方法无效可以尝试重启机器。
53+
54+
## 三、效果对比
55+
之前的样子:
56+
57+
![](./img/old.png)
58+
59+
修改完 hosts 的样子:
60+
61+
![](./img/new.png)
62+
63+
64+
## TODO
65+
- [ x ] 定时自动更新 hosts 内容
66+
- [ ] 寻到最有 ip 解析结果
67+
68+
69+
## 声明
70+
<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh"><img alt="知识共享许可协议" style="border-width: 0" src="https://licensebuttons.net/l/by-nc-nd/4.0/88x31.png"></a><br>本作品采用 <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh">署名-非商业性使用-禁止演绎 4.0 国际</a> 进行许可。

‎fetch_ips.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
#
4+
# Author : XueWeiHan
5+
# E-mail : 595666367@qq.com
6+
# Date : 2020-05-19 15:27
7+
# Desc : 获取最新的 GitHub 相关域名对应 IP
8+
import os
9+
import re
10+
import datetime
11+
from collections import Counter
12+
13+
from retry import retry
14+
15+
import requests
16+
17+
RAW_URL = [
18+
"github.githubassets.com",
19+
"camo.githubusercontent.com",
20+
"github.map.fastly.net",
21+
"github.global.ssl.fastly.net",
22+
"github.com",
23+
"api.github.com",
24+
"raw.githubusercontent.com",
25+
"avatars5.githubusercontent.com",
26+
"avatars4.githubusercontent.com",
27+
"avatars3.githubusercontent.com",
28+
"avatars2.githubusercontent.com",
29+
"avatars1.githubusercontent.com",
30+
"avatars0.githubusercontent.com"]
31+
IPADDRESS_PREFIX = ".ipaddress.com"
32+
33+
34+
def write_file(hosts_content: str):
35+
update_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
36+
output_file_path = os.path.join(os.path.dirname(__file__), "README.md")
37+
template_path = os.path.join(os.path.dirname(__file__),
38+
"README_template.md")
39+
with open(template_path, "r") as temp_fb:
40+
template_str = temp_fb.read()
41+
hosts_content = template_str.format(hosts_str=hosts_content,
42+
update_time=update_time)
43+
with open(output_file_path, "w") as output_fb:
44+
output_fb.write(hosts_content)
45+
46+
47+
def make_ipaddress_url(raw_url: str):
48+
"""
49+
生成 ipaddress 对应的 url
50+
:param raw_url: 原始 url
51+
:return: ipaddress 的 url
52+
"""
53+
dot_count = raw_url.count(".")
54+
if dot_count > 1:
55+
raw_url_list = raw_url.split(".")
56+
tmp_url = raw_url_list[-2] + "." + raw_url_list[-1]
57+
ipaddress_url = "https://" + tmp_url + IPADDRESS_PREFIX + "/" + raw_url
58+
else:
59+
ipaddress_url = "https://" + raw_url + IPADDRESS_PREFIX
60+
return ipaddress_url
61+
62+
63+
@retry(tries=3)
64+
def get_ip(session: requests.session, raw_url: str):
65+
url = make_ipaddress_url(raw_url)
66+
try:
67+
rs = session.get(url, timeout=5)
68+
print("request: {}".format(url))
69+
pattern = r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b"
70+
ip_list = re.findall(pattern, rs.text)
71+
ip_counter_obj = Counter(ip_list).most_common(1)
72+
if ip_counter_obj:
73+
return raw_url, ip_counter_obj[0][0]
74+
raise Exception("ip address empty")
75+
except Exception as ex:
76+
print("get: {}, error: {}".format(url, ex))
77+
raise Exception
78+
79+
80+
def main():
81+
hosts_template = """# GitHub520 Host Start\n{content}# GitHub520 Host End"""
82+
session = requests.session()
83+
content = ""
84+
for raw_url in RAW_URL:
85+
try:
86+
host_name, ip = get_ip(session, raw_url)
87+
content += ip.ljust(50) + host_name + "\n"
88+
except Exception:
89+
continue
90+
91+
if content:
92+
hosts_content = hosts_template.format(content=content)
93+
print(hosts_content)
94+
write_file(hosts_content)
95+
96+
97+
if __name__ == '__main__':
98+
main()

‎img/new.png

625 KB
Loading

‎img/old.png

196 KB
Loading

‎requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests==2.23.0
2+
retry==0.9.2

0 commit comments

Comments
 (0)
Please sign in to comment.