Fix compile issue and add -force flag to bypass VPN check
- Updated golang.org/x/net dependency to fix linking error with syscall.recvmsg - Added -force flag to allow running the tool while connected to VPN - Fixed linter errors: replaced deprecated ioutil with io/os, added error handling - Added test, fmt, and lint targets to Makefile
This commit is contained in:
@@ -2,8 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
@@ -23,15 +24,15 @@ type RelayLatency struct {
|
||||
|
||||
// MullvadIPResponse represents the response from the Mullvad IP check API
|
||||
type MullvadIPResponse struct {
|
||||
IP string `json:"ip"`
|
||||
Country string `json:"country"`
|
||||
City string `json:"city"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
MullvadExitIP bool `json:"mullvad_exit_ip"`
|
||||
MullvadExitIPHostname string `json:"mullvad_exit_ip_hostname,omitempty"`
|
||||
MullvadServerType string `json:"mullvad_server_type,omitempty"`
|
||||
Blacklisted struct {
|
||||
IP string `json:"ip"`
|
||||
Country string `json:"country"`
|
||||
City string `json:"city"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
MullvadExitIP bool `json:"mullvad_exit_ip"`
|
||||
MullvadExitIPHostname string `json:"mullvad_exit_ip_hostname,omitempty"`
|
||||
MullvadServerType string `json:"mullvad_server_type,omitempty"`
|
||||
Blacklisted struct {
|
||||
Blacklisted bool `json:"blacklisted"`
|
||||
Results []string `json:"results"`
|
||||
} `json:"blacklisted"`
|
||||
@@ -46,7 +47,7 @@ func livenessWorker(id int, jobs <-chan Relay, results chan<- RelayLatency, wg *
|
||||
if err == nil && isLive {
|
||||
results <- RelayLatency{Relay: relay, Latency: latency}
|
||||
}
|
||||
bar.Add(1)
|
||||
_ = bar.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +62,7 @@ func latencyWorker(id int, jobs <-chan RelayLatency, results chan<- RelayLatency
|
||||
Latency: latency,
|
||||
}
|
||||
}
|
||||
bar.Add(1)
|
||||
_ = bar.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +204,7 @@ func CheckMullvadExitIP() (bool, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to read response body: %v", err)
|
||||
}
|
||||
@@ -217,6 +218,10 @@ func CheckMullvadExitIP() (bool, error) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Define force flag
|
||||
force := flag.Bool("force", false, "Force execution even when connected to VPN")
|
||||
flag.Parse()
|
||||
|
||||
currentUser, err := user.Current()
|
||||
if err != nil {
|
||||
panic("Failed to get current user")
|
||||
@@ -231,8 +236,9 @@ func main() {
|
||||
panic(fmt.Sprintf("Error checking Mullvad exit IP: %v", err))
|
||||
}
|
||||
|
||||
if isMullvadExitIP {
|
||||
if isMullvadExitIP && !*force {
|
||||
fmt.Println("This program is designed to test latency between your actual IP and the Mullvad VPN servers. Please disconnect from the VPN and run the program again.")
|
||||
fmt.Println("Use -force flag to bypass this check.")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -46,7 +45,9 @@ func (r Relay) CheckLiveness() (bool, time.Duration, error) {
|
||||
pinger.Count = 1
|
||||
pinger.Timeout = 3 * time.Second // Increased timeout for the single ping
|
||||
pinger.SetPrivileged(true)
|
||||
pinger.Run()
|
||||
if err := pinger.Run(); err != nil {
|
||||
return false, 0, err
|
||||
}
|
||||
stats := pinger.Statistics()
|
||||
if stats.PacketsRecv == 0 {
|
||||
return false, 0, nil
|
||||
@@ -64,7 +65,9 @@ func (r Relay) MeasureLatency() (time.Duration, error) {
|
||||
pinger.Interval = 1 * time.Second // Adding interval between pings
|
||||
pinger.Timeout = 10 * time.Second // Increased overall timeout
|
||||
pinger.SetPrivileged(true)
|
||||
pinger.Run()
|
||||
if err := pinger.Run(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
stats := pinger.Statistics()
|
||||
return stats.MinRtt, nil
|
||||
}
|
||||
@@ -110,7 +113,7 @@ func ParseRelayData() ([]Relay, error) {
|
||||
return nil, fmt.Errorf("failed to read embedded file: %v", err)
|
||||
}
|
||||
} else {
|
||||
fileData, err = ioutil.ReadFile(filePath)
|
||||
fileData, err = os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read file: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user