4040import yaml
4141import requests
4242
43- with open ("config.yaml" ,"r" ) as stream :
43+ import exceptions
44+ from utils import verifyRecaptcha , generateInvite , verifyConfig
45+
46+ with open ("config.yaml" , "r" ) as f :
4447 try :
45- config = yaml .safe_load (stream )
48+ config = yaml .safe_load (f )
4649 except yaml .YAMLError as exc :
4750 print (exc )
4851 quit (1 )
49-
50-
51- if "dark_theme" not in config :
52- print ("!! Theme not defined" )
53- if "recaptcha" in config :
54- if config ["recaptcha" ]["public" ] == None :
55- print ("!! Recaptcha public key is not defined, exiting" )
56- quit (1 )
57- if config ["recaptcha" ]["private" ] == None :
58- print ("!! Recaptcha private key is not defined, exiting" )
59- quit (1 )
60- else :
61- print ("!! Recaptcha config doesnt exist, exiting" )
62- quit (1 )
63-
64- if "discord" in config :
65- if config ["discord" ]["welcome_room" ] == None :
66- print ("!! Discord welcome room not defined, exiting" )
67- quit (1 )
68- if config ["discord" ]["private" ] == None :
69- print ("!! Discord private key is not defined, exiting" )
70- quit (1 )
71- else :
72- print ("!! Discord config doesnt exist, exiting" )
73- quit (1 )
74-
75- if "server" in config :
76- if config ["server" ]["port" ] == None :
77- print ("!! Server port not defined, exiting" )
78- quit (1 )
79- else :
80- print ("!! Sever config not defined, exiting" )
81- quit (1 )
82-
83- def recaptcha (token ):
84- print (f"Verifying recaptcha { token [:15 ]} " )
85- recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'
86- payload = {
87- 'secret' : config ["recaptcha" ]["private" ],
88- 'response' : token ,
89- 'remoteip' : request .remote_addr ,
90- }
91- response = requests .post (recaptcha_url , data = payload )
92- result = response .json ()
93- return result
94-
95- def invite ():
96- print ("Generating new invite!" )
97- resp = requests .post (
98- 'https://discordapp.com/api/channels/{}/invites' .format (config ["discord" ]["welcome_room" ]),
99- headers = {'Authorization' : 'Bot {}' .format (config ["discord" ]["private" ])},
100- json = {'max_uses' : 1 , 'unique' : True , 'max_age' : 300 }
101- )
102- i = resp .json ()
103- # error handling for invite creation
104- if (i .get ('code' )):
105- print ("Generated new invite!" )
10652 else :
107- print (i )
108- return i ["code" ]
53+ verifyConfig (config )
10954
11055app = Flask (__name__ )
11156
@@ -114,17 +59,56 @@ def invite():
11459catpcha_theme = "dark" if config ["dark_theme" ] else "light"
11560
11661
117- @app .route ("/" ) # main function
62+ @app .route ("/" )
11863def index ():
119- key = request .args .get (' key' ) # get key parameter from URL
120- if key : # if key set
121- r = recaptcha (key ) # confirm captcha
122- if r .get ("success" ): # if ok
64+ key = request .args .get (" key" )
65+ if key : # User has submitted a captcha
66+ r = verifyRecaptcha (key , request , config )
67+ if r .get ("success" ): # Captcha is OK
12368 print (f"Recaptcha { key [:30 ]} verified!" )
124- i = invite () # generate new invite
125- return redirect (f"https://discord.gg/{ i } " ) # redirect user to new invite
126- else : # if captcha invalid
69+ inviteCode = generateInvite ( config )
70+ return redirect (f"https://discord.gg/{ inviteCode } " )
71+ else : # Captcha failed
12772 print (f"Recaptcha { key [:30 ]} failed!" )
128- return render_template ("index.html" , public = config ["recaptcha" ]["public" ], failed = True , theme = theme , border = border , catpcha_theme = catpcha_theme ) # return error page
129- # if not key
130- return render_template ("index.html" , public = config ["recaptcha" ]["public" ], failed = False , theme = theme , border = border , catpcha_theme = catpcha_theme ) # return normal page
73+ # Return error page
74+ return render_template (
75+ "index.html" ,
76+ public = config ["recaptcha" ]["public" ],
77+ failed = "Invalid captcha, try again" ,
78+ theme = theme ,
79+ border = border ,
80+ catpcha_theme = catpcha_theme ,
81+ )
82+
83+ return render_template (
84+ "index.html" ,
85+ public = config ["recaptcha" ]["public" ],
86+ failed = None ,
87+ theme = theme ,
88+ border = border ,
89+ catpcha_theme = catpcha_theme ,
90+ ) # Return normal page
91+
92+
93+ @app .errorhandler (500 )
94+ def internalError (error ):
95+ return render_template (
96+ "index.html" ,
97+ public = config ["recaptcha" ]["public" ],
98+ failed = "Internal server error, please try again later" ,
99+ theme = theme ,
100+ border = border ,
101+ catpcha_theme = catpcha_theme ,
102+ )
103+
104+
105+ @app .errorhandler (404 )
106+ def notFound (error ):
107+ return render_template (
108+ "index.html" ,
109+ public = config ["recaptcha" ]["public" ],
110+ failed = None ,
111+ theme = theme ,
112+ border = border ,
113+ catpcha_theme = catpcha_theme ,
114+ )
0 commit comments