Skip to content

Commit 75da2d7

Browse files
committed
Initial commit.
0 parents  commit 75da2d7

File tree

12 files changed

+368
-0
lines changed

12 files changed

+368
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,*.yml}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
/dist

client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('babel/register');
2+
require('./client/SimpleProxy');

client/Controls.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
let ipc = require('ipc');
2+
let React = require('react');
3+
let ReactDOM = require('react-dom');
4+
5+
var Controls = React.createClass({
6+
getInitialState: function() {
7+
return {
8+
port: 8888
9+
};
10+
},
11+
12+
portChanged: function(event) {
13+
this.setState({
14+
port: parseInt(event.target.value)
15+
});
16+
},
17+
18+
render: function() {
19+
if (this.props.proxyStarted) {
20+
return (
21+
<div className="controls">
22+
<p className="controls__input">Proxy running</p>
23+
<button className="controls__button" type="button" onClick={this.props.stopProxy}>Stop Proxy</button>
24+
</div>
25+
);
26+
} else {
27+
return (
28+
<div className="controls">
29+
<input className="controls__input" id="port" type="text" value={this.state.port} onChange={this.portChanged} />
30+
<button className="controls__button" type="button" onClick={() => this.props.startProxy(this.state.port)}>Start Proxy</button>
31+
</div>
32+
);
33+
}
34+
}
35+
});
36+
37+
module.exports = Controls;

client/LocalIP.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let React = require('react');
2+
let ReactDOM = require('react-dom');
3+
4+
var LocalIP = React.createClass({
5+
render: function() {
6+
return (
7+
<h2>{this.props.ip}</h2>
8+
);
9+
}
10+
});
11+
12+
module.exports = LocalIP;

client/SimpleProxy.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
let ipc = require('ipc');
2+
let React = require('react');
3+
let ReactDOM = require('react-dom');
4+
let Controls = require('./Controls');
5+
let LocalIP = require('./LocalIP');
6+
7+
function validPort(port) {
8+
return (port > 1023 && port < 65536)
9+
}
10+
11+
let SimpleProxy = React.createClass({
12+
getInitialState: function() {
13+
return {
14+
ip: '0.0.0.0',
15+
invalidPort: false,
16+
proxyStarted: false
17+
};
18+
},
19+
20+
componentDidMount: function() {
21+
ipc.send('connect');
22+
ipc.on('set-ip', (ip) => this.setIP(ip));
23+
},
24+
25+
setIP: function(ip) {
26+
this.setState({ip: ip});
27+
},
28+
29+
startProxy: function(port) {
30+
if (validPort(port)) {
31+
ipc.send('start-proxy', port);
32+
this.setState({
33+
invalidPort: false,
34+
proxyStarted: true
35+
});
36+
}
37+
else {
38+
this.setState({
39+
invalidPort: true
40+
});
41+
}
42+
},
43+
44+
stopProxy: function() {
45+
ipc.send('stop-proxy');
46+
this.setState({
47+
invalidPort: false,
48+
proxyStarted: false
49+
});
50+
},
51+
52+
render: function() {
53+
return (
54+
<div className="container">
55+
<header className="container__header">
56+
<h1>Simple Proxy</h1>
57+
</header>
58+
<section className="container__body">
59+
<Controls startProxy={this.startProxy} stopProxy={this.stopProxy} proxyStarted={this.state.proxyStarted} />
60+
</section>
61+
<footer className="container__footer">
62+
<LocalIP ip={this.state.ip} />
63+
</footer>
64+
</div>
65+
);
66+
}
67+
});
68+
69+
ReactDOM.render(
70+
<SimpleProxy />,
71+
document.getElementById('mount')
72+
);

index.css

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
html,
2+
body {
3+
padding: 0;
4+
margin: 0;
5+
}
6+
7+
body {
8+
font-family: -apple-system, 'Helvetica Neue', Helvetica, sans-serif;
9+
font-size: 24px;
10+
font-weight: 100;
11+
}
12+
13+
h1 {
14+
font-size: 60px;
15+
font-weight: 100;
16+
margin: 0;
17+
padding: 0;
18+
letter-spacing: 2px;
19+
}
20+
21+
h2 {
22+
font-size: 40px;
23+
font-weight: 100;
24+
margin: 0;
25+
padding: 0;
26+
letter-spacing: 2px;
27+
}
28+
29+
input,
30+
button {
31+
font-family: -apple-system, 'Helvetica Neue', Helvetica, sans-serif;
32+
}
33+
34+
.container {
35+
display: flex;
36+
flex-direction: column;
37+
}
38+
39+
.container__header {
40+
flex: 0 0 auto;
41+
padding: 20px;
42+
}
43+
44+
.container__body {
45+
flex: 1 1 auto;
46+
padding: 20px;
47+
}
48+
49+
.container__footer {
50+
flex: 0 0 auto;
51+
padding: 20px;
52+
}
53+
54+
.controls {
55+
display: flex;
56+
width: 100%;
57+
}
58+
59+
.controls__input {
60+
flex: 20 1 auto;
61+
height: 32px;
62+
font-size: 24px;
63+
}
64+
65+
.controls__button {
66+
flex: 1 0 auto;
67+
height: 38px;
68+
border: none;
69+
background: #3c3c3c;
70+
color: #fff;
71+
}

index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Electron boilerplate</title>
6+
<link rel="stylesheet" href="index.css">
7+
</head>
8+
<body>
9+
<div id="mount"></div>
10+
<script src="client.js"></script>
11+
</body>
12+
</html>

index.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
const app = require('app');
3+
const BrowserWindow = require('browser-window');
4+
5+
// adds debug features like hotkeys for triggering dev tools and reload
6+
require('electron-debug')();
7+
8+
let setup = require('proxy');
9+
let http = require('http');
10+
let ipc = require('ipc');
11+
let dns = require('dns');
12+
13+
let defaultPort = 8888;
14+
let mainWindow;
15+
let server = false;
16+
17+
function onClosed() {
18+
mainWindow = null;
19+
}
20+
21+
function createMainWindow() {
22+
const win = new BrowserWindow({
23+
width: 600,
24+
height: 400
25+
});
26+
27+
win.loadUrl(`file://${__dirname}/index.html`);
28+
win.on('closed', onClosed);
29+
30+
return win;
31+
}
32+
33+
app.on('window-all-closed', () => {
34+
if (server) {
35+
server.close();
36+
}
37+
if (process.platform !== 'darwin') {
38+
app.quit();
39+
}
40+
});
41+
42+
app.on('activate-with-no-open-windows', () => {
43+
if (!mainWindow) {
44+
mainWindow = createMainWindow();
45+
}
46+
});
47+
48+
app.on('ready', () => {
49+
mainWindow = createMainWindow();
50+
mainWindow.openDevTools();
51+
});
52+
53+
ipc.on('connect', function(event) {
54+
dns.lookup(require('os').hostname(), function (err, address, fam) {
55+
event.sender.send('set-ip', address);
56+
});
57+
});
58+
59+
ipc.on('start-proxy', function(event, port) {
60+
if (server) {
61+
server.close();
62+
}
63+
64+
server = setup(http.createServer());
65+
server.listen(port, function() {
66+
var port = server.address().port;
67+
console.log('HTTP(s) proxy server listening on port %d', port);
68+
});
69+
});
70+
71+
ipc.on('stop-proxy', function(event, port) {
72+
if (server) {
73+
server.close(function() {
74+
console.log('proxy stopped')
75+
});
76+
server = false;
77+
}
78+
});

0 commit comments

Comments
 (0)