wip usenet stuff

This commit is contained in:
zefie
2022-10-12 12:26:30 -04:00
parent 8075438a93
commit 976c79c92a
14 changed files with 619 additions and 46 deletions

View File

@@ -6,23 +6,55 @@ class WTVNews {
wtvshared = null;
service_name = null;
client = null;
username = null;
password = null;
constructor(minisrv_config, service_name) {
this.minisrv_config = minisrv_config;
this.service_name = service_name;
const { WTVShared } = require("./WTVShared.js");
this.wtvshared = new WTVShared(minisrv_config);
this.client = new this.newsie({
host: this.minisrv_config.services[service_name].upstream_address,
port: this.minisrv_config.services[service_name].upstream_port
})
}
initializeUsenet(host, port = 119, tls_options = null, username = null, password = null) {
// use local self-signed cert for local server
var newsie_options = {
host: host,
port: port,
tlsPort: (tls_options !== null) ? true : false,
}
if (newsie_options.tlsPort) newsie_options.tlsOptions = tls_options;
this.client = new this.newsie(newsie_options);
if (username && password) {
this.username = username;
this.password = password;
}
}
connectUsenet() {
return new Promise((resolve, reject) => {
this.client.connect().then((response) => {
if (response.code == 200) {
resolve(true);
if (response.code == 200 || response.code == 201) {
if (this.username && this.password) {
this.client.authInfoUser(this.username).then((res) => {
if (res.code == "381") {
res.authInfoPass(this.password).then((res) => {
if (res.code == 281) resolve(true);
else reject(res.description);
}).catch((e) => {
console.error(" * WTVNews Error:", "Command: connect", e);
reject("Could not connect to upstream usenet server");
});
} else {
reject(res.description)
}
}).catch((e) => {
console.error(" * WTVNews Error:", "Command: connect", e);
reject("Could not connect to upstream usenet server");
});
} else {
resolve(true);
}
}
}).catch((e) => {
console.error(" * WTVNews Error:", "Command: connect", e);
@@ -31,16 +63,19 @@ class WTVNews {
});
}
listGroup(group, page = 0, limit = 100) {
listGroup(group, page = 0, limit = 100, raw_range = null) {
return new Promise((resolve, reject) => {
this.selectGroup(group).then((res) => {
var range = {
start: (limit * page) + res.group.low,
if (!raw_range) {
var range = {
start: (limit * page) + res.group.low,
}
range.end = range.start + limit;
if (page) range.start++;
if (range.end > res.high) delete range.group.end;
} else {
range = raw_range;
}
range.end = range.start + limit;
if (page) range.start++;
if (range.end > res.high) delete range.group.end;
console.log(res, range);
this.client.listGroup(group, range).then((data) => {
resolve(data);
}).catch((e) => {
@@ -75,7 +110,8 @@ class WTVNews {
this.client.next().then((res) => {
data.next_article = res.article.articleNumber;
resolve(data.next_article);
}).catch(() => {
}).catch((e) => {
console.log(e);
data.next_article = null;
resolve(data.next_article);
})
@@ -150,15 +186,12 @@ class WTVNews {
promises.push(new Promise((resolve, reject) => {
this.selectGroup(group).then((res) => {
this.getArticleMessageID(article).then((data) => {
console.log(data);
messageid = data;
resolve(data);
}).catch((e) => {
console.log(e);
reject(e)
});
}).catch((e) => {
console.log(e);
reject(e)
});
}));
@@ -185,7 +218,6 @@ class WTVNews {
} else {
articleData.body = [];
}
console.log(articleData);
response.send(articleData).then((response) => {
this.client.quit();
if (response.code !== 240) {
@@ -233,7 +265,6 @@ class WTVNews {
if (data.article) messages.push(data.article)
resolve();
}).catch((e) => {
console.log(e, article);
reject(e);
});
}));
@@ -285,9 +316,6 @@ class WTVNews {
});
}
})
if (!found) {
message_id_roots.push({ "messageId": messageId, "index": j });
}
} else {
message_id_roots.push({ "messageId": messageId, "index": k });
}
@@ -306,7 +334,7 @@ class WTVNews {
var article_date = Date.parse(article.headers.DATE);
message_roots_sorted.push({ "article": article, "relation": null, "date": article_date });
});
message_roots_sorted.sort((a, b) => { return (a.date > b.date) });
message_roots_sorted.sort((a, b) => { return (a.date - b.date) });
Object.keys(message_roots_sorted).forEach((k) => {
sorted.push(message_roots_sorted[k]);
@@ -318,7 +346,7 @@ class WTVNews {
var article_date = Date.parse(article.headers.DATE);
relations.push({ "article": article, "relation": message_id_roots[k].messageId, "date": article_date })
});
relations.sort((a, b) => { return (a.date > b.date) });
relations.sort((a, b) => { return (a.date - b.date) });
Object.keys(relations).forEach((j) => {
sorted.push(relations[j]);
});

View File

@@ -0,0 +1,216 @@
class WTVNewsServer {
fs = require('fs');
path = require('path');
minisrv_config = null;
strftime = require('strftime');
wtvshared = null;
username = null;
password = null;
using_auth = false;
local_server = null;
data_path = null;
constructor(minisrv_config, local_server_port, using_auth = false, username = null, password = null) {
this.minisrv_config = minisrv_config;
const { WTVShared } = require("./WTVShared.js");
this.wtvshared = new WTVShared(minisrv_config);
const nntp_server = require('nntp-server');
var nntp_commands = {
...nntp_server.commands,
"LAST": {
head: 'LAST',
validate: /^LAST( [^\s]+)$/i,
// All supported params are defined in separate files
run() {
console.log('hi');
throw new Error('method LAST is not implemented');
}
},
"NEXT": {
head: 'NEXT',
validate: /^NEXT( [^\s]+)$/i,
// All supported params are defined in separate files
run() {
console.log('hi');
throw new Error('method NEXT is not implemented');
}
}
}
console.log(nntp_commands);
this.username == username || null;
this.password == password || null;
this.using_auth = using_auth;
if (using_auth && (!username && !password)) {
// using auth, but no auth info specified, so randomly generate it
this.username = this.wtvshared.generatePassword(8);
this.password = this.wtvshared.generatePassword(16);
}
// nntp-server module overrides
var self = this;
nntp_server.prototype = {
...nntp_server.prototype,
_authenticate: function (session) {
// authenticte
if (session.authinfo_user == self.username && session.authinfo_pass == self.password) return Promise.resolve(true);
return Promise.resolve(false);
},
_selectGroup: function (session, name) {
// selectGroup
var res = self.selectGroup(name);
if (!res.failed) {
session.group = res;
return true;
}
return false;
},
_buildHead: function (session, message) {
var out = "";
Object.keys(message.headers).forEach((k) => {
if (k.length > 0) out += `${k}: ${message.headers[k]}\r\n`;
});
out = out.substr(0,out.length - 2);
return out;
},
_buildHeaderField: function (session, message, field) {
var search = Object.keys(message.headers).find(e => (e.toLowerCase() == field.toLowerCase()));
if (search) return message.headers[search];
else return null;
},
_getArticle: function (session, message_id) {
// getArticle
return new Promise((resolve, reject) => {
self.getArticle(session.group.name, message_id).then((res) => {
resolve(res);
}).catch((e) => {
console.log(" * WTVNewsServer Error:", e);
reject(e);
});
});
},
_buildBody: function (session, message) {
return message.body;
},
_getRange: function (session, first, last) {
var res = self.listGroup(session.group.name, first, last)
if (res.failed) return false;
session.group = res.group_data;
return res.articleNumbers;
}
}
this.data_path = this.wtvshared.getAbsolutePath(this.minisrv_config.config.SessionStore + '/minisrv_internal_nntp');
this.createDataStore();
var tls_path = this.wtvshared.getAbsolutePath(this.minisrv_config.config.ServiceDeps + '/wtv-news');
var tls_options = {
ca: this.fs.readFileSync(tls_path + this.path.sep + 'localserver_ca.pem'),
key: this.fs.readFileSync(tls_path + this.path.sep + 'localserver_key.pem'),
cert: this.fs.readFileSync(tls_path + this.path.sep + 'localserver_cert.pem'),
}
this.local_server = new nntp_server({ requireAuth: using_auth, tls: tls_options, secure: true, commands: nntp_commands });
this.local_server.listen('nntps://localhost:' + local_server_port);
}
createDataStore() {
if (!this.fs.existsSync(this.data_path)) return this.fs.mkdirSync(this.data_path);
return true;
}
getGroupPath(group) {
return this.data_path + this.path.sep + group;
}
getArticlePath(group, article) {
return this.getGroupPath(group) + this.path.sep + article + ".newz";
}
createGroup(group) {
if (!this.fs.existsSync(getGroupPath(group))) return this.fs.mkdirSync(getGroupPath(group));
return true;
}
getArticle(group, article) {
return new Promise((resolve, reject) => {
const g = this.getArticlePath(group, article);
if (!this.fs.existsSync(g)) return false;
try {
var data = JSON.parse(this.fs.readFileSync(g));
resolve(data);
} catch (e) {
console.log(" * WTVNewsServer Error:", e);
reject(e)
}
});
}
selectGroup(group) {
var g = this.getGroupPath(group);
var out = {
total: 0,
min_index: null,
max_index: 0,
name: group
}
try {
this.fs.readdirSync(g).forEach(file => {
var articleNumber = parseInt(file.split('.')[0]);
if (out.min_index == null) out.min_index = articleNumber;
else if (articleNumber < out.min_index) out.min_index = articleNumber;
if (articleNumber > out.max_index) out.max_index = articleNumber;
out.total++;
});
} catch (e) {
out.failed = e;
}
if (out.min_index === null) out.min_index = 0;
return out;
}
listGroup(group, start, end) {
var g = this.getGroupPath(group);
var out = {
total: 0,
min_index: null,
max_index: 0,
name: group
}
var articleNumbers = [];
try {
this.fs.readdirSync(g).forEach(file => {
var articleNumber = parseInt(file.split('.')[0]);
if (articleNumber < start) return;
if (articleNumber > end) return false;
if (out.min_index == null) out.min_index = articleNumber;
else if (articleNumber < out.min_index) out.min_index = articleNumber;
if (articleNumber > out.max_index) out.max_index = articleNumber;
articleNumbers.push({ index: articleNumber });
out.total++;
});
} catch (e) {
console.log(e);
out.failed = e;
}
if (out.min_index === null) out.min_index = 0;
return {
articleNumbers: articleNumbers,
group_data: out
}
}
}
module.exports = WTVNewsServer;

View File

@@ -6,7 +6,7 @@
"UserServiceVault",
"ServiceVault"
],
"ServiceDeps": "ServiceDeps",
"ServiceDeps": "ServiceDeps",
"SessionStore": "SessionStore",
"SharedROMCache": "SharedROMCache",
"enable_shared_romcache": true,
@@ -31,8 +31,12 @@
"show_detailed_splash": true,
"show_diskmap": false,
"unauthorized_url": "wtv-1800:/unauthorized?",
"enable_port_isolation": true,
"allow_guests": true,
"domain_name": "wtv.zefie.com",
"ssid_block_list": [
"minisrv_internal_nntp"
],
"user_accounts": {
"max_users_per_account": 6,
"min_username_length": 5,
@@ -60,7 +64,8 @@
},
"wtv-news": {
"port": 1605,
"disabled": true,
"local_nntp_port": 57319,
"local_nntp_requires_auth": true,
"modules": [
"WTVNews"
]