-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathapp.py
executable file
·206 lines (167 loc) · 6.34 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import json
import ast
import requests
from walkoff_app_sdk.app_base import AppBase
class TWILIO(AppBase):
__version__ = "1.9.0"
app_name = "twilio"
def __init__(self, redis, logger, console_logger=None):
print("INIT")
"""
Each app should have this __init__ to set up Redis and logging.
:param redis:
:param logger:
:param console_logger:
"""
super().__init__(redis, logger, console_logger)
def splitheaders(self, headers):
parsed_headers = {}
if headers:
split_headers = headers.split("\n")
self.logger.info(split_headers)
for header in split_headers:
if ": " in header:
splititem = ": "
elif ":" in header:
splititem = ":"
elif "= " in header:
splititem = "= "
elif "=" in header:
splititem = "="
else:
self.logger.info("Skipping header %s as its invalid" % header)
continue
splitheader = header.split(splititem)
if len(splitheader) == 2:
parsed_headers[splitheader[0]] = splitheader[1]
else:
self.logger.info("Skipping header %s with split %s cus only one item" % (header, splititem))
continue
return parsed_headers
def checkbody(self, body):
# Indicates json
if isinstance(body, str):
if body.strip().startswith("{"):
body = json.dumps(ast.literal_eval(body))
# Not sure if loading is necessary
# Seemed to work with plain string into data=body too, and not parsed json=body
#try:
# body = json.loads(body)
#except json.decoder.JSONDecodeError as e:
# return body
return body
else:
return body
if isinstance(body, dict) or isinstance(body, list):
try:
body = json.dumps(body)
except:
return body
return body
def fix_url(self, url):
# Random bugs seen by users
if "hhttp" in url:
url = url.replace("hhttp", "http")
if "http:/" in url and not "http://" in url:
url = url.replace("http:/", "http://", -1)
if "https:/" in url and not "https://" in url:
url = url.replace("https:/", "https://", -1)
if "http:///" in url:
url = url.replace("http:///", "http://", -1)
if "https:///" in url:
url = url.replace("https:///", "https://", -1)
if not "http://" in url and not "http" in url:
url = f"http://{url}"
return url
def return_file(self, requestdata):
filedata = {
"filename": "response.txt",
"data": requestdata,
}
fileret = self.set_files([filedata])
if len(fileret) == 1:
return {"success": True, "file_id": fileret[0]}
return fileret
def prepare_response(self, request):
try:
parsedheaders = {}
for key, value in request.headers.items():
parsedheaders[key] = value
cookies = {}
if request.cookies:
for key, value in request.cookies.items():
cookies[key] = value
jsondata = request.text
try:
jsondata = json.loads(jsondata)
except:
pass
return {
"success": True,
"status": request.status_code,
"url": request.url,
"headers": parsedheaders,
"body": jsondata,
"cookies":cookies,
}
except Exception as e:
print(f"[WARNING] Failed in request: {e}")
return {
"success": False,
"status": "XXX",
"error": request.text
}
def summarize_responses(self, one_response, summary):
summary["results"].append(one_response)
# if ONE request fails, summary is marked as failed
if False == one_response["success"]:
summary["success"] = False
# if one status code is not 200, use this failure status code for summary
if "200" != one_response["status"]:
summary["status"] = one_response["status"]
return summary
def Send_SMS(self, url, headers="", username="", password="", body="", From="", To="", timeout=5):
url = self.fix_url(url)
parsed_headers = self.splitheaders(headers)
parsed_headers["User-Agent"] = "Shuffle Automation"
body = self.checkbody(body)
auth=None
if username or password:
# Shouldn't be used if authorization headers exist
if "Authorization" in parsed_headers:
#print("Found authorization - skipping username & pw")
pass
else:
auth = requests.auth.HTTPBasicAuth(username, password)
if not timeout:
timeout = 5
if timeout:
timeout = int(timeout)
summary = {
"success": True,
"status": "200",
"url": url,
"results": []
}
# send Twilio API request for every single receiver number
for receiver in To.split(","):
data = {'Body' : body, 'From' : From, 'To' : receiver.strip()}
request = requests.post(url, headers=parsed_headers, auth=auth, data=data, timeout=timeout)
response = self.prepare_response(request)
summary = self.summarize_responses(response, summary)
return json.dumps(summary)
# Run the actual thing after we've checked params
def run(request):
print("Starting cloud!")
action = request.get_json()
print(action)
print(type(action))
authorization_key = action.get("authorization")
current_execution_id = action.get("execution_id")
if action and "name" in action and "app_name" in action:
TWILIO.run(action)
return f'Attempting to execute function {action["name"]} in app {action["app_name"]}'
else:
return f'Invalid action'
if __name__ == "__main__":
TWILIO.run()