Minor improvements
- WebFinger: detect when to use http or https, better function naming - RequestPool: add user agent
This commit is contained in:
@@ -11,24 +11,61 @@ import singletons;
|
||||
import db.db;
|
||||
import net.request_pool;
|
||||
import webfinger;
|
||||
import ap.actor;
|
||||
import ap.util;
|
||||
import std.math.remainder;
|
||||
import util;
|
||||
|
||||
void commonInit() {
|
||||
auto provider = new DefaultProvider(true, Levels.DEBUG);
|
||||
configureLoggingProvider(provider);
|
||||
|
||||
RP = new RequestPool();
|
||||
RP.startBackground();
|
||||
Config cfg = loadConfig();
|
||||
initRequestPool(cfg);
|
||||
initDatabase(cfg);
|
||||
}
|
||||
|
||||
Config loadConfig() {
|
||||
Config cfg = new Config();
|
||||
cfg.load();
|
||||
return cfg;
|
||||
}
|
||||
|
||||
void initRequestPool(Config cfg) {
|
||||
Rp = new RequestPool();
|
||||
Rp.startBackground();
|
||||
}
|
||||
|
||||
void initDatabase(Config cfg) {
|
||||
Db = new DB(DBSettings.fromJson(cfg.v["db"]));
|
||||
Db.connect();
|
||||
}
|
||||
|
||||
void main() {
|
||||
commonInit();
|
||||
|
||||
scope (exit)
|
||||
RP.stop();
|
||||
scope (exit) {
|
||||
Db.close();
|
||||
Rp.stop();
|
||||
}
|
||||
|
||||
JSONValue js = requestAcct("localhost:8080", "admin");
|
||||
generateRSA();
|
||||
return;
|
||||
|
||||
infoF!"Response body: %s"(js.toJSON(true));
|
||||
Actor marisa = apResolveRemoteUsername("@admin@localhost:8080");
|
||||
infoF!"Actor type: %s"(marisa.type);
|
||||
infoF!"Actor preferredUsername: %s"(marisa.preferredUsername);
|
||||
infoF!"Actor sharedInbox: %s"(marisa.endpoints.sharedInbox);
|
||||
|
||||
PRequest followers = PRequest();
|
||||
|
||||
with (followers) {
|
||||
url = marisa.followers;
|
||||
headers["Accept"] = "application/activity+json";
|
||||
}
|
||||
|
||||
Response rs = Rp.request(followers, true);
|
||||
infoF!"followers: %s"(rs.responseBody);
|
||||
}
|
||||
|
||||
//int main() {
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
module net.request_pool;
|
||||
|
||||
import core.thread;
|
||||
import core.sync.semaphore;
|
||||
|
||||
import std.container;
|
||||
import std.parallelism;
|
||||
|
||||
@@ -32,6 +29,7 @@ class RequestPool {
|
||||
for (int i = 0; i < totalWorkers + 1; i++) {
|
||||
Request rq = Request();
|
||||
// TODO: add custom fields (such as user agent)
|
||||
rq.addHeaders(["User-Agent": "apd (development)"]);
|
||||
this.m_requests ~= rq;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,4 +4,4 @@ import db.db;
|
||||
import net.request_pool;
|
||||
|
||||
DB Db;
|
||||
RequestPool RP;
|
||||
RequestPool Rp;
|
||||
|
||||
@@ -3,6 +3,11 @@ module util;
|
||||
import std.format;
|
||||
import std.json;
|
||||
|
||||
import slf4d;
|
||||
|
||||
import deimos.openssl.rsa;
|
||||
import deimos.openssl.pem;
|
||||
|
||||
void optional(T)(ref JSONValue val, string key, ref T receiver) {
|
||||
const(JSONValue)* p = key in val;
|
||||
|
||||
@@ -24,3 +29,18 @@ T optional(T)(ref JSONValue val, string key) {
|
||||
void required(T)(ref JSONValue val, string key, ref T receiver) {
|
||||
receiver = val[key].get!T;
|
||||
}
|
||||
|
||||
void generateRSA() {
|
||||
RSA* rsa = RSA_generate_key(2048, 3, null, null);
|
||||
BIO* bio = BIO_new(BIO_s_mem());
|
||||
PEM_write_bio_RSAPrivateKey(bio, rsa, null, null, 0, null, null);
|
||||
|
||||
int keyLen = BIO_pending(bio);
|
||||
char[] pemKey = new char[keyLen + 1];
|
||||
BIO_read(bio, pemKey.ptr, keyLen);
|
||||
|
||||
BIO_free_all(bio);
|
||||
RSA_free(rsa);
|
||||
|
||||
infoF!"private key: %s"(pemKey);
|
||||
}
|
||||
|
||||
@@ -18,11 +18,15 @@ private bool checkValidWebfingerResponse(Response rs) {
|
||||
return AcceptedWebfingerContentType.canFind(rs.responseHeaders()["content-type"].split(";")[0]);
|
||||
}
|
||||
|
||||
PRequest buildRequest(string uri, string[string] params) {
|
||||
private PRequest buildRequest(string uri, string[string] params) {
|
||||
PRequest rq = PRequest();
|
||||
string schema = "https";
|
||||
|
||||
if (uri.startsWith("localhost"))
|
||||
schema = "http";
|
||||
|
||||
rq.method = "GET";
|
||||
rq.url = format("https://%s/.well-known/webfinger", uri);
|
||||
rq.url = format("%s://%s/.well-known/webfinger", schema, uri);
|
||||
rq.headers["Accept"] = AcceptedWebfingerContentType;
|
||||
|
||||
QueryParam[] p;
|
||||
@@ -41,8 +45,8 @@ PRequest buildAcctRequest(string uri, string acct) {
|
||||
return buildRequest(uri, params);
|
||||
}
|
||||
|
||||
JSONValue requestAcct(string uri, string acct) {
|
||||
Response rs = RP.request(buildAcctRequest(uri, acct), true);
|
||||
JSONValue wfRequestAccount(string uri, string acct) {
|
||||
Response rs = Rp.request(buildAcctRequest(uri, acct), true);
|
||||
|
||||
if (checkValidWebfingerResponse(rs) == false)
|
||||
throw new Exception("Invalid webfinger response");
|
||||
@@ -50,11 +54,8 @@ JSONValue requestAcct(string uri, string acct) {
|
||||
return parseJSON(rs.responseBody().toString());
|
||||
}
|
||||
|
||||
JSONValue requestAcct(string handle) {
|
||||
string uri, acct;
|
||||
JSONValue wfRequestAccount(string handle) {
|
||||
string[] uriAcct = handle.split("@")[$ - 2 .. $];
|
||||
|
||||
uri = handle.split("@")[$ - 1];
|
||||
acct = handle.split("@")[$ - 2];
|
||||
|
||||
return requestAcct(uri, acct);
|
||||
return wfRequestAccount(uriAcct[1], uriAcct[0]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user