Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hjmmc committed Sep 5, 2019
1 parent 25eecfc commit f15ada9
Show file tree
Hide file tree
Showing 46 changed files with 2,363 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

# Logs
logs
*.log
Expand Down
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Xip Dns Server

[xip.lhjmmc.cn](https://xip.lhjmmc.cn)

## Useage

1. set a NX record to your domain (such as `xip.lhjmmc.cn`)

```
...
NX xip.lhjmmc.cn your_server_ip
...
```

2. deploy

```bash
## clone
git clone https://github.com/hjmmc/xip-dns-server.git
cd xip-dns-server

## docker-compose
docker-compose up -d
```

check nsedit is success running [http://your_server_ip:5380](http://your_server_ip:5380)

add your zone in powerdns by nsedit or curl

```bash
curl -X POST --data '{"name":"xip.lhjmmc.cn.", "kind": "Native", "masters": [], "nameservers": ["ns1.xip.lhjmmc.cn.", "ns2.xip.lhjmmc.cn."]}' -v -H 'X-API-Key: 123456' http://localhost:5381/api/v1/servers/localhost/zones
```

add some A record for your domain

```
A xip.lhjmmc.cn your_server_ip
A *.xip.lhjmmc.cn your_server_ip
```

check dns server by nslookup

```bash
nslookup xip.lhjmmc.cn
nslookup 192-168-1-1.xip.lhjmmc.cn
```

3. gen certs

```bash
## http://acme.sh/
export PDNS_Url="http://localhost:5381"
export PDNS_ServerId="localhost"
export PDNS_Token="123456"
export PDNS_Ttl=60
acme.sh --issue --dns dns_pdns -d xip.lhjmmc.cn -d *.xip.lhjmmc.cn

## install-cert
mkdir /var/www/html/xip.lhjmmc.cn
acme.sh --install-cert -d xip.lhjmmc.cn --cert-file /var/www/html/xip.lhjmmc.cn/cert.pem --key-file /var/www/html/xip.lhjmmc.cn/key.pem --fullchain-file /var/www/html/xip
.lhjmmc.cn/fullchain.pem --reloadcmd "chmod 777 /var/www/html/xip.lhjmmc.cn/*"
```

4. Share cert by nginx conf

```
server {
listen 80;
listen 443 ssl;
server_name xip.lhjmmc.cn;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#SSL Support
ssl_certificate /var/www/html/xip.lhjmmc.cn/fullchain.pem;
ssl_certificate_key /var/www/html/xip.lhjmmc.cn/key.pem;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULLL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html/xip.lhjmmc.cn;
index index.html index.htm;
}
}
```

## Thands

[Mikroways/docker-powerdns](https://github.com/Mikroways/docker-powerdns)

[techguy613/native-dns-packet](https://github.com/techguy613/native-dns-packet)

[acme.sh](https://acme.sh)
14 changes: 14 additions & 0 deletions dns-proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:10

RUN mkdir -p /home/dns-proxy
WORKDIR /home/dns-proxy
COPY . /home/dns-proxy

ENV DNS_SEV 127.0.0.1
ENV DNS_PORT 53

RUN npm install

EXPOSE 53/udp

CMD [ "npm", "start" ]
1 change: 1 addition & 0 deletions dns-proxy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker build -t dns_proxy .
78 changes: 78 additions & 0 deletions dns-proxy/dns_proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const dgram = require('dgram')
const server = dgram.createSocket('udp4')

const Packet = require('./native-dns-packet')

const DNS_SEV = process.env.DNS_SEV || '127.0.0.1'
const DNS_PORT = process.env.DNS_PORT || 53

console.log('DNS:', DNS_SEV, DNS_PORT)

const ipReg = /(\d{1,3})[\.-](\d{1,3})[\.-](\d{1,3})[\.-](\d{1,3})/

function proxy(buffer, req, rinfo) {
console.log('req', buffer.toString('hex'))
console.log('req', JSON.stringify(req))
const client = dgram.createSocket('udp4');

client.on('error', (err) => {
console.log(`client error:` + err.stack)
client.close();
})
client.on('message', (resBuf, fbRinfo) => {
console.log("res", resBuf.toString('hex')); //获取响应报文
let res = Packet.parse(resBuf)
console.log('res', JSON.stringify(res))
server.send(resBuf, rinfo.port, rinfo.address, (err) => {
err && console.log(err);
});
client.close();
})

client.send(buffer, DNS_PORT, DNS_SEV, (err) => {
if (err) {
console.log(err);
client.close();
}
});
}

server.on('message', (buffer, rinfo) => {
let req = Packet.parse(buffer)
if (req.question.length == 1) {
let q = req.question[0]
if (q.type == 1 && ipReg.test(q.name)) {
req.header.qr = 1
req.header.ra = 1
req.answer = [{
"name": q.name,
"type": 1,
"class": 1,
"ttl": 3600,
"address": ipReg.exec(q.name).slice(1).join('.')
}]
let buf = Buffer.allocUnsafe(2048)
let len = Packet.write(buf, req)
let resBuf = buf.slice(0, len)

console.log("res1", resBuf.toString('hex')); //获取响应报文
console.log('res1', JSON.stringify(req))
console.log('res1', ipReg.exec(q.name).slice(1).join('.'))
server.send(resBuf, rinfo.port, rinfo.address, (err) => {
err && console.log(err);
})
return
}
}
proxy(buffer, req, rinfo); //转发
})

server.on('error', (err) => {
console.log('server error:' + err.stack)
server.close()
})
server.on('listening', () => {
const addr = server.address()
console.log(`run ${addr.address}:${addr.port}`)
})
server.bind(53);
4 changes: 4 additions & 0 deletions dns-proxy/native-dns-packet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules*
v8.log
*.swp
.idea/
19 changes: 19 additions & 0 deletions dns-proxy/native-dns-packet/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2011 Timothy J Fontaine <[email protected]>

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:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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
51 changes: 51 additions & 0 deletions dns-proxy/native-dns-packet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
native-dns-packet
-----------------

* `Packet.parse(buffer)` returns an instance of `Packet`
* `Packet.write(buffer, packet)` writes the given packet into the buffer,
truncating where appropriate

```javascript
var Packet = function () {
this.header = {
id: 0,
qr: 0,
opcode: 0,
aa: 0,
tc: 0,
rd: 1,
ra: 0,
res1: 0,
res2: 0,
res3: 0,
rcode: 0
};
this.question = [];
this.answer = [];
this.authority = [];
this.additional = [];
this.edns_options = [];
this.payload = undefined;
};
```

## History

###### 0.1.1 - October 5, 2014

- Fixing NPM tagging issue...

###### 0.1.0 - October 2, 2014

- Added TLSA support
- Fixed EDNS & NAPTR support + deprecates some EDNS fields on Packet
- Now includes support for forwarding EDNS responses (Packet.edns)
- Added many TODOs with suggested improvements
- Added many links to GH issues and RFCs
- Cleaned up code a bit to better please linters
- Added deprecation notices (see parseOpt)
- Handle unhandled RRs on writing packet instead of throwing exception.
- edns/opt should use BufferCursor.copy (Fixes #11)
- Updated `package.json` to include all authors
- Merged tj's `master` branch to add License info
- Updated README to include history of changes
Loading

0 comments on commit f15ada9

Please sign in to comment.