Skip to content

Commit 5323c4b

Browse files
committed
Add port forwarding page
Add the port forwarding page that has the table showing current port forwards and a link to add a new one. Using the generic tables infrastructure this was not a huge deal. The work went along these lines: 1. Add the model class to model.hpp/model.cpp 2. Add the model class to the two factories in model.cpp 3. Create a form that references the model 4. Add pyrobox.js and a call to json_forms_and_tables that references the form 5. Add the table to the html page that is referenced by the form 6. Wrap the table with a popup add class if desired 7. Fix any bugs that appear and do regression testing on existing tables to make sure changes didn't break those too. This is the path that I followed to add this page. In the bug fixing department, here is a laundry list of what I found: 1. Not all items are inline-form ready. I had to modify the select form item in pyrobox.js to render properly. 2. db.cpp was not properly handling null fields in the database 3. Table sorter does not appear to like empty tables. (not fixed) 4. The add new row logic does not accomodate empty tables. (not fixed) Signed-off-by: Vernon Mauery <[email protected]>
1 parent 2f32438 commit 5323c4b

File tree

6 files changed

+153
-20
lines changed

6 files changed

+153
-20
lines changed

fcgi/db.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ using namespace std;
77
static int parse_rows(void *arg, int col_count, char **columns, char **col_names) {
88
db::Results *results = (db::Results *)arg;
99
db::Result row;
10+
char * null="";
1011
for (int i=0; i<col_count; i++) {
11-
// cout << col_names[i] << " => " << columns[i] << ", ";
12-
row[col_names[i]] = columns[i];
12+
// info(col_names[i] << " => " << (columns[i]?columns[i]:null) << ", ");
13+
row[col_names[i]] = (columns[i]?columns[i]:null);
1314
}
14-
// cout << endl;
1515
results->push_back(row);
1616
return 0;
1717
}

fcgi/model.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ list<model::ptr> model::fetch_all(const string& type) {
5151
if (type == "deny_dhcp") {
5252
return deny_dhcp::all();
5353
}
54+
if (type == "firewall_forward") {
55+
return firewall_forward::all();
56+
}
5457
return list<model::ptr>();
5558
}
5659

@@ -62,6 +65,9 @@ model::ptr model::factory(const string& type, const db::Result& vals) {
6265
if (type == "deny_dhcp") {
6366
return deny_dhcp::create(vals);
6467
}
68+
if (type == "firewall_forward") {
69+
return firewall_forward::create(vals);
70+
}
6571
return model::ptr();
6672
}
6773

@@ -213,6 +219,41 @@ string deny_dhcp::json() {
213219
}
214220

215221

222+
//////////////////////////////////////////////////////////////////////////
223+
// class firewall_forward
224+
/////////////////////////////////////////////////////////////////////////
225+
const char firewall_forward::_table_name[] = "app_portforward";
226+
227+
const vector<string>& firewall_forward::names() const {
228+
static vector<string> _names;
229+
if (_names.size() == 0) {
230+
_names.push_back("id");
231+
_names.push_back("protocol");
232+
_names.push_back("src_port");
233+
_names.push_back("port_range");
234+
_names.push_back("dst_addr");
235+
_names.push_back("dst_port");
236+
}
237+
return _names;
238+
}
239+
240+
model_all(firewall_forward)
241+
model_create(firewall_forward)
242+
243+
string firewall_forward::json() {
244+
stringstream ss;
245+
ss << "{ "
246+
<< "\"id\": " << id() << ", "
247+
<< "\"protocol\": \"" << protocol() << "\", "
248+
<< "\"src_port\": \"" << src_port() << "\", "
249+
<< "\"port_range\": \"" << port_range() << "\", "
250+
<< "\"dst_addr\": \"" << dst_addr() << "\", "
251+
<< "\"dst_port\": \"" << dst_port() << "\", "
252+
<< " }";
253+
return ss.str();
254+
}
255+
256+
216257
//////////////////////////////////////////////////////////////////////////
217258
// class variable
218259
/////////////////////////////////////////////////////////////////////////

fcgi/model.hpp

