This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
154 lines (147 loc) · 7.37 KB
/
index.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
const { readFileSync, existsSync, writeFileSync } = require('fs');
const { Connection, constants, ApiClient } = require('chia-api');
const { homedir } = require('os');
const { join } = require('path');
const lodash = require('lodash');
const winston = require('winston');
const { format } = require('logform');
const { combine, timestamp, label, printf } = format;
const request = require('request');
(async() => {
async function Curl(url, method = 'GET', form = {}) {
return new Promise((resolve) => {
request({
method: method,
url: url,
formData: form
}, function(err, resp, body) {
if (err) resolve('Error');
if (body) resolve(body);
});
});
}
const logFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
});
var logger = winston.createLogger({
level: 'info',
format: combine(
timestamp(),
logFormat
),
transports: [
new winston.transports.Console()
]
});
process.on('uncaughtException', function(err) {
logger.error('Error: ' + err);
});
logger.info('connection to chia service');
const conn = new Connection('localhost:55400', {
cert: readFileSync(join(homedir(), '.chia', 'mainnet', 'config/ssl/daemon/private_daemon.crt')),
key: readFileSync(join(homedir(), '.chia', 'mainnet', 'config/ssl/daemon/private_daemon.key')),
});
logger.info('Initialize FullNode ApiClient');
const fullNode = new ApiClient.FullNode({ connection: conn, origin: 'chia-node-append' });
await fullNode.init();
var setting = {
node_source: existsSync('node_list.txt') ? 'node_list.txt' : 'node_list_api',
node_source_type: null,
disconnect_node: true,
remove_node: true
}
while (true) {
if (existsSync('chia-node-append-setting.json')) {
logger.info('use setting from chia-node-append-setting.json');
const setting_obj = JSON.parse(readFileSync('chia-node-append-setting.json', { encoding: 'utf8', flag: 'r' }));
if (setting_obj.setting_version == '2.0') {
setting = {
node_source: setting_obj.node_source,
node_source_type: null,
disconnect_node: setting_obj.disconnect_node,
remove_node: setting_obj.remove_node
}
} else {
logger.error(`chia-node-append-setting version don't match`)
logger.info('use default setting');
}
} else {
logger.info('use default setting');
}
if (setting.node_source == 'node_list_api') {
logger.info('import node list from chia-node-list-api');
setting.node_source_type = 'node_list_firebase';
var node_obj = JSON.parse(await Curl('https://chia-node-list-api.vercel.app/node'));
} else {
logger.info('import node list from ' + setting.node_source);
let node_source = '';
if (existsSync(setting.node_source)) {
node_source = setting.node_source;
} else if (existsSync)
setting.node_source_type = 'node_list_file';
var node_obj = readFileSync(setting.node_source, function(err, data) {
if (err) throw err;
}).toString().split('\n');
for (const property in node_obj) {
node_obj[property] = {
node_ip: node_obj[property].split(':')[0],
node_port: parseInt(node_obj[property].split(':')[1])
}
}
}
var currConnections = await fullNode.getConnections();
const currConn_obj = JSON.parse(JSON.stringify(currConnections))['connections'];
const node_chia = node_obj.filter(({ node_ip: node_1 }) => !currConn_obj.some(({ peer_host: node_2 }) => node_2 === node_1));
for (const property in node_chia) {
logger.info('add node connection ip: ' + node_chia[property].node_ip + ' port: ' + node_chia[property].node_port);
try {
const addNodeConnection = await fullNode.addNodeConnection({
host: node_chia[property].node_ip,
port: node_chia[property].node_port
});
} catch (exception) {
logger.error('failed to add node connection : ' + node_chia[property].node_ip + ':' + node_chia[property].node_port);
if (setting.remove_node == true && setting.node_source != 'node_list_api') {
var node_arr_file = readFileSync(setting.node_source, { encoding: 'utf8', flag: 'r' }).split('\n');
for (const property in node_arr_file) {
node_arr_file[property] = node_arr_file[property].replace('\r', '');
}
const node_index = node_arr_file.indexOf(node_chia[property].node_ip + ':' + node_chia[property].node_port);
node_arr_file.splice(node_index, 1);
var node_list_str = null;
for (const property in node_arr_file) {
node_list_str = node_list_str == null ? node_arr_file[property] : node_list_str + '\n' + node_arr_file[property];
}
writeFileSync(setting.node_source, node_list_str);
logger.info('remove node : ' + node_chia[property].node_ip + ':' + node_chia[property].node_port + ' from file ' + setting.node_source);
}
}
logger.info('current connections : ' + (Object.keys(JSON.parse(JSON.stringify(await fullNode.getConnections()))['connections']).length).toString());
}
if (setting.disconnect_node == true) {
const filter_node_conn = lodash.filter(JSON.parse(JSON.stringify(await fullNode.getConnections()))['connections'], function(node_type) { return node_type.type != 1 });
if (Object.keys(filter_node_conn).length > 0) {
for (const property in filter_node_conn) {
if (filter_node_conn[property].peer_host !== '127.0.0.1') {
try {
logger.info('close connection node with node_id : ' + filter_node_conn[property].node_id)
const closeNodeConnection = await fullNode.closeNodeConnection({
node_id: filter_node_conn[property].node_id
});
} catch (exception) {
logger.error('failed to close connection node : ' + exception);
}
}
}
}
}
currConnections = lodash.filter(JSON.parse(JSON.stringify(await fullNode.getConnections()))['connections'], obj_item => obj_item.type === 1);
for (const property in currConnections) {
logger.info('active node ip: ' + currConnections[property].peer_host + ' port: ' + currConnections[property].peer_server_port + ' to node list api');
await Curl('https://chia-node-list-api.vercel.app/node', 'PUT', {
node_ip: currConnections[property].peer_host,
node_port: currConnections[property].peer_server_port
});
}
}
})();