Skip to main content
Version: v2.0.2

Plugin Develop

All compiled plugins need to place in /usr/local/dragonfly/plugins/.

Plugin Design

Dragonfly2 use golang plugin to build its plugins, refer:

Resource Plugin for CDN and Dfget

The resource plugin is used to download custom resource like dfget -u d7yfs://host:56001/path/to/resource.

All resource plugins need to implement and a function

func DragonflyPluginInit(option map[string]string) (interface{}, map[string]string, error).

// ResourceClient defines the API interface to interact with source.
type ResourceClient interface {
// GetContentLength get length of resource content
// return source.UnknownSourceFileLen if response status is not StatusOK and StatusPartialContent
GetContentLength(request *Request) (int64, error)

// IsSupportRange checks if resource supports breakpoint continuation
// return false if response status is not StatusPartialContent
IsSupportRange(request *Request) (bool, error)

// IsExpired checks if a resource received or stored is the same.
// return false and non-nil err to prevent the source from exploding if
// fails to get the result, it is considered that the source has not expired
IsExpired(request *Request, info *ExpireInfo) (bool, error)

// Download downloads from source
Download(request *Request) (*Response, error)

// GetLastModified gets last modified timestamp milliseconds of resource
GetLastModified(request *Request) (int64, error)

Example Code

1. main.go

package main

import (


var data = "hello world"

var _ source.ResourceClient = (*client)(nil)

var (
buildCommit = "unknown"
buildTime = "unknown"
vendor = "d7y"

type client struct {

func (c *client) GetContentLength(request *source.Request) (int64, error) {
return int64(len(data)), nil

func (c *client) IsSupportRange(request *source.Request) (bool, error) {
return false, nil

func (c *client) IsExpired(request *source.Request, info *source.ExpireInfo) (bool, error) {
panic("implement me")

func (c *client) Download(request *source.Request) (*source.Response, error) {
return source.NewResponse(io.NopCloser(bytes.NewBufferString(data))), nil

func (c *client) DownloadWithExpireInfo(request *source.Request) (io.ReadCloser, *source.ExpireInfo, error) {
return io.NopCloser(bytes.NewBufferString(data)), nil, nil

func (c *client) GetLastModified(request *source.Request) (int64, error) {
panic("implement me")

func DragonflyPluginInit(option map[string]string) (interface{}, map[string]string, error) {
return &client{}, map[string]string{
"schema": "d7yfs",
"type": "resource",
"name": "d7yfs",
"buildCommit": buildCommit,
"buildTime": buildTime,
"vendor": vendor,
}, nil

2. go.mod


go 1.17

require v2.0.1

require ( v0.0.0-20181008091004-fed159eddc2a // indirect v0.9.1 // indirect v1.9.0 // indirect v1.5.0 // indirect v1.16.0 // indirect v1.39.0 // indirect v3.0.0-20210107192922-496545a6307b // indirect

// fix golang build error: `plugin was built with a different version of package`
replace => /Dragonfly2


We have created a plugin builder in docker, follow this document. With the plugin builder, go.mod will be ignored.

1. Build plugin with target Dragonfly2 commit

Update D7Y_COMMIT in the following script.

# golang plugin need cgo
# original Dragonfly2 image is built with CGO_ENABLED=0 for alpine linux
# "dfdaemon" and "cdn" need to re-compile with CGO_ENABLED=1
export CGO_ENABLED="1"

# ensure same commit of code base
git clone /Dragonfly2 && git reset --hard ${D7Y_COMMIT}
(cd /Dragonfly2 && make build-dfget build-cdn)

# build plugin
BUILD_TIME=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
BUILD_COMMIT=$(git rev-parse --short HEAD)
go mod tidy -compat=1.17
go build -ldflags="-X main.buildTime=${BUILD_TIME} -X main.buildCommit=${BUILD_COMMIT}" \
-buildmode=plugin -o=/usr/local/dragonfly/plugins/ ./main.go

2. Validate plugin

/Dragonfly2/bin/linux_amd64/dfget plugin

Example output:

search plugin in /usr/local/dragonfly/plugins
resource plugin d7yfs, location:, attribute: {"buildCommit":"bb65f13","buildTime":"2021-12-13T08:53:04Z","name":"d7yfs","schema":"d7yfs","type":"resource","vendor":"d7y"}