+40-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class static_dhcp : public model {
6565

6666
private:
6767
static const char _table_name[];
68-
68+
6969
// variable access interface
7070
public:
7171
virtual std::string json();
@@ -98,7 +98,7 @@ class deny_dhcp : public model {
9898

9999
private:
100100
static const char _table_name[];
101-
101+
102102
// variable access interface
103103
public:
104104
virtual std::string json();
@@ -108,6 +108,43 @@ class deny_dhcp : public model {
108108
void mac_addr(const std::string& value) { _set("mac_addr", value); }
109109
};
110110

111+
//////////////////////////////////////////////////////////////////////////
112+
// class firewall_forward
113+
/////////////////////////////////////////////////////////////////////////
114+
class firewall_forward : public model {
115+
public:
116+
typedef boost::shared_ptr<firewall_forward> ptr;
117+
118+
public:
119+
virtual ~firewall_forward() {}
120+
static std::list<model::ptr> all();
121+
static firewall_forward::ptr create(const db::Result& vals);
122+
123+
protected:
124+
firewall_forward() : model() {}
125+
firewall_forward(long int i) : model(i) {}
126+
firewall_forward(const db::Result& vals) : model(vals) {}
127+
virtual const char *table_name() const { return _table_name; }
128+
virtual const std::vector<std::string>& names() const;
129+
130+
private:
131+
static const char _table_name[];
132+
133+
// variable access interface
134+
public:
135+
virtual std::string json();
136+
std::string& protocol() { return _get("protocol"); }
137+
void protocol(const std::string& value) { _set("protocol", value); }
138+
std::string& src_port() { return _get("src_port"); }
139+
void src_port(const std::string& value) { _set("src_port", value); }
140+
std::string& port_range() { return _get("port_range"); }
141+
void port_range(const std::string& value) { _set("port_range", value); }
142+
std::string& dst_addr() { return _get("dst_addr"); }
143+
void dst_addr(const std::string& value) { _set("dst_addr", value); }
144+
std::string& dst_port() { return _get("dst_port"); }
145+
void dst_port(const std::string& value) { _set("dst_port", value); }
146+
};
147+
111148
//////////////////////////////////////////////////////////////////////////
112149
// class variable
113150
/////////////////////////////////////////////////////////////////////////
@@ -130,7 +167,7 @@ class variable : public model {
130167

131168
private:
132169
static const char _table_name[];
133-
170+
134171
// variable access interface
135172
public:
136173
virtual std::string json();

html/js/json/forms/firewall_forward

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
model:
2+
name: firewall_forward
3+
id: port-forward-form
4+
action: none
5+
actions: edit delete
6+
class: ajax json
7+
elements:
8+
hidden:
9+
name: id
10+
end: hidden
11+
select:
12+
name: protocol
13+
label: Protocol
14+
default_value: tcp
15+
options:
16+
tcp: TCP
17+
udp: UDP
18+
end: options
19+
end: select
20+
textbox:
21+
name: src_port
22+
label: Source Port
23+
end: textbox
24+
textbox:
25+
name: port_range
26+
label: Port Range
27+
end: textbox
28+
textbox:
29+
name: dst_addr
30+
label: Destination IP Address
31+
end: textbox
32+
textbox:
33+
name: dst_port
34+
label: Destination Port
35+
end: textbox
36+
end: elements
37+
end: model
38+

html/js/pyrobox.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,14 @@ function is_object(obj) {
294294
return ret;
295295
}
296296

297-
$.render_select = function(el) {
297+
$.render_select = function(el, input_only) {
298298
var attributes = [];
299299
element_common(el, attributes);
300+
if (input_only) {
301+
return '<select name="'+el.name+'" id="'+el.id+'">\n'+
302+
render_select_options(el) +
303+
'</select><span class="error"></span>\n</div>';
304+
}
300305
return '<div class="field-item">\n'+
301306
'<div class="label"><label for="'+el.id+'">'+el.label+'</label><div class="description">'+el.description+'</div></div>\n'+
302307
'<select name="'+el.name+'" id="'+el.id+'">\n'+
@@ -527,7 +532,15 @@ function is_object(obj) {
527532
if (el.type == 'hidden') continue;
528533
el.id = 'id_row_'+obj.id+'-'+form.name+'-'+el.name;
529534
var field = {type: el.type, name: el.name, id: el.id, value: obj[el.name]};
530-
ret += '<td id="row_'+obj.id+'-'+form.name+'-'+el.name+'">'+eval('$.render_'+el.type+'(field, 1)')+'</td>\n';
535+
if (el.options != null) {
536+
field.options = el.options;
537+
}
538+
ret += '<td id="row_'+obj.id+'-'+form.name+'-'+el.name+'">';
539+
if (typeof el.type == 'string' &&
540+
eval('typeof $.render_' + el.type) == 'function') {
541+
ret += eval('$.render_' + el.type + '(field, 1)');
542+
}
543+
ret += '</td>\n';
531544
}
532545
ret += '<td>';
533546
var actions = ['save', 'cancel'];

html/pages/firewall/forward.html

+16-12
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@
1212

1313
<script type="text/javascript" src="/js/jquery.js"></script>
1414
<script type="text/javascript" src="/js/jquery.combobox.js"></script>
15+
<script type="text/javascript" src="/js/jquery.tablesorter.js"></script>
16+
<script type="text/javascript" src="/js/jquery.form.js"></script>
1517
<script type="text/javascript" src="/js/pyrobox.js"></script>
1618

19+
<script type="text/javascript">
20+
21+
(function($) {$(function() {
22+
23+
$.json_forms_and_tables([], ['firewall_forward']);
24+
$.activate_add_popups();
25+
26+
}); })(jQuery);
27+
</script>
1728

1829
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1930
<link rel="shortcut icon" href="/theme/flames/images/favicon.ico" type="image/x-icon" />
@@ -62,8 +73,6 @@ <h2>Change Status</h2>
6273
<div id="blocks-pre"></div>
6374

6475

65-
66-
6776
<!--
6877
Here we need to render a two-level ul tree
6978
that represents the tabs for this section
@@ -72,16 +81,11 @@ <h2>Change Status</h2>
7281

7382
<div id="content">
7483

75-
76-
77-
78-
79-
80-
<form id="port-forward-form">
81-
</form>
82-
83-
84-
84+
<div id="port-forward-form-popup" class="add-popup">
85+
<h2>Port Forwarding</h2>
86+
<table class="tablesorter" id="port-forward-form">
87+
</table>
88+
</div>
8589

8690

8791
</div>

0 commit comments

Comments
 (0)