Compare commits

..

45 Commits

Author SHA1 Message Date
53e75d8209 cleanup: Consolidate countof macros.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 20m32s
2024-11-13 20:22:42 -05:00
5bdf970c10 ssb: Don't list broadcasts for identities to which we are already connected.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled
2024-11-13 19:23:04 -05:00
50089f72c6 ssb: We can show what state a connection is in. 2024-11-13 19:15:59 -05:00
62e15e0208 update: CodeMirror. 2024-11-13 19:03:01 -05:00
3d8b02a7f3 ssb+issues+core: prettier 2024-11-13 18:58:09 -05:00
20701d9cf1 ssb: Missed a few connection failures for context.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 18m5s
2024-11-13 18:44:14 -05:00
fa94442eb2 ssb: Populate more connection errors with context. 2024-11-13 18:35:17 -05:00
68ff77e172 ssb: Hook up connect error messages more thoroughly. 2024-11-13 18:20:14 -05:00
102e9be3a8 update: c-ares 1.34.3. 2024-11-13 17:54:10 -05:00
92bf01a183 ssb+issues: Fix missing dependencies of my commonmarkjs extensions.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 18m56s
2024-11-12 21:47:15 -05:00
559504ae29 security: Use commonmarkjs with {safe: true} as intended.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled
2024-11-12 20:43:03 -05:00
9b00b41a1e ssb: Connection result preliminary hookup to ui, and fix some fallout.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 18m5s
2024-11-11 22:24:54 -05:00
b1f6ad17e1 ssb: Pass around reasons for failing to connect. This will help get that information to the ui when I finish hooking it up. 2024-11-11 22:12:41 -05:00
e7979fe9db test: Add more retries until selenium cooperates.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 17m51s
2024-11-11 21:06:04 -05:00
7a276adbbc ssb: Size blob ID buffers appropriately. 2024-11-11 21:05:29 -05:00
db4997fdc4 bot: Remove the header.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 18m7s
2024-11-11 08:19:46 -05:00
44ebb841f0 bot: Fix empty buttfeed posts, and use requested RSS feed for Habitat.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 17m35s
2024-11-10 19:30:36 -05:00
09ae4e2096 ssb: Handle the inevitable %25 in a document hash better?
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 16m14s
2024-11-09 18:04:58 -05:00
0b46efe4ea test: Hack around an intermitted -t=auto failure.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 17m49s
2024-11-09 15:09:08 -05:00
f1dda43e66 ui: Click off the identity menu to close it.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled
2024-11-09 14:52:54 -05:00
ce483138d7 ssb: Fighting with profile CSS. 2024-11-09 14:41:40 -05:00
73cc39226d bot: Some fixes to get SecureScuttlebuttFeed running.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 16m26s
2024-11-09 09:24:13 -05:00
57257f63dd bot: Add a little script to post about recent development activity from a handful of RSS feeds I've gathered.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 17m1s
2024-11-09 09:01:34 -05:00
88b25790e8 ssb: Remove some pointless logging.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 19m3s
2024-11-06 20:56:10 -05:00
e01defc4aa update: CodeMirror. 2024-11-06 20:49:03 -05:00
cb50c43e93 build: We all cope in our own ways.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled
2024-11-06 20:42:49 -05:00
5908d15f91 js: Move default global settings to C.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled
2024-11-06 20:29:48 -05:00
f66cfaec12 http: Fix some caching issues.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 17m8s
2024-11-06 12:41:54 -05:00
259f92c53b http: Populate query and headers for handler.js like we used to.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 16m27s
2024-11-04 21:46:38 -05:00
a84f850e91 http: Bring back handler.js support, mostly. Partly in C, this time.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 16m25s
2024-11-03 21:09:57 -05:00
5a765e6f07 js: Also unused. 2024-11-03 07:44:31 -05:00
791889c659 js: Remove some unused code. 2024-11-03 07:38:52 -05:00
5da63faf1f http+js: Move app blob handling from JS to C. handler.js support has been temporarily removed. 2024-11-02 21:37:14 -04:00
30d108fc35 http: URL pattern matcher fixes. 2024-11-02 20:10:55 -04:00
a09fefab5e http: Add a more expressive but still nowhere near regex URL pattern matcher.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 14m57s
2024-11-02 19:22:04 -04:00
f74ca1c236 test: Remove some debug prints, whoops.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 15m13s
2024-11-02 16:32:05 -04:00
30e027092b test: Cover more ways to request apps and files.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 15m15s
2024-11-02 15:43:03 -04:00
fd4ac7c9b9 test: Test some expectes results from http requests to various paths.
Some checks failed
Build Tilde Friends / Build-All (push) Failing after 9m46s
2024-11-02 14:11:54 -04:00
4482049b94 log: Show the version number in the welcome banner.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 15m53s
2024-11-02 08:45:47 -04:00
5839380437 update: CodeMirror to latest. 2024-11-02 08:45:47 -04:00
2152470fdc update: libbacktrace to latest. 2024-11-02 08:45:47 -04:00
93b2a81495 test: Fix -t=publish on haiku.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 16m30s
2024-11-01 18:55:27 -04:00
e139e952c0 ssb: Fix some spacing in the permissions dialog.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 17m25s
2024-11-01 18:18:16 -04:00
cf1c57ccb8 build: Let's start work on 0.0.25.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 16m50s
2024-11-01 18:01:10 -04:00
f7a2138488 nix: Update version to 0.0.24. 2024-10-30 19:40:12 -04:00
49 changed files with 1057 additions and 602 deletions

View File

@ -3,9 +3,9 @@
MAKEFLAGS += --warn-undefined-variables MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules MAKEFLAGS += --no-builtin-rules
VERSION_CODE := 29 VERSION_CODE := 30
VERSION_NUMBER := 0.0.24 VERSION_NUMBER := 0.0.25-wip
VERSION_NAME := Honey bunches of boats. VERSION_NAME := This program kills fascists.
SQLITE_URL := https://www.sqlite.org/2024/sqlite-amalgamation-3470000.zip SQLITE_URL := https://www.sqlite.org/2024/sqlite-amalgamation-3470000.zip
BUNDLETOOL_URL := https://github.com/google/bundletool/releases/download/1.17.0/bundletool-all-1.17.0.jar BUNDLETOOL_URL := https://github.com/google/bundletool/releases/download/1.17.0/bundletool-all-1.17.0.jar

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "📜", "emoji": "📜",
"previous": "&miGORZ8BwjHg2YO0t4bms6SI28XWPYqnqOZ8u9zsbZc=.sha256" "previous": "&BEf0nraBdHk/+PWqx6tOSu5rheWVaxaL7orAOz3285M=.sha256"
} }

View File

@ -21,7 +21,7 @@ function* treeify(prefix, o) {
function markdown(md) { function markdown(md) {
let parsed = new commonmark.Parser().parse(md ?? '*undocumented*'); let parsed = new commonmark.Parser().parse(md ?? '*undocumented*');
return new commonmark.HtmlRenderer().render(parsed); return new commonmark.HtmlRenderer({safe: true}).render(parsed);
} }
function document(api) { function document(api) {

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🪵", "emoji": "🪵",
"previous": "&TIrBnpN3iz3O9L9MCCteAcVJZjA83EKdcfu4SCM76VE=.sha256" "previous": "&3jabNEk6W2uolzTvfXX6fcWF50N3501vtgZ6ZxFVJ1s=.sha256"
} }

View File

@ -52,8 +52,8 @@ export async function get_blog_message(id) {
} }
export function markdown(md) { export function markdown(md) {
let reader = new commonmark.Parser({safe: true}); let reader = new commonmark.Parser();
let writer = new commonmark.HtmlRenderer(); let writer = new commonmark.HtmlRenderer({safe: true});
let parsed = reader.parse(md || ''); let parsed = reader.parse(md || '');
let walker = parsed.walker(); let walker = parsed.walker();
let event, node; let event, node;

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🦟", "emoji": "🦟",
"previous": "&cUqvSDUls3jn0haD85LPFAGdkc8wFuy347TtATNcJgg=.sha256" "previous": "&O0huuEgL/UQC9bkUfhPOyZFo9eRiz+koOkba6QwCGNA=.sha256"
} }

View File

@ -1,5 +1,11 @@
import * as linkify from './commonmark-linkify.js'; import * as linkify from './commonmark-linkify.js';
var reUnsafeProtocol = /^javascript:|vbscript:|file:|data:/i;
var reSafeDataProtocol = /^data:image\/(?:png|gif|jpeg|webp)/i;
var potentiallyUnsafe = function (url) {
return reUnsafeProtocol.test(url) && !reSafeDataProtocol.test(url);
};
function image(node, entering) { function image(node, entering) {
if ( if (
node.firstChild?.type === 'text' && node.firstChild?.type === 'text' &&
@ -61,8 +67,8 @@ function image(node, entering) {
} }
export function markdown(md) { export function markdown(md) {
var reader = new commonmark.Parser({safe: true}); var reader = new commonmark.Parser();
var writer = new commonmark.HtmlRenderer(); var writer = new commonmark.HtmlRenderer({safe: true});
writer.image = image; writer.image = image;
var parsed = reader.parse(md || ''); var parsed = reader.parse(md || '');
parsed = linkify.transform(parsed); parsed = linkify.transform(parsed);

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "📝", "emoji": "📝",
"previous": "&b//KqE4Vx6kOSBRODK1p/8wjOLKZJ+CBB5IkaBt5YsM=.sha256" "previous": "&5LpOTEnor/rYFk3axyfmmehAoq9aEwNQRH4jwNhRQ7o=.sha256"
} }

View File

@ -18,8 +18,8 @@ class TfJournalEntryElement extends LitElement {
} }
markdown(md) { markdown(md) {
var reader = new commonmark.Parser({safe: true}); var reader = new commonmark.Parser();
var writer = new commonmark.HtmlRenderer(); var writer = new commonmark.HtmlRenderer({safe: true});
var parsed = reader.parse(md || ''); var parsed = reader.parse(md || '');
return writer.render(parsed); return writer.render(parsed);
} }

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🐌", "emoji": "🐌",
"previous": "&IH3DadMNF785idnMI/LuCpIJQxzvpg1PDp8BI7m1Nx0=.sha256" "previous": "&tL1A9Kt6d8wGwBFPAq5V5GtMqOatVCzuLIG6wwJhdyI=.sha256"
} }

View File

