Skip to content

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.

internal/plugin/plugin.go
// 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)
}
  • Check gives 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.
  • Resolve and Unrestrict return a Delivery: the direct URL, and when the service gives them, the Filename, the Size and MaxSegments (1 for a service that accepts a single connection). A forwarder fills Via with its display name. Header holds the headers the file server expects (cookie, referer); Decrypt returns the keystream of files the carrier encrypts (MEGA), from any offset, so that resume and segments keep working.
  • AccountInfo checks an account: Premium, ExpiresAt, Username, and a readable Detail (quota, points).
  • Accounts: a carrier says what its account is made of with Credentials() plugin.Credentials: CredKey (an API key, the default), CredLoginPassword or CredLoginKey (a user name in Account.Login and a password or premium key in Account.APIKey), or CredNone (public links only: the Carriers screen shows no account form).
  • Folders: a carrier that opens folder links implements plugin.Folder (IsFolder(u) and List(ctx, link, password, acc)). The listed files replace the folder link in the shipment, one parcel each. An Entry gives its subfolder in Dir ("a/b") when List walks the whole tree itself (MEGA), or is a subfolder link with Folder: true when it does not (Google Drive, MediaFire): Fret then opens it in turn. Either way, the warehouse gets the same tree.

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").

  • plugin.Web is an HTTP client with short timeouts and Fret’s User-Agent. Its Do sends JSON or form bodies and the Authorization: Bearer header, 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.ParseSize and plugin.Text read HTML forms, attributes and sizes; plugin.IsFile and plugin.DispositionName tell 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.

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.

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/plugintest provides a fake service with canned replies per route and the recorded requests.
  • See internal/plugin/alldebrid/alldebrid_test.go for 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.

Add the service to Supported services, to the tables of Carriers and forwarders when it takes an account, and to CHANGELOG.md.

Built with Kiln