├── .gitattributes ├── .gitignore ├── LICENSE.md ├── README.md ├── index.js └── package.json /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directories 27 | node_modules 28 | jspm_packages 29 | 30 | # Optional npm cache directory 31 | .npm 32 | 33 | # Optional REPL history 34 | .node_repl_history 35 | 36 | # ========================= 37 | # Operating System Files 38 | # ========================= 39 | 40 | # OSX 41 | # ========================= 42 | 43 | .DS_Store 44 | .AppleDouble 45 | .LSOverride 46 | 47 | # Thumbnails 48 | ._* 49 | 50 | # Files that might appear in the root of a volume 51 | .DocumentRevisions-V100 52 | .fseventsd 53 | .Spotlight-V100 54 | .TemporaryItems 55 | .Trashes 56 | .VolumeIcon.icns 57 | 58 | # Directories potentially created on remote AFP share 59 | .AppleDB 60 | .AppleDesktop 61 | Network Trash Folder 62 | Temporary Items 63 | .apdisk 64 | 65 | # Windows 66 | # ========================= 67 | 68 | # Windows image file caches 69 | Thumbs.db 70 | ehthumbs.db 71 | 72 | # Folder config file 73 | Desktop.ini 74 | 75 | # Recycle Bin used on file shares 76 | $RECYCLE.BIN/ 77 | 78 | # Windows Installer files 79 | *.cab 80 | *.msi 81 | *.msm 82 | *.msp 83 | 84 | # Windows shortcuts 85 | *.lnk 86 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 - Luiz Guilherme Thess Cardoso 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SyslogServer 2 | 3 | NodeJS Syslog Server. 4 | 5 | ### Quickstart 6 | 7 | ###### Installation 8 | ```shell 9 | $ npm install syslog-server 10 | ``` 11 | 12 | ###### Usage 13 | ```javascript 14 | const SyslogServer = require("syslog-server"); 15 | const server = new SyslogServer(); 16 | 17 | server.on("message", (value) => { 18 | console.log(value.date); // the date/time the message was received 19 | console.log(value.host); // the IP address of the host that sent the message 20 | console.log(value.protocol); // the version of the IP protocol ("IPv4" or "IPv6") 21 | console.log(value.message); // the syslog message 22 | }); 23 | 24 | server.start(); 25 | ``` 26 | 27 | ### Functions 28 | 29 | ###### .start([options], [callback]) 30 | 31 | - **options** - Optional - The options passed to the server. Supports the following properties: 32 | - port [Number] - Optional - Defaults to 514. 33 | - address [String] - Optional - Defaults to "0.0.0.0". 34 | - exclusive [Boolean] - Optional - Defaults to true. 35 | 36 | For more informatio on the options object, check NodeJS oficial [API documentation](https://nodejs.org/api/dgram.html#dgram_socket_bind_options_callback). 37 | 38 | - **callback** [Function] - Optional - Callback function called once the server starts, receives an error object as argument should it fail. 39 | 40 | The start function returns a Promise. 41 | 42 | ###### .stop([callback]) 43 | 44 | - **callback** [Function] - Optional - Callback function called once the server socket is closed, receives an error object as argument should it fail. 45 | 46 | The stop function returns a Promise. 47 | 48 | ###### .isRunning() 49 | 50 | The isRunning function is a synchronous function that returns a boolean value, if the server is ready to receive syslog messages or not. 51 | 52 | ### Events 53 | 54 | - **start** - fired once the server is ready to receive syslog messages 55 | - **stop** - fired once the server is shutdown 56 | - **error** - fired whenever an error occur, an error object is passed to the handler function 57 | - **message** - fired once the server receives a syslog message 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const dgram = require("dgram"); 4 | const EventEmitter = require("events"); 5 | 6 | class SyslogServer extends EventEmitter { 7 | 8 | constructor() { 9 | super(); 10 | this.socket = null; 11 | } 12 | 13 | start(options = { port: 514, address: "0.0.0.0", exclusive: true }, cb) { 14 | return new Promise((resolve, reject) => { 15 | if (this.isRunning()) { 16 | let errorObj = createErrorObject(null, "NodeJS Syslog Server is already running!"); 17 | if (cb) cb(errorObj, this); 18 | reject(errorObj); 19 | } else { 20 | this.socket = dgram.createSocket("udp4"); 21 | 22 | // Socket listening handler 23 | this.socket.on("listening", () => { 24 | this.emit("start", this); 25 | if(cb) cb(null, this); 26 | resolve(this); 27 | }); 28 | 29 | // Socket error handler 30 | this.socket.on("error", (err) => { 31 | this.emit("error", err); 32 | }); 33 | 34 | // Socket message handler 35 | this.socket.on("message", (msg, remote) => { 36 | let message = { 37 | date: new Date(), 38 | host: remote.address, 39 | message: msg.toString("utf8"), 40 | protocol: remote.family 41 | }; 42 | this.emit("message", message); 43 | }); 44 | 45 | // Socket close handler 46 | this.socket.on("close", () => { 47 | this.emit("stop"); 48 | }); 49 | 50 | this.socket.bind(options, (err) => { 51 | if (err) { 52 | let errorObj = createErrorObject(err, "NodeJS Syslog Server failed to start!"); 53 | if (cb) return cb(errorObj, this); 54 | return reject(errorObj); 55 | } 56 | }); 57 | } 58 | }); 59 | } 60 | 61 | stop(cb) { 62 | return new Promise((resolve, reject) => { 63 | try { 64 | this.socket.close(() => { 65 | this.socket = null; 66 | if (cb) return cb(null, this); 67 | return resolve(this); 68 | }); 69 | } catch (err) { 70 | let errorObj = createErrorObject(err, "NodeJS Syslog Server is not running!"); 71 | if (cb) return cb(errorObj, this); 72 | return reject(errorObj); 73 | } 74 | }); 75 | } 76 | 77 | isRunning() { 78 | return (this.socket !== null); 79 | } 80 | } 81 | 82 | function createErrorObject(err, message) { 83 | return { 84 | date: new Date(), 85 | error: err, 86 | message: message 87 | }; 88 | } 89 | 90 | module.exports = SyslogServer; 91 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "syslog-server", 3 | "version": "1.0.1", 4 | "description": "NodeJS Syslog Server", 5 | "main": "index.js", 6 | "author": { 7 | "name": "Luiz Guilherme Thess", 8 | "email": "guithess@gmail.com" 9 | }, 10 | "keywords": [ 11 | "syslog", 12 | "syslog-server" 13 | ], 14 | "homepage": "https://github.com/guithess/syslog-server#readme", 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/guithess/syslog-server.git" 18 | }, 19 | "license": "MIT" 20 | } 21 | --------------------------------------------------------------------------------