-
Notifications
You must be signed in to change notification settings - Fork 8
Initial addition of BitTorrent protocol #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RangerMauve
wants to merge
13
commits into
main
Choose a base branch
from
bittorrent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a332959
Initial addition of BitTorrent protocol
3aa8420
Rename bt: to bittorrent:
e99053e
Improve storage location info
c36a626
Add DNS handling for bittorrent
fed368c
Update package-lock
74397dc
Use bittorrent in DNSLink response
1407432
Ensure bittorrent is used in tests correctly
89e56f0
Rebuild package-lock
eb63c32
Hardcode torrent port
beb3ee4
Fix ts errors
b4d4068
Clean up sites from DB with default values on missing
7e61766
Fix magnet link syntax
e9710bb
Make ProtocolStatus fields optional
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,4 +109,7 @@ store | |
|
|
||
| *.key | ||
|
|
||
| # Don't commit our internal ansibles! | ||
| ansible/prod.yml | ||
| ansible/staging.yml | ||
| ansible/mauve.yml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| declare module 'bt-fetch' { | ||
| export type TorrentManagerOptions = Partial<{ | ||
| folder: string | ||
| timeout: number | ||
| reloadInterval: number | ||
| }> | ||
|
|
||
| export interface Torrent { | ||
| infoHash: Buffer | ||
| publicKey: Buffer | ||
| } | ||
|
|
||
| export type TorrentPublishOpts = Partial<{ | ||
| name: string | ||
| comment: string | ||
| createdBy: string | ||
| creationDate: string | ||
| }> | ||
|
|
||
| export interface KeyPair { | ||
| publicKey: string | ||
| secretKey: string | ||
| } | ||
|
|
||
| export class TorrentManager { | ||
| constructor (opts: TorrentManagerOptions) | ||
| stopSeedingPublicKey (publicKey: string): Promise<boolean> | ||
| republishPublicKey (publicKey: string, secretKey: string, opts: TorrentPublishOpts): Promise<Torrent> | ||
| createKeypair (petname?: string): KeyPair | ||
| destroy (): Promise<void> | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { Static } from '@sinclair/typebox' | ||
| import Protocol, { Ctx, SyncOptions } from './interfaces' | ||
| import { BitTorrentProtocolFields } from '../api/schemas' | ||
|
|
||
| import path from 'node:path' | ||
| import { cp } from 'node:fs/promises' | ||
|
|
||
| import { TorrentManager } from 'bt-fetch' | ||
|
|
||
| export interface BitTorrentProtocolOptions { | ||
| path: string | ||
| } | ||
|
|
||
| export class BitTorrentProtocol implements Protocol<Static<typeof BitTorrentProtocolFields>> { | ||
| options: BitTorrentProtocolOptions | ||
| manager: TorrentManager | null | ||
|
|
||
| constructor (options: BitTorrentProtocolOptions) { | ||
| this.options = options | ||
| this.manager = null | ||
| } | ||
|
|
||
| async load (): Promise<void> { | ||
| const folder = this.options.path | ||
| this.manager = new TorrentManager({ folder }) | ||
| } | ||
|
|
||
| async unload (): Promise<void> { | ||
| await this.manager?.destroy() | ||
| } | ||
|
|
||
| async sync (id: string, folderPath: string, options?: SyncOptions, ctx?: Ctx): Promise<Static<typeof BitTorrentProtocolFields>> { | ||
| ctx?.logger.info('[bittorrent] Sync Start') | ||
|
|
||
| const link = `bittorent://${id}/` | ||
|
|
||
| if (this.manager === null) { | ||
| throw new Error('[bittorrent] Torrent Manager Not Initialized') | ||
| } | ||
|
|
||
| const manager: TorrentManager = this.manager | ||
|
|
||
| // Create keypair from the site ID and the seed key | ||
RangerMauve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { publicKey, secretKey } = manager.createKeypair(id) | ||
|
|
||
| const storageFolder = path.join(this.options.path, 'data', publicKey, id) | ||
RangerMauve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const torrentInfo = { | ||
| name: id, | ||
| comment: `Content for ${link}`, | ||
| createdBy: 'distributed.press' | ||
| } | ||
|
|
||
| ctx?.logger.info('[bittorrent] Stop seeding existing') | ||
|
|
||
| await manager.stopSeedingPublicKey(publicKey) | ||
|
|
||
| ctx?.logger.info('[bittorrent] Copy data to storage') | ||
|
|
||
| await cp(folderPath + path.sep, storageFolder + path.sep, { recursive: true }) | ||
|
|
||
| ctx?.logger.info('[bittorrent] Generate new torrent and publish to DHT') | ||
|
|
||
| // Pass folder and options | ||
| const torrent = await manager.republishPublicKey(publicKey, secretKey, torrentInfo) | ||
|
|
||
| const infoHash = torrent.infoHash.toString('hex') | ||
|
|
||
| const subdomain = id.replaceAll('-', '--').replaceAll('.', '-') | ||
RangerMauve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // TODO: Pass in gateway from config | ||
| const gateway = `https://${subdomain}.bt.hypha.coop/` | ||
|
|
||
| const magnet = `magnet:?xt:urn:btih:${infoHash}&xs=urn:btpk:${publicKey}` | ||
RangerMauve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const infoHashURL = `bittorrent://${infoHash}/` | ||
| const publicKeyURL = `bittorrent://${publicKey}/` | ||
|
|
||
| ctx?.logger.info(`[bittorrent] Published: ${link}`) | ||
|
|
||
| const dnslink = `/bt/${publicKey}` | ||
RangerMauve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return { | ||
| enabled: true, | ||
| link, | ||
| gateway, | ||
| infoHash: infoHashURL, | ||
| pubKey: publicKeyURL, | ||
| magnet, | ||
| dnslink | ||
| } | ||
| } | ||
|
|
||
| async unsync (id: string, _site: Static<typeof BitTorrentProtocolFields>, ctx?: Ctx): Promise<void> { | ||
| } | ||
|
Comment on lines
+103
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there nothing for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops, not yet. It's a TODO. |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.