-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_dns.js
187 lines (174 loc) · 5.78 KB
/
proto_dns.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Licensed using GNU GPL terms
(c) 2010, Andrey Smirnov
TODO: proper (c) boilerplate
*/
// Class for x-dns protocol handler
function Proto_dns ( agent )
{
this.agent = agent;
this.database = {}; // db.<type>.<fqdname>[ #record ].value|.trust
// this.authorities = {}; // auth.<id>[ #attr ]
// Default resolver settings
this.add_record( 'NS', '.', 'sim.root-servers.net.', 0.8 )
this.add_record( 'A', 'sim.root-servers.net.', 'DR1', 0.8 )
// Authority resolver settings for . holder
if ( agent.id == 'DR1' )
{
this.add_record( 'NS', 'tld.', 'ns.tld.', 1 );
this.add_record( 'A', 'ns.tld.', 'DR2', 1 );
}
// Authority resolver settings for .tld holder
if ( agent.id == 'DR2' )
{
this.add_record( 'A', 'vasya.tld.', '3', 1 );
this.add_record( 'A', 'ns.tld.', 'DR2', 1 );
this.add_record( 'A', 'vasya2.tld.', '2', 1 );
this.add_record( 'URI', 'vasya3.tld.', 'x-direct://vasya.tld./index.txt', 1 );
}
}
Proto_dns.prototype = {
schema: 'x-dns',
/* add_auth: function ( id )
{
this.authorities[ id ] = {};
},
*/
log: function ( msg )
{
this.agent.log( this.schema + ": " + msg );
},
add_record: function ( type, fqdn, value, trust, agent_id )
{
if ( ! this.database[ type ] )
this.database[ type ] = {};
if ( ! this.database[ type ][ fqdn ] )
this.database[ type ][ fqdn ] = [];
this.database[ type ][ fqdn ].push( [ value, ( trust || 1 ), ( agent_id || this.agent.id ) ] );
},
add_response: function ( response, peer_id )
{
this.log( 'adding record: ' + response.dump() );
for ( var i in response.info )
{
var ri = response.info[ i ];
this.add_record( ri[2], ri[0], ri[1], null, peer_id );
}
},
handle_request: function ( args )
{
var res = this.query_local( args.type, args.fqdn );
return res;
},
query_WWW: function ( method, url, args )
{
var parts = url.split( ':', 2 );
if ( parts[0] != this.schema )
{
this.agent.log_error( 'Tried to process request for wrong schema ' + parts[0] );
return new Response( null, 501 );
}
if ( method != 'GET' )
{
this.agent.log_error( 'Methods other than GET not supported' );
return new Response( null, 501 );
}
// Parse query parameters
var parts1 = parts[1].split( '?', 2 );
var dn = parts1[0];
var type;
var class = 'IN';
if ( parts1[1] )
{
var parts2 = parts1[1].split( '&' );
for ( var i in parts2 )
{
var parts3 = parts2[i].split( '=' );
if ( parts3[0] == 'type' )
type = parts3[1];
}
}
if ( ! type ) type = 'A';
return this.query_dns_recursive( type, dn );
},
query_local: function ( type, fqdn )
{
this.log( "query_local: type=" + type + ", fqdn=" + fqdn );
if ( ! this.database[ type ] ) return new Response( null, 404 );
var values = this.database[ type ][ fqdn ];
if ( !values || ! values.length ) return new Response( null, 404 );
var response = new DNSResponse();
for ( var i in values )
{
this.log( "query_local: adding to response: " + "type=" + type + ", fqdn=" + fqdn + ", value=" + values[i][0] );
response.add_info( fqdn, values[i][0], type );
}
return response;
},
query_dns_server: function ( type, dn, server )
{
this.log( "query_dns_server: type=" + type + ", fqdn=" + dn + ", srv=" + server );
var res = this.agent.make_protocol_request( server, this.schema, { type: type, fqdn: dn } );
if ( res.is_error() )
return new Response( null, 404 );
this.add_response( res, server );
return res;
},
query_dns_recursive: function ( type, dn )
{
var dn_parts = dn.split( '.' );
//alert( dn_parts.toSource() );
//this.log( dn_parts.length );
var l = dn_parts.length - 1;
for ( var i = l - 1; i >= 0; i-- )
{
var cur_parts = dn_parts.slice( i, l );
var cur_dn = cur_parts.join( '.' ) + '.';
var zone_parts = dn_parts.slice( i + 1, l );
var cur_zone = zone_parts.join( '.' ) + '.';
//this.log( i + ": cur_dn: " + cur_dn + " , cur_zone: " + cur_zone );
// load cur_zone NS from local cache
var res_cur_zone_ns = this.query_local( 'NS', cur_zone );
if ( res_cur_zone_ns.is_error() ) return new Response( null, 404 );
//this.log( res_cur_zone_ns.is_error() + " " + cur_zone + " " + res_cur_zone_ns.get_value( 'NS', cur_zone ) + res_cur_zone_ns.dump() );
// load cur_zone NS's A from local cache
var res_cur_zone_a = this.query_local( 'A', res_cur_zone_ns.get_value( 'NS', cur_zone ) );
//this.log( res_cur_zone_a.is_error() + " " + res_cur_zone_a.dump() );
//this.log( '-' );
if ( res_cur_zone_a.is_error() ) return new Response( null, 404 );
zone_server_addr = res_cur_zone_a.get_value( 'A', res_cur_zone_ns.get_value( 'NS', cur_zone ) );
//this.log( "zone_server_addr: " + zone_server_addr );
//this.log( '--- i: ' + i );
if ( i != 0 )
{
// Load cur_dn zone NS record into local cache
var res_cur_dn = this.query_local( 'NS', cur_dn );
if ( res_cur_dn.is_error() )
res_cur_dn = this.query_dns_server( 'NS', cur_dn, zone_server_addr );
if ( res_cur_dn.is_error() )
return new Response( null, 404 );
// Load cur_dn zone NS's A record into local cache
var res_cur_dn_ns_a = this.query_local( 'A', res_cur_dn.get_value( 'NS', cur_dn ) );
if ( res_cur_dn_ns_a.is_error() )
res_cur_dn_ns_a = this.query_dns_server( 'A', res_cur_dn.get_value( 'NS', cur_dn ), zone_server_addr );
if ( res_cur_dn_ns_a.is_error() )
return new Response( null, 404 );
}
else
{
//this.log( '--- ' );
var res_cur_dn = this.query_local( type, cur_dn );
//this.log( '---: ' + res_cur_dn.dump() );
if ( res_cur_dn.is_error() )
res_cur_dn = this.query_dns_server( type, cur_dn, zone_server_addr );
//this.log( '---: ' + res_cur_dn.dump() );
if ( res_cur_dn.is_error() )
return new Response( null, 404 );
// Return succesfull result
return res_cur_dn;
}
}
//return new DNSResponse().add_info( 'vasya.root.', 'A', 1 );
return new Response( null, 404 );
}
};