Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/admin_ui/media/javascript/ui/panel/AutoRunTab.js
1155 views
1
const defaultRule = {
2
"name": "Display an alert",
3
"author": "mgeeky",
4
"modules": [
5
{
6
"name": "alert_dialog",
7
"condition": null,
8
"options": {
9
"text":"You've been BeEFed ;>"
10
}
11
}
12
],
13
"execution_order": [0],
14
"execution_delay": [0],
15
"chain_mode": "nested-forward"
16
};
17
18
19
20
/**
21
* Asynchronously returns the currently active rules in an array.
22
* Empty array means no rules are active.
23
* null if there was an error.
24
*/
25
getCurrentRules = async function(token) {
26
27
try {
28
var res = await fetch(`/api/autorun/rules?token=${token}`);
29
if (!res.ok) {
30
throw new Error(`Getting auto run rules failed with status ${res.status}`);
31
}
32
const data = await res.json();
33
const rules = JSON.parse(data.rules);
34
35
if (data.success === true && Array.isArray(rules)) {
36
return rules;
37
}
38
39
console.log("No active auto run rules.");
40
return [];
41
42
} catch(error) {
43
console.error(error);
44
console.error("Failed to get auto run rules.");
45
return null;
46
}
47
}
48
49
getModules = async function(token) {
50
try {
51
var res = await fetch(`/api/modules?token=${token}`);
52
if (!res.ok) {
53
throw new Error(`Getting auto run rules failed with status ${res.status}`);
54
}
55
const modules = await res.json();
56
57
return modules;
58
59
} catch(error) {
60
console.error(error);
61
console.error("Failed to get auto run rules.");
62
return null;
63
}
64
}
65
66
AutoRunTab = function() {
67
// RESTful API token.
68
var token = BeefWUI.get_rest_token();
69
70
// Heading container to describe general Auto Run state.
71
var ruleLoadingState = new Ext.Container({
72
html: "<p>Loading Auto Run rules...</p>",
73
})
74
var headingContainer = new Ext.Panel({
75
style: {
76
font: '11px tahoma,arial,helvetica,sans-serif'
77
},
78
padding:'10 10 10 10',
79
border: false,
80
items: [{
81
xtype: 'container',
82
html: '\
83
<div>\
84
<h4>Auto Run Rules</h4>\
85
<p>These determine what commands run automatically when a browser is hooked.</p>\
86
</div>'
87
},
88
ruleLoadingState,
89
{
90
xtype: 'button',
91
text: 'Add New Rule',
92
handler: addRule
93
}],
94
listeners: {
95
afterrender: loadRules
96
}
97
});
98
// Contains all of the rules and inputs to change each rule.
99
var ruleContainer = new Ext.Panel({
100
border: false,
101
listeners: {
102
afterrender: loadRules
103
}
104
});
105
106
async function deleteRule(id) {
107
const res = await fetch(`/api/autorun/rule/${id}?token=${token}`, {method: 'DELETE'});
108
if (!res.ok) {
109
console.error(`Failed when deleting rule with id ${id}. Failed with status ${res.status}.`);
110
return;
111
}
112
// Update the entire rules panel. Not very efficient.
113
loadRules();
114
}
115
116
async function addRule() {
117
const res = await fetch(`/api/autorun/rule/add?token=${token}`, {
118
method: 'POST',
119
headers: {'Content-Type': 'application/json'},
120
body: JSON.stringify(defaultRule)
121
});
122
if (!res.ok) {
123
console.error(`Failed when adding a new rule with status ${res.status}.`);
124
return;
125
}
126
// Update the entire rules panel. Not very efficient.
127
loadRules();
128
}
129
130
async function updateRule(id, newRuleData) {
131
const res = await fetch(`/api/autorun/rule/${id}?token=${token}`, {
132
method: 'PATCH',
133
headers: {'Content-Type': 'application/json'},
134
body: JSON.stringify(newRuleData)
135
});
136
if (!res.ok) {
137
console.error(`Failed when adding a new rule with status ${res.status}.`);
138
return;
139
}
140
// Update the entire rules panel. Not very efficient.
141
loadRules();
142
}
143
144
async function loadRules() {
145
let modules = [];
146
let rules = [];
147
try {
148
modules = await getModules(token);
149
rules = await getCurrentRules(token);
150
} catch (error) {
151
console.error(error);
152
console.error("Failed to load command modules and/or rules for Auto Run.");
153
ruleLoadingState.update("<p>Failed to load Auto Run rules.</p>");
154
return;
155
}
156
157
if (rules !== null) {
158
ruleLoadingState.update(`<p>Loaded ${rules.length} Auto Run rules.</p>`);
159
ruleContainer.removeAll();
160
161
for (let i = 0; i < rules.length; i++) {
162
ruleForm = new AutoRunRuleForm(
163
rules[i],
164
modules,
165
function() {deleteRule(rules[i].id)},
166
function(newRuleData) {updateRule(rules[i].id, newRuleData)}
167
);
168
ruleContainer.add(ruleForm);
169
}
170
ruleContainer.doLayout();
171
} else {
172
ruleLoadingState.update("<p>Failed to load Auto Run rules.</p>");
173
}
174
}
175
176
AutoRunTab.superclass.constructor.call(this, {
177
region: 'center',
178
items: [headingContainer, ruleContainer],
179
autoScroll: true,
180
border: false,
181
closable: false
182
});
183
};
184
185
Ext.extend(AutoRunTab, Ext.Panel, {});
186