<file_sep>'''
File: updateHosts.py
Author: <EMAIL>
Version: 0.0.1
Date: 2012-10-24 14:35:39
'''

import urllib
import os

def GetRemoteHosts(hostsUrl):
    fp = urllib.urlopen(hostsUrl)
    hosts = [line for line in fp]
    fp.close()
    return hosts

if __name__ == "__main__":
    hosts = GetRemoteHosts("http://tx.txthinking.com/hosts")
    hostsPath = "/etc/hosts"

    search = "#TX-HOSTS\n"
    yourHosts = ""
    if os.path.isfile(hostsPath):
        fp = open(hostsPath, "r")
        for line in fp:
            if line == search:
                break
            yourHosts += line
        fp.close()
    yourHosts += search

    os.rename(hostsPath, hostsPath + ".BAK")

    fp = open(hostsPath, "w")
    fp.write(yourHosts)
    fp.writelines(hosts) #iterable sequence hosts
    fp.close()

    print "Success"

<file_sep>//
// Update hosts for windows
//

package main

import (
    "os"
    "io"
    "bufio"
    "net/http"
    "time"
    "bytes"
    "io/ioutil"
)

var (
    HOSTS_PATH string = os.Getenv("SYSTEMROOT")+"\\system32\\drivers\\etc\\hosts"
    SEARCH_STRING []byte = []byte("#TX-HOSTS")
    HOSTS_SOURCE string = "http://tx.txthinking.com/hosts"
)

func main(){
    var hosts []byte
    f, err := os.OpenFile(HOSTS_PATH, os.O_RDONLY, 0444)
    if err == nil {
        bnr := bufio.NewReader(f)
        for{
            line, _, err := bnr.ReadLine()
            if bytes.Compare(line,SEARCH_STRING)==0 || err == io.EOF{
                break
            }
            hosts = append(hosts, append(line,[]byte("\r\n")...)...)
        }
        f.Close()
    }
    hosts = append(hosts, append(SEARCH_STRING,[]byte("\r\n")...)...)

    res, err := http.Get(HOSTS_SOURCE)
    if err != nil {
        println(err.Error())
        time.Sleep(3 * time.Second)
        return
    }
    data, err := ioutil.ReadAll(res.Body)
    if err != nil {
        println(err.Error())
        time.Sleep(3 * time.Second)
        return
    }
    data = bytes.Replace(data, []byte("\n"), []byte("\r\n"), -1)
    hosts = append(hosts, data...)

    os.Rename(HOSTS_PATH, HOSTS_PATH+"-BAK-TX-HOSTS")
    f, err = os.OpenFile(HOSTS_PATH, os.O_WRONLY|os.O_CREATE, 0644)
    if err != nil {
        println(err.Error())
        time.Sleep(3 * time.Second)
        return
    }
    f.Write(hosts)
    println("Success!")
    time.Sleep(3 * time.Second)
}
<file_sep>google-hosts
============

### 为什么有这个项目

因为我每天都要访问Google, 以及我的朋友们也会访问Google.<br/>
然而朋友们对其他更复杂的代理未做深入研究, 最简单方法便是帮他们修改 hosts.<br/>
网上其他 hosts 项目有时未能及时更新, 为了朋友们方便, 所以便弄了这个项目.<br/>
此项目参考了[smarthosts][smarthosts], [ipv6-hosts][ipv6-hosts].<br/>

IP不总是可用的, 因素可能是GFW封锁, Google IP变动.<br/>
另外Google的好多服务都已经不挂在北京的IP上了<br/>
你可以用此脚本自己去寻找可用IP.

***

### 脚本如何使用

直接使用DNS解析获取所有域名的IP(这个只能跳过DNS污染, 并不能保证IP是否被封锁)

```
$ cd google-hosts/scripts
$ make #结果会输出到google-hosts/hosts文件
```

查询某段IP详细信息(如:192.168.1.x)(这个可检测IP是否被封锁, 443端口是否被封锁)

```
$ cd google-hosts/scripts
$ ./getssl.sh 192.168.1
```

查询某段IP详细信息(如:192.168.x.x)

```
$ cd google-hosts/scripts
$ ./find.sh 192.168
```

输出的四个字段含义

| IP | LOSS | TIME | SSL |
| --- | --- | --- | --- |
| 此IP | 丢包率| PING值 | 可用ssl域名 |

另外获取Google IP段可供参考

```
$ nslookup -q=TXT _netblocks.google.com 8.8.8.8
```

