Skip to content

Commit

Permalink
go tools support all os
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Aug 8, 2014
1 parent a1bda82 commit 271f28c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ $ nslookup -q=TXT _netblocks.google.com 8.8.4.4
### 小技巧

* 请使用国际版google. 防止google本地化重定向: 访问一下<https://www.google.com/ncr>
* 请使用`https`而不是`http`访问.
* 请使用`https`替代`http`访问.

---

### Contributing

* vim:ts=4:sw=4:expandtab
* vim:ts=4:sw=4:expandtab:ff=unix:encoding=utf8
* Please create your pull request on `develop` branch

### License
Expand Down
Binary file modified tools/fuckGFW-32.exe
100644 → 100755
Binary file not shown.
Binary file modified tools/fuckGFW-64.exe
100644 → 100755
Binary file not shown.
74 changes: 45 additions & 29 deletions tools/fuckGFW.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Update hosts for windows
// Update hosts for all OS
//

package main
Expand All @@ -12,53 +12,69 @@ import (
"time"
"bytes"
"io/ioutil"
"runtime"
"fmt"
)

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

func Exit(message string){
fmt.Println(message)
time.Sleep(5 * time.Second)
os.Exit(0)
}

func main(){
var hosts []byte
BR := "\n"
HOSTS_PATH := "/etc/hosts"
if runtime.GOOS == "windows"{
BR = "\r\n"
HOSTS_PATH = os.Getenv("SYSTEMROOT")+"\\system32\\drivers\\etc\\hosts"
}

r, err := http.Get(HOSTS_SOURCE)
if err != nil {
Exit(err.Error() + "Try again.")
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
Exit(err.Error())
}
hosts := string(bytes.Replace(data, []byte("\n"), []byte(BR), -1))

yours := ""
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{
if bytes.Equal(line,[]byte(SEARCH_STRING)) || err == io.EOF{
break
}
hosts = append(hosts, append(line,[]byte("\r\n")...)...)
yours += string(line) + BR
}
err = f.Close()
if err != nil {
Exit(err.Error())
}
err = os.Rename(HOSTS_PATH, HOSTS_PATH+".BAK")
if err != nil {
Exit(err.Error())
}
f.Close()
}
hosts = append(hosts, append(SEARCH_STRING,[]byte("\r\n")...)...)
yours += SEARCH_STRING + BR

res, err := http.Get(HOSTS_SOURCE)
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
Exit(err.Error())
}
data, err := ioutil.ReadAll(res.Body)
_, err = f.WriteString(yours + hosts)
if err != nil {
println(err.Error())
time.Sleep(3 * time.Second)
return
Exit(err.Error())
}
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)
Exit("Success!")
}

0 comments on commit 271f28c

Please sign in to comment.