This is Pluto, the webhook server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
9.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import time,string
  2. from flask import Flask, render_template, redirect, url_for, request, g, make_response
  3. from model import *
  4. from util import *
  5. app = Flask('pluto')
  6. app.debug = True
  7. @app.before_request
  8. def init_globals():
  9. for cls in type.__subclasses__(DBObject):
  10. setattr(g, cls.__name__, cls)
  11. def ip_okay(ip):
  12. return ip.startswith("128.153.144.") or ip.startswith("128.153.145.") or ip.startswith("128.153.146.")
  13. app.jinja_env.filters['ctime'] = time.ctime
  14. app.jinja_env.filters['jloads'] = jloads
  15. app.jinja_env.filters['hloads'] = header_loads
  16. app.jinja_env.filters['split'] = string.split
  17. app.jinja_env.globals['safe_load'] = safe_load
  18. @app.errorhandler(404)
  19. def error_404(error):
  20. return render_template('404.html', entity='page'), 404
  21. @app.route('/')
  22. def root():
  23. if not ip_okay(request.remote_addr):
  24. return render_template('404.html', entity='page'), 404
  25. return render_template('index.html')
  26. @app.route('/hook', methods=['GET', 'POST'])
  27. def hook():
  28. response = make_response(render_template('api_hit.txt'))
  29. response.headers['Content-type'] = 'text/plain'
  30. triggered = filter(lambda hook: hook.trigger(request.path, request.headers, jloads(request.data), dict(request.values.items()), response), Hook.all())
  31. Log.create(time.time(), request.path, header_dumps(request.headers), request.data, ','.join(str(hook.rowid) for hook in triggered))
  32. return response
  33. @app.route('/logs')
  34. def logs():
  35. if not ip_okay(request.remote_addr):
  36. return render_template('404.html', entity='page'), 404
  37. n = request.values.get('n', 10)
  38. return render_template('logs.html', logs=Log.most_recent(n), n=n)
  39. @app.route('/debuglogs', methods=['GET', 'POST'])
  40. def debuglogs():
  41. if not ip_okay(request.remote_addr):
  42. return render_template('404.html', entity='page'), 404
  43. n = request.values.get('n', 10)
  44. return render_template('debuglogs.html', logs=DebugLog.most_recent(n), n=n)
  45. @app.route('/hooks')
  46. def hooks():
  47. if not ip_okay(request.remote_addr):
  48. return render_template('404.html', entity='page'), 404
  49. return render_template('hooks.html', hooks=Hook.all())
  50. @app.route('/hooks/new', methods=['GET', 'POST'])
  51. def hooks_new():
  52. if not ip_okay(request.remote_addr):
  53. return render_template('404.html', entity='page'), 404
  54. if request.method == 'POST':
  55. hook = Hook.create(
  56. request.values['name'], None, # FIXME no author yet
  57. checkbox(request, 'disabled'),
  58. checkbox(request, 'debugged'),
  59. )
  60. return redirect('/hook/%d'%(hook.rowid,))
  61. return render_template('hooks_new.html')
  62. @app.route('/hook/<int:hookid>')
  63. def hook_obj(hookid):
  64. if not ip_okay(request.remote_addr):
  65. return render_template('404.html', entity='page'), 404
  66. try:
  67. hook = Hook.get_one(rowid=hookid)
  68. except NoSuchEntity:
  69. return render_template('404.html', entity='hook'), 404
  70. except TooManyEntities:
  71. return render_template('500.html', cause='too many hooks'), 500
  72. return render_template('hook.html', hook=hook)
  73. @app.route('/hook/<int:hookid>/edit', methods=['GET', 'POST'])
  74. def hook_edit(hookid):
  75. if not ip_okay(request.remote_addr):
  76. return render_template('404.html', entity='page'), 404
  77. try:
  78. hook = Hook.get_one(rowid=hookid)
  79. except NoSuchEntity:
  80. return render_template('404.html', entity='hook'), 404
  81. except TooManyEntities:
  82. return render_template('500.html', cause='too many hooks'), 500
  83. if request.method == 'POST':
  84. hook.name = request.values['name']
  85. hook.disabled = checkbox(request, 'disabled')
  86. hook.debugged = checkbox(request, 'debugged')
  87. hook.update()
  88. return redirect('/hook/%d'%(hook.rowid,))
  89. return render_template('hook_edit.html', hook=hook)
  90. @app.route('/hook/<int:hookid>/newcond', methods=['GET', 'POST'])
  91. def hook_new_cond(hookid):
  92. if not ip_okay(request.remote_addr):
  93. return render_template('404.html', entity='page'), 404
  94. try:
  95. hook = Hook.get_one(rowid=hookid)
  96. except NoSuchEntity:
  97. return render_template('404.html', entity='hook'), 404
  98. except TooManyEntities:
  99. return render_template('500.html', cause='too many hooks'), 500
  100. if request.method == 'POST':
  101. cond = Condition.create(
  102. hook.rowid,
  103. request.values['selector'], request.values['s1'], request.values['s2'], request.values['s3'],
  104. request.values['test'], request.values['t1'], request.values['t2'], request.values['t3'],
  105. checkbox(request, 'invert'),
  106. )
  107. return redirect('/cond/%d'%(cond.rowid,))
  108. return render_template('hook_new_cond.html', hook=hook)
  109. @app.route('/hook/<int:hookid>/newact', methods=['GET', 'POST'])
  110. def hook_new_act(hookid):
  111. if not ip_okay(request.remote_addr):
  112. return render_template('404.html', entity='page'), 404
  113. try:
  114. hook = Hook.get_one(rowid=hookid)
  115. except NoSuchEntity:
  116. return render_template('404.html', entity='hook'), 404
  117. except TooManyEntities:
  118. return render_template('500.html', cause='too many hooks'), 500
  119. if request.method == 'POST':
  120. act = Action.create(
  121. hook.rowid,
  122. request.values['action'], request.values['a1'], request.values['a2'], request.values['a3'],
  123. )
  124. return redirect('/act/%d'%(act.rowid,))
  125. return render_template('hook_new_act.html', hook=hook)
  126. @app.route('/hook/<int:hookid>/delete', methods=['GET', 'POST'])
  127. def hook_delete(hookid):
  128. if not ip_okay(request.remote_addr):
  129. return render_template('404.html', entity='page'), 404
  130. try:
  131. hook = Hook.get_one(rowid=hookid)
  132. except NoSuchEntity:
  133. return render_template('404.html', entity='hook'), 404
  134. except TooManyEntities:
  135. return render_template('500.html', cause='too many hooks'), 500
  136. if request.method == 'POST':
  137. hook.delete()
  138. return redirect(url_for('hooks'))
  139. return render_template('hook_delete.html', hook=hook)
  140. @app.route('/cond/<int:condid>')
  141. def cond_obj(condid):
  142. if not ip_okay(request.remote_addr):
  143. return render_template('404.html', entity='page'), 404
  144. try:
  145. cond = Condition.get_one(rowid=condid)
  146. except NoSuchEntity:
  147. return render_template('404.html', entity='condition'), 404
  148. except TooManyEntities:
  149. return render_template('500.html', cause='too many conditions'), 500
  150. return render_template('cond.html', cond=cond)
  151. @app.route('/cond/<int:condid>/edit', methods=['GET', 'POST'])
  152. def cond_edit(condid):
  153. if not ip_okay(request.remote_addr):
  154. return render_template('404.html', entity='page'), 404
  155. try:
  156. cond = Condition.get_one(rowid=condid)
  157. except NoSuchEntity:
  158. return render_template('404.html', entity='condition'), 404
  159. except TooManyEntities:
  160. return render_template('500.html', cause='too many conditions'), 500
  161. if request.method == 'POST':
  162. cond.selector = request.values['selector']
  163. cond.s1 = request.values['s1']
  164. cond.s2 = request.values['s2']
  165. cond.s3 = request.values['s3']
  166. cond.test = request.values['test']
  167. cond.t1 = request.values['t1']
  168. cond.t2 = request.values['t2']
  169. cond.t3 = request.values['t3']
  170. cond.invert = checkbox(request, 'invert')
  171. cond.update()
  172. return redirect('/cond/%d'%(cond.rowid,))
  173. return render_template('cond_edit.html', cond=cond)
  174. @app.route('/cond/<int:condid>/delete', methods=['GET', 'POST'])
  175. def cond_delete(condid):
  176. if not ip_okay(request.remote_addr):
  177. return render_template('404.html', entity='page'), 404
  178. try:
  179. cond = Condition.get_one(rowid=condid)
  180. except NoSuchEntity:
  181. return render_template('404.html', entity='condition'), 404
  182. except TooManyEntities:
  183. return render_template('500.html', cause='too many conditions'), 500
  184. if request.method == 'POST':
  185. cond.delete()
  186. return redirect('/hook/%d'%(cond.hook,))
  187. return render_template('cond_delete.html', cond=cond)
  188. @app.route('/act/<int:actid>')
  189. def act_obj(actid):
  190. if not ip_okay(request.remote_addr):
  191. return render_template('404.html', entity='page'), 404
  192. try:
  193. act = Action.get_one(rowid=actid)
  194. except NoSuchEntity:
  195. return render_template('404.html', entity='action'), 404
  196. except TooManyEntities:
  197. return render_template('500.html', cause='too many actions'), 500
  198. return render_template('act.html', act=act)
  199. @app.route('/act/<int:actid>/edit', methods=['GET', 'POST'])
  200. def act_edit(actid):
  201. if not ip_okay(request.remote_addr):
  202. return render_template('404.html', entity='page'), 404
  203. try:
  204. act = Action.get_one(rowid=actid)
  205. except NoSuchEntity:
  206. return render_template('404.html', entity='action'), 404
  207. except TooManyEntities:
  208. return render_template('500.html', cause='too many actions'), 500
  209. if request.method == 'POST':
  210. act.action = request.values['action']
  211. act.a1 = request.values['a1']
  212. act.a2 = request.values['a2']
  213. act.a3 = request.values['a3']
  214. act.update()
  215. return redirect('/act/%d'%(act.rowid,))
  216. return render_template('act_edit.html', act=act)
  217. @app.route('/act/<int:actid>/delete', methods=['GET', 'POST'])
  218. def act_delete(actid):
  219. if not ip_okay(request.remote_addr):
  220. return render_template('404.html', entity='page'), 404
  221. try:
  222. act = Action.get_one(rowid=actid)
  223. except NoSuchEntity:
  224. return render_template('404.html', entity='action'), 404
  225. except TooManyEntities:
  226. return render_template('500.html', cause='too many actions'), 500
  227. if request.method == 'POST':
  228. act.delete()
  229. return redirect('/hook/%d'%(act.hook,))
  230. return render_template('act_delete.html', act=act)
  231. if __name__ == '__main__':
  232. app.run(host='0.0.0.0', port=8880)