diff --git a/main.py b/main.py new file mode 100644 index 0000000..d93a694 --- /dev/null +++ b/main.py @@ -0,0 +1,193 @@ +#!/usr/bin/python +import vultr +import threading +from tkinter import * + +#Set +Location = "25"; +API_Key = "" + + + +#Func +def use_proxy(port): + CMD = "export ALL_PROXY=socks5://127.0.0.1:"+port; + os.system(CMD) + +class Application(): + app = None + widgets = {} + + def __init__(self,title): + self.app = Tk(className=title) + def set_size(self,widget,width,height): + widget['height'] = height; + widget['width'] = width; + return widget + + def set_widget(self,widget,row,column,sticky): + widget.grid(column = column, row = row, sticky = sticky) + return widget + + def get_button(self,title,command,w_id): + app_button = Button(self.app) + app_button['text'] = title; + app_button['command'] = command; + self.widgets[w_id] = app_button; + return app_button; + def get_entry(self,w_id): + app_entry = Entry(self.app) + self.widgets[w_id] = app_entry; + return app_entry + def get_entry_long(self,w_id): + app_entry = Entry(self.app) + app_entry['width'] = 50; + self.widgets[w_id] = app_entry; + return app_entry + def get_label(self,text,w_id): + app_label = Label(self.app) + app_label['text'] = text + self.widgets[w_id] = app_label; + return app_label + def get_text(self,w_id): + app_text = Text(self.app) + self.widgets[w_id] = app_text; + return app_text + def get_menubtn(self,w_id): + app_menubtn = Menubutton(self.app) + self.widgets[w_id] = app_menubtn; + return app_menubtn + def set_munubtn(self,widget,text,items): + widget['text'] = text; + widget.menu = Menu(widget) + for item in items: + widget.menu.add_command(label=item) + widget['menu'] = widget.menu + return widget + + def refresh_text(self,widget,w_id): + info_text.delete('1.0', END) + + def get_widget(self,w_id): + return self.widgets[w_id] + + +class Display(Application): + display = None + info = {} + def __init__(self,title): + self.display = Toplevel() + info['decision'] = False + def click_yes(self,func): + info['decision'] = True + func(); + def click_no(self,func): + info['decision'] = False + func(); + + + +account = vultr.account() +app = Application("vultr manager") +app.get_label("API_Key: ",'key_label') +app.set_widget(app.widgets['key_label'],0,0,W) + +key_entry = app.get_entry_long('key_entry') +app.set_widget(app.get_widget('key_entry'),1,0,W) + +app.get_text('info_text') +app.set_size(app.get_widget('info_text'),65,20) +app.set_widget(app.get_widget('info_text'),4,0,W) + + +app.set_widget(app.get_label("SUBID: ",'sid_label'),6,0,E) +app.get_entry('sid_entry') +app.set_widget(app.get_widget('sid_entry'),6,1,E) + +app.set_widget(app.get_label('Status:','tstatus_label'),5,0,W) +app.set_widget(app.get_label('','status_label'),5,1,W) + +religions = ["Los Angeles","Tokyo","London","Sydney","Singapore"] + +app.set_widget(app.set_munubtn(app.get_menubtn('religion_mbtn'),"religion",religions),4,2,W) +app.set_widget(app.get_menubtn('plan_mbtn'),4,3,W) + +def Refresh_Info(): + app.get_widget('info_text').delete('1.0', END) + app.get_widget('info_text').insert(INSERT,"Account Info\n") + Balance = "Balance: "+str(account.balance)+"\n" + app.get_widget('info_text').insert(INSERT,Balance) + data = account.get_server_info() + for item in data: + server_data = data[item] + app.get_widget('info_text').insert(INSERT,"==========================\n") + app.get_widget('info_text').insert(INSERT,"Server "+server_data['SUBID']+"\n") + app.get_widget('info_text').insert(INSERT,"IP: "+server_data["main_ip"]+"\n") + app.get_widget('info_text').insert(INSERT,"Location: "+server_data["location"]+"\n") + app.get_widget('info_text').insert(INSERT,"OS: "+server_data["os"]+"\n") + app.get_widget('info_text').insert(INSERT,"Location: "+server_data["location"]+"\n") + app.get_widget('info_text').insert(INSERT,"Status: "+server_data["status"]+"\n") + try: + server_data["ssr_installation"] + except KeyError: + app.get_widget('info_text').insert(INSERT,"SSR: "+"None"+"\n") + else: + app.get_widget('info_text').insert(INSERT,"SSR: "+server_data["ssr_installation"]+"\n") + + + app.get_widget('info_text').insert(END," \n") + +def Set_Api_Key(): + API_Key = key_entry.get() + account.set_account(API_Key) + (back, stdout) = account.get_account() + if (back == "DONE"): + Refresh_Info() + else: + app.get_widget('info_text').delete('1.0', END) + app.get_widget('info_text').insert(INSERT,stdout); + + + +def Add_Server(): + if (account.CMD_Head != ""): + account.create_server('201','193','5'); + Refresh_Info(); + +def Thread_ISSR(server_data): + pass + + +def Install_SSR(): + + SUBID = app.get_widget('sid_entry').get() + server_data = account.servers[SUBID] + vultr.connect_ssh(server_data) + app.get_widget('status_label').config(text = SUBID+" Installing UFW and SSR"); + server_data['ssr_installation'] = "Installing" + #threading.Thread(target = Thread_ISSR,args = (server_data)) + + vultr.install_ssr(server_data) + app.get_widget('info_text').delete(END) + app.get_widget('status_label').insert(END,SUBID+" Installation Done."); + app.get_widget('status_label').config(text = SUBID+" UFW and SSR Installed"); + Refresh_Info(); + + +def Destroy_Server(): + #display = Display("Display") + #account.destroy_server(sid_text.get()) + SUBID = app.get_widget('sid_entry').get() + account.destroy_server(SUBID) + Refresh_Info() + +app.set_size(app.set_widget(app.get_button("Connect",Set_Api_Key,'connect_button'),1,5,W),10,1) +app.set_size(app.set_widget(app.get_button("Add a Server",Add_Server,'add_button'),4,5,W),10,1) +app.set_size(app.set_widget(app.get_button("Refresh",Refresh_Info,'refresh_button'),2,5,W),10,1) +app.set_size(app.set_widget(app.get_button("Install SSR",Install_SSR,'install_button'),5,5,W),10,1) +app.set_size(app.set_widget(app.get_button("Destroy Server",Destroy_Server,'destroy_button'),6,5,W),10,1) + + +app.app.mainloop() + + \ No newline at end of file diff --git a/vultr.py b/vultr.py new file mode 100644 index 0000000..831d026 --- /dev/null +++ b/vultr.py @@ -0,0 +1,163 @@ +import os +import json +import subprocess +import paramiko +import time + +class account: + API_Key = None; + balance = 0; + last_pay_date = None; + last_pay_amount = 0; + servers_subid = []; + servers = []; + + CMD_CMD = None + CMD_INFO = None + CMD_OPTION = None + CMD_Head = None + + def __init__(self): + pass + + def set_account(self,API_Key): + self.API_Key = API_Key; + self.CMD_CMD = "curl" + self.CMD_OPTION = " -s" + self.CMD_INFO = " -H 'API-Key:"+API_Key+"' " + self.CMD_Head = self.CMD_CMD+self.CMD_OPTION+self.CMD_INFO + + def do_command(self,CMD): + child = subprocess.Popen([CMD],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE); + child.wait(); + (stdout,error) = child.communicate() + str_out = stdout.decode() + str_err = error.decode() + if (str_err != ''): print("Error: "+error) + return str_out; + + def get_account(self): + CMD_HTML = "https://api.vultr.com/v1/account/info" + CMD = self.CMD_Head+CMD_HTML; + stdout = self.do_command(CMD); + try: + data = json.loads(stdout); + except json.decoder.JSONDecodeError: + print("Ask account information failed.") + print(stdout) + return "ERROR",stdout; + else: + self.balance = data["balance"]; + self.last_pay_date = data["last_payment_date"]; + self.last_pay_amount = data["last_payment_amount"]; + return "DONE","NONE"; + + def create_server(self, plan, os, religion): + CMD_HTML = "https://api.vultr.com/v1/server/create" + CMD_DATA = " --data 'DCID="+religion+"' --data 'VPSPLANID="+plan+"' --data 'OSID="+os+"'" + CMD = self.CMD_Head+CMD_HTML+CMD_DATA; + stdout = self.do_command(CMD); + data = json.loads(stdout); + self.servers_subid.append(data["SUBID"]); + self.servers_subid =[] + self.get_server_info() + + return data["SUBID"]; + + def create_ipv4(self,SUBID): + CMD_HTML = "https://api.vultr.com/v1/server/create_ipv4"; + CMD_DATA = " --data 'SUBID="+SUBID+"'" + CMD = self.CMD_Head + CMD_HTML + CMD_DATA; + self.do_command(CMD) + + def destroy_server(self,SUBID): + CMD_HTML = "https://api.vultr.com/v1/server/destroy"; + CMD_DATA = " --data 'SUBID="+SUBID+"'" + CMD = self.CMD_Head + CMD_HTML + CMD_DATA; + stdout = self.do_command(CMD) + + def destroy_ipv4(self,SUBID): + CMD_HTML = "https://api.vultr.com/v1/server/destroy_ipv4"; + CMD_DATA = " --data 'SUBID="+SUBID+"'" + CMD = self.CMD_Head + CMD_HTML + CMD_DATA; + self.do_command(CMD) + + + def server_reboot(self,SUBSID): + CMD_HTML = "https://api.vultr.com/v1/server/reboot"; + CMD_DATA = " --data 'SUBID="+SUBID+"'" + CMD = self.CMD_Head + CMD_HTML + CMD_DATA; + stdout = self.do_command(CMD) + + def get_server_info(self): + CMD_HTML = "https://api.vultr.com/v1/server/list"; + CMD = self.CMD_Head + CMD_HTML; + stdout = self.do_command(CMD); + data = json.loads(stdout); + self.servers = data + #print(data) + self.servers_subid = [] + for item in data: + self.servers_subid.append(item) + return data; + def get_sever_ipv4(self): + CMD_HTML = "https://api.vultr.com/v1/server/list_ipv4"; + CMD_DATA = "?SUBID="+SUBID + CMD = self.CMD_Head + CMD_HTML + CMD_DATA; + stdout = self.do_command(CMD) + +def connect_ssh(server_data): + if (server_data["status"] == "active"): + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect(server_data["main_ip"],22,"root",server_data["default_password"],timeout=8) + server_data['ssh_connection'] = ssh; + + return server_data + +def install_ssr(server_data): + ssh = server_data['ssh_connection'] + if(server_data["status"] == "active"): + print("Waiting for installation...") + + print("Installing Ufw...") + stdin,stdout,stderr = ssh.exec_command("apt-get install ufw") + stdin.write("Y\n"); + print(stdout.read()) + print("Ufw installed.") + server_data['ufw_installation'] = "Installed"; + + time.sleep(3) + + print("Setting Ufw...") + stdin,stdout,stderr = ssh.exec_command("ufw enable") + stdin.write("Y\n") + stdin,stdout,stderr = ssh.exec_command("ufw allow 9048") + stdin,stdout,stderr = ssh.exec_command("ufw allow 22") + stdin,stdout,stderr = ssh.exec_command("ufw default deny") + print(stdout.read()) + print("Ufw have been set"); + server_data['ufw_setting_allow'] = [22,9048]; + + time.sleep(3) + + print("Setting SSR...") + stdin,stdout,stderr = ssh.exec_command("wget --no-check-certificate https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocksR.sh") + stdin,stdout,stderr = ssh.exec_command("chmod +x shadowsocksR.sh"); + stdin,stdout,stderr = ssh.exec_command("./shadowsocksR.sh 2>&1 | tee shadowsocksR.log") + stdin.write("#a9b9fa3456\n"); + stdin.write("9048\n2\n7\n7\n\n"); + print(stdout.read()) + stdin,stdout,stderr = ssh.exec_command("systemctl start shadowsocks") + print("SSR have been set and now is running.") + print("Server IP :"+server_data["main_ip"]) + server_data['ssr_installation'] = "Installed"; + return "DONE" + +def print_server(server_data): + print(server_data["SUBID"]) + print(server_data["main_ip"]) + print(server_data["location"]) + print(server_data["os"]) + print(server_data["status"]) + print(server_data["default_password"]) \ No newline at end of file