├── Cargo.toml ├── README.md ├── github.com.html ├── hosts └── src └── main.rs /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "githubdns" 3 | version = "0.1.0" 4 | authors = ["bai "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | tokio = { version = "0.2", features = ["full"] } #{ version = "0.2.0", path = "../tokio", features = ["full"] } 11 | tokio-util ={ version = "0.2", features = ["full"] } #{ version = "0.2.0", path = "../tokio-util", features = ["full"] } 12 | tokio-tls= "0.3" # {version="0.3",path="../tokio-tls" } 13 | futures = "0.3.0" 14 | native-tls = "0.2" 15 | scraper = "0.11.0" 16 | sys-info="0.5" 17 | clap="2.33" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # githubdns 2 | 一键加速国内github访问速度 3 | 4 | ## 安装 5 | 可以下载release中的各个编译好的二进制程序,也可以自行编译. 6 | ```bash 7 | cargo build --release 8 | ``` 9 | ## 使用 10 | ### windows 11 | 请修改程序权限,以管理员运行. 然后双击即可. 12 | ### unix 13 | ```bash 14 | sudo githubdns 15 | ``` 16 | ### 默认域名 17 | 默认修改以下域名 18 | ``` 19 | codeload.github.com 20 | github.com 21 | github.global.ssl.fastly.net 22 | ``` 23 | 可以通过--domains参数来覆盖默认设置 24 | 25 | ## 工作原理 26 | 去ipaddress.com抓取你指定的域名对应的ip地址,然后写入到系统的hosts文件中 27 | 所以要正常运行必须以root权限运行. 28 | -------------------------------------------------------------------------------- /github.com.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | ▷ github.com : The world's leading software development platform · GitHub 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 1612 | 1613 | 1639 | 1640 | 1645 | 1651 | 1652 | 1653 | 1660 | 1756 | 1757 | 1758 |
1759 |
What Is My IP:
China Flag222.65.122.60
1763 |
1769 | 1770 |
1771 | 1772 |
1773 |
1774 |
1775 |

github.com - Github

1776 | 1777 | 1778 | 1780 | 1781 | 1782 | 1783 | 1785 | 1786 |

Domain Summary

1787 | 1788 | 1789 | 1790 | 1791 | 1792 | 1793 | 1794 | 1795 | 1796 | 1797 | 1798 | 1799 | 1800 | 1801 | 1802 | 1805 | 1806 | 1807 | 1808 | 1811 | 1812 | 1813 | 1814 | 1819 | 1820 | 1821 | 1822 | 1823 | 1824 |
Global Traffic Rank95
Estimated Visitors4.9 Million / Day
Estimated Page Impressions23.8 Million / Day
Domain Creation Date 1803 | 1804 |
Domain Age 1809 | 1810 |
IP Address 1815 |
    1816 |
  • 192.30.253.112
  • 1817 |
1818 |
Web Server LocationUnited States
1825 |
1826 | Updated: 1827 | 1828 | 1829 |

Frequently Asked Questions (FAQ)

1830 | 1831 | 1832 | 1833 | 1838 | 1839 | 1840 | 1845 | 1846 | 1847 | 1852 | 1853 | 1854 | 1876 | 1877 | 1878 | 1884 | 1885 | 1886 | 1889 | 1890 | 1891 | 1896 | 1897 | 1898 | 1903 | 1904 | 1905 | 1910 | 1911 | 1912 | 1915 | 1916 | 1917 | 1920 | 1921 | 1922 |

When was Github.com registered?

1834 |
Github.com was registered 4437 days ago on Tuesday, October 9, 1835 | 2007. 1836 |
1837 |

When will Github.com expire?

1841 |
This domain will expire in 311 days on Friday, October 9, 1842 | 2020. 1843 |
1844 |

When was the WHOIS for Github.com last updated?

1848 |
The WHOIS entry was last updated 203 days ago on Monday, May 13, 1849 | 2019. 1850 |
1851 |

What are Github.com's nameservers?

1855 |
DNS for Github.com is provided by the following nameservers: 1856 | 1874 |
1875 |

Who is the registrar for the Github.com domain?

1879 |
The domain has been registered at MarkMonitor Inc. You can visit the 1880 | registrar's website at http://www.markmonitor.com. 1881 | The registrar's WHOIS server can be reached at whois.markmonitor.com. 1882 |
1883 |

What is the traffic rank for Github.com?

1887 |
Github.com ranks 95 globally on Alexa.
1888 |

How many people visit Github.com each day?

1892 |
Github.com receives approximately 4.9 Million visitors and 23.8 1893 | Million page impressions per day. 1894 |
1895 |

From which countries does Github.com receive most of its visitors from?

1899 |
Github.com is mostly visited by people located in the United States, 1900 | China and Japan. 1901 |
1902 |

What IP address does Github.com resolve to?

1906 |
Github.com resolves to the IPv4 address 192.30.253.112. 1908 |
1909 |

In what country are Github.com servers located in?

1913 |
Github.com has servers located in the United States.
1914 |

What webserver software does Github.com use?

1918 |
Github.com is powered by "GitHub.com" webserver.
1919 |
1923 |
1924 |

Domain WHOIS Record

1925 | 1926 | 1927 | 1928 | 1929 | 1930 | 1931 | 1932 | 1933 | 1934 | 1935 | 1936 | 1937 | 1938 | 1939 | 1940 | 1941 | 1942 | 1943 | 1944 | 1945 | 1946 | 1947 | 1948 | 1949 | 1953 | 1954 | 1955 | 1956 | 1959 | 1960 | 1961 | 1962 | 1965 | 1966 | 1967 | 1968 | 1971 | 1972 | 1973 | 1974 | 1981 | 1982 | 1983 | 1984 | 1996 | 1997 | 1998 | 1999 | 2000 | 2001 | 2002 |
Domain Namegithub.com
Domain Extensioncom
Top-Level Domain (TLD).com
RegistrarMarkMonitor Inc.
Registrar WHOIS Serverwhois.markmonitor.com
Registrar URLhttp://www.markmonitor.com
Domain Updated Date 1957 | 1958 |
Domain Creation Date 1963 | 1964 |
Domain Expiry Date 1969 | 1970 |
Domain Status 1975 |
    1976 |
  • clientdeleteprohibited
  • 1977 |
  • clienttransferprohibited
  • 1978 |
  • clientupdateprohibited
  • 1979 |
1980 |
Nameservers 1985 |
    1986 |
  • ns-1283.awsdns-32.org
  • 1987 |
  • ns-1707.awsdns-21.co.uk
  • 1988 |
  • ns-421.awsdns-52.com
  • 1989 |
  • ns-520.awsdns-01.net
  • 1990 |
  • ns1.p16.dynect.net
  • 1991 |
  • ns2.p16.dynect.net
  • 1992 |
  • ns3.p16.dynect.net
  • 1993 |
  • ns4.p16.dynect.net
  • 1994 |
1995 |
DNSSECunsigned
2003 |
2004 |

IP Address and Server Location

2005 |

United States FlagUnited 2006 | States

2007 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 2026 | 2027 | 2028 | 2029 | 2030 | 2033 | 2034 | 2035 | 2036 | 2041 | 2042 | 2043 |
LocationUnited States
Latitude37.7510 / 37°45′3″ N
Longitude-97.8220 / 97°49′19″ W
TimezoneAmerica/Chicago
Local Time 2031 | 2032 |
IPv4 Addresses 2037 |
    2038 |
  • 192.30.253.112
  • 2039 |
2040 |
2044 |
2045 | 2046 | 2048 | 2049 |

Website and Web Server Information

2050 | 2051 | 2052 | 2053 | 2054 | 2058 | 2059 | 2060 | 2061 | 2062 | 2063 | 2064 | 2065 | 2069 | 2070 | 2071 | 2072 | 2073 | 2074 | 2075 | 2076 | 2077 | 2078 | 2079 | 2080 | 2081 | 2082 | 2083 | 2084 | 2085 | 2086 | 2087 |
Website AbstractGitHub : GitHub is the best place to share code with friends, 2055 | co-workers, classmates, and complete strangers. Over four million people use GitHub to build 2056 | amazing things together 2057 |
Website TitleThe world's leading software development platform · GitHub
Website DescriptionGitHub brings together the world's largest community of developers to 2066 | discover, share, and build better software. From open source projects to private team 2067 | repositories, we're your all-in-one platform for collaborative development. 2068 |
Website Hosthttps://github.com
Server SoftwareGitHub.com
Median Page Load Time1.746 seconds
Number of Sites Linking In84,088
2088 |
2089 |

DNS Resource Records

2090 | 2091 | 2092 | 2093 | 2094 | 2095 | 2096 | 2097 | 2098 | 2099 | 2100 | 2101 | 2102 | 2103 | 2104 | 2105 | 2107 | 2108 | 2109 | 2110 | 2111 | 2112 | 2113 | 2114 | 2115 | 2116 | 2117 | 2118 | 2120 | 2121 | 2122 | 2123 | 2124 | 2126 | 2127 | 2128 | 2129 | 2130 | 2133 | 2134 | 2135 | 2136 | 2137 | 2140 | 2141 | 2142 | 2143 | 2144 | 2146 | 2147 | 2148 | 2149 | 2150 | 2151 | 2152 | 2153 | 2154 | 2155 | 2156 | 2157 | 2158 | 2159 | 2160 | 2161 | 2162 | 2163 | 2164 | 2165 | 2166 | 2167 | 2168 | 2169 | 2170 | 2171 | 2172 | 2173 | 2174 | 2175 | 2176 | 2177 | 2178 | 2179 | 2180 | 2181 | 2182 | 2183 | 2184 | 2185 | 2186 | 2187 | 2188 | 2189 | 2190 | 2191 | 2192 | 2193 | 2194 | 2195 | 2196 | 2197 | 2198 | 2199 | 2200 | 2201 | 2202 | 2203 | 2204 | 2205 | 2208 | 2209 | 2210 |
NameTypeData
@SOAns1.p16.dynect.net. hostmaster.github.com. 1574918456 3600 600 604800 60
United States Flag@ 2106 | A192.30.253.112
@MX1 aspmx.l.google.com
@MX5 alt1.aspmx.l.google.com 2119 |
@MX5 alt2.aspmx.l.google.com 2125 |
@MX10 alt3.aspmx.l.google.com 2132 |
@MX10 alt4.aspmx.l.google.com 2139 |
@NSns-1283.awsdns-32.org 2145 |
@NSns-1707.awsdns-21.co.uk
@NSns-421.awsdns-52.com
@NSns-520.awsdns-01.net
@NSns1.p16.dynect.net
@NSns2.p16.dynect.net
@NSns3.p16.dynect.net
@NSns4.p16.dynect.net
@TXTdocusign=087098e3-3d46-47b7-9b4e-8a23028154cd
@TXTMS=6BF03E6AF5CB689E315FB6199603BABF2C88D805
@TXTMS=ms44452932
@TXTMS=ms58704441
@TXTv=spf1 ip4:192.30.252.0/22 ip4:208.74.204.0/22 ip4:46.19.168.0/23 include:_spf.google.com 2206 | include:esp.github.com include:_spf.createsend.com include:servers.mcsv.net ~all 2207 |
2211 |
2212 | @ represents the DNS zone origin github.com as often found in BIND zone files 2213 |

Traffic Summary

2214 | 2215 | 2216 | 2217 | 2218 | 2219 | 2220 | 2221 | 2222 | 2223 | 2224 | 2225 | 2226 | 2227 | 2228 | 2229 |
Global Traffic RankEstimated Monthly VisitorsEstimated Monthly Pageviews
95150.3 Million724.5 Million
2230 |
2231 |

Traffic Statistics by Country

2232 | 2233 | 2234 | 2235 | 2236 | 2237 | 2238 | 2239 | 2240 | 2241 | 2242 | 2243 | 2246 | 2247 | 2248 | 2249 | 2250 | 2251 | 2252 | 2253 | 2254 | 2255 | 2256 | 2257 | 2258 | 2259 | 2260 | 2261 | 2262 | 2263 | 2264 | 2265 | 2266 | 2267 | 2268 | 2269 | 2270 | 2271 | 2272 | 2273 | 2274 | 2275 | 2278 | 2279 | 2280 | 2281 | 2282 | 2283 | 2285 | 2286 | 2287 | 2288 | 2289 | 2290 | 2293 | 2294 | 2295 | 2296 | 2297 | 2298 | 2300 | 2301 | 2302 | 2303 | 2304 | 2305 | 2307 | 2308 | 2309 | 2310 | 2311 | 2312 | 2314 | 2315 | 2316 | 2317 | 2318 | 2319 | 2321 | 2322 | 2323 | 2324 | 2325 | 2326 | 2328 | 2329 | 2330 | 2331 | 2332 | 2333 | 2334 | 2335 | 2336 | 2337 | 2338 | 2339 | 2340 | 2341 | 2342 | 2343 | 2344 | 2345 | 2347 | 2348 | 2349 | 2350 | 2351 | 2352 | 2354 | 2355 | 2356 | 2357 | 2358 | 2359 | 2361 | 2362 | 2363 | 2364 | 2365 | 2366 | 2368 | 2369 | 2370 | 2371 | 2372 | 2373 | 2375 | 2376 | 2377 | 2378 | 2379 | 2380 | 2382 | 2383 | 2384 | 2385 | 2386 | 2387 | 2389 | 2390 | 2391 | 2392 | 2393 | 2394 | 2397 | 2398 | 2399 | 2400 | 2401 | 2402 | 2404 | 2405 | 2406 | 2407 | 2408 | 2409 | 2411 | 2412 | 2413 | 2414 | 2415 | 2416 | 2418 | 2419 | 2420 | 2421 | 2422 | 2423 | 2425 | 2426 | 2427 | 2428 | 2429 | 2430 | 2432 | 2433 | 2434 | 2435 | 2436 | 2437 | 2439 | 2440 | 2441 | 2442 | 2443 | 2444 | 2446 | 2447 | 2448 | 2449 | 2450 | 2451 | 2452 | 2453 | 2454 | 2455 | 2456 | 2457 |
CountryTraffic RankPageviewsVisitors
United States FlagUnited 2244 | States 2245 | 3628.7%27.3%
China FlagChina6411.1%12.2%
Japan FlagJapan3810.9%8.5%
India FlagIndia497.5%6.7%
Iran FlagIran383.2%3.6%
United Kingdom FlagUnited 2276 | Kingdom 2277 | 482.9%3.3%
Germany FlagGermany 2284 | 672.6%3.0%
South Korea FlagSouth 2291 | Korea 2292 | 622.6%2.6%
France FlagFrance 2299 | 742.2%2.4%
Canada FlagCanada 2306 | 452.4%2.3%
Brazil FlagBrazil 2313 | 1011.7%1.7%
Taiwan FlagTaiwan 2320 | 751.7%1.7%
Australia FlagAustralia 2327 | 571.3%1.4%
Italy FlagItaly1161.0%1.4%
Spain FlagSpain1061.1%1.3%
Poland FlagPoland 2346 | 700.9%1.1%
Netherlands FlagNetherlands 2353 | 460.8%0.9%
Turkey FlagTurkey 2360 | 1271.0%0.8%
Sweden FlagSweden 2367 | 370.7%0.8%
Vietnam FlagVietnam 2374 | 501.0%0.7%
Mexico FlagMexico 2381 | 1380.6%0.7%
Russia FlagRussia 2388 | 4290.4%0.7%
Hong Kong FlagHong 2395 | Kong 2396 | 700.7%0.6%
Norway FlagNorway 2403 | 430.6%0.6%
Nigeria FlagNigeria 2410 | 750.6%0.5%
Austria FlagAustria 2417 | 530.5%0.5%
Switzerland FlagSwitzerland 2424 | 490.5%0.5%
Belgium FlagBelgium 2431 | 610.4%0.5%
Portugal FlagPortugal 2438 | 490.4%0.5%
Denmark FlagDenmark 2445 | 360.4%0.5%
Other Countries9.5%10.5%
2458 |
2459 |

Traffic Statistics by Subdomain

2460 | 2461 | 2462 | 2463 | 2464 | 2465 | 2466 | 2467 | 2468 | 2469 | 2470 | 2471 | 2472 | 2473 | 2474 | 2475 | 2476 | 2477 | 2478 | 2479 | 2480 | 2481 | 2482 | 2483 | 2484 | 2485 | 2486 | 2487 | 2488 | 2489 | 2490 | 2491 | 2492 | 2493 | 2494 | 2495 | 2496 | 2497 | 2498 | 2499 | 2500 | 2501 | 2502 | 2503 | 2504 | 2505 | 2506 | 2507 | 2508 | 2509 | 2510 | 2511 | 2512 | 2513 | 2514 | 2515 | 2516 | 2517 | 2518 | 2519 | 2520 | 2521 | 2522 | 2523 | 2524 | 2525 | 2526 | 2527 | 2528 | 2529 | 2530 | 2531 | 2532 | 2533 | 2534 | 2535 | 2536 | 2537 | 2538 | 2539 | 2540 | 2541 | 2542 | 2543 | 2544 | 2545 | 2546 | 2547 | 2548 | 2549 | 2550 | 2551 | 2552 | 2553 | 2554 | 2555 | 2556 | 2557 | 2558 | 2559 | 2560 | 2561 |
SubdomainVisitorsPageviewsPageviews per Visitor
github.com94.61%91.88%4.8%
gist.github.com7.85%2.19%1.4%
help.github.com7.50%4.21%2.8%
codeload.github.com1.21%0.29%1.2%
desktop.github.com1.16%0.24%1.0%
guides.github.com0.66%0.15%1.1%
blog.github.com0.62%0.14%1.1%
status.github.com0.61%0.13%1.1%
developer.github.com0.42%0.15%1.8%
services.github.com0.35%0.08%1.2%
enterprise.github.com0.34%0.08%1.1%
education.github.com0.27%0.11%2.0%
pages.github.com0.27%0.06%1.0%
classroom.github.com0.17%0.08%2.5%
Other Subdomains0.20%
2562 |
2563 | 2564 | 2565 | 2567 | 2568 |

Similar Sites Like Github.com

2569 | 2570 | 2571 | 2572 | 2573 | 2574 | 2575 | 2576 | 2577 | 2578 | 2580 | 2581 | 2582 | 2583 | 2584 | 2585 | 2587 | 2588 | 2589 | 2591 | 2592 | 2593 | 2594 | 2595 | 2596 | 2597 | 2598 | 2599 | 2601 | 2602 | 2603 | 2604 | 2605 | 2606 |
zenofshen.com
Y Combinator - ycombinator.com
Xilinus - Développement de Web Application - 2579 | Consulting - Formati - xilinus.com
Xemion Web Design - xemion.com
Working With Rails - 2586 | workingwithrails.com
Best Open Source Resources for Web Developers | 2590 | WebAppers - webappers.com
Vimeo, Video Sharing For You - vimeo.com
Veille Perso - veilleperso.com
Uncooped | Outdoor adventure network - 2600 | uncooped.com
Ubuntu Linux - ubuntu.com
2607 |
2608 |

ODP Categories

2609 | 2610 | 2611 | 2612 | 2613 | 2614 | 2615 | 2618 | 2619 | 2620 |
Top » Computers » Open Source » Project Hosting
Top » Computers » Software » Configuration Management » Tools » 2616 | Git 2617 |
2621 |
2622 |

Reverse IP - Websites on the same IP Address

2623 | 2624 | 2625 | 2626 | 2628 | 2629 | 2630 | 2631 | 2632 | 2633 | 2634 | 2635 | 2636 | 2638 | 2639 | 2640 | 2641 | 2642 | 2643 | 2644 | 2645 | 2646 | 2647 | 2648 | 2649 |
lb-192-30-253-112-iad.github.com 2627 |
www.github.com
russbel.com
patch-diff.githubusercontent.com 2637 |
qqchushen.top
shop.github.com
lolkt.com
2650 |
2651 |

Websites with Similar Names

2652 | 2653 | 2654 | 2655 | 2656 | 2657 | 2658 | 2659 | 2660 | 2661 | 2662 | 2663 | 2664 | 2665 | 2666 | 2667 | 2668 | 2669 | 2670 | 2671 | 2672 | 2673 | 2674 | 2675 | 2676 | 2677 | 2678 | 2679 | 2680 | 2681 | 2682 | 2683 | 2684 | 2685 |
github.cm
github.cn
github.co
github.co.jp
github.coffee
github.com.cn
github.com.hk
github.community
github.de
github.design
2686 |
2687 |

See also: Domain List - Page 2688 | 998,450, Alexa Top Sites Rank 1 - 100

2689 | 2690 | 2691 | 2693 | 2694 |
2695 | 2851 |
2852 | 2864 | 2937 | 2938 | -------------------------------------------------------------------------------- /hosts: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1993-2009 Microsoft Corp. 2 | # 3 | # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. 4 | # 5 | # This file contains the mappings of IP addresses to host names. Each 6 | # entry should be kept on an individual line. The IP address should 7 | # be placed in the first column followed by the corresponding host name. 8 | # The IP address and the host name should be separated by at least one 9 | # space. 10 | # 11 | # Additionally, comments (such as these) may be inserted on individual 12 | # lines or following the machine name denoted by a '#' symbol. 13 | # 14 | 15 | # localhost name resolution is handled within DNS itself. 16 | # 127.0.0.1 localhost 17 | # ::1 localhost 18 | 19 | 127.0.0.1 activate.navicat.com 20 | 127.0.0.1 github.com 21 | 127.0.0.1 test.github.com -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use futures::future::*; 2 | use native_tls; 3 | use scraper::{Html, Selector}; 4 | use std::collections::HashMap; 5 | use std::error::Error; 6 | 7 | use std::net::{IpAddr, Ipv4Addr, ToSocketAddrs}; 8 | use std::sync::{Arc, RwLock}; 9 | 10 | use clap::{App, Arg}; 11 | use std::process::Command; 12 | use tokio::io::{AsyncReadExt, AsyncWriteExt}; 13 | use tokio::net::TcpStream; 14 | #[tokio::main] 15 | async fn main() -> Result<(), Box> { 16 | // let domains = [ 17 | // "github.com", 18 | // "github.global.ssl.fastly.net", 19 | // "codeload.github.com", 20 | // ]; 21 | let m = App::new("githubdns") 22 | .arg( 23 | Arg::with_name("domains") 24 | .long("domains") 25 | .help("set the dns needs to write to hosts") 26 | .default_value("github.com,github.global.ssl.fastly.net,codeload.github.com,assets-cdn.github.com") 27 | .required(false), 28 | ) 29 | .get_matches(); 30 | let domains_str = m.value_of("domains").unwrap().to_string(); 31 | let domains: Vec<&str> = domains_str.split(",").collect(); 32 | let domain_ips = Arc::new(RwLock::new(HashMap::new())); 33 | 34 | let mut v = Vec::new(); 35 | for domain in domains.iter() { 36 | let domain_ips = domain_ips.clone(); 37 | let domain = String::from(*domain); 38 | v.push(async move { 39 | let res = get(domain.clone()).await; 40 | if res.is_err() { 41 | println!( 42 | "get domain {},err {}", 43 | domain, 44 | res.unwrap_err().description() 45 | ); 46 | return; 47 | } 48 | let res = res.unwrap(); 49 | let ip = get_address(&res, domain.clone()); 50 | domain_ips.write().unwrap().insert(domain.clone(), ip); 51 | }); 52 | } 53 | join_all(v).await; 54 | 55 | // println!("domain_ips={:?}", domain_ips.read().unwrap()); 56 | let (hosts_file, enter) = get_hosts_file(); 57 | read_and_modify_hosts(domain_ips.clone(), &hosts_file, &enter).await; 58 | Ok(()) 59 | } 60 | //hosts文件路径,以及回车换行对应的是\r\n还是\n,\r 61 | //这里有一个副作用,会讲hosts文件的只读属性移除,可以考虑写入后再增加上, 62 | fn get_hosts_file() -> (String, String) { 63 | let info = sys_info::os_type(); 64 | if info.is_err() { 65 | panic!("unsupported os"); 66 | } 67 | let info = info.unwrap(); 68 | // Such as "Linux", "Darwin", "Windows". 69 | match info.as_str() { 70 | "Linux" => ("/etc/hosts".into(), "\n".into()), 71 | "Darwin" => ("/etc/hosts".into(), "\r".into()), 72 | "Windows" => { 73 | let path = r"C:\Windows\System32\drivers\etc\hosts"; 74 | //windows下hosts文件默认是只读的,如果不修改,后续会写不进去 75 | Command::new("attrib") 76 | .args(&["-R", path]) 77 | .output() 78 | .expect("remove readonly failed"); 79 | 80 | (path.into(), "\r\n".into()) 81 | } 82 | _ => panic!("not supported os {}", info), 83 | } 84 | } 85 | //读取hosts文件,如果其中已经有相关域名的设置,先删除,再添加 86 | //原有注释保持不动 87 | async fn read_and_modify_hosts( 88 | m: Arc>>, 89 | hosts_file: &str, 90 | enter: &str, 91 | ) { 92 | use tokio::fs; 93 | let flags = "# ----Generated By githubdns ---"; 94 | let hosts_file_name = hosts_file; 95 | let mut m = m.write().unwrap(); 96 | let contents = fs::read(hosts_file_name).await; 97 | if contents.is_err() { 98 | println!( 99 | "read {} err {}", 100 | hosts_file_name, 101 | contents.err().unwrap().description() 102 | ); 103 | return; 104 | } 105 | let contents = contents.unwrap(); 106 | let s = String::from_utf8(contents).unwrap(); 107 | let mut lines: Vec<&str> = s.split(enter).collect(); 108 | let mut i = 0; 109 | while i < lines.len() { 110 | let l = lines.get(i).unwrap().clone(); 111 | if l == flags { 112 | lines.remove(i); 113 | continue; 114 | } 115 | if l.trim_start().starts_with("#") { 116 | i += 1; 117 | continue; //注释行 118 | } 119 | let _ = m.iter().any(|(domain, ip)| { 120 | let pos = l.find(domain.as_str()); 121 | if pos.is_some() { 122 | //如果是这个domain的子域名,也不关心 123 | let pos = pos.unwrap(); 124 | if ip.len() > 0 125 | && pos > 0 126 | && (l.as_bytes()[pos - 1] == ' ' as u8 || l.as_bytes()[pos - 1] == '\t' as u8) 127 | { 128 | //是我们要找的完整的域名 129 | lines.remove(i); 130 | i -= 1; 131 | return true; 132 | } 133 | } 134 | return false; 135 | }); 136 | i += 1; 137 | } 138 | 139 | let mut lines: Vec<_> = lines.iter().map(|n| String::from(*n)).collect(); 140 | 141 | lines.push(flags.into()); 142 | for (domain, ip) in m.iter_mut() { 143 | if ip.len() > 0 { 144 | lines.push(format!("{}\t {}", ip, domain)); 145 | } 146 | } 147 | lines.push(flags.into()); 148 | let r = fs::write(hosts_file_name, lines.join(enter).as_bytes()).await; 149 | if r.is_err() { 150 | panic!("write to {} ,err={}", hosts_file, r.unwrap_err()); 151 | } 152 | } 153 | //解析url,返回对应的domain和path 154 | fn parse_url(domain: &str) -> (String, String) { 155 | let ss: Vec<_> = domain.split(".").collect(); 156 | let mut path = "/".into(); 157 | let mut domain: String = domain.into(); 158 | if ss.len() > 2 { 159 | path = format!("/{}", domain.clone()); 160 | domain = ss[ss.len() - 2..].join("."); 161 | } 162 | domain = format!("{}.ipaddress.com", domain); 163 | return (domain, path); 164 | } 165 | 166 | //根据url,获取其地址对应的html内容 167 | async fn get(domain: String) -> Result> { 168 | let (domain, path) = parse_url(domain.as_str()); 169 | println!("get {},{}", domain, path); 170 | // First up, resolve google.com 171 | let ip_port = format!("{}:443", domain.clone()); 172 | // println!("ip_port={}", ip_port); 173 | let addr = ip_port.to_socket_addrs().unwrap().next().unwrap(); 174 | // println!("addr={}", addr); 175 | let socket = TcpStream::connect(&addr).await.unwrap(); 176 | // Send off the request by first negotiating an SSL handshake, then writing 177 | // of our request, then flushing, then finally read off the response. 178 | let builder = native_tls::TlsConnector::builder(); 179 | let connector = builder.build().unwrap(); 180 | let connector = tokio_tls::TlsConnector::from(connector); 181 | let mut socket = connector.connect(domain.as_str(), socket).await?; 182 | socket 183 | .write_all(format!("GET {} HTTP/1.0\r\nHost:{}\r\n\r\n", path, domain).as_bytes()) 184 | .await?; 185 | 186 | let mut data = Vec::new(); 187 | socket.read_to_end(&mut data).await?; 188 | let s = String::from_utf8(data)?; 189 | let pos = s.find("\r\n\r\n").unwrap_or(0); 190 | let (_, body) = s.split_at(pos); 191 | // println!("body={}", body); 192 | Ok(String::from(body)) 193 | } 194 | //从html中提取domain对应的第一个ipv4地址 195 | fn get_address(data: &str, domain: String) -> String { 196 | let document = Html::parse_document(data); 197 | let ul_selector = Selector::parse("ul.comma-separated").unwrap(); 198 | let li_selector = Selector::parse("li").unwrap(); 199 | let ul = document.select(&ul_selector).next(); 200 | if ul.is_none() { 201 | println!("{} cannot found ul,data={}", domain, data); 202 | return String::new(); 203 | } 204 | let ul = ul.unwrap(); 205 | let mut ip_v4 = Ipv4Addr::new(127, 0, 0, 1); 206 | let found = ul.select(&li_selector).any(|n| { 207 | // println!("n={}", n.inner_html().trim()); 208 | let ip: Result = n.inner_html().trim().parse(); 209 | match ip { 210 | Err(_) => { 211 | return false; 212 | } 213 | Ok(ip) => match ip { 214 | IpAddr::V4(ipv4) => { 215 | ip_v4 = ipv4; 216 | return true; 217 | } 218 | _ => { 219 | return false; 220 | } 221 | }, 222 | } 223 | }); 224 | if found { 225 | return ip_v4.to_string(); 226 | } 227 | return String::new(); //没有找到就返回空 228 | } 229 | 230 | #[cfg(test)] 231 | mod test { 232 | use super::*; 233 | #[test] 234 | fn test_parse_url() { 235 | let u = parse_url("github.global.ssl.fastly.net"); 236 | assert_eq!( 237 | u, 238 | ( 239 | String::from("fastly.net.ipaddress.com"), 240 | String::from("/github.global.ssl.fastly.net") 241 | ) 242 | ); 243 | let u = parse_url("github.com"); 244 | assert_eq!( 245 | u, 246 | (String::from("github.com.ipaddress.com"), String::from("/")) 247 | ); 248 | } 249 | #[tokio::test] 250 | async fn test_get() { 251 | assert!(true); 252 | } 253 | #[test] 254 | fn test_get_address() { 255 | let data = std::fs::read_to_string("github.com.html").unwrap(); 256 | let ip = get_address(data.as_str(), String::from("github.com")); 257 | assert_eq!(ip, String::from("192.30.253.112")) 258 | } 259 | } 260 | --------------------------------------------------------------------------------