Git Product home page Git Product logo

tcp-bind's Introduction

tcp-bind

allocate a file descriptor to listen on a port later

This is handy if you want to run a server on a low port but want to drop privileges as soon as possible.

build status

example

Here we allocate a file descriptor for tcp port 80 and then drop permissions right away:

var http = require('http');
var alloc = require('tcp-bind');
var fd = alloc(80);

process.setgid(process.argv[3]);
process.setuid(process.argv[2]);

var server = http.createServer(function (req, res) {
    res.write('uid=' + process.getuid() + '\n');
    res.end('gid=' + process.getgid() + '\n');
});
server.listen({ fd: fd });

Now we're listening on port 80, but the rest is running under a regular user:

$ sudo node drop.js substack cdrom &
[1] 2855
$ curl localhost
uid=1000
gid=24

methods

var alloc = require('tcp-bind')

var fd = alloc(addr='0.0.0.0', port)

Allocate a file descriptor to listen on port at an ipv4 or ipv6 string address addr.

You can create a tcp server from the file descriptor fd later by passing it to the server's .listen() method:

server.listen({ fd: fd })

This method throws when the port is already bound and when the current user doesn't have permission to open the port requested.

install

With npm do:

npm install tcp-bind

license

MIT

tcp-bind's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.