# @ddn/server

> A Data Distribution Node server based on DDN.

Latest version **0.0.2** (published 2018-12-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ddn/server
pnpm add @ddn/server
yarn add @ddn/server
bun add @ddn/server
```

Provides the command `ddn-server`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2018-12-26 |
| First published | 2018-12-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 30 |
| Unpacked size | 85 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | DDN |
| Maintainers | ebookchain, evanlai, imfly |
| Keywords | ddn, dat, server |

## Links

- npm: https://www.npmjs.com/package/@ddn/server
- Repository: https://github.com/ddnlink/ddn-server
- Homepage: https://github.com/ddnlink/ddn-server#readme
- Issues: https://github.com/ddnlink/ddn-server/issues
- npm.io page: https://npm.io/package/@ddn/server

## Dependencies (30)

- [ms](https://npm.io/package/ms.md) ^2.1.1
- [mime](https://npm.io/package/mime.md) ^2.3.1
- [chalk](https://npm.io/package/chalk.md) ^2.4.1
- [vhost](https://npm.io/package/vhost.md) ^3.0.2
- [yargs](https://npm.io/package/yargs.md) ^11.1.0
- [colors](https://npm.io/package/colors.md) ^1.3.3
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [express](https://npm.io/package/express.md) ^4.16.3
- [figures](https://npm.io/package/figures.md) ^2.0.0
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.12.0
- [dat-node](https://npm.io/package/dat-node.md) ^3.5.9
- [strftime](https://npm.io/package/strftime.md) ^0.10.0
- [through2](https://npm.io/package/through2.md) ^2.0.3
- [greenlock](https://npm.io/package/greenlock.md) ^2.3.11
- [scoped-fs](https://npm.io/package/scoped-fs.md) ^1.1.3
- [untildify](https://npm.io/package/untildify.md) ^3.0.3
- [http-proxy](https://npm.io/package/http-proxy.md) ^1.17.0
- [compression](https://npm.io/package/compression.md) ^1.7.3
- [is-http-url](https://npm.io/package/is-http-url.md) ^2.0.0
- [lodash.pick](https://npm.io/package/lodash.pick.md) ^4.4.0
- [prom-client](https://npm.io/package/prom-client.md) ^11.0.0
- [range-parser](https://npm.io/package/range-parser.md) ^1.2.0
- [pauls-dat-api](https://npm.io/package/pauls-dat-api.md) ^8.0.0
- [response-time](https://npm.io/package/response-time.md) ^2.3.2
- [is-domain-name](https://npm.io/package/is-domain-name.md) ^1.0.1
- [lodash.flatten](https://npm.io/package/lodash.flatten.md) ^4.4.0
- [redirect-https](https://npm.io/package/redirect-https.md) ^1.1.6
- [http-body-parser](https://npm.io/package/http-body-parser.md) ^1.1.9
- [le-store-certbot](https://npm.io/package/le-store-certbot.md) ^2.1.3
- [identify-filetype](https://npm.io/package/identify-filetype.md) ^1.0.0

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.0.2 (latest) — 2018-12-26
- 0.0.1 — 2018-12-24

## README

# DDN Server

`ddn-server` is a Data Distribution Node (DDN) server based on DDN with the [Dat protocol](https://datprotocol.com).

`ddn-server` is for you if:

- You're comfortable with some server administration (or want to learn!)
- You want to keep your dat:// website/s online
- You want to publish your dat:// website/s to a domain name

## Table of contents

- [Features](#features)
- [Install](#install)
- [Running ddn-server](#running-ddn-server)
- [Examples](#examples)
- [Configuration](#configuration)
- [Advanced examples](#advanced-examples)
- [Troubleshooting](#troubleshooting)
- [Support](#support)
- [Changelog](#changelog)

## Features

- Keeps dat:// websites online
- Publishes dat:// websites to DNS shortnames
- Mirrors dat:// websites to https://
- Provisions and manages HTTPS with [Let's Encrypt](https://letsencrypt.org)
- Provides optional [Pinning Service API](https://www.datprotocol.com/deps/0003-http-pinning-service-api/) endpoints

## Install

If you already have [Node.js](https://nodejs.org) (8.0+) and [npm](https://npmjs.com) installed on your server, get started by installing DDN server with npm or [npx](https://github.com/zkat/npx).

```bash
npm install -g @ddn/server
```

Otherwise, install Node.js and npm first:

- [Install Node.js](https://nodejs.org/en/download/)
- [nvm](https://github.com/creationix/nvm) for managing Node versions
- [Fixing npm permissions problems](https://docs.npmjs.com/getting-started/fixing-npm-permissions)

Having trouble installing? See [Troubleshooting](#troubleshooting).

## Running server

To run `ddn-server` manually, simply invoke the `ddn-server` command:

```bash
ddn-server
```

To keep `ddn-server` running, you'll need to daemonize it. We like using [pm2](https://www.npmjs.com/package/pm2).

```bash
# install pm2
npm i -g pm2

# start server with pm2
pm2 start ddn-server
```

To stop the daemon, run:

```
# stop server
pm2 stop ddn-server
```

### Command line flags

- `--config <path>`
  - Use the config file at the given path instead of the default `~/.ddn-server.yml`. Overrides the value of the DDN_SERVER_CONFIG env var.

### Environment variables

- `DDN_SERVER_CONFIG=cfg_file_path`
  - Specify an alternative path to the config than `~/.ddn-server.yml`
- `NODE_ENV=debug|staging|production`
  - Set to debug or staging to use the Let's Encrypt testing servers.

## Examples

`ddn-server` uses a [configuration file](#configuration-file) (`~/.ddn-server.yml` by default) for managing its behavior. These examples show various configurations.

[See all configuration options](#configuration)

### Example: set up a website with a domain name and HTTP mirroring

This configuration file will "seed" or "pin" the files at `dat://123...456`, publish those files to `dat://alice.com`, and mirror them to `https://alice.com`.

This example uses a domain name, so in order for the domain name to resolve correctly, you'll need to update your DNS configuration first. In this case, you could set an `A` record that points to the `ddn-server` server's IP address.

```yaml
dats:
  - url: dat://123...456
    domains:
      - alice.com
httpMirror: true
letsencrypt:
  email: alice@mail.com
  agreeTos: true

```

### Example: seed multiple websites, with no domain names

This configuration simply "seeds" or "pins" the files at `dat://123...456` and `dat:///456...789`. No domain name is required for this configuration.

```yaml
dats:
  - url: dat://123...456
  - url: dat://456...789
```

### Example: seed multiple websites, enable Web API

In addition to being used to manage a website, `ddn-server` can be used with more specific configurations, like providing a mini-pinning service for a group of friends.

This example enables the [Pinning Service API](https://www.datprotocol.com/deps/0003-http-pinning-service-api/), which makes it possible for you (and maybe a few friends!) to publish to your `ddn-server` instance with [ddn](https://ebookchain.org), the [Dat CLI](https://npm.im/dat), or any client that supports the Pinning Service API.

If you choose to use a domain name for your API endpoint, be sure to configure your DNS to point to your `ddn-server` server.

[See full `webapi` reference](#webapi)

```yaml
# enable publishing to `ddn-server` from Eok and the Dat CLI
webapi:                # set to false to disable
  domain:              # the domain name for the web API (optional)
  username:            # the username for publishing from ddn or the Dat CLI
  password:            # the password for publishing from ddn or the Dat CLI
```

## Configuration

- [Configuration file](#configuration-file)
- [dashboard](#dashboard)
  - [dashboard.port](#dashboardport)
- [dats](#dats)
  - [dats.*.url](#datsurl)
  - [dats.*.domains](#datsdomains)
  - [dats.*.name](#datsname)
  - [dats.*.otherDomains](#datsotherdomains)
- [directory](#directory)
- [domain](#domain)
- [httpMirror](#httpmirror)
- [letsencrypt](#letsencrypt)
  - [letsencrypt.email](#letsencryptemail)
  - [letsencrypt.agreeTos](#letsencryptagreetos)
- [ports](#ports)
  - [ports.http](#portshttp)
  - [ports.https](#portshttps)
- [proxies](#proxies)
  - [proxies.*.from](#proxiesfrom)
  - [proxies.*.to](#proxiesto)
- [redirects](#redirects)
  - [redirects.*.from](#redirectsfrom)
  - [redirects.*.to](#redirectsto)
- [webapi](#webapi)
  - [webapi.username](#webapiusername)
  - [webapi.password](#webapipassword)
  - [webapi.domain](#webapidomain)

### Configuration file

`ddn-server` uses `~/.ddn-server.yml` as its default configuration file. You can specify an alternative config file using a [command line flag](#command-line-flags) or an [environment variable](#environment-variables).


```yaml
directory: ~/.ddn-server # where your data will be stored
httpMirror: true       # enables HTTP mirroring
ports:
  http: 80             # HTTP port for redirects or non-TLS serving
  https: 443           # HTTPS port for serving mirrored content and DNS data
letsencrypt:           # set to false to disable Let's Encrypt
  email:               # you must provide your email to LE for admin
  agreeTos: true       # you must agree to the LE terms (set to true)
dashboard:             # set to false to disable
  port: 8089           # port for accessing the metrics dashboard

# enable publishing to server from ddn and the Dat CLI
webapi:                # set to false to disable
  domain:              # enter your web API's domain here (optional, unless Let's Encrypt TLS is wanted on the web API)
  username:            # the username for publishing from ddn or the Dat CLI
  password:            # the password for publishing from ddn or the Dat CLI

# enter your pinned dats here
dats:
  - url:               # URL of the dat to be pinned
    domains:           # (optional) the domains of the dat

# enter any proxied routes here
proxies:
  - from:              # the domain to accept requests from
    to:                # the domain (& port) to target

# enter any redirect routes here
redirects:
  - from:              # the domain to accept requests from
    to:                # the domain to redirect to
```

### dashboard

Default `false`

Set to `true` to enable the [Prometheus metrics dashboard](#example-using-a-metrics-dashboard).

```yaml
dashboard:             # set to false to disable
  port: 8089           # port for accessing the metrics dashboard
```

#### dashboard.port

Default: `8089`

The port to serve the [Prometheus metrics dashboard](#example-using-a-metrics-dashboard).

### dats

A listing of the Dat archives to seed.

```yaml
dats:
  - url: dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03/
    domains:
      - mysite.com
      - my-site.com
```

#### dats.*.url

The Dat URL of the site to host. Should be a 'raw' dat url (no DNS hostname).

Example values:

```
# raw key
1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03

# URL with trailing slash
dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03/

# URL with no trailing slash
dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03
```

#### dats.*.domains

Additional domains of the Dat archive. Can be a string or a list of strings. Each string should be a domain name.

To use `dats.*.domains`, you'll first need to configure the DNS entry for your domain name to point to your server. For instance, to point `alice.com` with `ddn-server`, you'll need to update your DNS configuration to point `alice.com` to your server server's IP address.

Example values:

```
mysite.com
foo.bar.edu
best-site-ever.link
```

#### dats.*.name

**DEPRECATED**. See the [v2.0.0 migration guide](#migrating-to-v2-0-0).

The name of the Dat archive. Sets a subdomain relative to the Web API domain, similar to the way that [Hashbase](https://hashbase.io) does. Must be unique on the `ddn-server` instance.

#### dats.*.otherDomains

**DEPRECATED**. Use the [domains field](#dats-domains) instead.

Additional domains of the Dat archive. Can be a string or a list of strings. Each string should be a domain name. Example values:

```
mysite.com
foo.bar.edu
best-site-ever.link
```

### directory

Default:  `~/.ddn-server`

The directory where `ddn-server` will store your Dat archive's files.

### domain

**DEPRECATED**. See the [v2.0.0 migration guide](#migrating-to-v2-0-0).

The DNS domain of your server instance.

### httpMirror

Default: `false`

Set to `true` to provide https mirroring of your Dat archives.

### letsencrypt

Default: `false`

Set to `true` to enable Lets Encrypt's automatic TLS certificate provisioning.

```yaml
letsencrypt:           # set to false to disable Let's Encrypt
  email:               # you must provide your email to LE for admin
  agreeTos: true       # you must agree to the LE terms (set to true)
```

#### letsencrypt.email

The email to send Let's Encrypt notices to.

#### letsencrypt.agreeTos

Do you agree to the [terms of service of Lets Encrypt](https://letsencrypt.org/repository/)? Required, must be true.

### ports

The ports for HTTP and HTTPS.

```yaml
ports:
  http: 80             # HTTP port for redirects or non-TLS serving
  https: 443           # HTTPS port for serving mirrored content and DNS data
```

#### ports.http

Default: `80`

The port for serving HTTP sites.

HTTP automatically redirects to HTTPS.

#### ports.https

Default: `443`

The port for serving HTTPS sites.

### proxies

A listing of domains to proxy. Useful when your server has other services running that you need available.

```yaml
proxies:
  - from: my-proxy.com
    to: http://localhost:8080
```

#### proxies.*.from

The domain to proxy from. Should be a domain name.

Example values:

```
mysite.com
foo.bar.edu
best-site-ever.link
```

#### proxies.*.to

The protocol, domain, and port to proxy to. Should be an [origin](https://en.wikipedia.org/wiki/Same-origin_policy#Origin_determination_rules).

Example values:

```
https://mysite.com/
http://localhost:8080/
http://127.0.0.1:123/
```

### redirects

A listing of domains to redirect.

```yaml
redirects:
  - from: my-old-site.com
    to: https://my-site.com
```

#### redirects.*.from

The domain to redirect from. Should be a domain name.

Example values:

```
mysite.com
foo.bar.edu
best-site-ever.link
```

#### redirects.*.to

The base URL to redirect to. Should be an [origin](https://en.wikipedia.org/wiki/Same-origin_policy#Origin_determination_rules).

Example values:

```
https://mysite.com/
http://localhost:8080/
http://127.0.0.1:123/
```

### webapi

Default: `false`

Set to `true` to enable the [Pinning Service API](https://www.datprotocol.com/deps/0003-http-pinning-service-api/), which enables publishing to DDN server with [ddn](https://ebookchain.org), the [Dat CLI](https://npm.im/dat), or any other client that supports the Pinning Service API.

```yaml
# enable publishing to `ddn-server` from ddn and the Dat CLI
webapi:                # set to false to disable
  domain:              # the domain of the web api (optional)
  username:            # the username for publishing from ddn or the Dat CLI
  password:            # the password for publishing from ddn or the Dat CLI
```

#### webapi.username

The username for your pinning service API.

#### webapi.password

The password for your pinning service API.

#### webapi.domain

The DNS domain of your server Web API. Optional, but required if you want Let's Encrypt to provide your Web API with a TLS certificate.

## Advanced examples

### Example: proxies

If your `ddn-server` instance is running on ports 80/443, and you have other Web servers running on your server, you might need `ddn-server` to proxy to those other servers. You can do that with the `proxies` config. Here's an example proxy rule:

[See full `proxies` reference](#proxies)

```yaml
proxies:
  - from: my-proxy.com
    to: http://localhost:8080
```

### Example: redirecting requests

Sometimes you need to redirect old domains to new ones. You can do that with the `redirects` rule. Here's an example redirect rule:

[See full `redirects` reference](#redirects)

```yaml
redirects:
  - from: my-old-site.com
    to: https://my-site.com
```

### Example: using a metrics dashboard

`ddn-server` has built-in support for [Prometheus](https://prometheus.io), which can be visualized with [Grafana](http://grafana.org/).

![./grafana-screenshot.png](./grafana-screenshot.png)

DDN server exposes its metrics at port 8089. Prometheus periodically scrapes the metrics and stores them in a database. Grafana uses those metrics and provides a provides a nice dashboard visualization. It's a little daunting at first, but setup should be relatively painless.

Steps:

 1. [Install Prometheus](https://prometheus.io/download/) on your server
 2. [Install Grafana](http://grafana.org/download/) on your server
 3. Update the `prometheus.yml` config
 4. Start Prometheus and Grafana
 5. Login to Grafana
 6. Add Prometheus as a data source to Grafana (it should be running at `localhost:9090`
 7. Import [this Grafana dashboard](./grafana-dashboard.json)

Your `prometheus.yml` config should include have the `scrape_configs` option set like this:

```yaml
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'server'
    static_configs:
      - targets: ['localhost:8089']
```

### Example: running server behind Apache or Nginx

If you're running `ddn-server` on a server that uses Apache or Nginx, you may need to change your config to disable HTTPS. For instance, if you're using nginx and proxying to port `8080`, update your config to disable Let's Encrypt and to set the HTTP port:

```yaml
letsencrypt: false
ports:
  http: 8080
```

You will need to add all domains to your Nginx/Apache config.

### Example: running server in a docker container

1. Install [Docker](http://docker.com/). If you're on Linux, remember to [configure Docker to start on boot](https://docs.docker.com/install/linux/linux-postinstall/). Don't know of the equivalent for other systems.

2. Clone the project. Edit `.ddn-server.yml` according to your needs. Most importantly: **Change username and password**.  
If you don't want to think of a username and a password, just use [this](https://stackoverflow.com/a/1349426/6690391) but **increase the length**.

3. In the project root, run this command:

```
docker build -t ddn-server:latest . && docker run -d --name=ddn-server --restart=always -p 80:80 -p 443:443 -p 3282:3282 ddn-server:latest
```

**Notes:**  
1. Not an expert in Docker security or configuration.  
2. if you have ddn on the same machine, you may want to change the dat port `-p 3282:3282` to something like `-p 9999:3282`.  
3. To debug the running container:
   - Run `docker ps -a` to see the container running status.  
   - Run `docker logs ddn-server` to see the logs.
   - Run `docker exec -it ddn-server sh` to get into a terminal.
4. Didn't think about how you'd install a newer version of ddn-server while keeping the old configuration and data.

## Troubleshooting

### Installing build dependencies

When installing `ddn-server`, you may need to install additional build dependencies:

```
sudo apt-get install libtool m4 automake libcap2-bin build-essential
```

### Port setup (EACCES error)

For `ddn-server` to work correctly, you need to be able to access port 80 (http), 443 (https), and 3282 (dat). Your firewall should be configured to allow traffic on those ports.

If you get an EACCES error on startup, you either have a process using the port already, or you lack permission to use the port. Try `lsof -i tcp:80` or `lsof -i tcp:443` to see if there are any processes bound to the ports you need.

If the ports are not in use, then it's probably a permissions problem. We recommend using the following command to solve that:

```
# give node perms to use ports 80 and 443
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``
```

This will give nodejs the rights to use ports 80 and 443. This is preferable to running ddn-server as root, because that carries some risk of a bug in `ddn-server` allowing somebody to control your server.

## Support

`ddn-server` is built by the [DDN team](http://ddn.link/about). You can help us continue our work on ddn, `ddn-server`, and more. Thank you!

## Licence

Copyright (c) 2019 DDN.link
Copyright (c) 2018 Blue Link Labs

---
_Source: https://npm.io/package/@ddn/server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