@ -67,7 +67,7 @@ class TfElement extends LitElement {
} }
set_hash(hash) { set_hash(hash) {
this.hash = hash || '#'; this.hash = decodeURIComponent(hash || '#');
if (this.hash.startsWith('#q=')) { if (this.hash.startsWith('#q=')) {
this.tab = 'search'; this.tab = 'search';
} else if (this.hash === '#connections') { } else if (this.hash === '#connections') {

View File

@ -289,16 +289,10 @@ class TfProfileElement extends LitElement {
typeof profile.image == 'string' ? profile.image : profile.image?.link; typeof profile.image == 'string' ? profile.image : profile.image?.link;
image = this.editing?.image ?? image; image = this.editing?.image ?? image;
let description = this.editing?.description ?? profile.description; let description = this.editing?.description ?? profile.description;
return html`<div style="border: 2px solid black; background-color: rgba(255, 255, 255, 0.2); padding: 16px"> return html`<div class="w3-container" style="box-sizing: border-box; border: 2px solid black; background-color: rgba(255, 255, 255, 0.2)">
<tf-user id=${this.id} .users=${this.users}></tf-user> (${tfutils.human_readable_size(this.size)}) <p><tf-user id=${this.id} .users=${this.users}></tf-user> (${tfutils.human_readable_size(this.size)})
<div class="w3-row"> <input type="text" class="w3-input w3-border w3-theme-d1" readonly value=${this.id}></input>
<div class="w3-col s1 w3-container w3-right"> <button class="w3-button w3-theme-d1 w3-ripple" @click=${this.copy_id}>Copy</button>
<button class="w3-button w3-theme-d1 w3-ripple" @click=${this.copy_id}>Copy</button>
</div>
<div class="w3-rest w3-container">
<input type="text" class="w3-theme-d1" style="width: 100%; vertical-align: middle" readonly value=${this.id}></input>
</div>
</div>
<div style="display: flex; flex-direction: row; gap: 1em"> <div style="display: flex; flex-direction: row; gap: 1em">
${edit_profile} ${edit_profile}
<div style="flex: 1 0 50%"> <div style="flex: 1 0 50%">
@ -312,11 +306,11 @@ class TfProfileElement extends LitElement {
Blocking ${profile.blocking} identities. Blocking ${profile.blocking} identities.
Blocked by ${profile.blocked} identities. Blocked by ${profile.blocked} identities.
</div> </div>
<div> <p>
${edit} ${edit}
${follow} ${follow}
${block} ${block}
</div> </p>
</div>`; </div>`;
} }
} }

View File

@ -12,6 +12,9 @@ class TfTabConnectionsElement extends LitElement {
stored_connections: {type: Array}, stored_connections: {type: Array},
users: {type: Object}, users: {type: Object},
server_identity: {type: String}, server_identity: {type: String},
connect_attempt: {type: Object},
connect_message: {type: String},
connect_success: {type: Boolean},
}; };
} }
@ -88,20 +91,36 @@ class TfTabConnectionsElement extends LitElement {
`; `;
} }
render_message(connection) {
return html`<div
?hidden=${this.connect_message === undefined ||
this.connect_attempt != connection}
style="cursor: pointer"
class=${'w3-panel ' + (this.connect_success ? 'w3-green' : 'w3-red')}
@click=${() => (this.connect_attempt = undefined)}
>
<p>${this.connect_message}</p>
</div>`;
}
render_broadcast(connection) { render_broadcast(connection) {
let self = this;
return html` return html`
<li class="w3-bar" style="overflow: hidden; overflow-wrap: nowrap"> <li>
<button <div class="w3-bar" style="overflow: hidden; overflow-wrap: nowrap">
class="w3-bar-item w3-button w3-theme-d1" <button
@click=${() => tfrpc.rpc.connect(connection)} class="w3-bar-item w3-button w3-theme-d1"
> @click=${() => self.connect(connection)}
Connect >
</button> Connect
<div class="w3-bar-item"> </button>
${TfTabConnectionsElement.k_broadcast_emojis[connection.origin]} <div class="w3-bar-item">
<tf-user id=${connection.pubkey} .users=${this.users}></tf-user> ${TfTabConnectionsElement.k_broadcast_emojis[connection.origin]}
${this.render_connection_summary(connection)} <tf-user id=${connection.pubkey} .users=${this.users}></tf-user>
${this.render_connection_summary(connection)}
</div>
</div> </div>
${this.render_message(connection)}
</li> </li>
`; `;
} }
@ -163,6 +182,27 @@ class TfTabConnectionsElement extends LitElement {
tfrpc.rpc.sync(); tfrpc.rpc.sync();
} }
connect(address) {
let self = this;
self.connect_attempt = address;
self.connect_message = undefined;
self.connect_success = false;
tfrpc.rpc
.connect(address)
.then(function () {
if (self.connect_attempt == address) {
self.connect_message = 'Connected.';
self.connect_success = true;
}
})
.catch(function (error) {
if (self.connect_attempt == address) {
self.connect_message = 'Error: ' + error;
self.connect_success = false;
}
});
}
render() { render() {
let self = this; let self = this;
return html` return html`
@ -175,10 +215,11 @@ class TfTabConnectionsElement extends LitElement {
</button> </button>
<h2>New Connection</h2> <h2>New Connection</h2>
<textarea class="w3-input w3-theme-d1" id="code"></textarea> <textarea class="w3-input w3-theme-d1" id="code"></textarea>
${this.render_message(this.renderRoot.getElementById('code')?.value)}
<button <button
class="w3-button w3-theme-d1" class="w3-button w3-theme-d1"
@click=${() => @click=${() =>
tfrpc.rpc.connect(self.renderRoot.getElementById('code').value)} self.connect(self.renderRoot.getElementById('code')?.value)}
> >
Connect Connect
</button> </button>
@ -186,6 +227,7 @@ class TfTabConnectionsElement extends LitElement {
<ul class="w3-ul w3-border"> <ul class="w3-ul w3-border">
${this.broadcasts ${this.broadcasts
.filter((x) => x.address) .filter((x) => x.address)
.filter((x) => self.connections.map(c => c.id).indexOf(x.pubkey) == -1)
.map((x) => self.render_broadcast(x))} .map((x) => self.render_broadcast(x))}
</ul> </ul>
<h2>Connections</h2> <h2>Connections</h2>
@ -202,23 +244,26 @@ class TfTabConnectionsElement extends LitElement {
<ul class="w3-ul w3-border"> <ul class="w3-ul w3-border">
${this.stored_connections.map( ${this.stored_connections.map(
(x) => html` (x) => html`
<li class="w3-bar"> <li>
<button <div class="w3-bar">
class="w3-bar-item w3-button w3-theme-d1" <button
@click=${() => self.forget_stored_connection(x)} class="w3-bar-item w3-button w3-theme-d1"
> @click=${() => self.forget_stored_connection(x)}
Forget >
</button> Forget
<button </button>
class="w3-bar-item w3-button w3-theme-d1" <button
@click=${() => tfrpc.rpc.connect(x)} class="w3-bar-item w3-button w3-theme-d1"
> @click=${() => this.connect(x)}
Connect >
</button> Connect
<div class="w3-bar-item"> </button>
<tf-user id=${x.pubkey} .users=${self.users}></tf-user> <div class="w3-bar-item">
<div><small>${x.address}:${x.port}</small></div> <tf-user id=${x.pubkey} .users=${self.users}></tf-user>
<div><small>${x.address}:${x.port}</small></div>
</div>
</div> </div>
${this.render_message(x)}
</li> </li>
` `
)} )}

View File

@ -2,6 +2,12 @@ import * as hashtagify from './commonmark-hashtag.js';
const k_code_classes = 'w3-theme-l4 w3-theme-border w3-round'; const k_code_classes = 'w3-theme-l4 w3-theme-border w3-round';
var reUnsafeProtocol = /^javascript:|vbscript:|file:|data:/i;
var reSafeDataProtocol = /^data:image\/(?:png|gif|jpeg|webp)/i;
var potentiallyUnsafe = function (url) {
return reUnsafeProtocol.test(url) && !reSafeDataProtocol.test(url);
};
function image(node, entering) { function image(node, entering) {
if ( if (
node.firstChild?.type === 'text' && node.firstChild?.type === 'text' &&
@ -81,8 +87,8 @@ function attrs(node) {
} }
export function markdown(md) { export function markdown(md) {
let reader = new commonmark.Parser({safe: true}); let reader = new commonmark.Parser();
let writer = new commonmark.HtmlRenderer(); let writer = new commonmark.HtmlRenderer({safe: true});
writer.image = image; writer.image = image;
writer.code = code; writer.code = code;
writer.attrs = attrs; writer.attrs = attrs;

4
apps/test.json Normal file
View File

@ -0,0 +1,4 @@
{
"type": "tildefriends-app",
"emoji": "📦"
}

3
apps/test/app.js Normal file
View File

@ -0,0 +1,3 @@
app.setDocument(
'<p style="color: #fff">Maybe one day this app will run tests, but for now there is nothing to see here.</p>'
);

1
apps/test/hello.txt Normal file
View File

@ -0,0 +1 @@
Hello, world!

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "📝", "emoji": "📝",
"previous": "&DaYqKHRBKhjFGaOzbKZ1+/pLspJeEkDJYTF2B50tH6k=.sha256" "previous": "&4F4D8+QlJVaxXywChQrNTdSV4Y3TvJ0xxqdq/i9HUWA=.sha256"
} }

View File

@ -2,8 +2,8 @@ import * as utils from './utils.js';
import * as commonmark from './commonmark.min.js'; import * as commonmark from './commonmark.min.js';
function markdown(md) { function markdown(md) {
let reader = new commonmark.Parser({safe: true}); let reader = new commonmark.Parser();
let writer = new commonmark.HtmlRenderer(); let writer = new commonmark.HtmlRenderer({safe: true});
let parsed = reader.parse(md || ''); let parsed = reader.parse(md || '');
let walker = parsed.walker(); let walker = parsed.walker();
let event; let event;

View File

@ -20,8 +20,8 @@ class TfWikiDocElement extends LitElement {
} }
markdown(md) { markdown(md) {
let reader = new commonmark.Parser({safe: true}); let reader = new commonmark.Parser();
let writer = new commonmark.HtmlRenderer(); let writer = new commonmark.HtmlRenderer({safe: true});
let parsed = reader.parse(md || ''); let parsed = reader.parse(md || '');
let walker = parsed.walker(); let walker = parsed.walker();
let event; let event;

View File

@ -162,6 +162,10 @@ class TfNavigationElement extends LitElement {
class="w3-dropdown-content w3-bar-block w3-card-4" class="w3-dropdown-content w3-bar-block w3-card-4"
style="max-width: 100%; right: 0" style="max-width: 100%; right: 0"
> >
<div
style="position: fixed; left: 0; right: 0; top: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.25); z-index: -100"
@click=${self.toggle_id_dropdown}
></div>
<button <button
class="w3-bar-item w3-button w3-border" class="w3-bar-item w3-button w3-border"
@click=${() => (window.location.href = '/~core/identity')} @click=${() => (window.location.href = '/~core/identity')}
@ -1174,7 +1178,7 @@ function api_requestPermission(permission, id) {
let div = document.createElement('div'); let div = document.createElement('div');
div.appendChild( div.appendChild(
document.createTextNode('This app is requesting the following permission:') document.createTextNode('This app is requesting the following permission: ')
); );
let span = document.createElement('span'); let span = document.createElement('span');
span.style = 'font-weight: bold'; span.style = 'font-weight: bold';
@ -1190,6 +1194,7 @@ function api_requestPermission(permission, id) {
check.classList.add('w3-check'); check.classList.add('w3-check');
check.classList.add('w3-blue'); check.classList.add('w3-blue');
div.appendChild(check); div.appendChild(check);
div.appendChild(document.createTextNode(' '));
let label = document.createElement('label'); let label = document.createElement('label');
label.htmlFor = check.id; label.htmlFor = check.id;
label.appendChild(document.createTextNode('Remember this decision.')); label.appendChild(document.createTextNode('Remember this decision.'));

View File

@ -4,89 +4,6 @@ import * as http from './http.js';
let gProcesses = {}; let gProcesses = {};
let gStatsTimer = false; let gStatsTimer = false;
const k_content_security_policy =
'sandbox allow-downloads allow-top-navigation-by-user-activation';
const k_global_settings = {
index: {
type: 'string',
default_value: '/~core/apps/',
description: 'Default path.',
},
index_map: {
type: 'textarea',
default_value: undefined,
description:
'Mappings from hostname to redirect path, one per line, as in: "www.tildefriends.net=/~core/index/"',
},
room: {
type: 'boolean',
default_value: true,
description: 'Enable peers to tunnel through this instance as a room.',
},
room_name: {
type: 'string',
default_value: 'tilde friends tunnel',
description: 'Name of the room.',
},
replicator: {
type: 'boolean',
default_value: true,
description: 'Enable message and blob replication.',
},
code_of_conduct: {
type: 'textarea',
default_value: undefined,
description: 'Code of conduct presented at sign-in.',
},
http_redirect: {
type: 'string',
default_value: undefined,
description:
'If connecting by HTTP and HTTPS is configured, Location header prefix (ie, "https://example.com")',
},
fetch_hosts: {
type: 'string',
default_value: undefined,
description:
'Comma-separated list of host names to which HTTP fetch requests are allowed. None if empty.',
},
blob_fetch_age_seconds: {
type: 'integer',
default_value:
platform() == 'android' || platform() == 'iphone'
? 0.5 * 365 * 24 * 60 * 60
: undefined,
description:
'Only blobs mentioned more recently than this age will be automatically fetched.',
},
blob_expire_age_seconds: {
type: 'integer',
default_value:
platform() == 'android' || platform() == 'iphone'
? 1.0 * 365 * 24 * 60 * 60
: undefined,
description: 'Blobs older than this will be automatically deleted.',
},
seeds_host: {
type: 'string',
default_value: 'seeds.tildefriends.net',
description: 'Hostname for seed connections.',
},
peer_exchange: {
type: 'boolean',
default_value: false,
description:
'Enable discovery of, sharing of, and connecting to internet peer strangers, including announcing this instance.',
},
account_registration: {
type: 'boolean',
default_value: true,
description: 'Allow registration of new accounts.',
},
};
let kPingInterval = 60 * 1000; let kPingInterval = 60 * 1000;
/** /**
@ -489,7 +406,7 @@ async function getProcessBlob(blobId, key, options) {
}; };
if (process.credentials?.permissions?.administration) { if (process.credentials?.permissions?.administration) {
imports.core.globalSettingsDescriptions = async function () { imports.core.globalSettingsDescriptions = async function () {
let settings = Object.assign({}, k_global_settings); let settings = Object.assign({}, defaultGlobalSettings());
for (let [key, value] of Object.entries(await loadSettings())) { for (let [key, value] of Object.entries(await loadSettings())) {
if (settings[key]) { if (settings[key]) {
settings[key].value = value; settings[key].value = value;
@ -812,206 +729,6 @@ async function getProcessBlob(blobId, key, options) {
return process; return process;
} }
/**
* TODOC
* @param {*} response
* @param {*} data
* @param {*} type
* @param {*} headers
* @param {*} status_code
*/
function sendData(response, data, type, headers, status_code) {
if (data) {
response.writeHead(
status_code ?? 200,
Object.assign(
{
'Content-Type':
type ||
httpd.mime_type_from_magic_bytes(data) ||
'application/binary',
'Content-Length': data.byteLength,
},
headers || {}
)
);
response.end(data);
} else {
response.writeHead(
status_code ?? 404,
Object.assign(
{
'Content-Type': 'text/plain; charset=utf-8',
'Content-Length': 'File not found'.length,
},
headers || {}
)
);
response.end('File not found');
}
}
let g_handler_index = 0;
/**
* TODOC
* @param {*} response
* @param {*} handler_blob_id
* @param {*} path
* @param {*} query
* @param {*} headers
* @param {*} packageOwner
* @param {*} packageName
* @returns
*/
async function useAppHandler(
response,
handler_blob_id,
path,
query,
headers,
packageOwner,
packageName
) {
print('useAppHandler', packageOwner, packageName);
let do_resolve;
let promise = new Promise(async function (resolve, reject) {
do_resolve = resolve;
});
let process;
let result;
try {
process = await getProcessBlob(
handler_blob_id,
'handler_' + g_handler_index++,
{
script: 'handler.js',
imports: {
request: {
path: path,
query: query,
},
respond: do_resolve,
},
credentials: await httpd.auth_query(headers),
packageOwner: packageOwner,
packageName: packageName,
}
);
await process.ready;
result = await promise;
} finally {
if (process?.task) {
await process.task.kill();
}
}
return result;
}
/**
* TODOC
* @param {*} request
* @param {*} response
* @param {*} blobId
* @param {*} uri
* @returns
*/
async function blobHandler(request, response, blobId, uri) {
if (!uri) {
response.writeHead(303, {
Location:
(request.client.tls ? 'https://' : 'http://') +
(request.headers['x-forwarded-host'] ?? request.headers.host) +
blobId +
'/',
'Content-Length': '0',
});
response.end();
return;
}
let process;
let data;
let match;
let id;
let app_id = blobId;
let packageOwner;
let packageName;
if ((match = /^\/\~(\w+)\/(\w+)$/.exec(blobId))) {
packageOwner = match[1];
packageName = match[2];
let db = new Database(match[1]);
app_id = await db.get('path:' + match[2]);
}
let app_object = JSON.parse(utf8Decode(await ssb.blobGet(app_id)));
id = app_object?.files[uri.substring(1)];
if (!id && app_object?.files['handler.js']) {
let answer;
try {
answer = await useAppHandler(
response,
app_id,
uri.substring(1),
request.query ? form.decodeForm(request.query) : undefined,
request.headers,
packageOwner,
packageName
);
} catch (error) {
data = utf8Encode(
`Internal Server Error\n\n${error?.message}\n${error?.stack}`
);
response.writeHead(500, {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Length': data.length,
});
response.end(data);
return;
}
if (answer && typeof answer.data == 'string') {
answer.data = utf8Encode(answer.data);
}
sendData(
response,
answer?.data,
answer?.content_type,
Object.assign(answer?.headers ?? {}, {
'Access-Control-Allow-Origin': '*',
'Content-Security-Policy': k_content_security_policy,
}),
answer.status_code
);
} else if (id) {
if (
request.headers['if-none-match'] &&
request.headers['if-none-match'] == '"' + id + '"'
) {
let headers = {
'Access-Control-Allow-Origin': '*',
'Content-Security-Policy': k_content_security_policy,
'Content-Length': '0',
};
response.writeHead(304, headers);
response.end();
} else {
let headers = {
ETag: '"' + id + '"',
'Access-Control-Allow-Origin': '*',
'Content-Security-Policy': k_content_security_policy,
};
data = await ssb.blobGet(id);
let type =
httpd.mime_type_from_extension(uri) ||
httpd.mime_type_from_magic_bytes(data);
sendData(response, data, type, headers);
}
} else {
sendData(response, data, undefined, {});
}
}
ssb.addEventListener('message', function () { ssb.addEventListener('message', function () {
broadcastEvent('onMessage', [...arguments]); broadcastEvent('onMessage', [...arguments]);
}); });
@ -1037,7 +754,7 @@ async function loadSettings() {
} catch (error) { } catch (error) {
print('Settings not found in database:', error); print('Settings not found in database:', error);
} }
for (let [key, value] of Object.entries(k_global_settings)) { for (let [key, value] of Object.entries(defaultGlobalSettings())) {
if (data[key] === undefined) { if (data[key] === undefined) {
data[key] = value.default_value; data[key] = value.default_value;
} }
@ -1063,6 +780,73 @@ function sendStats() {
} }
} }
let g_handler_index = 0;
exports.callAppHandler = async function callAppHandler(
response,
app_blob_id,
path,
query,
headers,
package_owner,
package_name
) {
let answer;
try {
let do_resolve;
let promise = new Promise(async function (resolve, reject) {
do_resolve = resolve;
});
let process;
try {
process = await getProcessBlob(
app_blob_id,
'handler_' + g_handler_index++,
{
script: 'handler.js',
imports: {
request: {
path: path,
query: query,
},
respond: do_resolve,
},
credentials: await httpd.auth_query(headers),
packageOwner: package_owner,
packageName: package_name,
}
);
await process.ready;
answer = await promise;
} finally {
if (process?.task) {
await process.task.kill();
}
}
} catch (error) {
let data = utf8Encode(
`Internal Server Error\n\n${error?.message}\n${error?.stack}`
);
response.writeHead(500, {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Length': data.length,
});
response.end(data);
return;
}
if (typeof answer?.data == 'string') {
answer.data = utf8Encode(answer.data);
}
response.writeHead(answer?.status_code, {
'Content-Type': answer?.content_type,
'Content-Length': answer?.data?.length,
'Access-Control-Allow-Origin': '*',
'Content-Security-Policy':
'sandbox allow-downloads allow-top-navigation-by-user-activation',
});
response.end(answer?.data);
};
/** /**
* TODOC * TODOC
*/ */
@ -1072,16 +856,6 @@ loadSettings()
httpd.set_http_redirect(settings.http_redirect); httpd.set_http_redirect(settings.http_redirect);
} }
httpd.all('/app/socket', app.socket); httpd.all('/app/socket', app.socket);
httpd.all('', function default_http_handler(request, response) {
let match;
if ((match = /^(\/~[^\/]+\/[^\/]+)(\/?.*)$/.exec(request.uri))) {
return blobHandler(request, response, match[1], match[2]);
} else if (
(match = /^\/([&\%][^\.]{44}(?:\.\w+)?)(\/?.*)/.exec(request.uri))
) {
return blobHandler(request, response, match[1], match[2]);
}
});
let port = httpd.start(tildefriends.http_port); let port = httpd.start(tildefriends.http_port);
if (tildefriends.args.out_http_port_file) { if (tildefriends.args.out_http_port_file) {
print('Writing the port file.'); print('Writing the port file.');

View File

@ -21,14 +21,14 @@
}: }:
pkgs.stdenv.mkDerivation rec { pkgs.stdenv.mkDerivation rec {
pname = "tildefriends"; pname = "tildefriends";
version = "0.0.23"; version = "0.0.24";
src = pkgs.fetchFromGitea { src = pkgs.fetchFromGitea {
domain = "dev.tildefriends.net"; domain = "dev.tildefriends.net";
owner = "cory"; owner = "cory";
repo = "tildefriends"; repo = "tildefriends";
rev = "v${version}"; rev = "v${version}";
hash = "sha256-ukZpi+BXRTFGbdvd5ApmctTo8bjtPJMHjqFPgVSyBWU="; hash = "sha256-XlmRr08UmScY//qxUEXHzagXHCFqARRYr3q8RK/jKFY=";
fetchSubmodules = true; fetchSubmodules = true;
}; };

2
deps/c-ares vendored

@ -1 +1 @@
Subproject commit a57ff692eeab8d21c853dc1ddaf0164f517074c3 Subproject commit c29e75d54c3743783d51a609980495cf553b4bca

File diff suppressed because one or more lines are too long

186
deps/codemirror_src/package-lock.json generated vendored
View File

@ -19,9 +19,9 @@
} }
}, },
"node_modules/@codemirror/autocomplete": { "node_modules/@codemirror/autocomplete": {
"version": "6.18.1", "version": "6.18.3",
"resolved": "https://registry.npmjs.org/@codemirror/autocomplete/-/autocomplete-6.18.1.tgz", "resolved": "https://registry.npmjs.org/@codemirror/autocomplete/-/autocomplete-6.18.3.tgz",
"integrity": "sha512-iWHdj/B1ethnHRTwZj+C1obmmuCzquH29EbcKr0qIjA9NfDeBDJ7vs+WOHsFeLeflE4o+dHfYndJloMKHUkWUA==", "integrity": "sha512-1dNIOmiM0z4BIBwxmxEfA1yoxh1MF/6KPBbh20a5vphGV0ictKlgQsbJs6D6SkR6iJpGbpwRsa6PFMNlg9T9pQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@codemirror/language": "^6.0.0", "@codemirror/language": "^6.0.0",
@ -129,9 +129,9 @@
} }
}, },
"node_modules/@codemirror/search": { "node_modules/@codemirror/search": {
"version": "6.5.6", "version": "6.5.7",
"resolved": "https://registry.npmjs.org/@codemirror/search/-/search-6.5.6.tgz", "resolved": "https://registry.npmjs.org/@codemirror/search/-/search-6.5.7.tgz",
"integrity": "sha512-rpMgcsh7o0GuCDUXKPvww+muLA1pDJaFrpq/CCHtpQJYz8xopu4D1hPcKRoDD0YlF8gZaqTNIRa4VRBWyhyy7Q==", "integrity": "sha512-6+iLsXvITWKHYlkgHPCs/qiX4dNzn8N78YfhOFvPtPYCkuXqZq10rAfsUMhOq7O/1VjJqdXRflyExlfVcu/9VQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@codemirror/state": "^6.0.0", "@codemirror/state": "^6.0.0",
@ -158,9 +158,9 @@
} }
}, },
"node_modules/@codemirror/view": { "node_modules/@codemirror/view": {
"version": "6.34.1", "version": "6.34.2",
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.34.1.tgz", "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.34.2.tgz",
"integrity": "sha512-t1zK/l9UiRqwUNPm+pdIT0qzJlzuVckbTEMVNFhfWkGiBQClstzg+78vedCvLSX0xJEZ6lwZbPpnljL7L6iwMQ==", "integrity": "sha512-d6n0WFvL970A9Z+l9N2dO+Hk9ev4hDYQzIx+B9tCyBP0W5wPEszi1rhuyFesNSkLZzXbQE5FPH7F/z/TMJfoPA==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@codemirror/state": "^6.4.0", "@codemirror/state": "^6.4.0",
@ -370,9 +370,9 @@
} }
}, },
"node_modules/@rollup/rollup-android-arm-eabi": { "node_modules/@rollup/rollup-android-arm-eabi": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.26.0.tgz",
"integrity": "sha512-Q6HJd7Y6xdB48x8ZNVDOqsbh2uByBhgK8PiQgPhwkIw/HC/YX5Ghq2mQY5sRMZWHb3VsFkWooUVOZHKr7DmDIA==", "integrity": "sha512-gJNwtPDGEaOEgejbaseY6xMFu+CPltsc8/T+diUTTbOQLqD+bnrJq9ulH6WD69TqwqWmrfRAtUv30cCFZlbGTQ==",
"cpu": [ "cpu": [
"arm" "arm"
], ],
@ -383,9 +383,9 @@
] ]
}, },
"node_modules/@rollup/rollup-android-arm64": { "node_modules/@rollup/rollup-android-arm64": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.26.0.tgz",
"integrity": "sha512-ijLnS1qFId8xhKjT81uBHuuJp2lU4x2yxa4ctFPtG+MqEE6+C5f/+X/bStmxapgmwLwiL3ih122xv8kVARNAZA==", "integrity": "sha512-YJa5Gy8mEZgz5JquFruhJODMq3lTHWLm1fOy+HIANquLzfIOzE9RA5ie3JjCdVb9r46qfAQY/l947V0zfGJ0OQ==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -396,9 +396,9 @@
] ]
}, },
"node_modules/@rollup/rollup-darwin-arm64": { "node_modules/@rollup/rollup-darwin-arm64": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.26.0.tgz",
"integrity": "sha512-bIv+X9xeSs1XCk6DVvkO+S/z8/2AMt/2lMqdQbMrmVpgFvXlmde9mLcbQpztXm1tajC3raFDqegsH18HQPMYtA==", "integrity": "sha512-ErTASs8YKbqTBoPLp/kA1B1Um5YSom8QAc4rKhg7b9tyyVqDBlQxy7Bf2wW7yIlPGPg2UODDQcbkTlruPzDosw==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -409,9 +409,9 @@
] ]
}, },
"node_modules/@rollup/rollup-darwin-x64": { "node_modules/@rollup/rollup-darwin-x64": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.26.0.tgz",
"integrity": "sha512-X6/nOwoFN7RT2svEQWUsW/5C/fYMBe4fnLK9DQk4SX4mgVBiTA9h64kjUYPvGQ0F/9xwJ5U5UfTbl6BEjaQdBQ==", "integrity": "sha512-wbgkYDHcdWW+NqP2mnf2NOuEbOLzDblalrOWcPyY6+BRbVhliavon15UploG7PpBRQ2bZJnbmh8o3yLoBvDIHA==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -421,10 +421,36 @@
"darwin" "darwin"
] ]
}, },
"node_modules/@rollup/rollup-freebsd-arm64": {
"version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.26.0.tgz",
"integrity": "sha512-Y9vpjfp9CDkAG4q/uwuhZk96LP11fBz/bYdyg9oaHYhtGZp7NrbkQrj/66DYMMP2Yo/QPAsVHkV891KyO52fhg==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"freebsd"
]
},
"node_modules/@rollup/rollup-freebsd-x64": {
"version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.26.0.tgz",
"integrity": "sha512-A/jvfCZ55EYPsqeaAt/yDAG4q5tt1ZboWMHEvKAH9Zl92DWvMIbnZe/f/eOXze65aJaaKbL+YeM0Hz4kLQvdwg==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"freebsd"
]
},
"node_modules/@rollup/rollup-linux-arm-gnueabihf": { "node_modules/@rollup/rollup-linux-arm-gnueabihf": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.26.0.tgz",
"integrity": "sha512-0KXvIJQMOImLCVCz9uvvdPgfyWo93aHHp8ui3FrtOP57svqrF/roSSR5pjqL2hcMp0ljeGlU4q9o/rQaAQ3AYA==", "integrity": "sha512-paHF1bMXKDuizaMODm2bBTjRiHxESWiIyIdMugKeLnjuS1TCS54MF5+Y5Dx8Ui/1RBPVRE09i5OUlaLnv8OGnA==",
"cpu": [ "cpu": [
"arm" "arm"
], ],
@ -435,9 +461,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-arm-musleabihf": { "node_modules/@rollup/rollup-linux-arm-musleabihf": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.26.0.tgz",
"integrity": "sha512-it2BW6kKFVh8xk/BnHfakEeoLPv8STIISekpoF+nBgWM4d55CZKc7T4Dx1pEbTnYm/xEKMgy1MNtYuoA8RFIWw==", "integrity": "sha512-cwxiHZU1GAs+TMxvgPfUDtVZjdBdTsQwVnNlzRXC5QzIJ6nhfB4I1ahKoe9yPmoaA/Vhf7m9dB1chGPpDRdGXg==",
"cpu": [ "cpu": [
"arm" "arm"
], ],
@ -448,9 +474,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-arm64-gnu": { "node_modules/@rollup/rollup-linux-arm64-gnu": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.26.0.tgz",
"integrity": "sha512-i0xTLXjqap2eRfulFVlSnM5dEbTVque/3Pi4g2y7cxrs7+a9De42z4XxKLYJ7+OhE3IgxvfQM7vQc43bwTgPwA==", "integrity": "sha512-4daeEUQutGRCW/9zEo8JtdAgtJ1q2g5oHaoQaZbMSKaIWKDQwQ3Yx0/3jJNmpzrsScIPtx/V+1AfibLisb3AMQ==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -461,9 +487,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-arm64-musl": { "node_modules/@rollup/rollup-linux-arm64-musl": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.26.0.tgz",
"integrity": "sha512-9E6MKUJhDuDh604Qco5yP/3qn3y7SLXYuiC0Rpr89aMScS2UAmK1wHP2b7KAa1nSjWJc/f/Lc0Wl1L47qjiyQw==", "integrity": "sha512-eGkX7zzkNxvvS05ROzJ/cO/AKqNvR/7t1jA3VZDi2vRniLKwAWxUr85fH3NsvtxU5vnUUKFHKh8flIBdlo2b3Q==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -474,9 +500,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-powerpc64le-gnu": { "node_modules/@rollup/rollup-linux-powerpc64le-gnu": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-powerpc64le-gnu/-/rollup-linux-powerpc64le-gnu-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-powerpc64le-gnu/-/rollup-linux-powerpc64le-gnu-4.26.0.tgz",
"integrity": "sha512-2XFFPJ2XMEiF5Zi2EBf4h73oR1V/lycirxZxHZNc93SqDN/IWhYYSYj8I9381ikUFXZrz2v7r2tOVk2NBwxrWw==", "integrity": "sha512-Odp/lgHbW/mAqw/pU21goo5ruWsytP7/HCC/liOt0zcGG0llYWKrd10k9Fj0pdj3prQ63N5yQLCLiE7HTX+MYw==",
"cpu": [ "cpu": [
"ppc64" "ppc64"
], ],
@ -487,9 +513,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-riscv64-gnu": { "node_modules/@rollup/rollup-linux-riscv64-gnu": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.26.0.tgz",
"integrity": "sha512-M3Dg4hlwuntUCdzU7KjYqbbd+BLq3JMAOhCKdBE3TcMGMZbKkDdJ5ivNdehOssMCIokNHFOsv7DO4rlEOfyKpg==", "integrity": "sha512-MBR2ZhCTzUgVD0OJdTzNeF4+zsVogIR1U/FsyuFerwcqjZGvg2nYe24SAHp8O5sN8ZkRVbHwlYeHqcSQ8tcYew==",
"cpu": [ "cpu": [
"riscv64" "riscv64"
], ],
@ -500,9 +526,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-s390x-gnu": { "node_modules/@rollup/rollup-linux-s390x-gnu": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.26.0.tgz",
"integrity": "sha512-mjBaoo4ocxJppTorZVKWFpy1bfFj9FeCMJqzlMQGjpNPY9JwQi7OuS1axzNIk0nMX6jSgy6ZURDZ2w0QW6D56g==", "integrity": "sha512-YYcg8MkbN17fMbRMZuxwmxWqsmQufh3ZJFxFGoHjrE7bv0X+T6l3glcdzd7IKLiwhT+PZOJCblpnNlz1/C3kGQ==",
"cpu": [ "cpu": [
"s390x" "s390x"
], ],
@ -513,9 +539,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-x64-gnu": { "node_modules/@rollup/rollup-linux-x64-gnu": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.26.0.tgz",
"integrity": "sha512-ZXFk7M72R0YYFN5q13niV0B7G8/5dcQ9JDp8keJSfr3GoZeXEoMHP/HlvqROA3OMbMdfr19IjCeNAnPUG93b6A==", "integrity": "sha512-ZuwpfjCwjPkAOxpjAEjabg6LRSfL7cAJb6gSQGZYjGhadlzKKywDkCUnJ+KEfrNY1jH5EEoSIKLCb572jSiglA==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -526,9 +552,9 @@
] ]
}, },
"node_modules/@rollup/rollup-linux-x64-musl": { "node_modules/@rollup/rollup-linux-x64-musl": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.26.0.tgz",
"integrity": "sha512-w1i+L7kAXZNdYl+vFvzSZy8Y1arS7vMgIy8wusXJzRrPyof5LAb02KGr1PD2EkRcl73kHulIID0M501lN+vobQ==", "integrity": "sha512-+HJD2lFS86qkeF8kNu0kALtifMpPCZU80HvwztIKnYwym3KnA1os6nsX4BGSTLtS2QVAGG1P3guRgsYyMA0Yhg==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -539,9 +565,9 @@
] ]
}, },
"node_modules/@rollup/rollup-win32-arm64-msvc": { "node_modules/@rollup/rollup-win32-arm64-msvc": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.26.0.tgz",
"integrity": "sha512-VXBrnPWgBpVDCVY6XF3LEW0pOU51KbaHhccHw6AS6vBWIC60eqsH19DAeeObl+g8nKAz04QFdl/Cefta0xQtUQ==", "integrity": "sha512-WUQzVFWPSw2uJzX4j6YEbMAiLbs0BUysgysh8s817doAYhR5ybqTI1wtKARQKo6cGop3pHnrUJPFCsXdoFaimQ==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -552,9 +578,9 @@
] ]
}, },
"node_modules/@rollup/rollup-win32-ia32-msvc": { "node_modules/@rollup/rollup-win32-ia32-msvc": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.26.0.tgz",
"integrity": "sha512-xrNcGDU0OxVcPTH/8n/ShH4UevZxKIO6HJFK0e15XItZP2UcaiLFd5kiX7hJnqCbSztUF8Qot+JWBC/QXRPYWQ==", "integrity": "sha512-D4CxkazFKBfN1akAIY6ieyOqzoOoBV1OICxgUblWxff/pSjCA2khXlASUx7mK6W1oP4McqhgcCsu6QaLj3WMWg==",
"cpu": [ "cpu": [
"ia32" "ia32"
], ],
@ -565,9 +591,9 @@
] ]
}, },
"node_modules/@rollup/rollup-win32-x64-msvc": { "node_modules/@rollup/rollup-win32-x64-msvc": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.24.0.tgz", "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.26.0.tgz",
"integrity": "sha512-fbMkAF7fufku0N2dE5TBXcNlg0pt0cJue4xBRE2Qc5Vqikxr4VCgKj/ht6SMdFcOacVA9rqF70APJ8RN/4vMJw==", "integrity": "sha512-2x8MO1rm4PGEP0xWbubJW5RtbNLk3puzAMaLQd3B3JHVw4KcHlmXcO+Wewx9zCoo7EUFiMlu/aZbCJ7VjMzAag==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -590,9 +616,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/acorn": { "node_modules/acorn": {
"version": "8.13.0", "version": "8.14.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.13.0.tgz", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.14.0.tgz",
"integrity": "sha512-8zSiw54Oxrdym50NlZ9sUusyO1Z1ZchgRLWRaK6c86XJFClyCgFKetdowBg5bKxyp/u+CDBJG4Mpp0m3HLZl9w==", "integrity": "sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"bin": { "bin": {
@ -754,9 +780,9 @@
} }
}, },
"node_modules/rollup": { "node_modules/rollup": {
"version": "4.24.0", "version": "4.26.0",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-4.24.0.tgz", "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.26.0.tgz",
"integrity": "sha512-DOmrlGSXNk1DM0ljiQA+i+o0rSLhtii1je5wgk60j49d1jHT5YYttBv1iWOnYSTG+fZZESUOSNiAl89SIet+Cg==", "integrity": "sha512-ilcl12hnWonG8f+NxU6BlgysVA0gvY2l8N0R84S1HcINbW20bvwuCngJkkInV6LXhwRpucsW5k1ovDwEdBVrNg==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@types/estree": "1.0.6" "@types/estree": "1.0.6"
@ -769,22 +795,24 @@
"npm": ">=8.0.0" "npm": ">=8.0.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"@rollup/rollup-android-arm-eabi": "4.24.0", "@rollup/rollup-android-arm-eabi": "4.26.0",
"@rollup/rollup-android-arm64": "4.24.0", "@rollup/rollup-android-arm64": "4.26.0",
"@rollup/rollup-darwin-arm64": "4.24.0", "@rollup/rollup-darwin-arm64": "4.26.0",
"@rollup/rollup-darwin-x64": "4.24.0", "@rollup/rollup-darwin-x64": "4.26.0",
"@rollup/rollup-linux-arm-gnueabihf": "4.24.0", "@rollup/rollup-freebsd-arm64": "4.26.0",
"@rollup/rollup-linux-arm-musleabihf": "4.24.0", "@rollup/rollup-freebsd-x64": "4.26.0",
"@rollup/rollup-linux-arm64-gnu": "4.24.0", "@rollup/rollup-linux-arm-gnueabihf": "4.26.0",
"@rollup/rollup-linux-arm64-musl": "4.24.0", "@rollup/rollup-linux-arm-musleabihf": "4.26.0",
"@rollup/rollup-linux-powerpc64le-gnu": "4.24.0", "@rollup/rollup-linux-arm64-gnu": "4.26.0",
"@rollup/rollup-linux-riscv64-gnu": "4.24.0", "@rollup/rollup-linux-arm64-musl": "4.26.0",
"@rollup/rollup-linux-s390x-gnu": "4.24.0", "@rollup/rollup-linux-powerpc64le-gnu": "4.26.0",
"@rollup/rollup-linux-x64-gnu": "4.24.0", "@rollup/rollup-linux-riscv64-gnu": "4.26.0",
"@rollup/rollup-linux-x64-musl": "4.24.0", "@rollup/rollup-linux-s390x-gnu": "4.26.0",
"@rollup/rollup-win32-arm64-msvc": "4.24.0", "@rollup/rollup-linux-x64-gnu": "4.26.0",
"@rollup/rollup-win32-ia32-msvc": "4.24.0", "@rollup/rollup-linux-x64-musl": "4.26.0",
"@rollup/rollup-win32-x64-msvc": "4.24.0", "@rollup/rollup-win32-arm64-msvc": "4.26.0",
"@rollup/rollup-win32-ia32-msvc": "4.26.0",
"@rollup/rollup-win32-x64-msvc": "4.26.0",
"fsevents": "~2.3.2" "fsevents": "~2.3.2"
} }
}, },

