Java批量获取GitHub最佳IP

2020年10月3日11:19:36 评论 9 4402字阅读14分40秒

Java批量获取GitHub最佳IP

GitHub是个好东西,但是因为dns污染的原因,使用体验不是很好,

明明挂了上网工具还是刷不出来图就很奇怪。。。。

还是用的修改hosts的土办法解决的问题.。。。

hosts位置

Linux / MAC

sudo vi /etc/hosts

Windows

通过管理员打开文件 C:\Windows\System32\drivers\etc\hosts

需要解析的网址

可以打开GitHub,按F12,

Java批量获取GitHub最佳IP

可以在source中看到很多域名,这就是需要我们查询其IP的域名。

图中仅为GitHub首页的,全部域名如下,

github.com
nodeload.github.com
api.github.com
training.github.com
codeload.github.com

assets-cdn.github.com
documentcloud.github.com
help.github.com
githubstatus.com

github.global.ssl.fastly.net
raw.github.com
raw.githubusercontent.com
cloud.githubusercontent.com
gist.githubusercontent.com
marketplace-screenshots.githubusercontent.com
repository-images.githubusercontent.com
user-images.githubusercontent.com
desktop.githubusercontent.com

avatars0.githubusercontent.com
avatars1.githubusercontent.com
avatars2.githubusercontent.com
avatars3.githubusercontent.com
avatars4.githubusercontent.com
avatars5.githubusercontent.com
avatars6.githubusercontent.com
avatars7.githubusercontent.com
avatars8.githubusercontent.com

2020.10.3 hosts文件

140.82.114.4 github.com
140.82.114.9 nodeload.github.com
140.82.114.5 api.github.com
140.82.112.18 training.github.com
140.82.113.10 codeload.github.com

185.199.108.153 assets-cdn.github.com
185.199.108.153 documentcloud.github.com
185.199.108.154 help.github.com
185.199.108.153 githubstatus.com

199.232.69.194 github.global.ssl.fastly.net
199.232.68.133 raw.github.com
199.232.68.133 raw.githubusercontent.com
199.232.68.133 cloud.githubusercontent.com
199.232.68.133 gist.githubusercontent.com
199.232.68.133 marketplace-screenshots.githubusercontent.com
199.232.68.133 repository-images.githubusercontent.com
199.232.68.133 user-images.githubusercontent.com
199.232.68.133 desktop.githubusercontent.com

199.232.68.133 avatars0.githubusercontent.com
199.232.68.133 avatars1.githubusercontent.com
199.232.68.133 avatars2.githubusercontent.com
199.232.68.133 avatars3.githubusercontent.com
199.232.68.133 avatars4.githubusercontent.com
199.232.68.133 avatars5.githubusercontent.com
199.232.68.133 avatars6.githubusercontent.com
199.232.68.133 avatars7.githubusercontent.com
199.232.68.133 avatars8.githubusercontent.com

不一定保证每一项都是正确、有效的,但目前来说笔者实测能够稳定访问 Github。

Java脚本

需要先导入依赖

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.6</version>
</dependency>
<dependency>
  <groupId>org.jsoup</groupId>
  <artifactId>jsoup</artifactId>
  <version>1.10.2</version>
</dependency>

源代码如下:

public static void main(String[] args) {
  ArrayList<String> result = new ArrayList<>();
  // 1. 创建一个HTTPClient对象
  CloseableHttpClient httpClient = HttpClients.createDefault();
  // 2. 设置URL遍历
  List<String> urls = Arrays.asList(
    "github.com",
    "nodeload.github.com",
    "api.github.com",
    "training.github.com",
    "codeload.github.com",
    "assets-cdn.github.com",
    "documentcloud.github.com",
    "help.github.com",
    "githubstatus.com",
    "github.global.ssl.fastly.net",
    "raw.github.com",
    "raw.githubusercontent.com",
    "cloud.githubusercontent.com",
    "gist.githubusercontent.com",
    "marketplace-screenshots.githubusercontent.com",
    "repository-images.githubusercontent.com",
    "user-images.githubusercontent.com",
    "desktop.githubusercontent.com",
    "avatars0.githubusercontent.com",
    "avatars1.githubusercontent.com",
    "avatars2.githubusercontent.com",
    "avatars3.githubusercontent.com",
    "avatars4.githubusercontent.com",
    "avatars5.githubusercontent.com",
    "avatars6.githubusercontent.com",
    "avatars7.githubusercontent.com",
    "avatars8.githubusercontent.com");
  urls.forEach(url -> {
    String domain = "";
    String[] splits = url.split("\\.");
    for (int i = 0; i < splits.length; i++) {
      if (i == splits.length - 2) {
        domain += splits[i] + ".";
      }
      if (i == splits.length - 1) {
        domain += splits[i];
      }
    }
    // 3. 生成get请求
    // 模板: https://githubusercontent.com.ipaddress.com/camo.githubusercontent.com
    String getUrl = "https://" + domain + ".ipaddress.com/" + url;
    HttpGet httpGet = new HttpGet(getUrl);
    try {
      // 4. 执行得到返回结果
      CloseableHttpResponse response = httpClient.execute(httpGet);
      // 5. 解析
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String html = EntityUtils.toString(response.getEntity(), "utf-8");
        // 6. 使用jsoup解析
        Document document = Jsoup.parse(html);
        // 7. 获取链接
        Elements linkes = document.select("a[href]");
        for (Element link : linkes) {
          if (link.toString().contains("https://www.ipaddress.com/ipv4/")) {
            String res = link.text() + " " + url;
            result.add(res);
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  });
  result.forEach(r -> System.out.println(r));
}

刷新dns

大部分情况下是直接生效,如未生效可尝试下面的办法,刷新 DNS:

Linux 命令:sudo rcnscd restart

Mac 命令:sudo killall -HUP mDNSResponder

Windows:在 CMD 窗口输入:ipconfig /flushdns

参考文章

https://www.cnblogs.com/itzhouq/p/githubplus.html

https://www.cnblogs.com/itzhouq/p/githubplus.html

继续阅读
  • 本文由 发表于 2020年10月3日11:19:36
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接