Updat http/ffdb submodules, start integrating peer module

master
teknomunk 11 months ago
parent cdf9d17697
commit 5c8e403765

@ -14,6 +14,10 @@
#include "model/account.h"
#include "model/activity.h"
#include "model/outbox_envelope.h"
#include "model/peer.h"
// Submodules
#include "http/url.h"
// Stdlib
#include <stdlib.h>
@ -70,25 +74,42 @@ static bool process_envelope( struct outbox_envelope* env )
FILE* f = NULL;
struct ap_object* act = NULL;
struct account* to_account = NULL;
struct peer* p = NULL;
if( env->sent ) { return false; }
if( env->retry_after > time(NULL) ) { return false; }
if( env->account_id == 0 ) { goto discard; }
// Get outbox URL
to_account = account_from_id( env->account_id );
// Get inbox url
const char* inbox = to_account->inbox;
if( env->shared_inbox ) {
inbox = env->shared_inbox;
}
// Try to get peer information
char domain[512];
url_get_domain( inbox, domain,sizeof(domain) );
p = peer_create_from_domain( domain );
printf( "Processing outbox/%d.json\n", env->id );
printf( "shared_inbox=%s\b", env->shared_inbox );
printf( "inbox=%s\b", inbox );
printf( "account_id=%d\n", env->account_id );
printf( "activity_id=%d\n", env->activity_id );
if( p && p->admin_disable_outbox ) {
printf( "Delivery of activities to %s is administratively disabled\n", inbox );
goto discard;
}
// Check blacklist if at least one attempt to deliver has been made
if( ( env->retries > 0 ) && blacklisted( env ) ) {
printf( "Matched host blacklist\n" );
goto discard;
}
// Get outbox URL
to_account = account_from_id( env->account_id );
// Load crypto keys
keys = crypto_keys_new();
if( !crypto_keys_load_private( keys, "data/owner/private.pem" ) ) {
@ -119,11 +140,6 @@ static bool process_envelope( struct outbox_envelope* env )
postdata[size] = '\0';
printf( "post: %s\n", postdata );
const char* inbox = to_account->inbox;
if( env->shared_inbox ) {
inbox = env->shared_inbox;
}
struct http_signature hs;
memset( &hs, 0, sizeof(hs) );
hs.input.url = inbox;
@ -178,6 +194,10 @@ static bool process_envelope( struct outbox_envelope* env )
if( status_code == 200 || status_code == 202 ) {
printf( "Submitted successfully\n" );
if( p ) {
p->last_successful_delivery = time(NULL);
peer_save(p);
}
goto discard;
} else {
printf( "\nServer returned status code %ld\n", status_code );
@ -191,6 +211,10 @@ static bool process_envelope( struct outbox_envelope* env )
}
env->retry_after = time(NULL) + 60 * ( env->retries + 1 ) * ( env->retries + 1 );
outbox_envelope_save(env);
if( p ) {
p->last_failed_delivery = time(NULL);
peer_save(p);
}
}
goto failed;
@ -199,6 +223,7 @@ cleanup:
ap_object_free(act);
account_free(to_account);
free(postdata);
peer_free(p);
crypto_keys_free(keys);
if( f ) { fclose(f); }

@ -1 +1 @@
Subproject commit 9465f2748facb2d38d8cfb6157a30f8958526180
Subproject commit fee0874bb1517f7da546ca99fd7c3b9497ed54db

@ -1 +1 @@
Subproject commit 99e2cda6bb906d6effdf0a386f2665d206109804
Subproject commit e7caba38aee79997c147ca718b69bb16795eb827

@ -19,6 +19,36 @@
#include <stdlib.h>
#include <unistd.h>
static size_t handle_header( char* header, size_t size, size_t nitems, void* user )
{
int bytes = size * nitems;
int result = bytes;
printf( "? Header: |%.*s|\n", bytes, header );
if( 0 != strncmp("onion-location: ",header,sizeof("onion-location: ")-1 ) ) {
return result;
}
header += sizeof("onion-location: ")-1;
bytes -= sizeof("onion-location: ")-1;
for( int i = 0; i < bytes; ++i ) {
if( header[i] == '/' ) {
header[i] = '\0';
break;
}
}
char* onion_host = strndup( header, bytes );
printf( "+ Onion Host: |%s|\n", onion_host );
free(onion_host);
return result;
}
static bool do_fetch( const char* uri, FILE* result )
{
char user_agent[512];
@ -31,6 +61,7 @@ static bool do_fetch( const char* uri, FILE* result )
HTTP_REQ_HEADER, user_agent,
HTTP_REQ_OUTFILE, result,
HTTP_REQ_RESULT_STATUS, &status_code,
HTTP_RES_HEADER_CALLBACK, handle_header,
NULL,
};
printf( "GET %s\n", uri );

