-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: transition from DNS server to hosts file modification
Updated main.go to change the functionality: - Removed DNS server setup. - Added code to modify the hosts file on Windows. - Incorporated testing connection functionality using 'ping'. - Enhanced logging for better clarity.
- Loading branch information
BadEnd777
committed
Jan 21, 2024
1 parent
f37d219
commit 74233b4
Showing
7 changed files
with
213 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func createBackups(filePath string) ([]string, error) { | ||
var backups []string | ||
for i := 0; ; i++ { | ||
backupPath := fmt.Sprintf("%s.backup-%d", filePath, i) | ||
if _, err := os.Stat(backupPath); os.IsNotExist(err) { | ||
err := exec.Command("cmd", "/C", "copy", filePath, backupPath).Run() | ||
if err != nil { | ||
return nil, err | ||
} | ||
backups = append(backups, backupPath) | ||
break | ||
} | ||
} | ||
return backups, nil | ||
} | ||
|
||
func readHostsFile(filePath string) ([]string, error) { | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
var hosts []string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := strings.TrimSpace(scanner.Text()) | ||
if len(line) > 0 && !strings.HasPrefix(line, "#") { | ||
fields := strings.Fields(line) | ||
if len(fields) >= 2 { | ||
hosts = append(hosts, fields[1]) | ||
} | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return hosts, nil | ||
} | ||
|
||
func addHostToHostsFile(filePath, host string) error { | ||
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, os.ModeAppend) // Open file in append mode | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
if _, err := file.WriteString(fmt.Sprintf("0.0.0.0 %s\n", host)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func contains(slice []string, item string) bool { | ||
for _, s := range slice { | ||
if s == item { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func flushDNSCache() error { | ||
cmd := exec.Command("ipconfig", "/flushdns") | ||
err := cmd.Run() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
var hostsToAdd = []string{ | ||
"api.amplitude.com", | ||
"api2.amplitude.com", | ||
"api.lab.amplitude.com", | ||
"api.eu.amplitude.com", | ||
"regionconfig.amplitude.com", | ||
"regionconfig.eu.amplitude.com", | ||
"o1125869.ingest.sentry.io", | ||
"api3.amplitude.com", | ||
"cdn.amplitude.com", | ||
"info.amplitude.com", | ||
"static.amplitude.com", | ||
"api.uca.cloud.unity3d.com", | ||
"config.uca.cloud.unity3d.com", | ||
"perf-events.cloud.unity3d.com", | ||
"public.cloud.unity3d.com", | ||
"cdp.cloud.unity3d.com", | ||
"data-optout-service.uca.cloud.unity3d.com", | ||
"ecommerce.iap.unity3d.com", | ||
} | ||
|
||
func main() { | ||
// Get logger | ||
log := GetLogger() | ||
|
||
const hostsFilePath = "C:\\Windows\\System32\\drivers\\etc\\hosts" // C:\Windows\System32\drivers\etc\hosts | ||
existingHosts, err := readHostsFile(hostsFilePath) | ||
if err != nil { | ||
log.Error("Error reading hosts file in readHostsFile:", err) | ||
return | ||
} | ||
|
||
backups, err := createBackups(hostsFilePath) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("Error creating backups: %s", err)) | ||
} else { | ||
log.Success(fmt.Sprintf("Successfully created backups: %s", strings.Join(backups, ", "))) | ||
} | ||
|
||
for _, host := range hostsToAdd { | ||
if contains(existingHosts, host) { | ||
log.Warn(fmt.Sprintf("Host %s already exists in hosts file", host)) | ||
} else { | ||
err := addHostToHostsFile(hostsFilePath, host) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("Error adding host %s to hosts file: %s", host, err)) | ||
} else { | ||
log.Success(fmt.Sprintf("Successfully added host %s to hosts file", host)) | ||
} | ||
} | ||
} | ||
|
||
// Flush DNS cache | ||
err = flushDNSCache() | ||
if err != nil { | ||
log.Error(fmt.Sprintf("Error flushing DNS cache: %s", err)) | ||
} else { | ||
log.Success("Successfully flushed DNS cache") | ||
} | ||
|
||
comfirmation := log.Prompt("Do you want to test the connection? (y/n): ") | ||
for comfirmation != "y" && comfirmation != "n" { | ||
comfirmation = log.Prompt("Please enter y or n: ") | ||
} | ||
|
||
if comfirmation == "y" { | ||
// Test connection | ||
log.Info("Testing connection...") | ||
for _, host := range hostsToAdd { | ||
cmd := exec.Command("ping", host, "-n", "1", "-w", "1000") // Ping host once with 1 second timeout | ||
err := cmd.Run() | ||
if err != nil { | ||
log.Error(fmt.Sprintf("Error pinging host %s: %s", host, err)) | ||
} else { | ||
log.Success(fmt.Sprintf("Successfully pinged host %s", host)) | ||
} | ||
} | ||
|
||
log.Info("This program will block the a VRChat analytics and telemetry servers.") | ||
log.Info("If it works, you should see a lot of \"Error pinging host\" messages.") | ||
log.Info("Because the program blocks the servers, it will not be able to connect to them.") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.