Skip to content

Commit

Permalink
Update updateHosts.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ledu committed Jun 16, 2014
1 parent 26924ec commit e0b6cc7
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions scripts/updateHosts.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
//
// Update hosts for windows
// [email protected]
// 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:https://tx.txthinking.com/hosts"
SEARCH_STRING []byte = []byte("#TX-HOSTS")
HOSTS_SOURCE string = "http:https://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)
}

0 comments on commit e0b6cc7

Please sign in to comment.