2
deps/libbacktrace vendored

@ -1 +1 @@
Subproject commit 531aec7c52b66cd750a28a698f3c060f279b18b0 Subproject commit d48f84034ce3e53e501d10593710d025cb1121db

View File

@ -12,7 +12,7 @@
- nix - nix
- comment out the hash in default.nix - comment out the hash in default.nix
- update the version - update the version
- run `nix build` - run `nix-build`
- update the hash - update the hash
- bump the versions in GNUmakefile for the next release - bump the versions in GNUmakefile for the next release
- make - make

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unprompted.tildefriends" package="com.unprompted.tildefriends"
android:versionCode="29" android:versionCode="30"
android:versionName="0.0.24"> android:versionName="0.0.25-wip">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<application <application

View File

@ -67,7 +67,6 @@ typedef struct _tf_http_connection_t
typedef struct _tf_http_handler_t typedef struct _tf_http_handler_t
{ {
const char* pattern; const char* pattern;
bool is_wildcard;
tf_http_callback_t* callback; tf_http_callback_t* callback;
tf_http_cleanup_t* cleanup; tf_http_cleanup_t* cleanup;
void* user_data; void* user_data;
@ -129,9 +128,15 @@ static void _http_allocate_buffer(uv_handle_t* handle, size_t suggested_size, uv
*buf = uv_buf_init(connection->incoming, sizeof(connection->incoming)); *buf = uv_buf_init(connection->incoming, sizeof(connection->incoming));
} }
static bool _http_pattern_matches(const char* pattern, const char* path, bool is_wildcard) bool tf_http_pattern_matches(const char* pattern, const char* path)
{ {
if (!pattern || !*pattern || (!is_wildcard && strcmp(path, pattern) == 0)) if (!*pattern && !*path)
{
return true;
}
const char* k_word = "{word}";
bool is_wildcard = strchr(pattern, '*') || strstr(pattern, k_word);
if (!is_wildcard && strcmp(path, pattern) == 0)
{ {
return true; return true;
} }
@ -140,17 +145,36 @@ static bool _http_pattern_matches(const char* pattern, const char* path, bool is
{ {
int i = 0; int i = 0;
int j = 0; int j = 0;
while (pattern[i] && path[j] && pattern[i] != '*' && pattern[i] == path[j]) while (pattern[i] && path[j] && pattern[i] == path[j])
{ {
i++; i++;
j++; j++;
} }
if (pattern[i] == '*') size_t k_word_len = strlen(k_word);
if (strncmp(pattern + i, k_word, k_word_len) == 0 && ((path[j] >= 'a' && path[j] <= 'z') || (path[j] >= 'A' && path[j] <= 'Z')))
{ {
while (true) while (true)
{ {
if (_http_pattern_matches(pattern + i + 1, path + j, strchr(pattern + i + 1, '*') != NULL)) if ((path[j] >= 'a' && path[j] <= 'z') || (path[j] >= 'A' && path[j] <= 'Z') || (path[j] >= '0' && path[j] <= '9'))
{
if (tf_http_pattern_matches(pattern + i + k_word_len, path + j + 1))
{
return true;
}
}
else
{
break;
}
j++;
}
}
else if (pattern[i] == '*')
{
while (true)
{
if (tf_http_pattern_matches(pattern + i + 1, path + j))
{ {
return true; return true;
} }
@ -170,7 +194,7 @@ static bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callba
{ {
for (int i = 0; i < http->handlers_count; i++) for (int i = 0; i < http->handlers_count; i++)
{ {
if (_http_pattern_matches(http->handlers[i].pattern, path, http->handlers[i].is_wildcard)) if (tf_http_pattern_matches(http->handlers[i].pattern, path))
{ {
*out_callback = http->handlers[i].callback; *out_callback = http->handlers[i].callback;
*out_trace_name = http->handlers[i].pattern; *out_trace_name = http->handlers[i].pattern;
@ -741,7 +765,6 @@ void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_
http->handlers = tf_resize_vec(http->handlers, sizeof(tf_http_handler_t) * (http->handlers_count + 1)); http->handlers = tf_resize_vec(http->handlers, sizeof(tf_http_handler_t) * (http->handlers_count + 1));
http->handlers[http->handlers_count++] = (tf_http_handler_t) { http->handlers[http->handlers_count++] = (tf_http_handler_t) {
.pattern = tf_strdup(pattern), .pattern = tf_strdup(pattern),
.is_wildcard = pattern && strchr(pattern, '*') != NULL,
.callback = callback, .callback = callback,
.cleanup = cleanup, .cleanup = cleanup,
.user_data = user_data, .user_data = user_data,

View File

@ -228,4 +228,12 @@ void tf_http_request_websocket_upgrade(tf_http_request_t* request);
*/ */
const char* tf_http_status_text(int status); const char* tf_http_status_text(int status);
/**
** Match URL patterns. "*" matches anything, and "{word}" matches [a-zA-Z][a-zA-Z0-9]*".
** @param pattern The pattern to match.
** @param path The path to test.
** @return true if the path matches the pattern.
*/
bool tf_http_pattern_matches(const char* pattern, const char* path);
/** @} */ /** @} */

View File

@ -10,6 +10,7 @@
#include "tlscontext.js.h" #include "tlscontext.js.h"
#include "trace.h" #include "trace.h"
#include "util.js.h" #include "util.js.h"
#include "version.h"
#include "ow-crypt.h" #include "ow-crypt.h"
@ -29,10 +30,9 @@
#include <alloca.h> #include <alloca.h>
#endif #endif
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#define CYAN "\e[1;36m" #define CYAN "\e[1;36m"
#define MAGENTA "\e[1;35m" #define MAGENTA "\e[1;35m"
#define YELLOW "\e[1;33m"
#define RESET "\e[0m" #define RESET "\e[0m"
const int64_t k_refresh_interval = 1ULL * 7 * 24 * 60 * 60 * 1000; const int64_t k_refresh_interval = 1ULL * 7 * 24 * 60 * 60 * 1000;
@ -222,6 +222,17 @@ static void _httpd_message_callback(tf_http_request_t* request, int op_code, con
JS_FreeValue(context, on_message); JS_FreeValue(context, on_message);
} }
static JSValue _httpd_make_response_object(JSContext* context, tf_http_request_t* request)
{
JSValue response_object = JS_NewObjectClass(context, _httpd_request_class_id);
JS_SetOpaque(response_object, request);
JS_SetPropertyStr(context, response_object, "writeHead", JS_NewCFunction(context, _httpd_response_write_head, "writeHead", 2));
JS_SetPropertyStr(context, response_object, "end", JS_NewCFunction(context, _httpd_response_end, "end", 1));
JS_SetPropertyStr(context, response_object, "send", JS_NewCFunction(context, _httpd_response_send, "send", 2));
JS_SetPropertyStr(context, response_object, "upgrade", JS_NewCFunction(context, _httpd_websocket_upgrade, "upgrade", 2));
return response_object;
}
static void _httpd_callback_internal(tf_http_request_t* request, bool is_websocket) static void _httpd_callback_internal(tf_http_request_t* request, bool is_websocket)
{ {
http_handler_data_t* data = request->user_data; http_handler_data_t* data = request->user_data;
@ -248,14 +259,9 @@ static void _httpd_callback_internal(tf_http_request_t* request, bool is_websock
JS_SetPropertyStr(context, client, "tls", request->is_tls ? JS_TRUE : JS_FALSE); JS_SetPropertyStr(context, client, "tls", request->is_tls ? JS_TRUE : JS_FALSE);
JS_SetPropertyStr(context, request_object, "client", client); JS_SetPropertyStr(context, request_object, "client", client);
JSValue response_object = JS_NewObjectClass(context, _httpd_request_class_id); JSValue response_object = _httpd_make_response_object(context, request);
/* The ref is owned by the JS object and will be released by the finalizer. */ /* The ref is owned by the JS object and will be released by the finalizer. */
tf_http_request_ref(request); tf_http_request_ref(request);
JS_SetOpaque(response_object, request);
JS_SetPropertyStr(context, response_object, "writeHead", JS_NewCFunction(context, _httpd_response_write_head, "writeHead", 2));
JS_SetPropertyStr(context, response_object, "end", JS_NewCFunction(context, _httpd_response_end, "end", 1));
JS_SetPropertyStr(context, response_object, "send", JS_NewCFunction(context, _httpd_response_send, "send", 2));
JS_SetPropertyStr(context, response_object, "upgrade", JS_NewCFunction(context, _httpd_websocket_upgrade, "upgrade", 2));
JSValue args[] = { JSValue args[] = {
request_object, request_object,
response_object, response_object,
@ -423,7 +429,7 @@ static JSValue _httpd_endpoint_start(JSContext* context, JSValueConst this_val,
*listener = (httpd_listener_t) { .context = context, .tls = JS_DupValue(context, argv[1]) }; *listener = (httpd_listener_t) { .context = context, .tls = JS_DupValue(context, argv[1]) };
tf_tls_context_t* tls = tf_tls_context_get(listener->tls); tf_tls_context_t* tls = tf_tls_context_get(listener->tls);
int assigned_port = tf_http_listen(http, port, tls, _httpd_listener_cleanup, listener); int assigned_port = tf_http_listen(http, port, tls, _httpd_listener_cleanup, listener);
tf_printf(CYAN "~😎 Tilde Friends" RESET " is now up at " MAGENTA "http%s://127.0.0.1:%d/" RESET ".\n", tls ? "s" : "", assigned_port); tf_printf(CYAN "~😎 Tilde Friends" RESET " " YELLOW VERSION_NUMBER RESET " is now up at " MAGENTA "http%s://127.0.0.1:%d/" RESET ".\n", tls ? "s" : "", assigned_port);
return JS_NewInt32(context, assigned_port); return JS_NewInt32(context, assigned_port);
} }
@ -558,7 +564,7 @@ static bool _magic_bytes_match(const magic_bytes_t* magic, const uint8_t* actual
return true; return true;
} }
static const char* _httpd_mime_type_from_magic_bytes_internal(const uint8_t* bytes, size_t size) static const char* _httpd_mime_type_from_magic_bytes(const uint8_t* bytes, size_t size)
{ {
const char* type = "application/binary"; const char* type = "application/binary";
if (bytes) if (bytes)
@ -635,13 +641,6 @@ static const char* _httpd_mime_type_from_magic_bytes_internal(const uint8_t* byt
return type; return type;
} }
static JSValue _httpd_mime_type_from_magic_bytes(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
size_t size = 0;
uint8_t* bytes = tf_util_try_get_array_buffer(context, &size, argv[0]);
return JS_NewString(context, _httpd_mime_type_from_magic_bytes_internal(bytes, size));
}
static const char* _ext_to_content_type(const char* ext, bool use_fallback) static const char* _ext_to_content_type(const char* ext, bool use_fallback)
{ {
if (ext) if (ext)
@ -674,14 +673,6 @@ static const char* _ext_to_content_type(const char* ext, bool use_fallback)
return use_fallback ? "application/binary" : NULL; return use_fallback ? "application/binary" : NULL;
} }
static JSValue _httpd_mime_type_from_extension(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
const char* name = JS_ToCString(context, argv[0]);
const char* type = _ext_to_content_type(strrchr(name, '.'), false);
JS_FreeCString(context, name);
return type ? JS_NewString(context, type) : JS_UNDEFINED;
}
static void _httpd_finalizer(JSRuntime* runtime, JSValue value) static void _httpd_finalizer(JSRuntime* runtime, JSValue value)
{ {
tf_http_t* http = JS_GetOpaque(value, _httpd_class_id); tf_http_t* http = JS_GetOpaque(value, _httpd_class_id);
@ -966,6 +957,22 @@ static void _httpd_endpoint_static(tf_http_request_t* request)
tf_file_stat(task, path, _httpd_endpoint_static_stat, request); tf_file_stat(task, path, _httpd_endpoint_static_stat, request);
} }
static void _httpd_endpoint_add_slash(tf_http_request_t* request)
{
const char* host = tf_http_request_get_header(request, "x-forwarded-host");
if (!host)
{
host = tf_http_request_get_header(request, "host");
}
char url[1024];
snprintf(url, sizeof(url), "%s%s%s/", request->is_tls ? "https://" : "http://", host, request->path);
const char* headers[] = {
"Location",
url,
};
tf_http_respond(request, 303, headers, tf_countof(headers) / 2, "", 0);
}
typedef struct _user_app_t typedef struct _user_app_t
{ {
const char* user; const char* user;
@ -1016,12 +1023,204 @@ static user_app_t* _parse_user_app_from_path(const char* path, const char* expec
return result; return result;
} }
typedef struct _app_blob_t
{
tf_http_request_t* request;
bool found;
bool not_modified;
bool use_handler;
void* data;
size_t size;
char app_blob_id[k_blob_id_len];
const char* file;
user_app_t* user_app;
char etag[256];
} app_blob_t;
static void _httpd_endpoint_app_blob_work(tf_ssb_t* ssb, void* user_data)
{
app_blob_t* data = user_data;
tf_http_request_t* request = data->request;
if (request->path[0] == '/' && request->path[1] == '~')
{
const char* last_slash = strchr(request->path + 1, '/');
if (last_slash)
{
last_slash = strchr(last_slash + 1, '/');
}
data->user_app = last_slash ? _parse_user_app_from_path(request->path, last_slash) : NULL;
if (data->user_app)
{
size_t path_length = strlen("path:") + strlen(data->user_app->app) + 1;
char* app_path = tf_malloc(path_length);
snprintf(app_path, path_length, "path:%s", data->user_app->app);
const char* value = tf_ssb_db_get_property(ssb, data->user_app->user, app_path);
snprintf(data->app_blob_id, sizeof(data->app_blob_id), "%s", value);
tf_free(app_path);
tf_free((void*)value);
data->file = last_slash + 1;
}
}
else if (request->path[0] == '/' && request->path[1] == '&')
{
const char* end = strstr(request->path, ".sha256/");
if (end)
{
snprintf(data->app_blob_id, sizeof(data->app_blob_id), "%.*s", (int)(end + strlen(".sha256") - request->path - 1), request->path + 1);
data->file = end + strlen(".sha256/");
}
}
char* app_blob = NULL;
size_t app_blob_size = 0;
if (*data->app_blob_id && tf_ssb_db_blob_get(ssb, data->app_blob_id, (uint8_t**)&app_blob, &app_blob_size))
{
JSMallocFunctions funcs = { 0 };
tf_get_js_malloc_functions(&funcs);
JSRuntime* runtime = JS_NewRuntime2(&funcs, NULL);
JSContext* context = JS_NewContext(runtime);
JSValue app_object = JS_ParseJSON(context, app_blob, app_blob_size, NULL);
JSValue files = JS_GetPropertyStr(context, app_object, "files");
JSValue blob_id = JS_GetPropertyStr(context, files, data->file);
if (JS_IsUndefined(blob_id))
{
blob_id = JS_GetPropertyStr(context, files, "handler.js");
if (!JS_IsUndefined(blob_id))
{
data->use_handler = true;
}
}
else
{
const char* blob_id_str = JS_ToCString(context, blob_id);
if (blob_id_str)
{
snprintf(data->etag, sizeof(data->etag), "\"%s\"", blob_id_str);
const char* match = tf_http_request_get_header(data->request, "if-none-match");
if (match && strcmp(match, data->etag) == 0)
{
data->not_modified = true;
}
else
{
data->found = tf_ssb_db_blob_get(ssb, blob_id_str, (uint8_t**)&data->data, &data->size);
}
}
JS_FreeCString(context, blob_id_str);
}
JS_FreeValue(context, blob_id);
JS_FreeValue(context, files);
JS_FreeValue(context, app_object);
JS_FreeContext(context);
JS_FreeRuntime(runtime);
tf_free(app_blob);
}
}
static void _httpd_call_app_handler(tf_ssb_t* ssb, tf_http_request_t* request, const char* app_blob_id, const char* path, const char* package_owner, const char* app)
{
JSContext* context = tf_ssb_get_context(ssb);
JSValue global = JS_GetGlobalObject(context);
JSValue exports = JS_GetPropertyStr(context, global, "exports");
JSValue call_app_handler = JS_GetPropertyStr(context, exports, "callAppHandler");
JSValue response = _httpd_make_response_object(context, request);
tf_http_request_ref(request);
JSValue handler_blob_id = JS_NewString(context, app_blob_id);
JSValue path_value = JS_NewString(context, path);
JSValue package_owner_value = JS_NewString(context, package_owner);
JSValue app_value = JS_NewString(context, app);
JSValue query_value = request->query ? JS_NewString(context, request->query) : JS_UNDEFINED;
JSValue headers = JS_NewObject(context);
for (int i = 0; i < request->headers_count; i++)
{
char name[256] = "";
snprintf(name, sizeof(name), "%.*s", (int)request->headers[i].name_len, request->headers[i].name);
JS_SetPropertyStr(context, headers, name, JS_NewStringLen(context, request->headers[i].value, request->headers[i].value_len));
}
JSValue args[] = {
response,
handler_blob_id,
path_value,
query_value,
headers,
package_owner_value,
app_value,
};
JSValue result = JS_Call(context, call_app_handler, JS_NULL, tf_countof(args), args);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
JS_FreeValue(context, headers);
JS_FreeValue(context, query_value);
JS_FreeValue(context, app_value);
JS_FreeValue(context, package_owner_value);
JS_FreeValue(context, handler_blob_id);
JS_FreeValue(context, path_value);
JS_FreeValue(context, response);
JS_FreeValue(context, call_app_handler);
JS_FreeValue(context, exports);
JS_FreeValue(context, global);
}
static void _httpd_endpoint_app_blob_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
app_blob_t* data = user_data;
if (data->not_modified)
{
tf_http_respond(data->request, 304, NULL, 0, NULL, 0);
}
else if (data->use_handler)
{
_httpd_call_app_handler(ssb, data->request, data->app_blob_id, data->file, data->user_app->user, data->user_app->app);
}
else if (data->found)
{
const char* mime_type = _ext_to_content_type(strrchr(data->request->path, '.'), false);
if (!mime_type)
{
mime_type = _httpd_mime_type_from_magic_bytes(data->data, data->size);
}
const char* headers[] = {
"Access-Control-Allow-Origin",
"*",
"Content-Security-Policy",
"sandbox allow-downloads allow-top-navigation-by-user-activation",
"Content-Type",
mime_type ? mime_type : "application/binary",
"etag",
data->etag,
};
tf_http_respond(data->request, 200, headers, tf_countof(headers) / 2, data->data, data->size);
}
tf_free(data->user_app);
tf_free(data->data);
tf_http_request_unref(data->request);
tf_free(data);
}
static void _httpd_endpoint_app_blob(tf_http_request_t* request)
{
tf_http_request_ref(request);
tf_task_t* task = request->user_data;
tf_ssb_t* ssb = tf_task_get_ssb(task);
app_blob_t* data = tf_malloc(sizeof(app_blob_t));
*data = (app_blob_t) { .request = request };
tf_ssb_run_work(ssb, _httpd_endpoint_app_blob_work, _httpd_endpoint_app_blob_after_work, data);
}
typedef struct _view_t typedef struct _view_t
{ {
tf_http_request_t* request; tf_http_request_t* request;
const char** form_data; const char** form_data;
void* data; void* data;
size_t size; size_t size;
char etag[256];
bool not_modified; bool not_modified;
} view_t; } view_t;
@ -1045,7 +1244,7 @@ static void _httpd_endpoint_view_work(tf_ssb_t* ssb, void* user_data)
{ {
view_t* view = user_data; view_t* view = user_data;
tf_http_request_t* request = view->request; tf_http_request_t* request = view->request;
char blob_id[256] = ""; char blob_id[k_blob_id_len] = "";
user_app_t* user_app = _parse_user_app_from_path(request->path, "/view"); user_app_t* user_app = _parse_user_app_from_path(request->path, "/view");
if (user_app) if (user_app)
@ -1066,10 +1265,11 @@ static void _httpd_endpoint_view_work(tf_ssb_t* ssb, void* user_data)
if (*blob_id) if (*blob_id)
{ {
snprintf(view->etag, sizeof(view->etag), "\"%s\"", blob_id);
const char* if_none_match = tf_http_request_get_header(request, "if-none-match"); const char* if_none_match = tf_http_request_get_header(request, "if-none-match");
char match[258]; char match[258];
snprintf(match, sizeof(match), "\"%s\"", blob_id); snprintf(match, sizeof(match), "\"%s\"", blob_id);
if (if_none_match && strcmp(if_none_match, match)) if (if_none_match && strcmp(if_none_match, match) == 0)
{ {
view->not_modified = true; view->not_modified = true;
} }
@ -1097,7 +1297,9 @@ static void _httpd_endpoint_view_after_work(tf_ssb_t* ssb, int status, void* use
"Content-Security-Policy", "Content-Security-Policy",
"sandbox allow-downloads allow-top-navigation-by-user-activation", "sandbox allow-downloads allow-top-navigation-by-user-activation",
"Content-Type", "Content-Type",
view->data ? _httpd_mime_type_from_magic_bytes_internal(view->data, view->size) : "text/plain", view->data ? _httpd_mime_type_from_magic_bytes(view->data, view->size) : "text/plain",
"etag",
view->etag,
filename ? "Content-Disposition" : NULL, filename ? "Content-Disposition" : NULL,
filename ? content_disposition : NULL, filename ? content_disposition : NULL,
}; };
@ -1135,7 +1337,7 @@ typedef struct _save_t
{ {
tf_http_request_t* request; tf_http_request_t* request;
int response; int response;
char blob_id[256]; char blob_id[k_blob_id_len];
} save_t; } save_t;
static void _httpd_endpoint_save_work(tf_ssb_t* ssb, void* user_data) static void _httpd_endpoint_save_work(tf_ssb_t* ssb, void* user_data)
@ -1212,12 +1414,12 @@ static void _httpd_endpoint_save_work(tf_ssb_t* ssb, void* user_data)
size_t new_app_length = 0; size_t new_app_length = 0;
const char* new_app_str = JS_ToCStringLen(context, &new_app_length, new_app_json); const char* new_app_str = JS_ToCStringLen(context, &new_app_length, new_app_json);
char blob_id[250] = { 0 }; char blob_id[k_blob_id_len] = { 0 };
if (tf_ssb_db_blob_store(ssb, (const uint8_t*)new_app_str, new_app_length, blob_id, sizeof(blob_id), NULL) && if (tf_ssb_db_blob_store(ssb, (const uint8_t*)new_app_str, new_app_length, blob_id, sizeof(blob_id), NULL) &&
tf_ssb_db_set_property(ssb, user_app->user, app_path, blob_id)) tf_ssb_db_set_property(ssb, user_app->user, app_path, blob_id))
{ {
tf_ssb_db_add_value_to_array_property(ssb, user_app->user, "apps", user_app->app); tf_ssb_db_add_value_to_array_property(ssb, user_app->user, "apps", user_app->app);
snprintf(save->blob_id, sizeof(save->blob_id), "/%s", blob_id); snprintf(save->blob_id, sizeof(save->blob_id), "%s", blob_id);
save->response = 200; save->response = 200;
} }
@ -1242,10 +1444,10 @@ static void _httpd_endpoint_save_work(tf_ssb_t* ssb, void* user_data)
} }
else if (strcmp(request->path, "/save") == 0) else if (strcmp(request->path, "/save") == 0)
{ {
char blob_id[250] = { 0 }; char blob_id[k_blob_id_len] = { 0 };
if (tf_ssb_db_blob_store(ssb, request->body, request->content_length, blob_id, sizeof(blob_id), NULL)) if (tf_ssb_db_blob_store(ssb, request->body, request->content_length, blob_id, sizeof(blob_id), NULL))
{ {
snprintf(save->blob_id, sizeof(save->blob_id), "/%s", blob_id); snprintf(save->blob_id, sizeof(save->blob_id), "%s", blob_id);
save->response = 200; save->response = 200;
} }
} }
@ -1269,7 +1471,9 @@ static void _httpd_endpoint_save_after_work(tf_ssb_t* ssb, int status, void* use
tf_http_request_t* request = save->request; tf_http_request_t* request = save->request;
if (*save->blob_id) if (*save->blob_id)
{ {
tf_http_respond(request, 200, NULL, 0, save->blob_id, strlen(save->blob_id)); char body[256] = "";
int length = snprintf(body, sizeof(body), "/%s", save->blob_id);
tf_http_respond(request, 200, NULL, 0, body, length);
} }
tf_http_request_unref(request); tf_http_request_unref(request);
tf_free(save); tf_free(save);
@ -2099,11 +2303,16 @@ void tf_httpd_register(JSContext* context)
tf_http_add_handler(http, "/speedscope/*", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/speedscope/*", _httpd_endpoint_static, NULL, task);
tf_http_add_handler(http, "/static/*", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/static/*", _httpd_endpoint_static, NULL, task);
tf_http_add_handler(http, "/.well-known/*", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/.well-known/*", _httpd_endpoint_static, NULL, task);
tf_http_add_handler(http, "/~*/*/", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/&*.sha256", _httpd_endpoint_add_slash, NULL, task);
tf_http_add_handler(http, "/&*.sha256/", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/&*.sha256/", _httpd_endpoint_static, NULL, task);
tf_http_add_handler(http, "/*/view", _httpd_endpoint_view, NULL, task); tf_http_add_handler(http, "/&*.sha256/view", _httpd_endpoint_view, NULL, task);
tf_http_add_handler(http, "/~*/*/save", _httpd_endpoint_save, NULL, task); tf_http_add_handler(http, "/&*.sha256/*", _httpd_endpoint_app_blob, NULL, task);
tf_http_add_handler(http, "/~*/*/delete", _httpd_endpoint_delete, NULL, task); tf_http_add_handler(http, "/~{word}/{word}", _httpd_endpoint_add_slash, NULL, task);
tf_http_add_handler(http, "/~{word}/{word}/", _httpd_endpoint_static, NULL, task);
tf_http_add_handler(http, "/~{word}/{word}/save", _httpd_endpoint_save, NULL, task);
tf_http_add_handler(http, "/~{word}/{word}/delete", _httpd_endpoint_delete, NULL, task);
tf_http_add_handler(http, "/~{word}/{word}/view", _httpd_endpoint_view, NULL, task);
tf_http_add_handler(http, "/~{word}/{word}/*", _httpd_endpoint_app_blob, NULL, task);
tf_http_add_handler(http, "/save", _httpd_endpoint_save, NULL, task); tf_http_add_handler(http, "/save", _httpd_endpoint_save, NULL, task);
tf_http_add_handler(http, "/robots.txt", _httpd_endpoint_robots_txt, NULL, NULL); tf_http_add_handler(http, "/robots.txt", _httpd_endpoint_robots_txt, NULL, NULL);
@ -2116,13 +2325,10 @@ void tf_httpd_register(JSContext* context)
tf_http_add_handler(http, "/login/logout", _httpd_endpoint_logout, NULL, task); tf_http_add_handler(http, "/login/logout", _httpd_endpoint_logout, NULL, task);
tf_http_add_handler(http, "/login", _httpd_endpoint_login, NULL, task); tf_http_add_handler(http, "/login", _httpd_endpoint_login, NULL, task);
JS_SetPropertyStr(context, httpd, "handlers", JS_NewObject(context));
JS_SetPropertyStr(context, httpd, "all", JS_NewCFunction(context, _httpd_endpoint_all, "all", 2)); JS_SetPropertyStr(context, httpd, "all", JS_NewCFunction(context, _httpd_endpoint_all, "all", 2));
JS_SetPropertyStr(context, httpd, "start", JS_NewCFunction(context, _httpd_endpoint_start, "start", 2)); JS_SetPropertyStr(context, httpd, "start", JS_NewCFunction(context, _httpd_endpoint_start, "start", 2));
JS_SetPropertyStr(context, httpd, "set_http_redirect", JS_NewCFunction(context, _httpd_set_http_redirect, "set_http_redirect", 1)); JS_SetPropertyStr(context, httpd, "set_http_redirect", JS_NewCFunction(context, _httpd_set_http_redirect, "set_http_redirect", 1));
JS_SetPropertyStr(context, httpd, "auth_query", JS_NewCFunction(context, _httpd_auth_query, "auth_query", 1)); JS_SetPropertyStr(context, httpd, "auth_query", JS_NewCFunction(context, _httpd_auth_query, "auth_query", 1));
JS_SetPropertyStr(context, httpd, "mime_type_from_magic_bytes", JS_NewCFunction(context, _httpd_mime_type_from_magic_bytes, "mime_type_from_magic_bytes", 1));
JS_SetPropertyStr(context, httpd, "mime_type_from_extension", JS_NewCFunction(context, _httpd_mime_type_from_extension, "mime_type_from_extension", 1));
JS_SetPropertyStr(context, global, "httpd", httpd); JS_SetPropertyStr(context, global, "httpd", httpd);
JS_FreeValue(context, global); JS_FreeValue(context, global);
} }

View File

@ -39,10 +39,6 @@
#include "jni.h" #include "jni.h"
#endif #endif
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
struct backtrace_state* g_backtrace_state; struct backtrace_state* g_backtrace_state;
const char* k_db_path_default = "db.sqlite"; const char* k_db_path_default = "db.sqlite";
@ -264,7 +260,7 @@ static int _tf_command_export(const char* file, int argc, char* argv[])
"ssb", "ssb",
"todo", "todo",
}; };
for (int i = 0; i < (int)_countof(k_export); i++) for (int i = 0; i < tf_countof(k_export); i++)
{ {
char buffer[256]; char buffer[256];
snprintf(buffer, sizeof(buffer), "/~%s/%s", user, k_export[i]); snprintf(buffer, sizeof(buffer), "/~%s/%s", user, k_export[i]);
@ -346,8 +342,8 @@ static int _tf_command_publish(const char* file, int argc, char* argv[])
{ {
tf_printf("\n%s publish [options]\n\n", file); tf_printf("\n%s publish [options]\n\n", file);
tf_printf("options:\n"); tf_printf("options:\n");
tf_printf(" -y, --user user User owning identity with which to publish.\n"); tf_printf(" -u, --user user User owning identity with which to publish.\n");
tf_printf(" -i, --identity identity Identity with which to publish message.\n"); tf_printf(" -i, --id identity Identity with which to publish message.\n");
tf_printf(" -d, --db-path db_path SQLite database path (default: %s).\n", k_db_path_default); tf_printf(" -d, --db-path db_path SQLite database path (default: %s).\n", k_db_path_default);
tf_printf(" -c, --content json JSON content of message to publish.\n"); tf_printf(" -c, --content json JSON content of message to publish.\n");
tf_printf(" -h, --help Show this usage information.\n"); tf_printf(" -h, --help Show this usage information.\n");
@ -778,7 +774,7 @@ static int _tf_command_usage(const char* file)
{ {
tf_printf("Usage: %s command [command-options]\n", file); tf_printf("Usage: %s command [command-options]\n", file);
tf_printf("commands:\n"); tf_printf("commands:\n");
for (int i = 0; i < (int)_countof(k_commands); i++) for (int i = 0; i < tf_countof(k_commands); i++)
{ {
tf_printf(" %s - %s\n", k_commands[i].name, k_commands[i].description); tf_printf(" %s - %s\n", k_commands[i].name, k_commands[i].description);
} }
@ -908,7 +904,7 @@ static jint _tf_server_main(JNIEnv* env, jobject this_object, jstring files_dir,
}; };
tf_task_set_android_service_callbacks(_tf_service_start, _tf_service_stop); tf_task_set_android_service_callbacks(_tf_service_start, _tf_service_stop);
result = _tf_command_run(apk, _countof(args), (char**)args); result = _tf_command_run(apk, tf_countof(args), (char**)args);
tf_task_set_android_service_callbacks(NULL, NULL); tf_task_set_android_service_callbacks(NULL, NULL);
(*env)->ReleaseStringUTFChars(env, files_dir, files); (*env)->ReleaseStringUTFChars(env, files_dir, files);
@ -939,7 +935,7 @@ static jint _tf_sandbox_main(JNIEnv* env, jobject this_object, int pipe_fd)
fd, fd,
}; };
int result = _tf_command_sandbox(NULL, _countof(args), (char**)args); int result = _tf_command_sandbox(NULL, tf_countof(args), (char**)args);
tf_mem_shutdown(); tf_mem_shutdown();
tf_printf("tf_sandbox_main finished with %d.", result); tf_printf("tf_sandbox_main finished with %d.", result);
@ -971,7 +967,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{ "tf_server_main", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/net/ConnectivityManager;)I", _tf_server_main }, { "tf_server_main", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/net/ConnectivityManager;)I", _tf_server_main },
{ "tf_sandbox_main", "(I)I", _tf_sandbox_main }, { "tf_sandbox_main", "(I)I", _tf_sandbox_main },
}; };
int result = (*env)->RegisterNatives(env, c, methods, (int)_countof(methods)); int result = (*env)->RegisterNatives(env, c, methods, tf_countof(methods));
if (result != JNI_OK) if (result != JNI_OK)
{ {
return result; return result;
@ -1018,7 +1014,7 @@ int main(int argc, char* argv[])
int result = 0; int result = 0;
if (argc >= 2) if (argc >= 2)
{ {
for (int i = 0; i < (int)_countof(k_commands); i++) for (int i = 0; i < tf_countof(k_commands); i++)
{ {
const command_t* command = &k_commands[i]; const command_t* command = &k_commands[i];
if (strcmp(argv[1], command->name) == 0) if (strcmp(argv[1], command->name) == 0)

140
src/ssb.c
View File

@ -31,10 +31,6 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
#define GREEN "\e[1;32m" #define GREEN "\e[1;32m"
#define MAGENTA "\e[1;35m" #define MAGENTA "\e[1;35m"
#define CYAN "\e[1;36m" #define CYAN "\e[1;36m"
@ -367,6 +363,9 @@ typedef struct _tf_ssb_connection_t
uint64_t last_notified_active; uint64_t last_notified_active;
int flags; int flags;
tf_ssb_connect_callback_t* connect_callback;
void* connect_callback_user_data;
} tf_ssb_connection_t; } tf_ssb_connection_t;
static JSClassID _connection_class_id; static JSClassID _connection_class_id;
@ -386,6 +385,23 @@ static void _tf_ssb_start_update_settings(tf_ssb_t* ssb);
static void _tf_ssb_update_settings(tf_ssb_t* ssb); static void _tf_ssb_update_settings(tf_ssb_t* ssb);
static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t size); static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t size);
static const char* _tf_ssb_connection_state_to_string(tf_ssb_state_t state)
{
switch (state)
{
case k_tf_ssb_state_invalid: return "invalid";
case k_tf_ssb_state_connected: return "connected";
case k_tf_ssb_state_sent_hello: return "sent hello";
case k_tf_ssb_state_sent_identity: return "sent identity";
case k_tf_ssb_state_verified: return "verified";
case k_tf_ssb_state_server_wait_hello: return "server wait hello";
case k_tf_ssb_state_server_wait_client_identity: return "server wait client identity";
case k_tf_ssb_state_server_verified: return "server verified";
case k_tf_ssb_state_closing: return "closing";
default: return "unknown";
}
}
static void _tf_ssb_add_debug_close(tf_ssb_t* ssb, tf_ssb_connection_t* connection, const char* reason) static void _tf_ssb_add_debug_close(tf_ssb_t* ssb, tf_ssb_connection_t* connection, const char* reason)
{ {
if (!ssb->store_debug_messages) if (!ssb->store_debug_messages)
@ -508,7 +524,9 @@ static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t si
if (result) if (result)
{ {
tf_ssb_connection_adjust_write_count(connection, -1); tf_ssb_connection_adjust_write_count(connection, -1);
_tf_ssb_connection_close(connection, "write failed"); char buffer[256];
snprintf(buffer, sizeof(buffer), "write failed : %s", uv_strerror(result));
_tf_ssb_connection_close(connection, buffer);
tf_free(write); tf_free(write);
} }
} }
@ -1321,6 +1339,12 @@ static void _tf_ssb_connection_verify_identity(tf_ssb_connection_t* connection,
JS_SetPropertyStr(context, connection->object, "is_client", JS_TRUE); JS_SetPropertyStr(context, connection->object, "is_client", JS_TRUE);
connection->state = k_tf_ssb_state_verified; connection->state = k_tf_ssb_state_verified;
if (connection->connect_callback)
{
connection->connect_callback(connection, NULL, connection->connect_callback_user_data);
connection->connect_callback = NULL;
connection->connect_callback_user_data = NULL;
}
if (connection->handshake_timer.data) if (connection->handshake_timer.data)
{ {
uv_timer_stop(&connection->handshake_timer); uv_timer_stop(&connection->handshake_timer);
@ -1878,6 +1902,12 @@ static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const ch
{ {
tf_ssb_t* ssb = connection->ssb; tf_ssb_t* ssb = connection->ssb;
connection->closing = true; connection->closing = true;
if (connection->connect_callback)
{
connection->connect_callback(NULL, reason, connection->connect_callback_user_data);
connection->connect_callback = NULL;
connection->connect_callback_user_data = NULL;
}
if (!connection->destroy_reason) if (!connection->destroy_reason)
{ {
connection->destroy_reason = reason; connection->destroy_reason = reason;
@ -2112,8 +2142,9 @@ static bool _tf_ssb_connection_read_start(tf_ssb_connection_t* connection)
int result = uv_read_start((uv_stream_t*)&connection->tcp, _tf_ssb_connection_on_tcp_alloc, _tf_ssb_connection_on_tcp_recv); int result = uv_read_start((uv_stream_t*)&connection->tcp, _tf_ssb_connection_on_tcp_alloc, _tf_ssb_connection_on_tcp_recv);
if (result && result != UV_EALREADY) if (result && result != UV_EALREADY)
{ {
tf_printf("uv_read_start => %s\n", uv_strerror(result)); char reason[1024];
_tf_ssb_connection_close(connection, "uv_read_start failed"); snprintf(reason, sizeof(reason), "uv_read_start failed: %s", uv_strerror(result));
_tf_ssb_connection_close(connection, reason);
return false; return false;
} }
return true; return true;
@ -2124,8 +2155,9 @@ static bool _tf_ssb_connection_read_stop(tf_ssb_connection_t* connection)
int result = uv_read_stop((uv_stream_t*)&connection->tcp); int result = uv_read_stop((uv_stream_t*)&connection->tcp);
if (result && result != UV_EALREADY) if (result && result != UV_EALREADY)
{ {
tf_printf("uv_read_stop => %s\n", uv_strerror(result)); char reason[1024];
_tf_ssb_connection_close(connection, "uv_read_stop failed"); snprintf(reason, sizeof(reason), "uv_read_stop failed: %s", uv_strerror(result));
_tf_ssb_connection_close(connection, reason);
return false; return false;
} }
return true; return true;
@ -2145,7 +2177,9 @@ static void _tf_ssb_connection_on_connect(uv_connect_t* connect, int status)
} }
else else
{ {
_tf_ssb_connection_close(connection, "uv_tcp_connect failed"); char reason[1024];
snprintf(reason, sizeof(reason), "uv_tcp_connect failed: %s", uv_strerror(status));
_tf_ssb_connection_close(connection, reason);
} }
} }
@ -2190,7 +2224,7 @@ static void _tf_ssb_trace_timer(uv_timer_t* timer)
ssb->broadcasts_changed_count, ssb->broadcasts_changed_count,
}; };
tf_trace_counter(ssb->trace, "ssb", _countof(values), names, values); tf_trace_counter(ssb->trace, "ssb", tf_countof(values), names, values);
} }
void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats) void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats)
@ -2698,22 +2732,33 @@ static void _tf_ssb_connection_handshake_timer_callback(uv_timer_t* timer)
} }
} }
tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, const struct sockaddr_in* addr, const uint8_t* public_key) static tf_ssb_connection_t* _tf_ssb_connection_create(
tf_ssb_t* ssb, const char* host, const struct sockaddr_in* addr, const uint8_t* public_key, tf_ssb_connect_callback_t* callback, void* user_data)
{ {
for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next) for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next)
{ {
if (memcmp(connection->serverpub, public_key, k_id_bin_len) == 0 && connection->state != k_tf_ssb_state_invalid) if (memcmp(connection->serverpub, public_key, k_id_bin_len) == 0 && connection->state != k_tf_ssb_state_invalid)
{ {
char id[k_id_base64_len]; if (callback)
tf_ssb_id_bin_to_str(id, sizeof(id), public_key); {
tf_printf("Not connecting to %s:%d, because we are already connected to %s (state = %d).\n", host, ntohs(addr->sin_port), id, connection->state); char id[k_id_base64_len];
tf_ssb_id_bin_to_str(id, sizeof(id), public_key);
char reason[1024];
snprintf(reason, sizeof(reason), "Already connected to %s (%s).", id, _tf_ssb_connection_state_to_string(connection->state));
callback(NULL, reason, user_data);
}
return NULL; return NULL;
} }
else if (memcmp(ssb->pub, public_key, k_id_bin_len) == 0) else if (memcmp(ssb->pub, public_key, k_id_bin_len) == 0)
{ {
char id[k_id_base64_len]; if (callback)
tf_ssb_id_bin_to_str(id, sizeof(id), public_key); {
tf_printf("Not connecting to %s:%d, because they appear to be ourselves %s.\n", host, ntohs(addr->sin_port), id); char id[k_id_base64_len];
tf_ssb_id_bin_to_str(id, sizeof(id), public_key);
char reason[1024];
snprintf(reason, sizeof(reason), "Not connecting to ourself: %s.", id);
callback(NULL, reason, user_data);
}
return NULL; return NULL;
} }
} }
@ -2731,6 +2776,8 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c
connection->port = ntohs(addr->sin_port); connection->port = ntohs(addr->sin_port);
connection->async.data = connection; connection->async.data = connection;
uv_async_init(ssb->loop, &connection->async, _tf_ssb_connection_process_message_async); uv_async_init(ssb->loop, &connection->async, _tf_ssb_connection_process_message_async);
connection->connect_callback = callback;
connection->connect_callback_user_data = user_data;
connection->handshake_timer.data = connection; connection->handshake_timer.data = connection;
uv_timer_init(ssb->loop, &connection->handshake_timer); uv_timer_init(ssb->loop, &connection->handshake_timer);
@ -2751,9 +2798,10 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c
int result = uv_tcp_connect(&connection->connect, &connection->tcp, (const struct sockaddr*)addr, _tf_ssb_connection_on_connect); int result = uv_tcp_connect(&connection->connect, &connection->tcp, (const struct sockaddr*)addr, _tf_ssb_connection_on_connect);
if (result) if (result)
{ {
tf_printf("uv_tcp_connect(%s): %s\n", host, uv_strerror(result)); char reason[1024];
snprintf(reason, sizeof(reason), "uv_tcp_connect(%s) => %s", host, uv_strerror(result));
connection->connect.data = NULL; connection->connect.data = NULL;
_tf_ssb_connection_destroy(connection, "connect failed"); _tf_ssb_connection_destroy(connection, reason);
} }
else else
{ {
@ -2836,6 +2884,8 @@ typedef struct _connect_t
int port; int port;
int flags; int flags;
uint8_t key[k_id_bin_len]; uint8_t key[k_id_bin_len];
tf_ssb_connect_callback_t* callback;
void* user_data;
} connect_t; } connect_t;
static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, struct addrinfo* info) static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, struct addrinfo* info)
@ -2847,26 +2897,36 @@ static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, s
{ {
struct sockaddr_in addr = *(struct sockaddr_in*)info->ai_addr; struct sockaddr_in addr = *(struct sockaddr_in*)info->ai_addr;
addr.sin_port = htons(connect->port); addr.sin_port = htons(connect->port);
tf_ssb_connection_t* connection = tf_ssb_connection_create(connect->ssb, connect->host, &addr, connect->key); tf_ssb_connection_t* connection = _tf_ssb_connection_create(connect->ssb, connect->host, &addr, connect->key, connect->callback, connect->user_data);
if (connection) if (connection)
{ {
connection->flags = connect->flags; connection->flags = connect->flags;
} }
} }
else else if (connect->callback)
{ {
tf_printf("getaddrinfo(%s) => %s\n", connect->host, uv_strerror(result)); char reason[1024];
snprintf(reason, sizeof(reason), "uv_getaddrinfo(%s) => %s", connect->host, uv_strerror(result));
connect->callback(NULL, reason, connect->user_data);
} }
} }
else if (connect->callback)
{
connect->callback(NULL, "Shutting down.", connect->user_data);
}
uv_freeaddrinfo(info); uv_freeaddrinfo(info);
tf_ssb_unref(connect->ssb); tf_ssb_unref(connect->ssb);
tf_free(connect); tf_free(connect);
} }
void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key, int connect_flags) void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key, int connect_flags, tf_ssb_connect_callback_t* callback, void* user_data)
{ {
if (ssb->shutting_down) if (ssb->shutting_down)
{ {
if (callback)
{
callback(NULL, "Shutting down.", user_data);
}
return; return;
} }
connect_t* connect = tf_malloc(sizeof(connect_t)); connect_t* connect = tf_malloc(sizeof(connect_t));
@ -2875,6 +2935,8 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke
.port = port, .port = port,
.flags = connect_flags, .flags = connect_flags,
.req.data = connect, .req.data = connect,
.callback = callback,
.user_data = user_data,
}; };
char id[k_id_base64_len] = { 0 }; char id[k_id_base64_len] = { 0 };
tf_ssb_id_bin_to_str(id, sizeof(id), key); tf_ssb_id_bin_to_str(id, sizeof(id), key);
@ -2885,6 +2947,12 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke
int r = uv_getaddrinfo(ssb->loop, &connect->req, _tf_on_connect_getaddrinfo, host, NULL, &(struct addrinfo) { .ai_family = AF_INET }); int r = uv_getaddrinfo(ssb->loop, &connect->req, _tf_on_connect_getaddrinfo, host, NULL, &(struct addrinfo) { .ai_family = AF_INET });
if (r < 0) if (r < 0)
{ {
if (callback)
{
char reason[1024];
snprintf(reason, sizeof(reason), "uv_getaddr_info(%s): %s", host, uv_strerror(r));
callback(NULL, reason, user_data);
}
tf_printf("uv_getaddrinfo(%s): %s\n", host, uv_strerror(r)); tf_printf("uv_getaddrinfo(%s): %s\n", host, uv_strerror(r));
tf_free(connect); tf_free(connect);
tf_ssb_unref(ssb); tf_ssb_unref(ssb);
@ -2918,15 +2986,21 @@ static void _tf_ssb_on_connection(uv_stream_t* stream, int status)
connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); connection->object = JS_NewObjectClass(ssb->context, _connection_class_id);
JS_SetOpaque(connection->object, connection); JS_SetOpaque(connection->object, connection);
if (uv_tcp_init(ssb->loop, &connection->tcp) != 0) int result = uv_tcp_init(ssb->loop, &connection->tcp);
if (result != 0)
{ {
_tf_ssb_connection_destroy(connection, "init failed"); char reason[1024];
snprintf(reason, sizeof(reason), "uv_tcp_init() => %s", uv_strerror(result));
_tf_ssb_connection_destroy(connection, reason);
return; return;
} }
if (uv_accept(stream, (uv_stream_t*)&connection->tcp) != 0) result = uv_accept(stream, (uv_stream_t*)&connection->tcp);
if (result != 0)
{ {
_tf_ssb_connection_destroy(connection, "accept failed"); char reason[1024];
snprintf(reason, sizeof(reason), "uv_accept() => %s", uv_strerror(result));
_tf_ssb_connection_destroy(connection, reason);
return; return;
} }
@ -3164,16 +3238,18 @@ static bool _tf_ssb_parse_broadcast(const char* in_broadcast, tf_ssb_broadcast_t
return false; return false;
} }
void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address, int connect_flags) void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address, int connect_flags, tf_ssb_connect_callback_t* callback, void* user_data)
{ {
tf_ssb_broadcast_t broadcast = { 0 }; tf_ssb_broadcast_t broadcast = { 0 };
if (_tf_ssb_parse_broadcast(address, &broadcast)) if (_tf_ssb_parse_broadcast(address, &broadcast))
{ {
tf_ssb_connect(ssb, broadcast.host, ntohs(broadcast.addr.sin_port), broadcast.pub, connect_flags); tf_ssb_connect(ssb, broadcast.host, ntohs(broadcast.addr.sin_port), broadcast.pub, connect_flags, callback, user_data);
} }
else else if (callback)
{ {
tf_printf("Unable to parse: %s\n", address); char reason[1024] = "";
snprintf(reason, sizeof(reason), "Unable to parse: '%s'.", address);
callback(NULL, reason, user_data);
} }
} }

View File

@ -3,16 +3,13 @@
#include "log.h" #include "log.h"
#include "mem.h" #include "mem.h"
#include "ssb.h" #include "ssb.h"
#include "util.js.h"
#include "sqlite3.h" #include "sqlite3.h"
#include "uv.h" #include "uv.h"
#include <string.h> #include <string.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
typedef struct _tf_ssb_connections_t typedef struct _tf_ssb_connections_t
{ {
tf_ssb_t* ssb; tf_ssb_t* ssb;
@ -106,7 +103,7 @@ static void _tf_ssb_connections_get_next_after_work(tf_ssb_t* ssb, int status, v
uint8_t key_bin[k_id_bin_len]; uint8_t key_bin[k_id_bin_len];
if (tf_ssb_id_str_to_bin(key_bin, next->key)) if (tf_ssb_id_str_to_bin(key_bin, next->key))
{ {
tf_ssb_connect(ssb, next->host, next->port, key_bin, 0); tf_ssb_connect(ssb, next->host, next->port, key_bin, 0, NULL, NULL);
} }
} }
tf_free(next); tf_free(next);
@ -116,8 +113,8 @@ static void _tf_ssb_connections_timer(uv_timer_t* timer)
{ {
tf_ssb_connections_t* connections = timer->data; tf_ssb_connections_t* connections = timer->data;
tf_ssb_connection_t* active[4]; tf_ssb_connection_t* active[4];
int count = tf_ssb_get_connections(connections->ssb, active, _countof(active)); int count = tf_ssb_get_connections(connections->ssb, active, tf_countof(active));
if (count < (int)_countof(active)) if (count < tf_countof(active))
{ {
tf_ssb_connections_get_next_t* next = tf_malloc(sizeof(tf_ssb_connections_get_next_t)); tf_ssb_connections_get_next_t* next = tf_malloc(sizeof(tf_ssb_connections_get_next_t));
*next = (tf_ssb_connections_get_next_t) { *next = (tf_ssb_connections_get_next_t) {
@ -286,7 +283,7 @@ static void _tf_ssb_connections_sync_broadcast_visit(
} }
else else
{ {
tf_ssb_connect(ssb, host, ntohs(addr->sin_port), pub, k_tf_ssb_connect_flag_one_shot); tf_ssb_connect(ssb, host, ntohs(addr->sin_port), pub, k_tf_ssb_connect_flag_one_shot, NULL, NULL);
} }
} }
@ -332,7 +329,7 @@ static void _tf_ssb_connections_get_all_after_work(tf_ssb_t* ssb, int status, vo
tf_ssb_connections_get_all_work_t* work = user_data; tf_ssb_connections_get_all_work_t* work = user_data;
for (int i = 0; i < work->connections_count; i++) for (int i = 0; i < work->connections_count; i++)
{ {
tf_ssb_connect_str(ssb, work->connections[i], k_tf_ssb_connect_flag_one_shot); tf_ssb_connect_str(ssb, work->connections[i], k_tf_ssb_connect_flag_one_shot, NULL, NULL);
tf_free(work->connections[i]); tf_free(work->connections[i]);
} }
tf_free(work->connections); tf_free(work->connections);

View File

@ -767,14 +767,6 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char*
} }
tf_ssb_release_db_writer(ssb, db); tf_ssb_release_db_writer(ssb, db);
if (rows)
{
if (!out_new)
{
tf_printf("blob stored %s %zd => %d\n", id, size, result);
}
}
if (result && out_id) if (result && out_id)
{ {
snprintf(out_id, out_id_size, "%s", id); snprintf(out_id, out_id_size, "%s", id);
@ -1733,7 +1725,6 @@ bool tf_ssb_db_register_account(uv_loop_t* loop, sqlite3* db, JSContext* context
{ {
if (sqlite3_bind_text(statement, 1, value, value_length, NULL) == SQLITE_OK) if (sqlite3_bind_text(statement, 1, value, value_length, NULL) == SQLITE_OK)
{ {
tf_printf("added user to properties\n");
result = sqlite3_step(statement) == SQLITE_DONE; result = sqlite3_step(statement) == SQLITE_DONE;
} }
sqlite3_finalize(statement); sqlite3_finalize(statement);

View File

@ -90,7 +90,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key)
return; return;
} }
char app_blob_id[64] = { 0 }; char app_blob_id[k_blob_id_len] = { 0 };
sqlite3* db = tf_ssb_acquire_db_reader(ssb); sqlite3* db = tf_ssb_acquire_db_reader(ssb);
sqlite3_busy_timeout(db, 10000); sqlite3_busy_timeout(db, 10000);
sqlite3_stmt* statement; sqlite3_stmt* statement;

View File

@ -348,6 +348,14 @@ const char** tf_ssb_get_connection_ids(tf_ssb_t* ssb);
*/ */
int tf_ssb_get_connections(tf_ssb_t* ssb, tf_ssb_connection_t** out_connections, int out_connections_count); int tf_ssb_get_connections(tf_ssb_t* ssb, tf_ssb_connection_t** out_connections, int out_connections_count);
/**
** Callback for completing establishing a connection.
** @param connection The established connection if successful or null.
** @param reason The reason for failure if the connection failed.
** @param user_data User data.
*/
typedef void(tf_ssb_connect_callback_t)(tf_ssb_connection_t* connection, const char* reason, void* user_data);
/** /**
** Establish an SHS connection with a host. ** Establish an SHS connection with a host.
** @param ssb The SSB instance. ** @param ssb The SSB instance.
@ -355,16 +363,20 @@ int tf_ssb_get_connections(tf_ssb_t* ssb, tf_ssb_connection_t** out_connections,
** @param port The host's SHS port. ** @param port The host's SHS port.
** @param key The host's SSB identity. ** @param key The host's SSB identity.
** @param connect_flags Flags affecting the connection. ** @param connect_flags Flags affecting the connection.
** @param callback Completion callback.
** @param user_data User data to be passed to the callback.
*/ */
void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key, int connect_flags); void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key, int connect_flags, tf_ssb_connect_callback_t* callback, void* user_data);
/** /**
** Establish an SHS connection with a host by string address. ** Establish an SHS connection with a host by string address.
** @param ssb The SSB instance. ** @param ssb The SSB instance.
** @param address The address. ** @param address The address.
** @param connect_flags Flags affecting the connection. ** @param connect_flags Flags affecting the connection.
** @param callback Completion callback.
** @param user_data User data to be passed to the callback.
*/ */
void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address, int connect_flags); void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address, int connect_flags, tf_ssb_connect_callback_t* callback, void* user_data);
/** /**
** Begin listening for SHS connections on the given port. ** Begin listening for SHS connections on the given port.

View File

@ -20,10 +20,6 @@
#include <assert.h> #include <assert.h>
#include <inttypes.h> #include <inttypes.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
static const int k_sql_async_timeout_ms = 60 * 1000; static const int k_sql_async_timeout_ms = 60 * 1000;
static JSClassID _tf_ssb_classId; static JSClassID _tf_ssb_classId;
@ -271,7 +267,7 @@ static JSValue _set_server_following_internal(tf_ssb_t* ssb, JSValueConst this_v
server_id, server_id,
message, message,
}; };
JSValue result = _tf_ssb_appendMessageWithIdentity(context, this_val, _countof(args), args); JSValue result = _tf_ssb_appendMessageWithIdentity(context, this_val, tf_countof(args), args);
JS_FreeValue(context, server_id); JS_FreeValue(context, server_id);
JS_FreeValue(context, server_user); JS_FreeValue(context, server_user);
JS_FreeValue(context, message); JS_FreeValue(context, message);
@ -1088,7 +1084,7 @@ static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, in
if (ssb) if (ssb)
{ {
tf_ssb_connection_t* connections[32]; tf_ssb_connection_t* connections[32];
int count = tf_ssb_get_connections(ssb, connections, _countof(connections)); int count = tf_ssb_get_connections(ssb, connections, tf_countof(connections));
result = JS_NewArray(context); result = JS_NewArray(context);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
@ -1633,17 +1629,41 @@ static JSValue _tf_ssb_getBroadcasts(JSContext* context, JSValueConst this_val,
return result; return result;
} }
typedef struct _connect_t
{
JSContext* context;
JSValue promise[2];
} connect_t;
static void _tf_ssb_connect_callback(tf_ssb_connection_t* connection, const char* reason, void* user_data)
{
connect_t* connect = user_data;
JSContext* context = connect->context;
JSValue arg = connection ? JS_UNDEFINED : JS_NewString(context, reason);
JSValue result = JS_Call(context, connection ? connect->promise[0] : connect->promise[1], JS_UNDEFINED, connection ? 0 : 1, &arg);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
JS_FreeValue(context, connect->promise[0]);
JS_FreeValue(context, connect->promise[1]);
JS_FreeValue(context, arg);
tf_free(connect);
}
static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{ {
JSValue result = JS_UNDEFINED;
JSValue args = argv[0]; JSValue args = argv[0];
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
if (ssb) if (ssb)
{ {
connect_t* connect = tf_malloc(sizeof(connect_t));
*connect = (connect_t) { .context = context };
result = JS_NewPromiseCapability(context, connect->promise);
if (JS_IsString(args)) if (JS_IsString(args))
{ {
const char* address_str = JS_ToCString(context, args); const char* address_str = JS_ToCString(context, args);
tf_printf("Connecting to %s\n", address_str); tf_printf("Connecting to %s\n", address_str);
tf_ssb_connect_str(ssb, address_str, 0); tf_ssb_connect_str(ssb, address_str, 0, _tf_ssb_connect_callback, connect);
JS_FreeCString(context, address_str); JS_FreeCString(context, address_str);
} }
else else
@ -1660,11 +1680,11 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar
tf_printf("Connecting to %s:%d\n", address_str, port_int); tf_printf("Connecting to %s:%d\n", address_str, port_int);
uint8_t pubkey_bin[k_id_bin_len]; uint8_t pubkey_bin[k_id_bin_len];
tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str); tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str);
tf_ssb_connect(ssb, address_str, port_int, pubkey_bin, 0); tf_ssb_connect(ssb, address_str, port_int, pubkey_bin, 0, _tf_ssb_connect_callback, connect);
} }
else else
{ {
tf_printf("Not connecting to null.\n"); _tf_ssb_connect_callback(NULL, "Not connecting to null.", connect);
} }
JS_FreeCString(context, pubkey_str); JS_FreeCString(context, pubkey_str);
JS_FreeCString(context, address_str); JS_FreeCString(context, address_str);
@ -1673,7 +1693,7 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar
JS_FreeValue(context, pubkey); JS_FreeValue(context, pubkey);
} }
} }
return JS_UNDEFINED; return result;
} }
typedef struct _forget_stored_connection_t typedef struct _forget_stored_connection_t

View File

@ -14,10 +14,6 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
static void _tf_ssb_connection_send_history_stream( static void _tf_ssb_connection_send_history_stream(
tf_ssb_connection_t* connection, int32_t request_number, const char* author, int64_t sequence, bool keys, bool live, bool end_request); tf_ssb_connection_t* connection, int32_t request_number, const char* author, int64_t sequence, bool keys, bool live, bool end_request);
static void _tf_ssb_rpc_send_peers_exchange(tf_ssb_connection_t* connection); static void _tf_ssb_rpc_send_peers_exchange(tf_ssb_connection_t* connection);
@ -240,7 +236,7 @@ static void _tf_ssb_request_blob_wants_work(tf_ssb_connection_t* connection, voi
if (sqlite3_prepare(db, "SELECT id FROM blob_wants_view WHERE id > ? AND timestamp > ? ORDER BY id LIMIT ?", -1, &statement, NULL) == SQLITE_OK) if (sqlite3_prepare(db, "SELECT id FROM blob_wants_view WHERE id > ? AND timestamp > ? ORDER BY id LIMIT ?", -1, &statement, NULL) == SQLITE_OK)
{ {
if (sqlite3_bind_text(statement, 1, blob_wants->last_id, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, timestamp) == SQLITE_OK && if (sqlite3_bind_text(statement, 1, blob_wants->last_id, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, timestamp) == SQLITE_OK &&
sqlite3_bind_int(statement, 3, _countof(work->out_id)) == SQLITE_OK) sqlite3_bind_int(statement, 3, tf_countof(work->out_id)) == SQLITE_OK)
{ {
while (sqlite3_step(statement) == SQLITE_ROW) while (sqlite3_step(statement) == SQLITE_ROW)
{ {
@ -456,7 +452,7 @@ static void _tf_ssb_rpc_room_attendants(tf_ssb_connection_t* connection, uint8_t
JSValue ids = JS_NewArray(context); JSValue ids = JS_NewArray(context);
int id_count = 0; int id_count = 0;
tf_ssb_connection_t* connections[1024]; tf_ssb_connection_t* connections[1024];
int count = tf_ssb_get_connections(ssb, connections, _countof(connections)); int count = tf_ssb_get_connections(ssb, connections, tf_countof(connections));
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
@ -1292,7 +1288,7 @@ static void _tf_ssb_rpc_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_chang
JS_SetPropertyStr(context, left, "type", JS_NewString(context, "left")); JS_SetPropertyStr(context, left, "type", JS_NewString(context, "left"));
JS_SetPropertyStr(context, left, "id", JS_NewString(context, id)); JS_SetPropertyStr(context, left, "id", JS_NewString(context, id));
tf_ssb_connection_t* connections[1024]; tf_ssb_connection_t* connections[1024];
int count = tf_ssb_get_connections(ssb, connections, _countof(connections)); int count = tf_ssb_get_connections(ssb, connections, tf_countof(connections));
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
if (tf_ssb_connection_is_attendant(connections[i])) if (tf_ssb_connection_is_attendant(connections[i]))

View File

@ -268,7 +268,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
uint8_t id0bin[k_id_bin_len]; uint8_t id0bin[k_id_bin_len];
tf_ssb_id_str_to_bin(id0bin, id0); tf_ssb_id_str_to_bin(id0bin, id0);
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0); tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0, NULL, NULL);
tf_printf("Waiting for connection.\n"); tf_printf("Waiting for connection.\n");
while (test.connection_count0 != 1 || test.connection_count1 != 1) while (test.connection_count0 != 1 || test.connection_count1 != 1)
@ -480,8 +480,8 @@ void tf_ssb_test_rooms(const tf_test_options_t* options)
uint8_t id0bin[k_id_bin_len]; uint8_t id0bin[k_id_bin_len];
tf_ssb_id_str_to_bin(id0bin, id0); tf_ssb_id_str_to_bin(id0bin, id0);
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0); tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0, NULL, NULL);
tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin, 0); tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin, 0, NULL, NULL);
tf_printf("Waiting for connection.\n"); tf_printf("Waiting for connection.\n");
while (test.connection_count0 != 2 || test.connection_count1 != 1 || test.connection_count2 != 1) while (test.connection_count0 != 2 || test.connection_count1 != 1 || test.connection_count2 != 1)
@ -712,7 +712,7 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
tf_ssb_register(tf_ssb_get_context(ssb1), ssb1); tf_ssb_register(tf_ssb_get_context(ssb1), ssb1);
tf_ssb_server_open(ssb0, 12347); tf_ssb_server_open(ssb0, 12347);
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0); tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0, NULL, NULL);
tf_printf("Waiting for messages.\n"); tf_printf("Waiting for messages.\n");
clock_gettime(CLOCK_REALTIME, &start_time); clock_gettime(CLOCK_REALTIME, &start_time);
@ -881,8 +881,8 @@ void tf_ssb_test_go_ssb_room(const tf_test_options_t* options)
tf_ssb_add_broadcasts_changed_callback(ssb0, _ssb_test_room_broadcasts_changed, NULL, NULL); tf_ssb_add_broadcasts_changed_callback(ssb0, _ssb_test_room_broadcasts_changed, NULL, NULL);
tf_ssb_connect_str(ssb0, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=", 0); tf_ssb_connect_str(ssb0, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=", 0, NULL, NULL);
tf_ssb_connect_str(ssb1, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=", 0); tf_ssb_connect_str(ssb1, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=", 0, NULL, NULL);
uv_run(&loop, UV_RUN_DEFAULT); uv_run(&loop, UV_RUN_DEFAULT);
@ -982,8 +982,8 @@ void tf_ssb_test_peer_exchange(const tf_test_options_t* options)
tf_ssb_whoami(ssb0, id0, sizeof(id0)); tf_ssb_whoami(ssb0, id0, sizeof(id0));
uint8_t id0bin[k_id_bin_len]; uint8_t id0bin[k_id_bin_len];
tf_ssb_id_str_to_bin(id0bin, id0); tf_ssb_id_str_to_bin(id0bin, id0);
tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0); tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin, 0, NULL, NULL);
tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin, 0); tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin, 0, NULL, NULL);
while (_count_broadcasts(ssb0) != 2 || _count_broadcasts(ssb1) != 1 || _count_broadcasts(ssb2) != 1) while (_count_broadcasts(ssb0) != 2 || _count_broadcasts(ssb1) != 1 || _count_broadcasts(ssb2) != 1)
{ {
@ -1015,6 +1015,8 @@ void tf_ssb_test_publish(const tf_test_options_t* options)
char id[k_id_base64_len] = { 0 }; char id[k_id_base64_len] = { 0 };
tf_ssb_whoami(ssb, id, sizeof(id)); tf_ssb_whoami(ssb, id, sizeof(id));
tf_ssb_destroy(ssb);
char executable[1024]; char executable[1024];
size_t size = sizeof(executable); size_t size = sizeof(executable);
uv_exepath(executable, &size); uv_exepath(executable, &size);
@ -1033,8 +1035,6 @@ void tf_ssb_test_publish(const tf_test_options_t* options)
printf("returned %d\n", WEXITSTATUS(result)); printf("returned %d\n", WEXITSTATUS(result));
assert(WEXITSTATUS(result) == 0); assert(WEXITSTATUS(result) == 0);
tf_ssb_destroy(ssb);
uv_run(&loop, UV_RUN_DEFAULT); uv_run(&loop, UV_RUN_DEFAULT);
uv_loop_close(&loop); uv_loop_close(&loop);
} }

View File

@ -43,10 +43,6 @@
#include <malloc.h> #include <malloc.h>
#endif #endif
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
static JSClassID _import_class_id; static JSClassID _import_class_id;
static int _count; static int _count;
@ -919,7 +915,7 @@ char* tf_task_get_hitches(tf_task_t* task)
JSContext* context = task->_context; JSContext* context = task->_context;
tf_trace_begin(task->_trace, __func__); tf_trace_begin(task->_trace, __func__);
JSValue object = JS_NewObject(context); JSValue object = JS_NewObject(context);
for (int i = 0; i < (int)_countof(task->hitches); i++) for (int i = 0; i < tf_countof(task->hitches); i++)
{ {
if (*task->hitches[i].name) if (*task->hitches[i].name)
{ {
@ -1658,13 +1654,13 @@ static void _tf_task_trace_to_parent(tf_trace_t* trace, const char* buffer, size
static void _tf_task_record_hitch(const char* name, uint64_t duration_ns, void* user_data) static void _tf_task_record_hitch(const char* name, uint64_t duration_ns, void* user_data)
{ {
tf_task_t* task = user_data; tf_task_t* task = user_data;
for (int i = 0; i < (int)_countof(task->hitches); i++) for (int i = 0; i < tf_countof(task->hitches); i++)
{ {
if (duration_ns > task->hitches[i].duration_ns) if (duration_ns > task->hitches[i].duration_ns)
{ {
if (i + 1 < (int)_countof(task->hitches)) if (i + 1 < tf_countof(task->hitches))
{ {
memmove(task->hitches + i + 1, task->hitches + i, sizeof(hitch_t) * ((int)_countof(task->hitches) - i - 1)); memmove(task->hitches + i + 1, task->hitches + i, sizeof(hitch_t) * (tf_countof(task->hitches) - i - 1));
} }
snprintf(task->hitches[i].name, sizeof(task->hitches[i].name), "%s", name); snprintf(task->hitches[i].name, sizeof(task->hitches[i].name), "%s", name);
task->hitches[i].duration_ns = duration_ns; task->hitches[i].duration_ns = duration_ns;
@ -1869,7 +1865,6 @@ void tf_task_destroy(tf_task_t* task)
{ {
JSValue global = JS_GetGlobalObject(task->_context); JSValue global = JS_GetGlobalObject(task->_context);
JS_SetPropertyStr(task->_context, global, "httpd", JS_UNDEFINED); JS_SetPropertyStr(task->_context, global, "httpd", JS_UNDEFINED);
JS_SetPropertyStr(task->_context, global, "gProcesses", JS_NewObject(task->_context));
JS_FreeValue(task->_context, global); JS_FreeValue(task->_context, global);
} }

View File

@ -4,6 +4,8 @@
#include "http.h" #include "http.h"
#include "log.h" #include "log.h"
#include "mem.h" #include "mem.h"
#include "ssb.db.h"
#include "ssb.h"
#include "ssb.tests.h" #include "ssb.tests.h"
#include "util.js.h" #include "util.js.h"
@ -782,6 +784,140 @@ static void _test_http(const tf_test_options_t* options)
uv_thread_join(&thread); uv_thread_join(&thread);
} }
static int _http_get_status_code(const char* url)
{
char command[1024];
snprintf(command, sizeof(command), "curl -s -o /dev/null -w '%%{http_code}' \"%s\"", url);
char buffer[256] = "";
FILE* file = popen(command, "r");
char* result = fgets(buffer, sizeof(buffer), file);
pclose(file);
return result ? atoi(result) : -1;
}
static void _http_check_status_code(const char* url, int expected_code)
{
char command[1024];
snprintf(command, sizeof(command), "curl -s -o /dev/null -w '%%{http_code}' \"%s\"", url);
char buffer[256] = "";
FILE* file = popen(command, "r");
char* result = fgets(buffer, sizeof(buffer), file);
tf_printf("%s => %s\n", command, result);
assert(atoi(buffer) == expected_code);
assert(file);
int status = pclose(file);
(void)status;
assert(WEXITSTATUS(status) == 0);
}
static void _http_check_body_contains(const char* url, const char* expected)
{
char command[1024];
snprintf(command, sizeof(command), "curl -s \"%s\"", url);
char buffer[1024] = "";
FILE* file = popen(command, "r");
bool found = false;
while (!found)
{
char* result = fgets(buffer, sizeof(buffer), file);
if (!result)
{
break;
}
found = strstr(buffer, expected) != NULL;
if (found)
{
tf_printf("%s => found: \"%s\"\n", url, expected);
}
}
if (!found)
{
tf_printf("Didn't find \"%s\" in %s.\n", expected, url);
}
assert(found);
assert(file);
int status = pclose(file);
(void)status;
assert(WEXITSTATUS(status) == 0);
}
static void _test_httpd(const tf_test_options_t* options)
{
uv_loop_t loop = { 0 };
uv_loop_init(&loop);
unlink("out/test_db0.sqlite");
char command[256];
snprintf(command, sizeof(command), "%s run -b 0 --db-path=out/test_db0.sqlite" TEST_ARGS, options->exe_path);
uv_stdio_container_t stdio[] = {
[STDIN_FILENO] = { .flags = UV_IGNORE },
[STDOUT_FILENO] = { .flags = UV_INHERIT_FD },
[STDERR_FILENO] = { .flags = UV_INHERIT_FD },
};
uv_process_t process = { 0 };
uv_spawn(&loop, &process,
&(uv_process_options_t) {
.file = options->exe_path,
.args = (char*[]) { (char*)options->exe_path, "run", "-b0", "--db-path=out/test_db0.sqlite", "--http-port=8080", "--https-port=0", NULL },
.stdio_count = sizeof(stdio) / sizeof(*stdio),
.stdio = stdio,
});
for (int i = 0; i < 100; i++)
{
if (_http_get_status_code("http://localhost:8080/debug") == 200)
{
break;
}
uv_sleep(1000);
}
tf_ssb_t* ssb = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL);
const char* app_id = tf_ssb_db_get_property(ssb, "core", "path:test");
tf_ssb_destroy(ssb);
_http_check_status_code("http://localhost:8080/404", 404);
_http_check_status_code("http://localhost:8080/", 303);
_http_check_status_code("http://localhost:8080/~core/apps/", 200);
_http_check_status_code("http://localhost:8080/~core/apps", 303);
_http_check_status_code("http://localhost:8080/~core/apps/view", 200);
_http_check_body_contains("http://localhost:8080/~core/apps/", "<title>Tilde Friends</title>");
_http_check_body_contains("http://localhost:8080/~core/apps/view", "\"type\":\"tildefriends-app\"");
_http_check_body_contains("http://localhost:8080/~core/test/hello.txt", "Hello, world!");
_http_check_status_code("http://localhost:8080/~core/test/nonexistent.txt", 404);
_http_check_body_contains("http://localhost:8080/&MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=.sha256/view", "Hello, world!");
char url[1024];
snprintf(url, sizeof(url), "http://localhost:8080/%s/", app_id);
_http_check_body_contains(url, "<title>Tilde Friends</title>");
snprintf(url, sizeof(url), "http://localhost:8080/%s/view", app_id);
_http_check_body_contains(url, "\"type\":\"tildefriends-app\"");
snprintf(url, sizeof(url), "http://localhost:8080/%s/hello.txt", app_id);
_http_check_body_contains(url, "Hello, world!");
tf_free((void*)app_id);
uv_process_kill(&process, SIGTERM);
uv_close((uv_handle_t*)&process, NULL);
uv_run(&loop, UV_RUN_ONCE);
uv_loop_close(&loop);
}
static void _test_pattern(const tf_test_options_t* options)
{
assert(tf_http_pattern_matches("/~core/test/", "/~core/test/"));
assert(tf_http_pattern_matches("/~core/test/*", "/~core/test/"));
assert(tf_http_pattern_matches("/~core/test/*", "/~core/test/blah"));
assert(tf_http_pattern_matches("*/~core/test/", "/~core/test/"));
assert(tf_http_pattern_matches("*/~core/test/", "blah/~core/test/"));
assert(tf_http_pattern_matches("/~*/*/", "/~core/test/"));
assert(tf_http_pattern_matches("/~{word}/*", "/~core/test"));
assert(tf_http_pattern_matches("/~{word}/{word}/", "/~core/test/"));
assert(tf_http_pattern_matches("/~{word}/{word}", "/~core/test"));
assert(!tf_http_pattern_matches("/~{word}/{word}", "/~foo/bar/baz"));
}
static void _test_auto_process_exit(uv_process_t* process, int64_t status, int termination_signal) static void _test_auto_process_exit(uv_process_t* process, int64_t status, int termination_signal)
{ {
tf_printf("Process exit %d signal=%d.\n", (int)WEXITSTATUS(status), termination_signal); tf_printf("Process exit %d signal=%d.\n", (int)WEXITSTATUS(status), termination_signal);
@ -887,6 +1023,8 @@ void tf_tests(const tf_test_options_t* options)
#if !TARGET_OS_IPHONE #if !TARGET_OS_IPHONE
_tf_test_run(options, "bip39", _test_bip39, false); _tf_test_run(options, "bip39", _test_bip39, false);
_tf_test_run(options, "http", _test_http, false); _tf_test_run(options, "http", _test_http, false);
_tf_test_run(options, "httpd", _test_httpd, false);
_tf_test_run(options, "pattern", _test_pattern, false);
_tf_test_run(options, "ssb", tf_ssb_test_ssb, false); _tf_test_run(options, "ssb", tf_ssb_test_ssb, false);
_tf_test_run(options, "ssb_id", tf_ssb_test_id_conversion, false); _tf_test_run(options, "ssb_id", tf_ssb_test_id_conversion, false);
_tf_test_run(options, "ssb_following", tf_ssb_test_following, false); _tf_test_run(options, "ssb_following", tf_ssb_test_following, false);

View File

@ -16,8 +16,6 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
enum enum
{ {
k_buffer_size = 4 * 1024 * 1024, k_buffer_size = 4 * 1024 * 1024,

View File

@ -304,6 +304,66 @@ static JSValue _util_parseHttpResponse(JSContext* context, JSValueConst this_val
return result; return result;
} }
#if defined(__APPLE__)
#include <TargetConditionals.h>
#endif
static bool _is_mobile()
{
#if defined(__ANDROID__) || (defined(__APPLE__) && TARGET_OS_IPHONE)
return true;
#else
return false;
#endif
}
static JSValue _util_defaultGlobalSettings(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
typedef struct _setting_t
{
const char* name;
const char* type;
const char* description;
JSValue default_value;
} setting_t;
const setting_t k_settings[] = {
{ .name = "code_of_conduct", .type = "textarea", .description = "Code of conduct presented at sign-in." },
{ .name = "blob_fetch_age_seconds",
.type = "integer",
.description = "Only blobs mentioned more recently than this age will be automatically fetched.",
.default_value = _is_mobile() ? JS_NewInt32(context, (int)(0.5f * 365 * 24 * 60 * 60)) : JS_UNDEFINED },
{ .name = "blob_expire_age_seconds",
.type = "integer",
.description = "Blobs older than this will be automatically deleted.",
.default_value = _is_mobile() ? JS_NewInt32(context, (int)(1.0f * 365 * 24 * 60 * 60)) : JS_UNDEFINED },
{ .name = "fetch_hosts", .type = "string", .description = "Comma-separated list of host names to which HTTP fetch requests are allowed. None if empty." },
{ .name = "http_redirect", .type = "string", .description = "If connecting by HTTP and HTTPS is configured, Location header prefix (ie, \"http://example.com\")" },
{ .name = "index", .type = "string", .description = "Default path.", .default_value = JS_NewString(context, "/~core/apps") },
{ .name = "index_map", .type = "textarea", .description = "Mappings from hostname to redirect path, one per line, as in: \"www.tildefriends.net=/~core/index/\"" },
{ .name = "peer_exchange",
.type = "boolean",
.description = "Enable discovery of, sharing of, and connecting to internet peer strangers, including announcing this instance.",
.default_value = JS_FALSE },
{ .name = "replicator", .type = "boolean", .description = "Enable message and blob replication.", .default_value = JS_TRUE },
{ .name = "room", .type = "boolean", .description = "Enable peers to tunnel through this instance as a room.", .default_value = JS_TRUE },
{ .name = "room_name", .type = "string", .description = "Name of the room.", .default_value = JS_NewString(context, "tilde friends tunnel") },
{ .name = "seeds_host", .type = "string", .description = "Hostname for seed connections.", .default_value = JS_NewString(context, "seeds.tildefriends.net") },
{ .name = "account_registration", .type = "boolean", .description = "Allow registration of new accounts.", .default_value = JS_TRUE },
};
JSValue settings = JS_NewObject(context);
for (int i = 0; i < tf_countof(k_settings); i++)
{
JSValue entry = JS_NewObject(context);
JS_SetPropertyStr(context, entry, "type", JS_NewString(context, k_settings[i].type));
JS_SetPropertyStr(context, entry, "description", JS_NewString(context, k_settings[i].description));
JS_SetPropertyStr(context, entry, "default_value", k_settings[i].default_value);
JS_SetPropertyStr(context, settings, k_settings[i].name, entry);
}
return settings;
}
JSValue tf_util_new_uint8_array(JSContext* context, const uint8_t* data, size_t size) JSValue tf_util_new_uint8_array(JSContext* context, const uint8_t* data, size_t size)
{ {
JSValue array_buffer = JS_NewArrayBufferCopy(context, data, size); JSValue array_buffer = JS_NewArrayBufferCopy(context, data, size);
@ -327,6 +387,7 @@ void tf_util_register(JSContext* context)
JS_SetPropertyStr(context, global, "bip39Bytes", JS_NewCFunction(context, _util_bip39_bytes, "bip39Bytes", 1)); JS_SetPropertyStr(context, global, "bip39Bytes", JS_NewCFunction(context, _util_bip39_bytes, "bip39Bytes", 1));
JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1)); JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1));
JS_SetPropertyStr(context, global, "parseHttpResponse", JS_NewCFunction(context, _util_parseHttpResponse, "parseHttpResponse", 2)); JS_SetPropertyStr(context, global, "parseHttpResponse", JS_NewCFunction(context, _util_parseHttpResponse, "parseHttpResponse", 2));
JS_SetPropertyStr(context, global, "defaultGlobalSettings", JS_NewCFunction(context, _util_defaultGlobalSettings, "defaultGlobalSettings", 2));
JS_FreeValue(context, global); JS_FreeValue(context, global);
} }

View File

@ -150,4 +150,11 @@ const char* tf_util_function_to_string(void* function);
_a > _b ? _b : _a; \ _a > _b ? _b : _a; \
}) })
/**
** Get the number of elements in an array.
** @param a The array.
** @return The number of array elements.
*/
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
/** @} */ /** @} */

