· 6 years ago · Oct 14, 2019, 06:30 PM
1import os
2import requests
3import json
4import tkinter as tk
5from tkinter import messagebox, simpledialog, ttk
6from tkinter.filedialog import askopenfilename
7
8class MainWindow(tk.Tk):
9 def __init__(self, parent):
10 tk.Tk.__init__(self, parent)
11 self.parent = parent
12 self.init_tabs()
13
14 def init_tabs(self):
15 self.tab_parent = ttk.Notebook(self.parent)
16
17 self.tab1 = StartBotFrame(self.tab_parent)
18 self.tab2 = WaypointFrame(self.tab_parent)
19 self.tab3 = LogFrame(self.tab_parent)
20 self.tab4 = PartnersFrame(self.tab_parent)
21
22 self.tab_parent.add(self.tab1, text="Start")
23 self.tab_parent.add(self.tab2, text="Waypoints")
24 self.tab_parent.add(self.tab3, text="Logs")
25 self.tab_parent.add(self.tab4, text="Partners")
26
27 self.tab_parent.pack()
28
29class LogFrame(tk.Frame):
30 def __init__(self, parent):
31 tk.Frame.__init__(self, parent)
32 self.parent = parent
33
34 self.initialize()
35
36 def initialize(self):
37 tk.Label(self, text="Type IP/VNC Pass of the session to get the log").pack()
38
39 tk.Label(self, text="IP").pack()
40 self.varIP = tk.StringVar()
41 self.entryIP = tk.Entry(self, textvar=self.varIP, width=20)
42 self.entryIP.pack()
43
44 tk.Label(self, text="Pass").pack()
45 self.varPass = tk.StringVar()
46 self.entryPass = tk.Entry(self, textvar=self.varPass, width=20)
47 self.entryPass.pack()
48
49 self.btnGetLog = tk.Button(self, text="Get Log", command=lambda : API.get_log(self))
50 self.btnGetLog.pack()
51
52
53class WaypointFrame(tk.Frame):
54 def __init__(self, parent):
55 tk.Frame.__init__(self, parent)
56 self.parent = parent
57
58 self.initialize()
59
60 def select_file(self):
61 self.varPath.set(askopenfilename(initialdir="C:/",
62 filetypes =(("WPT File", "*.in"),("All Files","*.*")),
63 title = "Choose waypoint to upload."
64 ))
65
66 def initialize(self):
67 self.user_key = "" #self.parent.varKey.get()
68 if os.path.exists('api_key.txt'):
69 self.user_key = open('api_key.txt').read()
70
71 self.lblKey = tk.Label(self, text="Key:")
72 self.lblKey.pack()
73
74 self.varKey = tk.StringVar()
75 self.entryKey = tk.Entry(self, textvar=self.varKey, width=20)
76 self.entryKey.insert(1, self.user_key)
77 self.entryKey.pack()
78
79 self.varPath = tk.StringVar()
80
81 tk.Label(self, text="Select the waypoint to send to machine").pack()
82
83 self.varPath = tk.StringVar()
84 self.entryPath = tk.Entry(self, textvariable=self.varPath, width=30, state='disabled')
85 self.entryPath.pack()
86
87 self.btnWpt = tk.Button(self, text="Browse", command=self.select_file)
88 self.btnWpt.pack()
89
90 self.btnSend = tk.Button(self, text="Send", command=lambda: API.send_waypoint(self))
91 self.btnSend.pack()
92
93class CustomDialog(simpledialog.Dialog):
94 def __init__(self, parent, hours, ip, password):
95 self.ip = ip
96 self.password = password
97 self.hours = hours
98 self.parent = parent
99 simpledialog.Dialog.__init__(self, parent, title="VNC Credential")
100
101 def copy(self, txt):
102 self.clipboard_clear()
103 self.clipboard_append(txt)
104
105 def body(self, parent):
106 #messagebox.showinfo("Setup Completed Successfully",
107 #'''Connect to the VM using VNC
108 # The VM will disconnect after {} hours
109 # IP: {}
110 # PASS: {}'''.format(HOURS, resp['vnc-address'], resp['vnc-password']))
111 tk.Label(self, text="Connect to the VM using VNC").pack()
112 tk.Label(self, text="The VM will disconnect after {} hours".format(self.hours)).pack()
113 tk.Label(self, text="IP: {}".format(self.ip)).pack()
114
115 self.btnCopyIP = tk.Button(self, text="Copy", command=lambda : self.copy(self.ip))
116 self.btnCopyIP.pack()
117
118 #self.text = tk.Text(self, width=20, height=1)
119 #self.text.insert(tk.END, self.ip)
120 #self.text.configure(state='disabled')
121 #self.text.pack()
122
123 tk.Label(self, text="VNC Password: {}".format(self.password)).pack()
124 self.btnCopyPass = tk.Button(self, text="Copy", command=lambda : self.copy(self.password))
125 self.btnCopyPass.pack()
126
127 #self.text = tk.Text(self, width=20, height=1)
128 #self.text.insert(tk.END, self.password)
129 #self.text.configure(state='disabled')
130 #self.text.pack()
131
132 return
133
134class API:
135 def check_key(app):
136 key = app.varKey.get()
137 print(key)
138 if key == '':
139 messagebox.showerror("Error","You need to provide a valid key")
140 return
141
142 r = requests.get("https://cloudbotkeys-139203.herokuapp.com/get_key/{}".format(key))
143 if r.status_code in (200, 201):
144 resp = r.json()
145 if 'error' in resp:
146 messagebox.showerror("Error", resp['error'])
147 else:
148 hours = r.json().get('hours', 0)
149 messagebox.showinfo("Key status", "Key hours remaining:\n {}".format(hours))
150 else:
151 messagebox.showerror("Server Error","{}".format(r.text))
152
153 def get_log(app):
154 ip = app.varIP.get()
155 passwd = app.varPass.get()
156
157 if len(ip.split('.')) < 4:
158 messagebox.showerror("Error","You need to provide a valid ip")
159 return
160
161 if passwd == '':
162 messagebox.showerror("Error","You need to provide the VNC Password")
163 return
164
165 if ':' in ip:
166 ip = ip.split(':')[0]
167
168 print('Get log from', ip, passwd)
169 data = {
170 "ip": ip,
171 "vncpass": passwd,
172 }
173
174 r = requests.post("https://cloudbotapi-139203.herokuapp.com/api/v1.0/get_log", json=data)
175 if r.status_code in (200, 201):
176 log = r.json().get('log', '')
177 print(log, file=open('log.txt', 'w'))
178 messagebox.showinfo("Success", "Your log was saved to log.txt")
179 else:
180 print('Server returned', r.status_code, r.reason)
181 messagebox.showerror("Error","{}".format(r.text))
182
183 def send_waypoint(app):
184 user_key = app.varKey.get()
185 wpt_path = app.varPath.get()
186
187 if user_key == '':
188 messagebox.showerror("Error","You need to provide a valid key with dedicated VM to upload waypoints")
189 return
190 if wpt_path == '':
191 messagebox.showerror("Error","You need to provide the path to the waypoint")
192 return
193
194 print('User Key', user_key)
195 with open(wpt_path, 'r') as f:
196 wpt = f.read()
197
198 data = {
199 "apikey": user_key,
200 "waypoint": wpt,
201 }
202
203 r = requests.post("https://cloudbotapi-139203.herokuapp.com/api/v1.0/send_waypoint", json=data)
204 if r.status_code in (200, 201):
205 print('Send waypoint was successful')
206 resp = r.json()
207 print(resp)
208 messagebox.showinfo("Success", "Your waypoint was uploaded to script 'my_script'")
209 else:
210 print('Server returned', r.status_code, r.reason)
211 print(r.text)
212 messagebox.showerror("Error","{}".format(r.text))
213
214
215 def start_bot(app):
216 user_key = app.varKey.get()
217 setup_path = app.varPath.get()
218
219 if setup_path == '':
220 messagebox.showerror("Error","You need to provide a setup.json file with the config of your script")
221 return
222 if user_key == '':
223 messagebox.showerror("Error","You need to provide a valid key to start the VM")
224 return
225
226 print('User Key', user_key)
227 with open('api_key.txt', 'w') as f:
228 f.write(user_key)
229
230 with open(setup_path, 'r') as f:
231 setup = json.load(f)
232
233 script_name = app.varScript.get()
234 print('Starting:', script_name)
235 script = app.scripts.script_map()[script_name]
236 print('Script:', script)
237 hours = app.varHours.get()
238 print('Hours:', hours)
239 data = {
240 "apikey": user_key,
241 "hours": hours,
242 "script": script,
243 "setup": setup,
244 }
245 r = requests.post("https://cloudbotapi-139203.herokuapp.com/api/v1.0/start_script", json=data)
246 if r.status_code in (200, 201):
247 print('Start', script, 'was successful')
248 resp = r.json()
249 print(resp)
250 CustomDialog(app, ip=resp['vnc-address'], password=resp['vnc-password'], hours=hours)
251 with open('password.txt', 'w') as f:
252 print(resp['vnc-address'], file=f)
253 print(resp['vnc-password'], file=f)
254 else:
255 print('Start', script, 'failed')
256 print('Server returned', r.status_code, r.reason)
257 print(r.text)
258 messagebox.showerror("Error","{}".format(r.text))
259
260class Scripts:
261 def __init__(self):
262 self.scripts = [
263 (45, 'Knight', 'Forest Fury Carlin', 'forest_fury'),
264 (8, 'Knight', 'Swamp Troll Venore', 'swamp_troll'),
265 (8, 'All', 'Buy Blessing', 'buy_blessing'),
266 (200, 'EK', 'Krailos Wailing Widow Cave -2', 'krailos_spider_cave'),
267 (150, 'EK', 'Krailos Wailing Widow Cave -1', 'krailos_spider_cave'),
268 (180, 'EK', 'Krailos Nightmare Cave -2', 'krailos_nightmare'),
269 (90, 'EK', 'Killer Caimans Cave', 'killer_caimans'),
270 (50, 'EK', 'Pirates Yalahar', 'pirates_yalahar'),
271 (40, 'EK', 'Mutated Humans Yalahar', 'mutated_human_yalahar'),
272 (30, 'EK', 'Laguna Island', 'laguna_island'),
273 (20, 'EK', 'Tarantula Cave Port Hope', 'tarantula_cave'),
274 (100, 'EK', 'Giant Spider Cave Port Hope', 'giant_spider_cave'),
275 (200, 'EK', 'Feyrist Mountain', 'feyrist_mountain'),
276 (180, 'EK/RP', 'Hero Fortress -2', 'hero_fortress'),
277 (200, 'RP', 'Hero Fortress -3', 'hero_fortress_down'),
278 (220, 'EK/RP', 'Feyrist Nightmare Cave -1', 'feyrist_nightmare_cave'),
279 (250, 'EK', 'Feyrist Nightmare Cave -2', 'feyrist_nightmare_cave'),
280 (250, 'EK/RP', 'Glooth Bandit South', 'glooth_bandit_south'),
281 (250, 'EK/RP', 'Glooth Bandit East', 'glooth_bandit_east'),
282 (270, 'EK/RP', 'Glooth Bandit West', 'glooth_bandit_west'),
283 (280, 'EK', 'Oramond West Plains', 'oramond_west'),
284 (300, 'EK/RP', 'Oramond Sewers', 'oramond_sewers'),
285 (350, 'EK', 'Oramond Tower', 'oramond_tower'),
286 (400, 'EK', 'Medusa Tower Port Hope', 'medusa_tower'),
287 (8, 'Any', 'Custom Waypoint', 'my_script'),
288 ]
289
290 def names(self):
291 names = ['{}+ {} - {}'.format(lvl, voc, name) for (lvl, voc, name, _) in sorted(self.scripts)]
292 return names
293
294 def script_map(self):
295 names = {'{}+ {} - {}'.format(lvl, voc, name):script for (lvl, voc, name, script) in sorted(self.scripts)}
296 return names
297
298class PartnersFrame(tk.Frame):
299 def __init__(self, parent):
300 tk.Frame.__init__(self, parent)
301 self.parent = parent
302
303 self.partners()
304
305 def partners(self):
306 self.lbl = tk.Label(self, text="Partner1")
307 self.lbl.pack()
308
309class StartBotFrame(tk.Frame):
310 def __init__(self, parent):
311 tk.Frame.__init__(self, parent)
312 self.parent = parent
313
314 self.grid()
315
316 # Start components
317 self.initialize()
318
319 def initialize(self):
320 self.scripts = Scripts()
321 self.setup_path = ""
322 self.script_name = ""
323 self.user_key = ""
324 if os.path.exists('api_key.txt'):
325 self.user_key = open('api_key.txt').read()
326 self.hours_run = '1'
327
328 ## Key
329 self.lblKey = tk.Label(self, text="Key:")
330 self.lblKey.grid(row=0, column=0, sticky=tk.W)
331
332 self.varKey = tk.StringVar()
333 self.entryKey = tk.Entry(self, textvar=self.varKey, width=20)
334 self.entryKey.insert(1, self.user_key)
335 self.entryKey.grid(row=1, column=0, columnspan=2)
336
337 self.btnCheckKey = tk.Button(self, text="Check Key", command=lambda : API.check_key(self))
338 self.btnCheckKey.grid(row=2, column=1, sticky=tk.W)
339
340 # Hours
341 self.lblHours = tk.Label(self, text="Hours:")
342 self.lblHours.grid(row=0, column=2, sticky=tk.W)
343
344 self.varHours = tk.StringVar()
345 self.spinboxHours = tk.Spinbox(self, from_=1, to=24, width=5, textvariable=self.varHours)
346 self.spinboxHours.grid(row=1, column=2)
347
348 # Vocation
349 self.lblVoc = tk.Label(self, text="Character vocation:")
350 self.lblVoc.grid(row=3, columnspan=3, sticky=tk.W)
351
352 self.varVoc = tk.IntVar()
353 self.optionVocKnight = tk.Radiobutton(self, text="Knight", padx = 20, variable=self.varVoc, value=0)
354 self.optionVocPaladin = tk.Radiobutton(self, text="Paladin", padx = 20, variable=self.varVoc, value=1)
355 self.optionVocMage = tk.Radiobutton(self, text="Mage", padx = 20, variable=self.varVoc, value=2)
356 self.varVoc.set(0)
357
358 self.optionVocKnight.grid(row=4, column=0, sticky=tk.W)
359 self.optionVocPaladin.grid(row=4, column=1, sticky=tk.W)
360 self.optionVocMage.grid(row=4, column=2, sticky=tk.W)
361
362 # Script
363 self.lblScript = tk.Label(self, text="Script Name:")
364 self.lblScript.grid(row=5, columnspan=3, sticky=tk.W)
365
366 self.varScript = tk.StringVar()
367 self.comboboxScript = ttk.Combobox(self, width=30, textvariable=self.varScript, values=self.scripts.names())
368 self.comboboxScript.current(0)
369 self.comboboxScript.grid(row=6, columnspan=3)
370
371 # Setup
372 self.lblSetup = tk.Label(self, text="Setup File:")
373 self.lblSetup.grid(row=7, columnspan=3, sticky=tk.W)
374
375 self.varPath = tk.StringVar()
376 self.entryPath = tk.Entry(self, textvariable=self.varPath, width=20, state='disabled')
377 self.entryPath.grid(row=8, column=0, columnspan=2)
378
379 self.btnSetup = tk.Button(self, text="Browse", command=self.select_file)
380 self.btnSetup.grid(row=8, column=2, sticky=tk.W)
381
382 self.btnStartVM = tk.Button(self, text="Start VM", command=lambda: API.start_bot(self))
383 self.btnStartVM.grid(row=10, column=1)
384
385 def select_file(self):
386 self.varPath.set(askopenfilename(initialdir="C:/",
387 filetypes =(("JSON File", "*.json"),("All Files","*.*")),
388 title = "Choose script setup."
389 ))
390
391class Frame1(tk.Frame):
392 def __init__(self, parent):
393 tk.Frame.__init__(self, parent)
394 self.parent = parent
395 self.widgets()
396
397 def widgets(self):
398 self.text = tk.Text(self)
399 self.text.insert(tk.INSERT, "Hello World\t")
400 self.text.insert(tk.END, "This is the first frame")
401 self.text.pack()
402
403
404if __name__ == "__main__":
405
406 app = MainWindow(None)
407 #app = CloudBotStarter(None) #criamos uma aplicação sem nenhum pai, pois é a principal.
408 app.title('CloudBot') #especificamos o título de nossa aplicação
409 app.mainloop()