Skip to content

Commit

Permalink
Separate hostname and displayed name of servers, use punycode hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanbeevers committed Dec 26, 2017
1 parent ce9cd25 commit 1a40140
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"once": "^1.3.2",
"please-upgrade-node": "^3.0.0",
"pug": "^2.0.0-beta11",
"punycode": "^2.1.0",
"respawn": "^2.4.1",
"server-ready": "^0.3.1",
"strip-ansi": "^4.0.0",
Expand Down
23 changes: 14 additions & 9 deletions src/daemon/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const afterAll = require('after-all')
const httpProxy = require('http-proxy')
const serverReady = require('server-ready')
const log = require('./log')
const normalize = require('./normalize')
const tcpProxy = require('./tcp-proxy')
const daemonConf = require('../conf')
const getCmd = require('../get-cmd')
Expand Down Expand Up @@ -61,15 +62,19 @@ class Group extends EventEmitter {
return this._list[id]
}

add(id, conf) {
add(name, conf) {
conf.name = name
const id = normalize(name)

if (conf.target) {
log(`Add target ${id}`)
log(`Add target ${name}`)
this._list[id] = conf

this._change()
return
}

log(`Add server ${id}`)
log(`Add server ${name}`)

const HTTP_PROXY = `http://127.0.0.1:${daemonConf.port}/proxy.pac`

Expand Down Expand Up @@ -97,7 +102,8 @@ class Group extends EventEmitter {

const mon = respawn(command, {
...conf,
maxRestarts: 0
maxRestarts: 0,
name
})

this._list[id] = mon
Expand Down Expand Up @@ -145,7 +151,8 @@ class Group extends EventEmitter {
this._change()
}

remove(id, cb) {
remove(name, cb) {
const id = normalize(name)
const item = this.find(id)
if (item) {
delete this._list[id]
Expand Down Expand Up @@ -204,10 +211,8 @@ class Group extends EventEmitter {
exists(req, res, next) {
// Resolve using either hostname `app.tld`
// or id param `http://localhost:2000/app`
const tld = new RegExp(`.${daemonConf.tld}$`)
const id = req.params.id
? this.resolve(req.params.id)
: this.resolve(req.hostname.replace(tld, ''))
const tld = new RegExp(`\\.${daemonConf.tld}$`)
const id = this.resolve(req.params.id || req.hostname.replace(tld, ''))

// Find item
const item = this.find(id)
Expand Down
12 changes: 12 additions & 0 deletions src/daemon/normalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const punycode = require('punycode') // eslint-disable-line node/no-deprecated-api

module.exports = function normalize(name) {
return name
? punycode
.encode(name)
.toLowerCase()
.replace(/[^a-z0-9-.]/g, '-')
.replace(/-+/g, '-')
.replace(/-$/, '')
: name
}
4 changes: 2 additions & 2 deletions src/front/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<a
:href="href(id)"
:title="title(id)"
target="_blank">{{ id }}</a>
target="_blank">{{ item.name }}</a>
</p>
<p>
<small
Expand Down Expand Up @@ -344,4 +344,4 @@ export default {
}
}
}
</script>
</script>
49 changes: 49 additions & 0 deletions test/daemon/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,55 @@ test('group.resolve should find the correct server or target id', t => {
t.is(group.resolve('baz.foo.app'), 'foo.app')
})

test('group.exists should 404 on missing id', t => {
const group = Group()
const conf = { target: 'http://example.com' }

group.add('app', conf)

const request = { params: { id: 'bar' }, hostname: 'localhost' }

const sendSpy = sinon.stub()
const statusSpy = sinon.stub().returns({ send: sendSpy })
const response = { status: statusSpy }

group.exists(request, response)

t.is(statusSpy.callCount, 1)
t.is(statusSpy.lastCall.args[0], 404)
t.is(sendSpy.callCount, 1)
})

test.cb('group.exists should find group by param', t => {
const group = Group()
const conf = { target: 'http://example.com' }

group.add('App Foo', conf)

const request = { params: { id: 'app-foo' }, hostname: 'localhost' }
const response = {}

group.exists(request, response, () => {
t.is(request.hotel.id, 'app-foo')
t.end()
})
})

test.cb('group.exists should find group by hostname', t => {
const group = Group()
const conf = { target: 'http://example.com' }

group.add('bar-baz-∫', conf)

const request = { params: {}, hostname: 'bar-baz-tl7d.dev' }
const response = {}

group.exists(request, response, () => {
t.is(request.hotel.id, 'bar-baz-tl7d')
t.end()
})
})

test('group.handleUpgrade with proxy', t => {
const group = Group()
const target = 'example.com'
Expand Down

0 comments on commit 1a40140

Please sign in to comment.