Skip to content

Commit

Permalink
edit config
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed Jun 7, 2023
1 parent b75ff5a commit c3ae37a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
47 changes: 38 additions & 9 deletions free/common/conf.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import os
import json
import random
import string

STORE_DIR_ENV_NAME = "FREE_VPN_STORE_DIR"

DEFAULT_AES_KEY = "x^)dixf&*1$free]"
DEFAULT_LOCAL_PROXY = "http://localhost:2019"

class Config(object):
def __init__(self, file_path:str=""):
self.dir: str = file_path if file_path else os.getcwd()
self.path = os.path.join(self.dir, "free_vpn_config.json")
self.dict: dict = dict()
self.load()
if len(self.dict) > 0:
self._key = self.dict.get("key", "x^)dixf&*1$free]")
self._key = self.dict.get("key", DEFAULT_AES_KEY)
self._store_dir = self.dict.get("store_dir", os.getcwd())
self._proxy = self.dict.get("proxy", "http://localhost:2019")
self._proxy = self.dict.get("proxy", DEFAULT_LOCAL_PROXY)
else:
self.set_config()
store_dir = getattr(self, "store_dir")
Expand All @@ -29,32 +34,56 @@ def save(self):
json.dump(self.dict, f, indent=4, ensure_ascii=True)

def set_config(self):
self._key = input("Set encryption key:\n").strip(" ")
print("How to set AES encryption key:")
print("1> Set encryption key by default.[Default]")
print("2> Set encryption key by input.")
print("3> Set encryption key by auto-generation.")
operation = input("Choose 1, 2 or 3, then press <Enter>: ").strip(" ")
if not operation:
operation = "1"

print(f"You have chosen [{operation}]")
if operation == "1":
self._key = DEFAULT_AES_KEY
elif operation == "2":
self._key = input("Please input your encryption key [length should be 16]: ")
else:
self._key = self.random_key()

self._store_dir = input("Set restore directory:\n").strip(" ")
self._proxy = input("Set local proxy:\n").strip(" ")

if len(self.key) != 16:
self._key = "x^)dixf&*1$free]"
self._key = DEFAULT_AES_KEY
if not os.path.exists(self.store_dir):
self._store_dir = os.getcwd()
if not self._proxy:
self._proxy = "http://localhost:2019"
self._proxy = DEFAULT_LOCAL_PROXY
self.dict["key"] = self._key
self.dict["store_dir"] = self._store_dir
self.dict["proxy"] = self._proxy
print("You are saving freevpn config file...")
print(f"EncryptionKey: {self._key}, StoreDir: {self._store_dir}, LocalProxy: {self._proxy}")
self.save()

@property
def key(self):
def key(self)->bytes:
return self._key.encode("utf-8")

@property
def store_dir(self):
def store_dir(self)->str:
return self._store_dir

@property
def proxy(self):
return self._proxy

def random_key(self)->str:
sample_str_all = string.ascii_letters + string.digits + string.punctuation
_key = "n" + "".join(random.sample(sample_str_all, 15))
return _key

if __name__ == "__main__":
# TODO: config file stored to current working directory.
print(os.getcwd())
# TODO: automatically generate aes key.
print(random.sample('abcdefghijklmnopqrstuvwxyz!@#$%^&*1234567890',16))
print(string.punctuation)
16 changes: 15 additions & 1 deletion free/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding=UTF-8
import os
import sys
import json
import datetime
import subprocess
Expand Down Expand Up @@ -131,10 +132,23 @@ def git_push(self):
subprocess.call(["git", "commit", "-m", "update"])
subprocess.call(["git", "push"])
os.chdir(self.cwd)

def set_config_file(self):
self.conf.set_config()

def show_config(self):
print(f"EncryptionKey: {self.conf.key}, StoreDir: {self.conf.store_dir}, LocalProxy: {self.conf.proxy}")
print(f"freevpn config file: [{self.conf.path}]")

def Run():
v = VPN()
v.run()
args = sys.argv
if len(args) > 2:
v.show_config()
elif len(args) == 2:
v.set_config_file()
else:
v.run()

if __name__ == "__main__":
Run()

0 comments on commit c3ae37a

Please sign in to comment.