Allow browsing on Apogee via TOR hidden service

master
teknomunk 11 months ago
parent 4351c3ef52
commit 95485f1a38

@ -21,6 +21,22 @@ static struct json_object_field app_args_layout[] = {
#undef OBJ_TYPE
struct app_args* g_server = NULL;
void app_args_refresh_tor_hidden_service()
{
if( !g_server ) { return; }
free( g_server->tor_hidden_service );
g_server->tor_hidden_service = NULL;
FILE* f = fopen("data/tor/hidden_service/hostname","r");
size_t n;
getline( &g_server->tor_hidden_service, &n, f );
n = strlen( g_server->tor_hidden_service );
if( g_server->tor_hidden_service[n-1] == '\n' ) {
g_server->tor_hidden_service[n-1] = '\0';
}
fclose(f);
}
struct app_args* app_args_new( int argc, char** argv )
{
struct app_args* args = (struct app_args*)malloc( sizeof(struct app_args) );
@ -36,14 +52,7 @@ struct app_args* app_args_new( int argc, char** argv )
json_read_object_layout_from_file( "data/server.json", app_args_layout, args );
FILE* f = fopen("data/tor/hidden_service/hostname","r");
size_t n;
getline( &args->tor_hidden_service, &n, f );
n = strlen( args->tor_hidden_service );
if( args->tor_hidden_service[n-1] == '\n' ) {
args->tor_hidden_service[n-1] = '\0';
}
fclose(f);
app_args_refresh_tor_hidden_service();
if( ( argc > 1 ) && ( 0 != strncmp(argv[1],"--",2) ) ) {
handle_command( argv, argc );

@ -42,13 +42,14 @@ failed:
goto cleanup;
}
bool http_request_is_tor_request( struct http_request* req );
bool handle_mastodon_api_show_account( struct http_request* req, struct account* a )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
api_Account_write(a,f,0);
api_Account_write(a,f,0, http_request_is_tor_request(req) );
return true;
}
@ -85,7 +86,7 @@ static bool handle_search( struct http_request* req )
fprintf( f, "[" );
if( a ) {
api_Account_write(a,f,1);
api_Account_write(a,f,1,false);
}
fprintf( f, "]" );

@ -192,9 +192,28 @@ failed:
goto cleanup;
}
bool http_request_is_tor_request( struct http_request* req )
{
if( g_server->tor_hidden_service ) {
const char* host = http_request_get_header( req, "Host" );
if( host ) {
if( 0 == strcmp( host, g_server->tor_hidden_service ) ) {
return true;
}
}
}
return false;
}
// route: /api/v1/
bool route_mastodon_api( struct http_request* req )
{
bool is_tor_request = http_request_is_tor_request(req);
if( is_tor_request ) {
printf( "\tTOR request\n" );
}
if( http_request_route_term( req, "apps" ) ) {
if( http_request_route_method( req, "POST" ) ) {
return handle_mastodon_api_apps(req);

@ -35,6 +35,8 @@ enum account_type
struct account
{
unsigned int id;
void* user;
bool local;
char* handle;
char* server;

@ -5,11 +5,17 @@
#include "util/format.h"
#include "model/account.h"
#include "model/server.h"
#include "view/api/Relationship.h"
#include <string.h>
struct Account_user_data
{
bool is_tor_request;
};
#define OBJ_TYPE struct string_pair
static struct json_object_field emoji_layout[] = {
{
@ -129,6 +135,25 @@ bool int_to_string_callback( void* field_data, bool is_read, char** res )
}
return false;
}
bool account_url_string_callback( void* field_data, bool is_read, char** res )
{
struct account* a = field_data;
struct Account_user_data* ud = a->user;
if( is_read ) { return false; }
if( !ud || !ud->is_tor_request || a->id != owner_account_id ) {
if( !a->account_url ) {
* res = strdup("");
} else {
*res = safe_strdup( a->account_url );
}
return true;
}
*res = aformat( "http://%s/owner/account", g_server->tor_hidden_service );
return true;
}
struct json_object_field api_Account_layout[] = {
{
.key = "acct",
@ -208,8 +233,9 @@ struct json_object_field api_Account_layout[] = {
},
{
.key = "url",
.offset = offsetof( OBJ_TYPE, account_url ),
.type = &json_field_string
//.offset = offsetof( OBJ_TYPE, account_url ),
.type = &json_field_callback_string,
.string_callback = account_url_string_callback,
},
{
.key = "username",
@ -245,13 +271,18 @@ struct json_field_type api_Account_type = {
.layout = api_Account_layout,
};
void api_Account_write( struct account* a, FILE* f, int indent )
void api_Account_write( struct account* a, FILE* f, int indent, bool is_tor_request )
{
struct Account_user_data ud;
ud.is_tor_request = is_tor_request;
struct json_writer jw = {
.f = f,
.indentation = "\t",
.indent = indent,
.user = &ud,
};
a->user = &ud;
json_write_pretty_object_layout( &jw, api_Account_layout, a );
}

@ -1,10 +1,11 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
struct json_field_type;
struct account;
extern struct json_field_type api_Account_type;
void api_Account_write( struct account* a, FILE* f, int indent );
void api_Account_write( struct account* a, FILE* f, int indent, bool is_tor_request );

@ -278,6 +278,8 @@ static struct json_field_type EmojiReact_type = {
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 ) {

@ -81,7 +81,11 @@
"thumbnail":"https://pl.polaris-1.work/media/27313e491a834971b8d868e121fcf3e279b109dca0351b87310a2a367b3a6237.png",
"title":"%s{g_server->domain}",
"upload_limit":1600000000,
"uri":"https:/%s{g_server->domain}",
%( if( is_tor_request ) { )
"uri":"http://%s{g_server->tor_hidden_service}",
%( } else { )
"uri":"https://%s{g_server->domain}",
%( } )
"urls":{
%(/*"streaming_api":"wss://pl.polaris-1.work"*/)
},

Loading…
Cancel
Save