Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit ff47941

Browse files
committed
Bridge Backend :: allow custom host in config
1 parent 5e15292 commit ff47941

File tree

6 files changed

+60
-35
lines changed

6 files changed

+60
-35
lines changed

README.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
##### Listen to the outside world 🌍 from your bedroom 🛌 — 📡 HTTP request forwarding with no hassle
44

5-
Hermes is a dev tool designed to help teams to collaborate in the use of third party services that emit HTTP requests and need to be forwarded to the local machines.
5+
Hermes is a dev tool designed to help teams to collaborate in the use of third party services that emit HTTP requests and need to be forwarded to your team's local machines. It provides a multi-users alternative to [ngrok](https://github.com/nclsndr/hermes#why-not-use-ngrok) or localtunnel.
66

77
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
88

99
## Motivation
1010

11-
You have an online service that need to hit your local server (e.g. webhooks). Basically, your computer is connected to your personal network (behind your NAT), so your localhost is invisible from the outside world...
11+
You are working on your local machine on a piece of code that need to listen from an online service — providing HTTP requests (e.g. webhooks, chatBot service...). Basically, your computer is connected to your personal network (behind your NAT), so your localhost is invisible from the outside world...
1212

13-
Then Hermes comes and provides two modules:
13+
The solution is well known: you need a HTTP/SSH tunneling system that forwards the requests to your computer. It's perfect when you're alone! **But now, you are working as a team** and each developer is running his own instance of the code locally and he would like to receive the payloads also. By design tunneling systems are limited to communicate with one user at a time.
1414

15-
- The *bridge* is running on your remote server and listen to any *provider* requests
16-
- The *adaptor* is running on your localhost and replicate all the requests to your *local server*
15+
**Here comes Hermes** and provides two modules:
16+
17+
- The *bridge* is running on any of your servers online and listen to any *provider* requests
18+
- The *adaptor* is running on each local machine and replicate all the requests to the *local server*
1719

1820
When the response is emitted from the *local server*, it brings the response back to the *provider* following the same logic.
1921

@@ -26,19 +28,19 @@ This is the most basic case. It applies for a single developer working on his ma
2628