View File

@ -1,2 +1,2 @@
#define VERSION_NUMBER "0.0.24" #define VERSION_NUMBER "0.0.25-wip"
#define VERSION_NAME "Honey bunches of boats." #define VERSION_NAME "This program kills fascists."

View File

@ -198,7 +198,12 @@ try:
wait.until(expected_conditions.presence_of_element_located((By.ID, 'content'))) wait.until(expected_conditions.presence_of_element_located((By.ID, 'content')))
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'identity').click() while True:
try:
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'identity').click()
break
except:
pass
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click() driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click()
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guest_label').click() driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guest_label').click()
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guestButton').click() driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guestButton').click()
@ -255,7 +260,12 @@ try:
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'confirm').send_keys('new_password') driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'confirm').send_keys('new_password')
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'loginButton').click() driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'loginButton').click()
wait.until(expected_conditions.presence_of_element_located((By.ID, 'document'))) wait.until(expected_conditions.presence_of_element_located((By.ID, 'document')))
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'identity').click() while True:
try:
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'identity').click()
break
except:
pass
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click() driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click()
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click() driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click()
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser') driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser')

55
tools/buttfeed.py Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env python3
import argparse
import feedparser
import json
import subprocess
import time
parser = argparse.ArgumentParser()
parser.add_argument('--state_file', help = 'Path to a file in which to store state.')
args = parser.parse_args()
k_feeds = {
'tildefriends': 'https://dev.tildefriends.net/cory/tildefriends.rss',
'erlbutt': 'https://github.com/cmoid/erlbutt/commits/main.atom',
'habitat': 'https://tildegit.org/jeremylist/habitat.rss',
'manyverse': 'https://gitlab.com/staltz/manyverse/-/commits/master?format=atom',
'ahau': 'https://gitlab.com/ahau/ahau/-/commits/master.atom',
}
def fix_title(entry):
if entry.summary.startswith('<a href=') and '\n' in entry.summary and entry.summary and '\n' in entry.summary:
return entry.summary.split('\n')[1]
return entry.title.split('\n')[0]
def get_entries():
results = []
for name, url in k_feeds.items():
feed = feedparser.parse(url)
for entry in feed.entries:
results.append((time.mktime(entry.get('updated_parsed')), name, entry.link, fix_title(entry)))
results.sort()
results.reverse()
return results
state = {}
if args.state_file:
try:
with open(args.state_file, 'r') as f:
state = json.load(f)
except:
pass
cutoff = state.get('last_update') or (time.time() - 2 * 7 * 24 * 60 * 60)
entries = [entry for entry in get_entries() if entry[0] > cutoff]
if entries:
text = '\n'.join([f' * [{entry[1]}] [{entry[3]}]({entry[2]})' for entry in entries])
state['last_update'] = entries[0][0]
if args.state_file:
content = json.dumps({'type': 'post', 'text': text, 'mentions': []})
subprocess.check_call(['out/debug/tildefriends', 'publish', '--user', 'cory', '--id', '@DnYDqFfmxdNkYQlpflF9Wkltk2HIhJ5u1MW5njKPLzM=.ed25519', '--content', content])
with open(args.state_file, 'w') as f:
json.dump(state, f)
else:
print(text)