From cd583582031b69b1c94554e2e225613e661213c4 Mon Sep 17 00:00:00 2001 From: ledu Date: Mon, 16 Jun 2014 20:35:01 +0800 Subject: [PATCH 1/3] Update updateHosts.go --- scripts/updateHosts.go | 92 +++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/scripts/updateHosts.go b/scripts/updateHosts.go index 0ad3fb1f..836b9ec2 100644 --- a/scripts/updateHosts.go +++ b/scripts/updateHosts.go @@ -1,58 +1,66 @@ // -// @file updateHostsForWindows.go -// @brief for windows -// @author cloud@txthinking.com -// @version 0.0.1 -// @date 2013-03-15 +// Update hosts for windows +// cloud@txthinking.com +// date 2013-03-15 // package main import ( "os" - "io" + "io" "bufio" - "strings" - "net/http" + "net/http" + "time" + "bytes" + "io/ioutil" ) -const ( - HOSTS_PATH string = "C:\\windows\\system32\\drivers\\etc\\hosts" - SEARCH_STRING string = "#TX-HOSTS" - HOSTS_SOURCE string = "http://tx.txthinking.com/hosts" +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 string - f, _ := os.OpenFile(HOSTS_PATH, os.O_RDONLY, 0444) - bnr := bufio.NewReader(f) - for{ - line, err := bnr.ReadString('\n') - if strings.Contains(line, SEARCH_STRING) { - break - } - hosts += line - if err==io.EOF { - break - } - } - f.Close(); - hosts += "\r\n" - hosts += SEARCH_STRING - hosts += "\r\n" + 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, _ := http.Get(HOSTS_SOURCE) - bnr = bufio.NewReader(res.Body) - for{ - line, err := bnr.ReadString('\n') - hosts += line[0:len(line)-1] + "\r\n" - if err==io.EOF { - break - } - } + 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") - f, _ = os.OpenFile(HOSTS_PATH, os.O_WRONLY|os.O_CREATE, 0644) - f.WriteString(hosts); - println("Success!") + 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) } From 26924ec46440ad89d0e9606e7a768e3fb7a1c29e Mon Sep 17 00:00:00 2001 From: ledu Date: Mon, 16 Jun 2014 20:35:58 +0800 Subject: [PATCH 2/3] format --- scripts/updateHosts.go | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/updateHosts.go b/scripts/updateHosts.go index 836b9ec2..60fa7ed8 100644 --- a/scripts/updateHosts.go +++ b/scripts/updateHosts.go @@ -63,4 +63,3 @@ func main(){ println("Success!") time.Sleep(3 * time.Second) } - From e0b6cc728291b19360badfa49ac0ba34ec70e304 Mon Sep 17 00:00:00 2001 From: ledu Date: Mon, 16 Jun 2014 20:40:30 +0800 Subject: [PATCH 3/3] Update updateHosts.go --- scripts/updateHosts.go | 87 +++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/scripts/updateHosts.go b/scripts/updateHosts.go index 60fa7ed8..ffa2d176 100644 --- a/scripts/updateHosts.go +++ b/scripts/updateHosts.go @@ -1,65 +1,64 @@ // // Update hosts for windows -// cloud@txthinking.com -// date 2013-03-15 // + package main import ( "os" - "io" + "io" "bufio" - "net/http" - "time" - "bytes" - "io/ioutil" + "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" + SEARCH_STRING []byte = []byte("#TX-HOSTS") + HOSTS_SOURCE string = "http://tx.txthinking.com/hosts" ) func main(){ - var hosts []byte + 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")...)...) + 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 - } + 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...) + 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") + 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) + if err != nil { + println(err.Error()) + time.Sleep(3 * time.Second) + return + } + f.Write(hosts) + println("Success!") + time.Sleep(3 * time.Second) }