Writing a plugin
New file hosters and debrid services are the most useful contributions. A plugin is a Go package in
internal/plugin/<name>/, compiled into the binary: a carrier (a file hoster) or a forwarder
(a debrid service).
A plugin knows nothing about the database or the dock. It receives a link (and, when there is one, an account with its credentials already decrypted) and returns a direct link, or a classified error that tells the dock what to do next.
The interfaces
Section titled “The interfaces”// A carrier.type Hoster interface { Name() string // stable lowercase identifier: "1fichier" Title() string // display name: "1fichier" Domains() []string // recognised domains, the main one first Match(u *url.URL) bool // is this link mine? FreeSlots() int // parcels at a time without a premium account (0 = no limit) Check(ctx context.Context, link string) (FileInfo, error) Resolve(ctx context.Context, link, password string, acc *Account) (Delivery, error) // acc nil: free mode AccountInfo(ctx context.Context, acc Account) (AccountStatus, error)}
// A forwarder.type Debrid interface { Name() string Title() string Hosts(ctx context.Context, acc Account) ([]string, error) // supported domains, cached 6 h by Fret Unrestrict(ctx context.Context, link, password string, acc Account) (Delivery, error) AccountInfo(ctx context.Context, acc Account) (AccountStatus, error)}Checkgives the name, the size and the availability of a file before departure (the waybill check of New shipment). It must be quick and never download the file.ResolveandUnrestrictreturn aDelivery: the directURL, and when the service gives them, theFilename, theSizeandMaxSegments(1for a service that accepts a single connection). A forwarder fillsViawith its display name.Headerholds the headers the file server expects (cookie, referer);Decryptreturns the keystream of files the carrier encrypts (MEGA), from any offset, so that resume and segments keep working.AccountInfochecks an account:Premium,ExpiresAt,Username, and a readableDetail(quota, points).
Optional interfaces
Section titled “Optional interfaces”- Accounts: a carrier says what its account is made of with
Credentials() plugin.Credentials:CredKey(an API key, the default),CredLoginPasswordorCredLoginKey(a user name inAccount.Loginand a password or premium key inAccount.APIKey), orCredNone(public links only: the Carriers screen shows no account form). - Folders: a carrier that opens folder links implements
plugin.Folder(IsFolder(u)andList(ctx, link, password, acc)). The listed files replace the folder link in the shipment, one parcel each. AnEntrygives its subfolder inDir("a/b") whenListwalks the whole tree itself (MEGA), or is a subfolder link withFolder: truewhen it does not (Google Drive, MediaFire): Fret then opens it in turn. Either way, the warehouse gets the same tree.
Classifying errors
Section titled “Classifying errors”The dock decides what happens from the error kind, created with plugin.Errorf(kind, …) or
plugin.WaitFor(delay, …):
| Kind | The dock |
|---|---|
ErrTemporary |
Missed delivery: tries again later, after Wait if given |
ErrWait |
Held at customs until Wait is over (wait imposed by the hoster) |
ErrOffline |
Return to sender (dead link); no other route is tried |
ErrPassword |
Held: password required; no other route is tried |
ErrPremium |
Held: subscribers only |
ErrAuth |
The account turns Refused, and the next route is tried |
ErrQuota |
The account is set aside for an hour, and the next route is tried |
ErrUnsupported |
This forwarder does not take this link: the next route is tried |
ErrPermanent |
Held: action needed |
An unclassified error counts as temporary. Error messages are shown to the user as the parcel’s
Reason: write them in plain English and start with the service name ("MEGA: transfer quota exceeded").
Helpers
Section titled “Helpers”plugin.Webis an HTTP client with short timeouts and Fret’s User-Agent. ItsDosends JSON or form bodies and theAuthorization: Bearerheader, and returns network errors already classified and stripped of the URL (which may hold a key).plugin.HTTPStatusError(status, "Service")classifies an HTTP status.- For carriers without an API,
plugin.Forms,plugin.FindForm,plugin.Attrs,plugin.ParseSizeandplugin.Textread HTML forms, attributes and sizes;plugin.IsFileandplugin.DispositionNametell a file from a web page and read its name. - Expose the base address of the service in a field (
Base,API…) so that tests can point it at a fake server.
Registering it
Section titled “Registering it”Add the plugin to the registry in internal/app/app.go:
registry := plugin.NewRegistry( []plugin.Hoster{ onefichier.New(pluginWeb), // … myhoster.New(pluginWeb), }, []plugin.Debrid{alldebrid.New(pluginWeb), realdebrid.New(pluginWeb)},)The Carriers screen shows it right away. To give it its colour, initials and help text, add it to
brand in web/src/routes/(app)/carriers/+page.svelte, and its carrier.help.<name> text to
web/src/lib/i18n/en.ts and fr.ts (or reuse carrier.help.public for public links). Mark it
noFree when it has no usable free mode, customs when its free mode imposes waits.
Testing it
Section titled “Testing it”Tests never call the real service: they imitate it with httptest, with answers copied from its
documentation or recorded by hand (and stripped of anything personal).
internal/plugin/plugintestprovides a fake service with canned replies per route and the recorded requests.- See
internal/plugin/alldebrid/alldebrid_test.gofor a complete example: direct link, delayed link, wrong key, and one case per documented error code. - Cover at least: a valid link, a dead link, a refused account, and every wait or limit the service documents.
For a one-off check against the real service, use a demo key when the service publishes one (AllDebrid:
staticDemoApikeyPrem), and never commit a personal key or link.
Documenting it
Section titled “Documenting it”Add the service to Supported services, to the tables of
Carriers and forwarders when it takes an account, and to CHANGELOG.md.