Skip to content

Instantly share code, notes, and snippets.

@reynldi
Created December 10, 2024 06:26
Show Gist options
  • Select an option

  • Save reynldi/8ceee73cf54323a3d76d55737887abc9 to your computer and use it in GitHub Desktop.

Select an option

Save reynldi/8ceee73cf54323a3d76d55737887abc9 to your computer and use it in GitHub Desktop.
Go VPN Detector
package main
import (
"bufio"
"io"
"log"
"net"
"net/http"
"os"
"strings"
)
type CIDRRecord struct {
Network *net.IPNet
}
func loadCIDRDatabase(filePath string) ([]CIDRRecord, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var records []CIDRRecord
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue // Skip empty lines or comments
}
_, network, err := net.ParseCIDR(line)
if err != nil {
log.Printf("Failed to parse CIDR: %v", line)
continue
}
records = append(records, CIDRRecord{Network: network})
}
if err := scanner.Err(); err != nil {
return nil, err
}
return records, nil
}
func detectVPN(ip string, records []CIDRRecord) bool {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
log.Printf("Invalid IP address: %s", ip)
return false
}
for _, record := range records {
if record.Network.Contains(parsedIP) {
return true
}
}
return false
}
func getPublicIP() (string, error) {
resp, err := http.Get("https://api.ipify.org?format=text")
if err != nil {
return "", err
}
defer resp.Body.Close()
ip, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(ip), nil
}
func main() {
databasePath := "./ipv4.txt" // Download file from https://github.com/X4BNet/lists_vpn/blob/main/ipv4.txt
records, err := loadCIDRDatabase(databasePath)
if err != nil {
log.Fatalf("Failed to load database: %v", err)
}
publicIP, err := getPublicIP()
if err != nil {
log.Fatalf("Failed to detect public IP: %v", err)
}
log.Printf("Public IP Detected: %s", publicIP)
if detectVPN(publicIP, records) {
log.Println("The IP address is detected as VPN/Proxy.")
} else {
log.Println("No VPN detected for this IP.")
}
}
@reynldi

reynldi commented Dec 10, 2024

Copy link
Copy Markdown
Author

Download list of VPN IP database from https://github.com/X4BNet/lists_vpn/blob/main/ipv4.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment