Skip to content

Commit

Permalink
Please change your Windows to UTF-8
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Jun 13, 2018
1 parent 9c747fd commit 8b5e51e
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 67 deletions.
42 changes: 34 additions & 8 deletions sysproxy/system_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"regexp"
)

func getNetworkServices() ([]string, error) {
// GetNetworkInterfaces returns interface list
func GetNetworkInterfaces() ([]string, error) {
c := exec.Command("networksetup", "-listallnetworkservices")
out, err := c.CombinedOutput()
if err != nil {
return nil, errors.New("ns lans:" + string(out) + ":" + err.Error())
return nil, errors.New(string(out) + err.Error())
}
nss := make([]string, 0)
reg := regexp.MustCompile(`\d+\.\d+\.\d+\.\d+`)
Expand All @@ -24,7 +25,7 @@ func getNetworkServices() ([]string, error) {
c := exec.Command("networksetup", "-getinfo", ns)
out, err := c.CombinedOutput()
if err != nil {
return nil, errors.New("ns gi:" + string(out) + ":" + err.Error())
return nil, errors.New(string(out) + err.Error())
}
if !reg.MatchString(string(out)) {
continue
Expand All @@ -39,34 +40,59 @@ func getNetworkServices() ([]string, error) {

// TurnOnSystemProxy used to enable system pac proxy, pac is a URL.
func TurnOnSystemProxy(pac string) error {
nss, err := getNetworkServices()
nss, err := GetNetworkInterfaces()
if err != nil {
return err
}
for _, v := range nss {
c := exec.Command("networksetup", "-setautoproxyurl", v, pac)
if out, err := c.CombinedOutput(); err != nil {
return errors.New("ns apu:" + string(out) + ":" + err.Error())
return errors.New(string(out) + err.Error())
}
c = exec.Command("networksetup", "-setautoproxystate", v, "on")
if out, err := c.CombinedOutput(); err != nil {
return errors.New("ns aps:" + string(out) + ":" + err.Error())
return errors.New(string(out) + err.Error())
}
}
return nil
}

// TurnOffSystemProxy used to disable system pac proxy
func TurnOffSystemProxy() error {
nss, err := getNetworkServices()
nss, err := GetNetworkInterfaces()
if err != nil {
return err
}
for _, v := range nss {
c := exec.Command("networksetup", "-setautoproxystate", v, "off")
if out, err := c.CombinedOutput(); err != nil {
return errors.New("ns aps:" + string(out) + ":" + err.Error())
return errors.New(string(out) + err.Error())
}
}
return nil
}

// SetDNSServer used to set system DNS server
func SetDNSServer(server string) error {
nis, err := GetNetworkInterfaces()
if err != nil {
return err
}
for _, v := range nis {
c := exec.Command("networksetup", "-setdnsservers", v, server)
if out, err := c.CombinedOutput(); err != nil {
return errors.New(string(out) + err.Error())
}
}
return nil
}

// GetDefaultGateway returns default gateway
func GetDefaultGateway() (string, error) {
c := exec.Command("sh", "-c", "route get default | grep gateway | awk '{print $2}'")
out, err := c.CombinedOutput()
if err != nil {
return "", errors.New(string(out) + err.Error())
}
return string(out), nil
}
7 changes: 7 additions & 0 deletions sysproxy/system_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ func TestGetNetworkServices(t *testing.T) {
t.Log("|" + v + "|")
}
}

func TestSetDNSServer(t *testing.T) {
err := SetDNSServer("8.8.4.4")
if err != nil {
t.Fatal(err)
}
}
45 changes: 45 additions & 0 deletions sysproxy/system_linux.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
package sysproxy

import (
"errors"
"os"
"os/exec"
"regexp"
)

// GetNetworkInterfaces returns interface list
func GetNetworkInterfaces() ([]string, error) {
return []string{}, nil
}

func TurnOnSystemProxy(pac string) error {
return nil
}

func TurnOffSystemProxy() error {
return nil
}

// SetDNSServer used to set system DNS server
func SetDNSServer(server string) error {
f, err := os.OpenFile("/etc/resolv.conf", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString("nameserver " + server)
if err != nil {
return err
}
return nil
}

// GetDefaultGateway returns default gateway
func GetDefaultGateway() (string, error) {
c := exec.Command("ip", "route")
out, err := c.CombinedOutput()
if err != nil {
return "", errors.New(string(out) + err.Error())
}
r, err := regexp.Compile(`default.*?(\d+.\d+\.\d+\.\d+)`)
if err != nil {
return "", err
}
ss := r.FindStringSubmatch(string(out))
if len(ss) == 0 {
return "", errors.New("Can not find default gateway")
}
return ss[1], nil
}
136 changes: 77 additions & 59 deletions sysproxy/system_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"syscall"
"time"

"github.com/txthinking/ant"
)

// TurnOnSystemProxy used to enable system pac proxy, pac is a URL.
func TurnOnSystemProxy(pac string) error {
c := exec.Command(`reg`, `add`, `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings`, `/v`, `AutoConfigURL`, `/t`, `REG_SZ`, `/d`, pac, `/f`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("reg add acu g2u:" + e.Error())
}
return errors.New("reg add acu:" + string(u) + ":" + err.Error())
return errors.New(string(out) + err.Error())
}
c = exec.Command(`reg`, `add`, `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings`, `/v`, `ProxyEnable`, `/t`, `REG_DWORD`, `/d`, `0`, `/f`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("reg add pe g2u:" + e.Error())
}
return errors.New("reg add pe:" + string(u) + ":" + err.Error())
return errors.New(string(out) + err.Error())
}
if err := restartWindowsIE(); err != nil {
return err
Expand All @@ -42,27 +33,15 @@ func TurnOnSystemProxy(pac string) error {
func TurnOffSystemProxy() error {
c := exec.Command(`reg`, `query`, `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("reg query acu g2u:" + e.Error())
}
return errors.New("reg query acu:" + string(u) + ":" + err.Error())
} else {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("reg query acu g2u:" + e.Error())
}
if bytes.Contains(bytes.ToLower(u), []byte("autoconfigurl")) {
c := exec.Command(`reg`, `delete`, `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings`, `/v`, `AutoConfigURL`, `/f`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("reg delete acu g2u:" + e.Error())
}
return errors.New("reg delete acu:" + string(u) + ":" + err.Error())
}
out, err := c.CombinedOutput()
if err != nil {
return errors.New("Can not query proxy settings: " + err.Error())
}
if bytes.Contains(bytes.ToLower(out), []byte("autoconfigurl")) {
c := exec.Command(`reg`, `delete`, `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings`, `/v`, `AutoConfigURL`, `/f`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
return errors.New(string(out) + err.Error())
}
}
if err := restartWindowsIE(); err != nil {
Expand All @@ -74,43 +53,82 @@ func TurnOffSystemProxy() error {
func restartWindowsIE() error {
c := exec.Command(`tasklist`, `/fo`, `list`, `/fi`, `imagename eq iexplore.exe`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("tasklist ie g2u:" + e.Error())
}
return errors.New("tasklist ie:" + string(u) + ":" + err.Error())
} else {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("tasklist ie g2u:" + e.Error())
}
if bytes.Contains(bytes.ToLower(u), []byte("iexplore.exe")) {
c := exec.Command(`taskkill`, `/f`, `/t`, `/im`, `iexplore.exe`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("taskkill ie before start g2u:" + e.Error())
}
return errors.New("taskkill ie before start:" + string(u) + ":" + err.Error())
}
out, err := c.CombinedOutput()
if err != nil {
return errors.New(string(out) + err.Error())
}
if bytes.Contains(bytes.ToLower(out), []byte("iexplore.exe")) {
c := exec.Command(`taskkill`, `/f`, `/t`, `/im`, `iexplore.exe`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
return errors.New(string(out) + err.Error())
}
}
c = exec.Command(fmt.Sprintf(`%s\Internet Explorer\iexplore.exe`, os.Getenv("PROGRAMFILES")), `-nohome`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if err := c.Start(); err != nil {
return errors.New("ie start:" + err.Error())
return errors.New("Start IE:" + err.Error())
}
time.Sleep(2 * time.Second)
c = exec.Command(`taskkill`, `/f`, `/t`, `/im`, `iexplore.exe`)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
u, e := ant.GBK2UTF8(out)
if e != nil {
return errors.New("taskkill ie after start g2u:" + e.Error())
return errors.New(string(out) + err.Error())
}
return nil
}

// GetNetworkInterfaces returns interface list
func GetNetworkInterfaces() ([]string, error) {
c := exec.Command("netsh", "interface", "ipv4", "show", "address")
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := c.CombinedOutput()
if err != nil {
return nil, errors.New(string(out) + err.Error())
}
r, err := regexp.Compile(`"(.+)".*\n.*Yes`)
if err != nil {
return nil, err
}
ss := r.FindAllStringSubmatch(string(out), -1)
is := make([]string, 0)
for _, v := range ss {
is = append(is, v[1])
}
return is, nil
}

// SetDNSServer used to set system DNS server
func SetDNSServer(server string) error {
nis, err := GetNetworkInterfaces()
if err != nil {
return err
}
for _, v := range nis {
c := exec.Command("netsh", "interface", "ip", "set", "dnsservers", v, "static", server, "primary")
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if out, err := c.CombinedOutput(); err != nil {
return errors.New(string(out) + err.Error())
}
return errors.New("taskkill ie after start:" + string(u) + ":" + err.Error())
}
return nil
}

// GetDefaultGateway returns default gateway
func GetDefaultGateway() (string, error) {
c := exec.Command("netsh", "interface", "ipv4", "show", "address")
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := c.CombinedOutput()
if err != nil {
return "", errors.New(string(out) + err.Error())
}
r, err := regexp.Compile(`Default Gateway.*?(\d+.\d+\.\d+\.\d+)`)
if err != nil {
return "", err
}
ss := r.FindStringSubmatch(string(out))
if len(ss) == 0 {
return "", errors.New("Can not find default gateway")
}
return ss[1], nil
}

0 comments on commit 8b5e51e

Please sign in to comment.