├── .github └── workflows │ └── freegithub.yml ├── README.md ├── README_template.md ├── free_github.go ├── go.mod └── hosts /.github/workflows/freegithub.yml: -------------------------------------------------------------------------------- 1 | name: FreeGithub 2 | 3 | on: 4 | schedule: 5 | - cron: '0 0 0 */1 *' 6 | 7 | jobs: 8 | 9 | build: 10 | name: Build 11 | runs-on: ubuntu-latest 12 | steps: 13 | 14 | - name: Set up Go 1.x 15 | uses: actions/setup-go@v2 16 | with: 17 | go-version: ^1.14 18 | id: go 19 | 20 | - name: Check out code into the Go module directory 21 | uses: actions/checkout@v2 22 | 23 | - name: Get dependencies 24 | run: | 25 | go get -v -t -d ./... 26 | if [ -f Gopkg.toml ]; then 27 | curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh 28 | dep ensure 29 | fi 30 | 31 | # - name: Build 32 | # run: go build -v . 33 | 34 | # - name: Test 35 | # run: go test -v . 36 | 37 | - name: Updata README.md 38 | run: go run -v . 39 | 40 | - name: commit 41 | run: | 42 | git config --global user.email whr1314520@qq.com 43 | git config --global user.name action_bot 44 | git add README.md hosts 45 | git commit -m"auto update README.md" 46 | 47 | - name: push 48 | uses: ad-m/github-push-action@master 49 | with: 50 | github_token: ${{ secrets.GITHUB_TOKEN}} 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FreeGithub 2 | 自动获取github相关网站的ip地址,解决github链接不畅通的问题,该项目的README会自动更新,大家可以直接复制使用。 3 | 在workflows/freegithub.yml文件中可以设置自动更新的时间,本代码设置的是一个月自动更新一次。大家可以按照自己的需求改写cron表达式 4 | 5 | ## hosts列表 6 | ```base 7 | #Github Host Start 8 | github.githubassets.com 54.165.242.31 9 | camo.githubusercontent.com 3.236.227.46 10 | github.map.fastly.net 3.230.154.141 11 | github.global.ssl.fastly.net 44.198.186.243 12 | github.com 44.197.131.181 13 | api.github.com 44.198.186.243 14 | raw.githubusercontent.com 18.206.194.198 15 | favicons.githubusercontent.com 34.207.248.206 16 | avatars5.githubusercontent.com 54.86.230.221 17 | avatars4.githubusercontent.com 54.163.29.143 18 | avatars3.githubusercontent.com 18.206.194.198 19 | avatars2.githubusercontent.com 54.89.231.40 20 | avatars1.githubusercontent.com 35.175.120.159 21 | avatars0.githubusercontent.com 34.235.150.65 22 | # Github Host End 23 | ``` 24 | 25 | 更新时间:2021-11-01 08:23:16 26 | 27 | ## 修改本机的hosts文件 28 | ### 存放位置 29 | * Windows 系统:C:\Windows\System32\drivers\etc\hosts 30 | * Linux 系统:/etc/hosts 31 | * Mac(苹果电脑)系统:/etc/hosts 32 | 33 | ### 生效 34 | * Windows:在 CMD 窗口输入:ipconfig /flushdns 35 | * Linux 命令:sudo rcnscd restart 36 | * Mac 命令:sudo killall -HUP mDNSResponder -------------------------------------------------------------------------------- /README_template.md: -------------------------------------------------------------------------------- 1 | # FreeGithub 2 | 自动获取github相关网站的ip地址,解决github链接不畅通的问题,该项目的README会自动更新,大家可以直接复制使用。 3 | 在workflows/freegithub.yml文件中可以设置自动更新的时间,本代码设置的是一个月自动更新一次。大家可以按照自己的需求改写cron表达式 4 | 5 | ## hosts列表 6 | ```base 7 | {{hosts}} 8 | ``` 9 | 10 | 更新时间:{{time}} 11 | 12 | ## 修改本机的hosts文件 13 | ### 存放位置 14 | * Windows 系统:C:\Windows\System32\drivers\etc\hosts 15 | * Linux 系统:/etc/hosts 16 | * Mac(苹果电脑)系统:/etc/hosts 17 | 18 | ### 生效 19 | * Windows:在 CMD 窗口输入:ipconfig /flushdns 20 | * Linux 命令:sudo rcnscd restart 21 | * Mac 命令:sudo killall -HUP mDNSResponder -------------------------------------------------------------------------------- /free_github.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io/ioutil" 5 | "log" 6 | "net/http" 7 | "os" 8 | "regexp" 9 | "strings" 10 | "time" 11 | ) 12 | 13 | var RAW_URLS = []string{ 14 | "github.githubassets.com", 15 | "camo.githubusercontent.com", 16 | "github.map.fastly.net", 17 | "github.global.ssl.fastly.net", 18 | "github.com", 19 | "api.github.com", 20 | "raw.githubusercontent.com", 21 | "favicons.githubusercontent.com", 22 | "avatars5.githubusercontent.com", 23 | "avatars4.githubusercontent.com", 24 | "avatars3.githubusercontent.com", 25 | "avatars2.githubusercontent.com", 26 | "avatars1.githubusercontent.com", 27 | "avatars0.githubusercontent.com", 28 | } 29 | 30 | const IPADDRESS_PREFIX = ".ipaddress.com" 31 | 32 | func Hosts() { 33 | var content string = "#Github Host Start\n" 34 | for _, rawUrl := range RAW_URLS { 35 | ip := getIp(rawUrl) 36 | content += rawUrl + " " + ip + "\n" 37 | } 38 | content += "# Github Host End" 39 | 40 | writeToFile(content) 41 | } 42 | 43 | func getIp(rawUrl string) string { 44 | url := makeIpaddressUrl(rawUrl) 45 | resp, err := http.Get(url) 46 | if err != nil { 47 | log.Println("error url get: ", err) 48 | return "" 49 | } 50 | defer resp.Body.Close() 51 | 52 | re, err := regexp.Compile("\\b([0-9]{1,3}\\.){3}[0-9]{1,3}") 53 | if err != nil { 54 | log.Println("regx err: ", err.Error()) 55 | } 56 | 57 | bytes, err := ioutil.ReadAll(resp.Body) 58 | if err != nil { 59 | log.Println("read all : ", err.Error()) 60 | } 61 | ip := re.FindString(string(bytes)) 62 | return ip 63 | 64 | } 65 | 66 | func makeIpaddressUrl(rawUrl string) (ipaddress string) { 67 | dotCount := strings.Count(rawUrl, ".") 68 | if dotCount > 1 { 69 | rawUrlList := strings.Split(rawUrl, ".") 70 | tempUrl := rawUrlList[dotCount-1] + "." + rawUrlList[dotCount] 71 | ipaddress = "https://" + tempUrl + IPADDRESS_PREFIX + "/" + rawUrl 72 | } else { 73 | ipaddress = "https://" + rawUrl + IPADDRESS_PREFIX 74 | } 75 | return 76 | } 77 | 78 | func writeToFile(content string) { 79 | file, err := os.OpenFile("hosts", os.O_RDWR|os.O_CREATE, 0777) 80 | if err != nil { 81 | log.Println("open file err:", err) 82 | } 83 | defer file.Close() 84 | 85 | oldContentBytes, err := ioutil.ReadAll(file) 86 | if err != nil { 87 | log.Println("read host err:", err) 88 | } 89 | oldContent := string(oldContentBytes) 90 | if strings.Compare(content, oldContent) == 0 { 91 | log.Println("未发生更改") 92 | return 93 | } 94 | file.Truncate(0) 95 | file.Seek(0, 0) 96 | file.WriteString(content) 97 | 98 | bytes, err := ioutil.ReadFile("README_template.md") 99 | if err != nil { 100 | log.Println("open README_template.md err, ", err) 101 | } 102 | readMeContent := strings.Replace(string(bytes), "{{hosts}}", content, -1) 103 | // 当放在github workflows中执行的时候使用的是其他地区的服务器,Local并不代表本地时间 104 | time.Local = time.FixedZone("CST", 8*60*60) 105 | time := time.Now().Local().Format("2006-01-02 15:04:05") 106 | readMeContent = strings.ReplaceAll(readMeContent, "{{time}}", time) 107 | 108 | readMeFile, err := os.OpenFile("README.md", os.O_WRONLY|os.O_TRUNC, 0600) 109 | if err != nil { 110 | log.Println("open README.md err, ", err) 111 | } 112 | defer readMeFile.Close() 113 | readMeFile.WriteString(readMeContent) 114 | 115 | } 116 | 117 | func main() { 118 | //timeTicker := time.NewTicker(time.Hour * 2) 119 | //for { 120 | // Hosts() 121 | // <-timeTicker.C 122 | //} 123 | Hosts() 124 | } 125 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module FreeGithub 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /hosts: -------------------------------------------------------------------------------- 1 | #Github Host Start 2 | github.githubassets.com 54.165.242.31 3 | camo.githubusercontent.com 3.236.227.46 4 | github.map.fastly.net 3.230.154.141 5 | github.global.ssl.fastly.net 44.198.186.243 6 | github.com 44.197.131.181 7 | api.github.com 44.198.186.243 8 | raw.githubusercontent.com 18.206.194.198 9 | favicons.githubusercontent.com 34.207.248.206 10 | avatars5.githubusercontent.com 54.86.230.221 11 | avatars4.githubusercontent.com 54.163.29.143 12 | avatars3.githubusercontent.com 18.206.194.198 13 | avatars2.githubusercontent.com 54.89.231.40 14 | avatars1.githubusercontent.com 35.175.120.159 15 | avatars0.githubusercontent.com 34.235.150.65 16 | # Github Host End --------------------------------------------------------------------------------