mitmproxy/libmproxy/web/static/js/app.js

420 lines
12 KiB
JavaScript
Raw Normal View History

2014-09-15 16:08:26 +00:00
const PayloadSources = {
VIEW_ACTION: "VIEW_ACTION",
SERVER_ACTION: "SERVER_ACTION"
};
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
function Dispatcher() {"use strict";
this.callbacks = [];
}
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
Dispatcher.prototype.register=function(callback){"use strict";
this.callbacks.push(callback);
};
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
Dispatcher.prototype.unregister=function(callback){"use strict";
var index = this.callbacks.indexOf(f);
if (index >= 0) {
this.callbacks.splice(this.callbacks.indexOf(f), 1);
2014-09-14 00:44:13 +00:00
}
2014-09-15 16:08:26 +00:00
};
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
Dispatcher.prototype.dispatch=function(payload){"use strict";
console.debug("dispatch", payload);
this.callbacks.forEach(function(callback) {
callback(payload);
});
};
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
AppDispatcher = new Dispatcher();
AppDispatcher.dispatchViewAction = function(action){
action.actionSource = PayloadSources.VIEW_ACTION;
this.dispatch(action);
};
var ActionTypes = {
SETTINGS_UPDATE: "SETTINGS_UPDATE",
LOG_ADD: "LOG_ADD"
};
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
var SettingsActions = {
update:function(settings) {
settings = _.merge({}, SettingsStore.getSettings(), settings);
AppDispatcher.dispatchViewAction({
actionType: ActionTypes.SETTINGS_UPDATE,
settings: settings
});
}
};
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
function EventEmitter() {"use strict";
this.listeners = {};
}
EventEmitter.prototype.emit=function(event) {"use strict";
if (!(event in this.listeners)) {
return;
}
this.listeners[event].forEach(function(listener) {
listener(event, this);
}.bind(this));
};
EventEmitter.prototype.addListener=function(event, f) {"use strict";
this.listeners[event] = this.listeners[event] || [];
this.listeners[event].push(f);
};
EventEmitter.prototype.removeListener=function(event, f) {"use strict";
if (!(event in this.listeners)) {
return false;
}
var index = this.listeners[event].indexOf(f);
if (index >= 0) {
this.listeners[event].splice(index, 1);
}
};
for(var EventEmitter____Key in EventEmitter){if(EventEmitter.hasOwnProperty(EventEmitter____Key)){_SettingsStore[EventEmitter____Key]=EventEmitter[EventEmitter____Key];}}var ____SuperProtoOfEventEmitter=EventEmitter===null?null:EventEmitter.prototype;_SettingsStore.prototype=Object.create(____SuperProtoOfEventEmitter);_SettingsStore.prototype.constructor=_SettingsStore;_SettingsStore.__superConstructor__=EventEmitter;
function _SettingsStore() {"use strict";
/*jshint validthis: true */
EventEmitter.call(this);
this.settings = { version: "0.12", showEventLog: true }; //FIXME: Need to get that from somewhere.
}
_SettingsStore.prototype.getSettings=function() {"use strict";
return this.settings;
};
_SettingsStore.prototype.handle=function(action) {"use strict";
switch (action.actionType) {
case ActionTypes.SETTINGS_UPDATE:
this.settings = action.settings;
this.emit("change");
break;
default:
return;
}
};
var SettingsStore = new _SettingsStore();
AppDispatcher.register(SettingsStore.handle.bind(SettingsStore));
var SettingsMixin = {
getInitialState:function(){
return {
settings: SettingsStore.getSettings()
};
},
componentDidMount:function(){
SettingsStore.addListener("change", this._onSettingsChange);
},
componentWillUnmount:function(){
SettingsStore.removeListener("change", this._onSettingsChange);
},
_onSettingsChange:function(){
this.setState({
settings: SettingsStore.getSettings()
});
}
};
for(var EventEmitter____Key in EventEmitter){if(EventEmitter.hasOwnProperty(EventEmitter____Key)){_EventLogStore[EventEmitter____Key]=EventEmitter[EventEmitter____Key];}}var ____SuperProtoOfEventEmitter=EventEmitter===null?null:EventEmitter.prototype;_EventLogStore.prototype=Object.create(____SuperProtoOfEventEmitter);_EventLogStore.prototype.constructor=_EventLogStore;_EventLogStore.__superConstructor__=EventEmitter;
function _EventLogStore() {"use strict";
/*jshint validthis: true */
EventEmitter.call(this);
this.log = [];
}
_EventLogStore.prototype.getAll=function() {"use strict";
return this.log;
};
_EventLogStore.prototype.handle=function(action) {"use strict";
switch (action.actionType) {
case ActionTypes.LOG_ADD:
this.log.push(action.message);
this.emit("change");
break;
default:
return;
}
};
var EventLogStore = new _EventLogStore();
AppDispatcher.register(EventLogStore.handle.bind(EventLogStore));
var EventLogMixin = {
getInitialState:function(){
return {
log: EventLog.getAll()
};
},
componentDidMount:function(){
SettingsStore.addListener("change", this._onEventLogChange);
},
componentWillUnmount:function(){
SettingsStore.removeListener("change", this._onEventLogChange);
},
_onEventLogChange:function(){
this.setState({
log: EventLog.getAll()
});
}
};
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
function Connection(root){"use strict";
if(!root){
root = location.origin + "/api/v1";
}
this.root = root;
this.openWebSocketConnection();
2014-09-14 00:44:13 +00:00
}
2014-09-15 16:08:26 +00:00
Connection.prototype.openWebSocketConnection=function(){"use strict";
this.ws = new WebSocket(this.root.replace("http","ws") + "/ws");
var ws = this.ws;
ws.onopen = this.onopen.bind(this);
ws.onmessage = this.onmessage.bind(this);
ws.onerror = this.onerror.bind(this);
ws.onclose = this.onclose.bind(this);
};
Connection.prototype.onopen=function(open){"use strict";
console.log("onopen", this, arguments);
};
Connection.prototype.onmessage=function(message){"use strict";
console.log("onmessage", this, arguments);
};
Connection.prototype.onerror=function(error){"use strict";
console.log("onerror", this, arguments);
};
Connection.prototype.onclose=function(close){"use strict";
console.log("onclose", this, arguments);
2014-09-14 00:44:13 +00:00
};
2014-09-15 16:08:26 +00:00
function Connection(root){"use strict";
if(!root){
root = location.origin + "/api/v1";
}
this.root = root;
this.openWebSocketConnection();
2014-09-14 00:44:13 +00:00
}
2014-09-15 16:08:26 +00:00
Connection.prototype.openWebSocketConnection=function(){"use strict";
this.ws = new WebSocket(this.root.replace("http","ws") + "/ws");
var ws = this.ws;
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
ws.onopen = this.onopen.bind(this);
ws.onmessage = this.onmessage.bind(this);
ws.onerror = this.onerror.bind(this);
ws.onclose = this.onclose.bind(this);
2014-09-14 00:44:13 +00:00
};
2014-09-15 16:08:26 +00:00
Connection.prototype.onopen=function(open){"use strict";
console.log("onopen", this, arguments);
2014-09-14 00:44:13 +00:00
};
2014-09-15 16:08:26 +00:00
Connection.prototype.onmessage=function(message){"use strict";
console.log("onmessage", this, arguments);
2014-09-14 00:44:13 +00:00
};
2014-09-15 16:08:26 +00:00
Connection.prototype.onerror=function(error){"use strict";
console.log("onerror", this, arguments);
};
Connection.prototype.onclose=function(close){"use strict";
console.log("onclose", this, arguments);
2014-09-14 00:44:13 +00:00
};
2014-09-15 16:08:26 +00:00
2014-09-14 00:44:13 +00:00
/** @jsx React.DOM */
var MainMenu = React.createClass({displayName: 'MainMenu',
2014-09-15 16:08:26 +00:00
mixins: [SettingsMixin],
handleSettingsChange:function() {
SettingsActions.update({
showEventLog: this.refs.showEventLogInput.getDOMNode().checked
});
},
render:function(){
return React.DOM.div(null,
React.DOM.label(null,
React.DOM.input({type: "checkbox", ref: "showEventLogInput", checked: this.state.settings.showEventLog, onChange: this.handleSettingsChange}),
"Show Event Log"
)
);
2014-09-14 00:44:13 +00:00
}
});
var ToolsMenu = React.createClass({displayName: 'ToolsMenu',
2014-09-15 16:08:26 +00:00
render:function(){
2014-09-14 00:44:13 +00:00
return (React.DOM.div(null, "Tools Menu"));
}
});
var ReportsMenu = React.createClass({displayName: 'ReportsMenu',
2014-09-15 16:08:26 +00:00
render:function(){
2014-09-14 00:44:13 +00:00
return (React.DOM.div(null, "Reports Menu"));
}
});
2014-09-15 16:08:26 +00:00
2014-09-14 00:44:13 +00:00
var _Header_Entries = {
main: {
title: "Traffic",
route: "main",
menu: MainMenu
},
tools: {
title: "Tools",
route: "main",
menu: ToolsMenu
},
reports: {
title: "Visualization",
route: "reports",
menu: ReportsMenu
}
};
var Header = React.createClass({displayName: 'Header',
2014-09-15 16:08:26 +00:00
mixins: [SettingsMixin],
getInitialState:function(){
return {
active: "main"
};
2014-09-14 00:44:13 +00:00
},
2014-09-15 16:08:26 +00:00
handleClick:function(active){
2014-09-14 00:44:13 +00:00
this.setState({active: active});
ReactRouter.transitionTo(_Header_Entries[active].route);
return false;
},
2014-09-15 16:08:26 +00:00
handleFileClick:function(){
2014-09-14 00:44:13 +00:00
console.log("File click");
},
2014-09-15 16:08:26 +00:00
render:function(){
2014-09-14 00:44:13 +00:00
var header = [];
for(var item in _Header_Entries){
var classes = this.state.active == item ? "active" : "";
header.push(React.DOM.a({key: item, href: "#", className: classes,
2014-09-15 16:08:26 +00:00
onClick: this.handleClick.bind(this, item)}, _Header_Entries[item].title));
2014-09-14 00:44:13 +00:00
}
var menu = _Header_Entries[this.state.active].menu();
return (
2014-09-15 16:08:26 +00:00
React.DOM.header(null,
React.DOM.div({className: "title-bar"},
"mitmproxy ", this.state.settings.version
),
React.DOM.nav(null,
React.DOM.a({href: "#", className: "special", onClick: this.handleFileClick}, " File "),
header
),
React.DOM.div({className: "menu"},
menu
)
));
2014-09-14 00:44:13 +00:00
}
});
/** @jsx React.DOM */
var TrafficTable = React.createClass({displayName: 'TrafficTable',
2014-09-15 16:08:26 +00:00
/*getInitialState: function(){
2014-09-14 00:44:13 +00:00
return {
flows: []
};
2014-09-15 16:08:26 +00:00
},*/
2014-09-14 00:44:13 +00:00
componentDidMount: function () {
2014-09-15 16:08:26 +00:00
/*var flowStore = new DummyFlowStore([]);
2014-09-14 00:44:13 +00:00
this.setState({flowStore: flowStore});
flowStore.addChangeListener(this.onFlowsChange);
$.getJSON("/flows.json").success(function (flows) {
flows.forEach(function (flow, i) {
window.setTimeout(function () {
flowStore.addFlow(flow);
}, _.random(i*400,i*400+1000));
});
2014-09-15 16:08:26 +00:00
}.bind(this));*/
2014-09-14 00:44:13 +00:00
},
componentWillUnmount: function(){
2014-09-15 16:08:26 +00:00
//this.state.flowStore.close();
2014-09-14 00:44:13 +00:00
},
onFlowsChange: function(event, flows){
2014-09-15 16:08:26 +00:00
//this.setState({flows: flows.getAll()});
2014-09-14 00:44:13 +00:00
},
render: function () {
2014-09-15 16:08:26 +00:00
/*var flows = this.state.flows.map(function(flow){
return <div>{flow.request.method} {flow.request.scheme}://{flow.request.host}{flow.request.path}</div>;
}); *//**/
x = "WTF";
i = 12;
while(i--) x += x;
return React.DOM.div(null, React.DOM.pre(null, x));
2014-09-14 00:44:13 +00:00
}
});
2014-09-15 16:08:26 +00:00
/** @jsx React.DOM */
2014-09-14 00:44:13 +00:00
2014-09-15 16:08:26 +00:00
var EventLog = React.createClass({displayName: 'EventLog',
render:function(){
return (
React.DOM.div({className: "eventlog"},
React.DOM.pre(null,
"much log."
)
)
);
}
});
/** @jsx React.DOM */
var Footer = React.createClass({displayName: 'Footer',
render:function(){
return (
React.DOM.footer(null,
React.DOM.span({className: "label label-success"}, "transparent mode")
)
);
}
});
/** @jsx React.DOM */
//TODO: Move out of here, just a stub.
2014-09-14 00:44:13 +00:00
var Reports = React.createClass({displayName: 'Reports',
2014-09-15 16:08:26 +00:00
render:function(){
2014-09-14 00:44:13 +00:00
return (React.DOM.div(null, "Report Editor"));
}
});
2014-09-15 16:08:26 +00:00
var ProxyAppMain = React.createClass({displayName: 'ProxyAppMain',
mixins: [SettingsMixin],
render:function() {
return (
React.DOM.div({id: "container"},
Header(null),
React.DOM.div({id: "main"}, this.props.activeRouteHandler(null)),
this.state.settings.showEventLog ? EventLog(null) : null,
Footer(null)
)
);
}
});
var ProxyApp = (
2014-09-14 00:44:13 +00:00
ReactRouter.Routes({location: "hash"},
2014-09-15 16:08:26 +00:00
ReactRouter.Route({name: "app", path: "/", handler: ProxyAppMain},
2014-09-14 00:44:13 +00:00
ReactRouter.Route({name: "main", handler: TrafficTable}),
ReactRouter.Route({name: "reports", handler: Reports}),
ReactRouter.Redirect({to: "main"})
)
)
);
2014-09-15 16:08:26 +00:00
$(function(){
app = React.renderComponent(ProxyApp, document.body);
});
2014-09-14 00:44:13 +00:00
//# sourceMappingURL=app.js.map