bashhub 3.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
bashhub/rest_client.py ADDED
@@ -0,0 +1,251 @@
1
+ #!/usr/bin/python
2
+ import json
3
+ import sys
4
+ import requests
5
+ from requests import ConnectionError
6
+ import os
7
+
8
+ from .model import MinCommand
9
+ from .model import StatusView
10
+ from .model import Command
11
+ from .model import LoginResponse
12
+ from .model import System
13
+ from .bashhub_globals import BH_URL, BH_AUTH
14
+ from .version import __version__
15
+ from requests import ConnectionError
16
+ from requests import HTTPError
17
+
18
+ # Build our user agent string
19
+ user_agent = 'bashhub/%s' % __version__
20
+
21
+ base_headers = {'User-Agent': user_agent, 'X-Bashhub-version': __version__}
22
+
23
+ json_headers = dict(
24
+ {'content-type': 'application/json',
25
+ 'Accept': 'application/json'}, **base_headers)
26
+
27
+
28
+ def json_auth_headers():
29
+ return dict({'Authorization': 'Bearer {0}'.format(BH_AUTH())},
30
+ **json_headers)
31
+
32
+
33
+ def base_auth_headers():
34
+ return dict({'Authorization': 'Bearer {0}'.format(BH_AUTH())},
35
+ **base_headers)
36
+
37
+
38
+ def register_user(register_user):
39
+ url = BH_URL + "/api/v1/user"
40
+ try:
41
+ response = requests.post(url,
42
+ data=register_user.to_JSON(),
43
+ headers=json_headers)
44
+ response.raise_for_status()
45
+
46
+ # Return our username on a successful response
47
+ return register_user.username
48
+
49
+ except ConnectionError as error:
50
+ print("Looks like there's a connection error. Please try again later")
51
+ except HTTPError as error:
52
+ if response.status_code in (409, 422):
53
+ print(response.text)
54
+ else:
55
+ print(error)
56
+ print("Please try again...")
57
+ return None
58
+
59
+
60
+ def login_user(login_form):
61
+ url = BH_URL + "/api/v1/login"
62
+ try:
63
+ response = requests.post(url,
64
+ data=login_form.to_JSON(),
65
+ headers=json_headers)
66
+
67
+ response.raise_for_status()
68
+ login_response_json = json.dumps(response.json())
69
+ return LoginResponse.from_JSON(login_response_json).access_token
70
+
71
+ except ConnectionError as error:
72
+ print("Looks like there's a connection error. Please try again later")
73
+ return None
74
+ except HTTPError as error:
75
+ if response.status_code in (409, 401):
76
+ print(response.text)
77
+ else:
78
+ print(error)
79
+ print("Please try again...")
80
+ return None
81
+
82
+
83
+ def register_system(register_system):
84
+ url = BH_URL + "/api/v1/system"
85
+ headers = {'content-type': 'application/json'}
86
+ try:
87
+ response = requests.post(url,
88
+ data=register_system.to_JSON(),
89
+ headers=json_auth_headers())
90
+ response.raise_for_status()
91
+ return register_system.name
92
+
93
+ except ConnectionError as error:
94
+ print("Looks like there's a connection error. Please try again later")
95
+ except HTTPError as error:
96
+ if response.status_code == 409:
97
+ print(response.text)
98
+ else:
99
+ print(error)
100
+ print("Please try again...")
101
+ return None
102
+
103
+
104
+ def get_system_information(mac):
105
+ url = BH_URL + '/api/v1/system'
106
+ payload = {'mac': mac}
107
+ try:
108
+ response = requests.get(url,
109
+ params=payload,
110
+ headers=json_auth_headers())
111
+ response.raise_for_status()
112
+ system_json = json.dumps(response.json())
113
+ return System.from_JSON(system_json)
114
+ except ConnectionError as error:
115
+ print("Looks like there's a connection error. Please try again later")
116
+ except HTTPError as error:
117
+ return None
118
+
119
+
120
+ def get_command(uuid):
121
+ url = BH_URL + "/api/v1/command/{0}".format(uuid)
122
+ try:
123
+ response = requests.get(url, headers=json_auth_headers())
124
+ response.raise_for_status()
125
+ json_command = json.dumps(response.json())
126
+ return Command.from_JSON(json_command)
127
+
128
+ except ConnectionError as error:
129
+ print("Looks like there's a connection error. Please try again later")
130
+ except HTTPError as error:
131
+ print(error)
132
+ print("Please try again...")
133
+
134
+ return None
135
+
136
+
137
+ def delete_command(uuid):
138
+ url = BH_URL + "/api/v1/command/{0}".format(uuid)
139
+ try:
140
+ response = requests.delete(url, headers=base_auth_headers())
141
+ response.raise_for_status()
142
+ return uuid
143
+
144
+ except ConnectionError as error:
145
+ pass
146
+ except HTTPError as error:
147
+ print(error)
148
+
149
+ return None
150
+
151
+
152
+ def patch_system(system_patch, mac):
153
+
154
+ url = BH_URL + "/api/v1/system/{0}".format(mac)
155
+
156
+ r = None
157
+ try:
158
+ r = requests.patch(url,
159
+ data=system_patch.to_JSON(),
160
+ headers=json_auth_headers())
161
+ r.raise_for_status()
162
+ return r.status_code
163
+ except Exception as error:
164
+ if r is not None and r.status_code in (403, 401):
165
+ print("Permissions Issue. Run bashhub setup to re-login.")
166
+ return None
167
+
168
+
169
+ def search(limit=None, path=None, query=None, system_name=None, unique=None, session_id=None):
170
+
171
+ payload = dict()
172
+
173
+ if limit:
174
+ payload["limit"] = limit
175
+
176
+ if path:
177
+ payload["path"] = path
178
+
179
+ if query:
180
+ payload["query"] = query
181
+
182
+ if system_name:
183
+ payload["systemName"] = system_name
184
+
185
+ if session_id:
186
+ payload["sessionUuid"] = session_id
187
+
188
+ payload["unique"] = str(unique).lower()
189
+ url = BH_URL + "/api/v1/command/search"
190
+
191
+ r = None
192
+ try:
193
+ r = requests.get(url, params=payload, headers=json_auth_headers())
194
+ return MinCommand.from_JSON_list(r.json())
195
+
196
+ except ConnectionError as error:
197
+ print("Sorry, looks like there's a connection error. Please try again later")
198
+ except Exception as error:
199
+ if r is not None:
200
+ if r.status_code in (403, 401):
201
+ print("Permissions Issue. Run bashhub setup to re-login.")
202
+ elif r.status_code in [400]:
203
+ print(
204
+ "Sorry, an error occurred communicating with Bashhub. Response Code: "
205
+ + str(r.status_code))
206
+ print(r.text)
207
+ else:
208
+ print(
209
+ "Sorry, an error occurred communicating with Bashhub. Response Code: "
210
+ + str(r.status_code))
211
+ print(error)
212
+ return []
213
+
214
+
215
+ def save_command(command):
216
+ url = BH_URL + "/api/v1/command"
217
+
218
+ r = None
219
+ try:
220
+ r = requests.post(url,
221
+ data=command.to_JSON(),
222
+ headers=json_auth_headers())
223
+ except ConnectionError as error:
224
+ print("Sorry, looks like there's a connection error")
225
+ pass
226
+ except Exception as error:
227
+ if r is not None and r.status_code in (403, 401):
228
+ print("Permissions Issue. Run bashhub setup to re-login.")
229
+
230
+
231
+ def get_status_view(process_id, start_time):
232
+ url = BH_URL + "/api/v1/client-view/status"
233
+
234
+ payload = {'processId': process_id, 'startTime': start_time}
235
+ r = None
236
+ try:
237
+ r = requests.get(url, params=payload, headers=json_auth_headers())
238
+ status_view_json = json.dumps(r.json())
239
+ return StatusView.from_JSON(status_view_json)
240
+ except ConnectionError as error:
241
+ print("Sorry, looks like there's a connection error")
242
+ return None
243
+ except Exception as error:
244
+ if r is not None:
245
+ if r.status_code in (403, 401):
246
+ print("Permissions Issue. Run bashhub setup to re-login.")
247
+ else:
248
+ print(
249
+ "Sorry, an error occurred communicating with Bashhub. Response Code: "
250
+ + str(r.status_code))
251
+ return None
@@ -0,0 +1,105 @@
1
+ #
2
+ # bashhub.fish
3
+ # Main file that is sourced onto our path for fish.
4
+ #
5
+
6
+ #
7
+ # Checks if an element is present in an array.
8
+ #
9
+ # @param The element to check if present
10
+ # @param the array to check in
11
+ # @return 0 if present 1 otherwise
12
+ #
13
+ function contains_element --argument-names element array
14
+ for e in $array
15
+ [ "$e" = "$element" ] && return 0
16
+ end
17
+
18
+ return 1
19
+ end
20
+
21
+ function __bh_path_add --argument-names item
22
+ if [ -d "$item" ] && not contains_element "$item" "$PATH"
23
+ set -x PATH "$item" "$PATH"
24
+ end
25
+ end
26
+
27
+ function __bh_interactive
28
+ fish -c "bh -i"
29
+ end
30
+
31
+ # Avoid duplicate inclusion
32
+ if [ "$__bh_imported" = "defined" ]
33
+ __bh_path_add "$HOME/.bashhub/bin"
34
+ else
35
+ set -Ux __bh_imported "defined"
36
+ set -Ux BH_HOME_DIRECTORY "$HOME/.bashhub/"
37
+
38
+ source "$BH_HOME_DIRECTORY/deps/fish/functions/__bh_check_bashhub_installation.fish"
39
+ bind \cb __bh_interactive
40
+ end
41
+
42
+ function __bh_preexec --on-event fish_preexec
43
+ set -g __BH_PWD "$PWD"
44
+ set -g __BH_SAVE_COMMAND "$argv[1]"
45
+ end
46
+
47
+ function __bh_precmd --on-event fish_prompt
48
+ set -x __BH_EXIT_STATUS $status
49
+
50
+ if [ -e "$BH_HOME_DIRECTORY/response.bh" ]
51
+ set -l cmd (head -n 1 "$BH_HOME_DIRECTORY/response.bh")
52
+ rm "$BH_HOME_DIRECTORY/response.bh"
53
+ echo $cmd
54
+ end
55
+
56
+ if [ -n "$BH_HOME_DIRECTORY" ]
57
+ set -g bashhub_dir "$BH_HOME_DIRECTORY"
58
+ else
59
+ set -g bashhub_dir "~/.bashhub"
60
+ end
61
+
62
+ set -x working_directory "$__BH_PWD"
63
+ set -x cmd "$__BH_SAVE_COMMAND"
64
+ set -x process_id $fish_pid
65
+
66
+ if [ -n "$__BH_SAVE_COMMAND" ]
67
+ set -e __BH_SAVE_COMMAND
68
+ else
69
+ return 0
70
+ end
71
+
72
+ if [ -e "$bashhub_dir" ]
73
+ fish -c '__bh_process_command "$cmd" "$working_directory" "$process_id" &' >> "$bashhub_dir"/log.txt 2>&1
74
+ end
75
+ end
76
+
77
+ #
78
+ # Send our command to the server if everything
79
+ # looks good.
80
+ #
81
+ function __bh_process_command --argument-names cmd dir pid
82
+ set -x bh_command (string trim $cmd)
83
+
84
+ # sanity check
85
+ if [ -z "$bh_command" ]
86
+ return 0
87
+ end
88
+
89
+ # ensure that bashhub is installed
90
+ if not type "bashhub" > /dev/null 2>&1
91
+ return 0
92
+ end
93
+
94
+ set -x working_directory "$dir"
95
+ set -x process_id "$pid"
96
+
97
+ # This is non-standard across systems. As GNU and BSD Date convert epochs
98
+ # differently, use python for cross-system compatibility.
99
+ set -l process_start_stamp (env LC_ALL=C ps -p $fish_pid -o lstart=)
100
+
101
+ set -x process_start (bashhub util parsedate "$process_start_stamp")
102
+ set -x exit_status "$__BH_EXIT_STATUS"
103
+
104
+ fish -c 'bashhub save "$bh_command" "$working_directory" "$process_id" "$process_start" "$exit_status" &'
105
+ end
@@ -0,0 +1,73 @@
1
+ #
2
+ # bashhub.sh
3
+ # Main file that is sourced onto our path for Bash.
4
+ #
5
+
6
+ # Avoid duplicate inclusion
7
+ if [[ "$__bh_imported" == "defined" ]]; then
8
+ __bh_path_add "$HOME/.bashhub/bin"
9
+ return 0
10
+ fi
11
+
12
+ __bh_imported="defined"
13
+
14
+ export BH_HOME_DIRECTORY="$HOME/.bashhub/"
15
+
16
+ BH_DEPS_DIRECTORY=${BH_DEPS_DIRECTORY:=$BH_HOME_DIRECTORY/deps}
17
+
18
+ __bh_setup_bashhub() {
19
+
20
+ # check that we're using bash and that all our
21
+ # dependencies are satisfied.
22
+ if [[ -n $BASH_VERSION ]] && \
23
+ [[ -f $BH_DEPS_DIRECTORY/lib-bashhub.sh ]] && \
24
+ [[ -f $BH_DEPS_DIRECTORY/bash-preexec.sh ]]; then
25
+
26
+ # Pull in our libs
27
+ source "$BH_DEPS_DIRECTORY/lib-bashhub.sh"
28
+ source "$BH_DEPS_DIRECTORY/bash-preexec.sh"
29
+
30
+ # Hook bashhub into preexec and precmd.
31
+ __bh_hook_bashhub
32
+
33
+ # Install our tab completion (requires bash 4.0+ for compopt)
34
+ if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
35
+ eval "$(_BASHHUB_COMPLETE=bash_source bashhub)"
36
+ fi
37
+ fi
38
+ }
39
+
40
+ __bh_hook_bashhub() {
41
+
42
+ if [ -t 1 ]; then
43
+ # Alias to bind Ctrl + B
44
+ bind '"\C-b":"\C-ubh -i\n"'
45
+ fi
46
+
47
+ # Hook into preexec and precmd functions
48
+ if ! contains_element __bh_preexec "${preexec_functions[@]}"; then
49
+ preexec_functions+=(__bh_preexec)
50
+ fi
51
+
52
+ if ! contains_element __bh_precmd "${precmd_functions[@]}"; then
53
+ # Order seems to matter here due to the fork at the end of __bh_precmd
54
+ precmd_functions+=(__bh_bash_precmd)
55
+ precmd_functions+=(__bh_precmd)
56
+ fi
57
+ }
58
+
59
+ __bh_bash_precmd() {
60
+ if [[ -e $BH_HOME_DIRECTORY/response.bh ]]; then
61
+ local command=$(head -n 1 "$BH_HOME_DIRECTORY/response.bh")
62
+ rm "$BH_HOME_DIRECTORY/response.bh"
63
+ history -s "$command"
64
+ # Save that we're executing this command again by calling bashhub's
65
+ # preexec and precmd functions
66
+ __bh_preexec "$command"
67
+ echo "$command"
68
+ eval "$command"
69
+ __bh_precmd
70
+ fi;
71
+ }
72
+
73
+ __bh_setup_bashhub
@@ -0,0 +1,65 @@
1
+ #
2
+ # bashhub.zsh
3
+ # Main file that is sourced onto our path for Zsh.
4
+ #
5
+
6
+ # Avoid duplicate inclusion
7
+ if [[ "$__bh_imported" == "defined" ]]; then
8
+ __bh_path_add "$HOME/.bashhub/bin"
9
+ return 0
10
+ fi
11
+
12
+ __bh_imported="defined"
13
+
14
+ export BH_HOME_DIRECTORY="$HOME/.bashhub/"
15
+
16
+ BH_DEPS_DIRECTORY=${BH_DEPS_DIRECTORY:=$BH_HOME_DIRECTORY/deps}
17
+
18
+ __bh_setup_bashhub() {
19
+
20
+ # check that we're using zsh and that all our
21
+ # dependencies are satisfied.
22
+ if [[ -n $ZSH_VERSION ]] && [[ -f $BH_DEPS_DIRECTORY/lib-bashhub.sh ]]; then
23
+
24
+ # Pull in our library.
25
+ source $BH_DEPS_DIRECTORY/lib-bashhub.sh
26
+
27
+ # Hook bashhub into preexec and precmd.
28
+ __bh_hook_bashhub
29
+
30
+ # Install our tab completion.
31
+ autoload compinit && compinit
32
+ eval "$(_BASHHUB_COMPLETE=zsh_source bashhub)"
33
+
34
+ # Turn on Bash style comments. Otherwise zsh tries to execute #some-comment.
35
+ setopt interactivecomments
36
+
37
+ fi
38
+ }
39
+
40
+ __bh_hook_bashhub() {
41
+
42
+ # Bind ctrl + b to bh -i
43
+ bindkey -s '^b' "bh -i\n"
44
+
45
+ # Hook into preexec and precmd functions if they're not already
46
+ # present there.
47
+ if ! contains_element __bh_preexec $preexec_functions; then
48
+ preexec_functions+=(__bh_preexec)
49
+ fi
50
+
51
+ if ! contains_element __bh_precmd $precmd_functions; then
52
+ precmd_functions+=(__bh_zsh_precmd)
53
+ precmd_functions+=(__bh_precmd)
54
+ fi
55
+ }
56
+
57
+ __bh_zsh_precmd() {
58
+ if [[ -e $BH_HOME_DIRECTORY/response.bh ]]; then
59
+ local COMMAND="`head -n 1 $BH_HOME_DIRECTORY/response.bh`"
60
+ rm $BH_HOME_DIRECTORY/response.bh
61
+ print -z $COMMAND
62
+ fi;
63
+ }
64
+
65
+ __bh_setup_bashhub