-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>r2ce ws</title> | ||
<meta content="width=device-width" name="viewport"> | ||
<script src="./ws.js" type="text/javascript"></script> | ||
</head> | ||
|
||
<body> | ||
r2ce ws | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const version = 'v0.9'; | ||
// | ||
|
||
let _socket, _isConnected = false; | ||
|
||
const _publishData = (data) => { | ||
if (!_isConnected) { | ||
return; | ||
} | ||
_socket.send(data); | ||
} | ||
|
||
const init = () => { | ||
try { | ||
_socket = new WebSocket(`ws://${location.hostname}:${location.port}/`, ['r2ce']); | ||
|
||
_socket.onopen = function (a) { | ||
console.log('ws connect'); | ||
_isConnected = true; | ||
_publishData(`hello server`); | ||
}; | ||
|
||
_socket.onerror = function (err) { | ||
_isConnected = false; | ||
console.log('ws error ', err); | ||
}; | ||
|
||
_socket.onmessage = function (e) { | ||
console.log('ws message: ', e.data); | ||
}; | ||
|
||
_socket.onclose = function () { | ||
_isConnected = false; | ||
console.log('ws close'); | ||
}; | ||
} catch (err) { | ||
console.err(err); | ||
} | ||
|
||
console.log('init done'); | ||
}; | ||
|
||
window.onload = () => init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const { v4: uuidv4 } = require('uuid'); | ||
const express = require('express'); | ||
const ws = require('ws'); | ||
const path = require('path'); | ||
|
||
const _port = 6022; | ||
const _app = express(); | ||
|
||
const _activePulseList = {}; | ||
const _pendingCommands = {}; | ||
|
||
_app.use('/static', express.static(path.join(__dirname, 'public'))) | ||
_app.use(express.json()); | ||
|
||
_app.use((req, res, next) => { | ||
console.log(`@(${new Date().toISOString()}) | [${req.ip}] ${req.method} ${req.url}`); | ||
next(); | ||
}); | ||
|
||
_app.get('/', (req, res) => { | ||
res.send('r2ce-server'); | ||
}); | ||
|
||
_app.get('/command/pending', (req, res) => { | ||
res.send(_pendingCommands); | ||
}); | ||
|
||
_app.get('/command/:hostname/:cmd', (req, res) => { | ||
if (_activePulseList[req.params.hostname]) { | ||
_pendingCommands[req.params.hostname] = { command: req.params.cmd, id: uuidv4() }; | ||
res.sendStatus(201); | ||
} | ||
else { | ||
res.sendStatus(406); | ||
} | ||
}); | ||
|
||
_app.get('/pulse/list', (req, res) => { | ||
res.send(_activePulseList); | ||
}); | ||
|
||
_app.post('/pulse', (req, res) => { | ||
console.log(`>> incoming pulse from ${req.body.COMPUTERNAME} @ ${req.body.HOMEPATH}`); | ||
|
||
_activePulseList[req.body.COMPUTERNAME] = { | ||
env: req.body, | ||
expires: Date.now() + 60 * 1000 | ||
}; | ||
|
||
const pendingCommand = _pendingCommands[req.body.COMPUTERNAME]; | ||
if (pendingCommand) { | ||
delete _pendingCommands[req.body.COMPUTERNAME]; | ||
res.status(201).header('cid', pendingCommand.id).send(pendingCommand.command); | ||
} | ||
else { | ||
res.sendStatus(204); | ||
} | ||
}); | ||
|
||
const server = _app.listen(_port, () => { | ||
console.log(`r2ce-server @ http://localhost:${_port}`) | ||
}); | ||
|
||
const wss = new ws.Server({ noServer: true }); | ||
|
||
wss.on('connection', (socket, request) => { | ||
socket.id = uuidv4(); | ||
|
||
console.log(`+ wss.connection [${socket.id}]`); | ||
|
||
socket.send(`establish-uid=${socket.id}`); | ||
|
||
socket.on('message', (data) => { | ||
console.log(`+ socket.message [${socket.id}]: ${data}`); | ||
}); | ||
|
||
socket.on('data', (data) => { | ||
console.log(`+ socket.data [${socket.id}]: ${data}`); | ||
}); | ||
|
||
socket.on('disconnect', () => { | ||
console.log(`+ socket.disconnected [${socket.id}]`); | ||
}); | ||
}); | ||
|
||
wss.on('close', (arg) => { | ||
console.log(`+ wss.close ${arg}}`); | ||
}); | ||
|
||
server.on('upgrade', (request, socket, head) => { | ||
console.log(`server.upgrade`); | ||
wss.handleUpgrade(request, socket, head, socket => { | ||
wss.emit('connection', socket, request); | ||
}); | ||
}); |