***

> \>\>[hosts][hosts]\<\< *UPDATE: Mon Jun 9 07:21:51 UTC 2014* <br/>
> 下面的两个程序是用来将此hosts文件替换你系统hosts. 尤其是你的不懂程序的Windows朋友<br/>
> **注意**: **如果**此hosts文件内的IP失效, 就需要你自己用脚本查询了(如果你查到好的IP不妨pull一下 :D)<br/>

### Windows 用户

* 下载[fuckGFW-64.exe][fuckGFW-64.exe](64位)或[fuckGFW-32.exe][fuckGFW-32.exe](32位)双击运行一下即可
* 要更新的话, 也是双击运行一下
* 此程序不会覆盖你原有的 hosts

### *nix/OSX 用户

* 下载此脚本 [updateHosts.py][updateHosts.py]
* 执行 `$ sudo python updateHosts.py`
* 此程序不会覆盖你原有的 hosts

[hosts]: http://tx.txthinking.com/hosts
[fuckGFW-64.exe]: http://tx.txthinking.com/fuckGFW-64.exe
[fuckGFW-32.exe]: http://tx.txthinking.com/fuckGFW-32.exe
[smarthosts]: https://code.google.com/p/smarthosts/
[ipv6-hosts]: https://code.google.com/p/ipv6-hosts/
[updateHosts.py]: https://github.com/txthinking/google-hosts/tree/master/scripts/updateHosts.py
<file_sep>#!/usr/bin/env bash
#
# 会查询一个IP段的IP得到其 PING值,丢包率,SSL可用于的域名
#
# EP: 查询192.168.1.x的IP
# $ ./getssl.sh 192.168.1 
#
# Author: <EMAIL>
#

if [ ! -d output ]
then
    mkdir output;
fi
output=output/$1.x;
> $output;
echo -e "IP\tLOSS\tTIME\tSSL"
for((i=0;i<255;i++))
do
    ip=${1}.${i};
    c=$(nmap --host-timeout 3s $ip -p 443 2>/dev/null | grep -Ec "443/tcp open");
    if [ $c -ne 1 ]
    then
        echo -e "$ip\tNO\tNO\tNO";
        echo -e "$ip\tNO\tNO\tNO" >> $output;
        continue;
    fi
    cer=$(curl https://$ip 2>&1 | grep -Eo "'\S*'" |head -1);
    if [ -z $cer ]
    then
        echo -e "$ip\tNO\tNO\tNO";
        echo -e "$ip\tNO\tNO\tNO" >> $output;
        continue;
    fi
    ping -c 5 -w 5 $ip > /tmp/ping;
    loss=$(grep -Eo "\w+%" /tmp/ping);
    c=$(grep -c "time=" /tmp/ping);
    if [ $c -eq 0 ]
    then
        echo -e "$ip\t$loss\tNO\t$cer";
        echo -e "$ip\t$loss\tNO\t$cer" >> $output;
        continue;
    fi
    avgtime=$(grep -E "time=" /tmp/ping | awk '{print $7}' | awk 'BEGIN {FS="=";s=0;c=0;}{s+=$2;c++;} END {print s/c}');
    echo -e "$ip\t$loss\t$avgtime\t$cer";
    echo -e "$ip\t$loss\t$avgtime\t$cer" >> $output;
done
sort -k4 -k3n $output -o $output;
sed -i "1iIP\tLOSS\tTIME\tSSL" $output;
cat $output;
echo "[INFO] Done in $output";

<file_sep>#!/usr/bin/env bash
#
# 将hosts.all内容更新到../hosts文件
#
# Author: <EMAIL>
#

> ../hosts
echo "#" >> ../hosts
echo "# link: https://github.com/txthinking/google-hosts" >> ../hosts
echo "#" >> ../hosts
echo "# UPDATE: `date -u`" >> ../hosts
echo "#" >> ../hosts
echo "127.0.0.1    localhost" >> ../hosts
cat hosts.all >> ../hosts

<file_sep>all: do apply

do:
	chmod +x getip.sh
	./getip.sh

apply:
	chmod +x apply.sh
	./apply.sh

.PHONY: all
<file_sep>#!/usr/bin/env bash
#
# 这个是getssl.sh的一个wrapper
# 
# EP: 查询192.168.x.x的IP
# $ ./find 192.168
#
# Author: <EMAIL>
#

for((i=0;i<255;i++))
do
    ./getssl.sh ${1}.${i}
done