2729
![](https://github.com/nclsndr/hermes/blob/master/docs/assets/basic.jpg?raw=true)
2830

29-
### Concurrency
31+
### Concurrent
3032

3133
Concurrency mode allows **concurrent calls among clients** (aka Adaptors). By default, the first response received from any client (*adaptor*) will be used as the final response for the *provider*. This feature is particularly useful for teams of developers working in parallel when the provider do not require a client-specific response.
3234

3335
![](https://github.com/nclsndr/hermes/blob/master/docs/assets/concurrent.jpg?raw=true)
3436

35-
#### Exclusive
37+
### Exclusive
3638

3739
Exclusive mode permits to temporally **choose a single adaptor** as responsible for building the response sent to the *provider*.
3840

3941
![](https://github.com/nclsndr/hermes/blob/master/docs/assets/exclusive.jpg?raw=true)
4042

41-
## Setup
43+
# Setup
4244

4345
### Requirements
4446

@@ -47,7 +49,7 @@ Exclusive mode permits to temporally **choose a single adaptor** as responsible
4749
---
4850
### 🌍 Bridge: Server side
4951

50-
You need to procure any kind of server able to allow specific port access (different from 80 and 443) — you'll need 3 ports.
52+
You need to procure any kind of server able to allow specific port access (different from 80 and 443) — you'll need 3 available ports.
5153

5254
> All the code snippets provided below are gathered into this [example directory](https://github.com/nclsndr/hermes/tree/master/examples/basic-bridge)
5355
@@ -80,21 +82,25 @@ const responseFallback = {
8082
}
8183

8284
createBridgeServer({
83-
httpPort: 8000, // make sure you can access from outside of your machine
85+
httpHost: 'localhost', // default '127.0.0.1'
86+
httpPort: 8000, // port on which the Bridge will listen
87+
socketHost: 'localhost', // default httpHost || '127.0.0.1'
8488
socketPort: 9000, // this port will be reused for the adaptor configuration
85-
loggerLevel: 'verbose', // 'info'
8689
dashboard: { // for you to control your adaptors in real time
90+
host: 'localhost', // default '127.0.0.1'
8791
port: 8001, // port to access the dashboard
8892
adminAuth: { // credentials to connect onto the dashboard
8993
username: 'admin',
90-
password: 'admin', // should be placed into .env
91-
jwtSecret: 'jiReKLKbTVA2qnjHun8ma2hgDcApuZ' // should be placed into .env also!
94+
password: dotEnv.parsed.ADMIN_AUTH_PASSWORD, // Change it into your .env file!
95+
jwtSecret: dotEnv.parsed.ADMIN_AUTH_JWT_SECRET // Change it also into your .env file!
9296
}
9397
},
98+
loggerLevel: 'verbose', // 'info',
9499
defaultResponse: responseFallback // by default, hermes provide the fallback described above
95100
})
96101
```
97102

103+
**Notes:** If you plan to not use any internal routing system (Apache, Nginx...), you should set `httpHost`, `socketHost`, `dashboard.host` to `'0.0.0.0'` to expose your Bridge and Dashboard to the world.
98104

99105
#### 4. 🚀 Launch it!
100106

@@ -110,7 +116,7 @@ $ node index.js
110116
- DNS resolution for the socket endpoint is possible but not as easy as it seems. We recommend to use the IP for configuring the adaptor.
111117
- [Heroku](https://www.heroku.com/) users, as far as the service doesn't allow multi-port exposure, hermes-bridge will not be compatible.
112118

113-
#### 5. 🖥 Dashboard: register your Adaptor(s)
119+
#### 5. Dashboard: register your Adaptor(s)
114120

115121
Navigate to the URL (or IP:port) you set up for your Hermes *dashboard*. You should have something like:
116122

@@ -125,7 +131,7 @@ You can now add a new *adaptor* with the auth token of your choice — you will
125131

126132
---
127133

128-
### 🛌 Adaptor: Local dev env
134+
### 🛌 Adaptor: Your (local) development environment
129135
The adaptor can be set in any dev environment that support NodeJS 8+.
130136

131137
> All the information provided here are gathered into this [example directory](https://github.com/nclsndr/hermes/tree/master/examples/basic-adaptor)
@@ -145,8 +151,8 @@ Create a `hermes.js` file.
145151
const adaptor = require('hermes-adaptor')
146152

147153
adaptor.init({
148-
bridgeHost: 'my-hermes-bridge-domain.com',
149-
bridgeSocketPort: 9000,
154+
bridgeHost: 'YOUR_SERVER_IP', // if set to 0.0.0.0 in the Bridge config
155+
bridgeSocketPort: 9000, // the port you chose previously
150156
localServerProtocol: 'http',
151157
localServerHost: 'localhost',
152158
localServerPort: 8888,
@@ -170,7 +176,7 @@ You need to launch your local server too if you want the all system to do his jo
170176
**Notes**
171177

172178
- We recommend to use [concurrently](https://www.npmjs.com/package/concurrently) in order to start your dev-server in parallel with the adaptor
173-
- For setup testing, you can try a `curl http://my-bridge.com`, then you should receive the request on your adaptor console (verbose = true).
179+
- For testing your setup, you can try a `curl http://YOUR_SERVER_IP:YOUR_HTTP_PORT`, then you should receive the request on your adaptor console (verbose = true).
174180

175181
Here you should have something like:
176182

examples/basic-adaptor/hermes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
const createAdaptor = require('hermes-adaptor')
55

66
createAdaptor({
7-
bridgeHost: 'my-hermes-bridge-domain.com', // NB without http://
8-
bridgeSocketPort: 9000,
7+
bridgeHost: 'YOUR_SERVER_IP', // NB without http:// | if set to 0.0.0.0 in the Bridge config
8+
bridgeSocketPort: 9000, // the port you chose previously
99
localServerProtocol: 'http',
1010
localServerHost: 'localhost',
1111
localServerPort: 8888,

examples/basic-bridge/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@ const responseFallback = {
1919
}
2020

2121
createBridgeServer({
22-
httpPort: 8000, // make sure you can access from outside of your machine
22+
httpHost: 'localhost', // default '127.0.0.1' (set to 0.0.0.0 to access from the outside by default)
23+
httpPort: 8000, // port on which the Bridge will listen
24+
socketHost: 'localhost', // default httpHost || '127.0.0.1' (set to 0.0.0.0 to access from the outside by default)
2325
socketPort: 9000, // this port will be reused for the adaptor configuration
24-
loggerLevel: 'verbose', // 'info'
2526
dashboard: { // for you to control your adaptors in real time
27+
host: 'localhost', // default '127.0.0.1' (set to 0.0.0.0 to access from the outside by default)
2628
port: 8001, // port to access the dashboard
2729
adminAuth: { // credentials to connect onto the dashboard
2830
username: 'admin',
2931
password: dotEnv.parsed.ADMIN_AUTH_PASSWORD, // Change it into your .env file!
3032
jwtSecret: dotEnv.parsed.ADMIN_AUTH_JWT_SECRET // Change it also into your .env file!
3133
}
3234
},
35+
loggerLevel: 'verbose', // 'info'
3336
defaultResponse: responseFallback // by default, hermes provide the fallback described above
3437
})

packages/hermes-backend/lib/server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const createServer = (initialConfig = {}) => {
3232

3333
registerGlobals(['JWT_SECRET', config.adminAuth.jwtSecret])
3434

35-
const { port } = config
35+
const { host, port } = config
3636

3737
const app = express()
3838
const server = http.Server(app)
@@ -42,9 +42,9 @@ const createServer = (initialConfig = {}) => {
4242

4343
app.use(createDashboardRouter())
4444

45-
server.listen(port, () => {
45+
server.listen({ host, port }, () => {
4646
logger.line('info', 'cyan')
47-
logger.info(`Backend server running on port: ${port}`.cyan)
47+
logger.info(` Dashboard server running on port: ${host}:${port}`.cyan)
4848
logger.line('info', 'cyan')
4949
})
5050
return {

packages/hermes-backend/lib/utils/validateConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const validateConfig = config => {
1717
}
1818
return {
1919
...config,
20+
host: config.host || '127.0.0.1',
2021
port: config.port || 3000
2122
}
2223
}

packages/hermes-bridge/lib/index.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const responseFallback = {
1717
}
1818

1919
const createBridgeServer = ({
20+
httpHost,
2021
httpPort,
22+
socketHost,
2123
socketPort,
2224
loggerLevel = 'verbose',
2325
clients,
@@ -37,18 +39,31 @@ const createBridgeServer = ({
3739
const manager = new ClientsManager(defaultResponse)
3840

3941
const httpServer = http.createServer(manager.createProviderRequestHandler.bind(manager))
40-
httpServer.listen(httpPort, () => {
41-
logger.line('verbose', 'cyan')
42-
logger.verbose(`Bridge HTTP server running on ${httpPort}`.cyan)
43-
logger.line('verbose', 'cyan')
44-
})
42+
httpServer.listen(
43+
{
44+
host: httpHost || '127.0.0.1',
45+
port: httpPort
46+
},
47+
() => {
48+
logger.line('verbose', 'cyan')
49+
logger.verbose(` Bridge HTTP server running on ${httpHost || '127.0.0.1'}:${httpPort}`.cyan)
50+
logger.line('verbose', 'cyan')
51+
}
52+
)
4553

4654
const socketServer = net.createServer(manager.addAdaptor.bind(manager))
47-
socketServer.listen(socketPort, () => {
48-
logger.line('verbose', 'cyan')
49-
logger.verbose(`Bridge Socket server running on ${socketPort}`.cyan)
50-
logger.line('verbose', 'cyan')
51-
})
55+
socketServer.listen(
56+
{
57+
host: socketHost || httpHost || '127.0.0.1',
58+
port: socketPort
59+
},
60+
() => {
61+
logger.line('verbose', 'cyan')
62+
logger.verbose(
63+
` Bridge Socket server running on ${httpHost || '127.0.0.1'}:${socketPort}`.cyan)
64+
logger.line('verbose', 'cyan')
65+
}
66+
)
5267
}
5368

5469
module.exports = createBridgeServer

0 commit comments

Comments
 (0)