import os
import subprocess
import requests
from datetime import datetime
class JapanVPN:
def __init__(self):
self.config_path = "/etc/wireguard/wg-jp.conf" # Configuration file path
self.vpn_status = False # Track VPN connection status
self.server_ip = "YOUR_JAPAN_SERVER_IP" # Replace with your Japan server IP
self.server_port = "51820" # Default WireGuard port
def check_wireguard_installed(self):
"""Check if WireGuard is installed on the system"""
try:
subprocess.run(["wg", "--version"], check=True, stdout=subprocess.PIPE)
return True
except:
return False
def install_wireguard(self):
"""Install WireGuard package"""
print("Installing WireGuard...")
try:
subprocess.run(["sudo", "apt", "update"], check=True)
subprocess.run(["sudo", "apt", "install", "wireguard", "-y"], check=True)
return True
except subprocess.CalledProcessError:
return False
def generate_keys(self):
"""Generate private and public keys for WireGuard"""
print("Generating keys...")
try:
# Generate private key
private_key = subprocess.run(["wg", "genkey"], capture_output=True, text=True).stdout.strip()
# Generate public key from private key
public_key = subprocess.run(
["wg", "pubkey"],
input=private_key,
capture_output=True,
text=True
).stdout.strip()
return private_key, public_key
except:
return None, None
def create_config(self, private_key, server_public_key):
"""Create WireGuard configuration file"""
config_content = f"""[Interface]
PrivateKey = {private_key}
Address = 10.0.0.2/24
DNS = 8.8.8.8
[Peer]
PublicKey = {server_public_key}
Endpoint = {self.server_ip}:{self.server_port}
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
"""
try:
with open(self.config_path, "w") as f:
f.write(config_content)
os.chmod(self.config_path, 0o600) # Set secure permissions
return True
except:
return False
def connect_vpn(self):
"""Connect to the VPN"""
try:
subprocess.run(["sudo", "wg-quick", "up", "wg-jp"], check=True)
self.vpn_status = True
print("Successfully connected to Japan VPN")
self.show_connection_info()
return True
except subprocess.CalledProcessError:
print("Error connecting to VPN")
return False
def disconnect_vpn(self):
"""Disconnect from the VPN"""
try:
subprocess.run(["sudo", "wg-quick", "down", "wg-jp"], check=True)
self.vpn_status = False
print("VPN disconnected successfully")
return True
except subprocess.CalledProcessError:
return False
def check_ip(self):
"""Check current public IP address"""
try:
response = requests.get("https://api.ipify.org?format=json", timeout=5)
return response.json()["ip"]
except:
return None
def show_connection_info(self):
"""Display connection information"""
current_ip = self.check_ip()
if current_ip:
print(f"Current IP Address: {current_ip}")
print(f"Connected VPN Server: {self.server_ip}")
print(f"Connection Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
def menu(self):
"""Display menu options"""
print("\nJapan VPN Application")
print("1. Connect VPN")
print("2. Disconnect VPN")
print("3. Check Current IP")
print("4. Exit")
choice = input("Enter your choice: ")
return choice
def main():
vpn = JapanVPN()
# Check if WireGuard is installed
if not vpn.check_wireguard_installed():
print("WireGuard needs to be installed")
if not vpn.install_wireguard():
print("Error installing WireGuard")
return
# Create keys and config file if they don't exist
if not os.path.exists(vpn.config_path):
private_key, public_key = vpn.generate_keys()
if not private_key:
print("Error generating keys")
return
# Get server public key from user
server_public_key = input("Please enter the server's public key: ")
if not vpn.create_config(private_key, server_public_key.strip()):
print("Error creating config file")
return
# Main application loop
while True:
choice = vpn.menu()
if choice == "1":
if not vpn.vpn_status:
vpn.connect_vpn()
else