Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: More deep error messages #159

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions cmd/spoof-dpi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {

if *config.SystemProxy {
if err := util.SetOsProxy(*config.Port); err != nil {
log.Fatalf("error while changing proxy settings: %s", err)
log.Fatalf("error while changing proxy settings: %v", err)
}
}

Expand All @@ -68,7 +68,7 @@ func main() {

if *config.SystemProxy {
if err := util.UnsetOsProxy(); err != nil {
log.Fatal(err)
log.Fatalf("error while unsetting os proxy: %v", err)
}
}
}
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
25 changes: 13 additions & 12 deletions util/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ func SetOsProxy(port int) error {

network, err := getDefaultNetwork()
if err != nil {
return err
return fmt.Errorf("failed to get network interfaces: %w", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be network or network service. We extract an interface from the route command and try to find a NS that uses it.

Copy link
Author

@TeaDove TeaDove Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote it as failed to get default network, cause we are calling getDefaultNetwork.

}

args := fmt.Sprintf("'%s' 127.0.0.1 %d", network, port)

_, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+args).Output()
out, err := exec.Command("sh", "-c", "networksetup -setwebproxy "+args).Output()
if err != nil {
return err
return fmt.Errorf("failed to set web proxy, stdout: %s: %w", string(out), err)
}

_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+args).Output()
out, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+args).Output()
if err != nil {
return err
return fmt.Errorf("failed to set secure web proxy, stdout: %s: %w", string(out), err)
}

return nil
Expand All @@ -44,17 +44,17 @@ func UnsetOsProxy() error {

network, err := getDefaultNetwork()
if err != nil {
return err
return fmt.Errorf("failed to get network interfaces: %w", err)
}

_, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+network+"'"+" off").Output()
out, err := exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+network+"'"+" off").Output()
if err != nil {
return err
return fmt.Errorf("failed to set web proxy, stdout: %s: %w", string(out), err)
}

_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+network+"'"+" off").Output()
out, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+network+"'"+" off").Output()
if err != nil {
return err
return fmt.Errorf("failed to set secure web proxy, stdout: %s: %w", string(out), err)
}

return nil
Expand All @@ -63,8 +63,9 @@ func UnsetOsProxy() error {
func getDefaultNetwork() (string, error) {
network, err := exec.Command("sh", "-c", getDefaultNetworkCMD).Output()
if err != nil {
return "", err
} else if len(network) == 0 {
return "", fmt.Errorf("failed to get default network, stdout: %s: %w", string(network), err)
}
if len(network) == 0 {
return "", errors.New("no available networks")
}
return strings.TrimSpace(string(network)), nil
Expand Down