@ -9,6 +9,8 @@
#include <string.h>
#include <sys/stat.h>
#define PEERS_BY_DOMAIN "data/peers/by_domain"
#define OBJ_TYPE struct peer
static struct json_object_field peer_layout[] = {
JSON_FIELD_STRING( domain, true ),
@ -17,7 +19,7 @@ static struct json_object_field peer_layout[] = {
JSON_FIELD_DATETIME( last_successful_delivery, false ),
JSON_FIELD_DATETIME( last_failed_delivery, false ),
JSON_FIELD_BOOL( admin_disable, false ),
JSON_FIELD_BOOL( admin_disable_outbox, false ),
JSON_FIELD_END,
};
@ -42,13 +44,85 @@ struct peer* peer_from_id( int id )
struct peer* p = peer_new();
char filename[512];
snprintf( filename,sizeof(filename), "data/peer/%d.json", id );
snprintf( filename,sizeof(filename), "data/peers/%d.json", id );
if( !json_read_object_layout_from_file( filename, peer_layout, p ) ) {
peer_free(p);
return NULL;
}
p->id = id;
return p;
}
struct peer* peer_from_domain( const char* domain )
{
struct peer* res = NULL;
char* id_str = ffdb_trie_get( PEERS_BY_DOMAIN, domain );
if( !id_str ) { return NULL; }
int id = 0;
if( sscanf( id_str, "%d", &id ) ) {
res = peer_from_id(id);
}
free(id_str);
// Make sure the index is still valid
if( 0 == strcmp(res->domain,domain) ) {
return res;
}
if( 0 == strcmp(res->tor_hidden_service,domain) ) {
return res;
}
// Nope. Index entry is invalid, clear
ffdb_trie_set( PEERS_BY_DOMAIN, domain, NULL );
peer_free(res);
return NULL;
}
void peer_save( struct peer* p )
{
char filename[512];
if( !p->id ) {
p->id = fs_list_inc( "data/peers/HEAD" );
}
snprintf( filename,sizeof(filename), "data/peers/%d.json", p->id );
json_write_object_layout_to_file( filename, "\t", peer_layout, p );
char id_str[32];
snprintf( id_str,sizeof(id_str), "%d", p->id );
if( p->domain ) {
ffdb_trie_set( PEERS_BY_DOMAIN, p->domain, id_str );
}
if( p->tor_hidden_service ) {
ffdb_trie_set( PEERS_BY_DOMAIN, p->tor_hidden_service, id_str );
}
}
struct peer* peer_create_from_domain( const char* domain )
{
struct peer* p = peer_from_domain(domain);
if( p ) { return p; }
p = peer_new();
p->domain = strdup(domain);
peer_save(p);
return p;
}
void peer_free( struct peer* p )
{
if( !p ) { return; }
free( p->domain );
free( p->tor_hidden_service );
free(p);
}

@ -5,11 +5,13 @@
struct peer
{
int id;
char* domain;
char* tor_hidden_service;
time_t last_successful_delivery;
time_t last_failed_delivery;
bool admin_disable;
bool admin_disable_outbox;
};
void peer_model_init();
@ -17,6 +19,7 @@ void peer_model_init();
struct peer* peer_new();
struct peer* peer_from_id( int id );
struct peer* peer_from_domain( const char* domain );
struct peer* peer_free( struct peer* p );
struct peer* peer_create_from_domain( const char* domain );
void peer_save( struct peer* p );
void peer_free( struct peer* p );

@ -594,7 +594,6 @@ void status_save( struct status* s )
{
char filename[512];
json_write_object_layout_to_file( get_status_data_filename(s->id,filename,sizeof(filename)), "\t", status_layout, s );
//printf( "Written to %s\n", filename );
// Index the status
if( s->url ) {

@ -12,6 +12,7 @@
#include "json/json.h"
#include "json/layout.h"
#include "util/format.h"
#include "http/url.h"
#include <string.h>
@ -276,29 +277,6 @@ static struct json_field_type EmojiReact_type = {
.size = sizeof(struct status_react*),
};
static bool get_hostname_from_url( const char* url, char* buffer, int limit )
{
if( !url ) { return false; }
if( strstr(url,"https://") == url ) {
url += 8;
} else if( strstr(url,"http://") == url ) {
url += 7;
} else {
return false;
}
//printf( "url=%s\n", url );
const char* finish = strchr(url,'/');
if( finish ) {
int len = finish - url;
if( limit > len ) { limit = len; }
}
//printf( "limit=%d\n", limit );
strncpy( buffer, url, limit );
return true;
}
bool render_source_callback( void* field_data, bool is_read, char** res )
{
struct status* s = field_data;
@ -306,7 +284,7 @@ bool render_source_callback( void* field_data, bool is_read, char** res )
char hostname[128];
memset(hostname,0,sizeof(hostname));
const char* default_server = NULL;
if( get_hostname_from_url( s->url, hostname, sizeof(hostname) ) ) {
if( url_get_domain( s->url, hostname, sizeof(hostname) ) ) {
default_server = hostname;
}

Loading…
Cancel
Save