159 lines
5.5 KiB
Go
159 lines
5.5 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/downloader"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
// IDDownloaderParallel is the built-in optional multi-connection downloader plugin.
|
|
const IDDownloaderParallel = "builtin.downloader.parallel"
|
|
|
|
func defDownloaderParallel(ver string, cb HostCallbacks, desiredFn func(string, bool) bool) plugin.PluginDef {
|
|
minTimeout := 5.0
|
|
maxTimeout := 60.0
|
|
contrib := plugin.DownloaderContribution{
|
|
ID: IDDownloaderParallel,
|
|
Name: plugin.LocalizedText{ZhCN: "多线程直连下载加速", EnUS: "Parallel Direct Downloader"},
|
|
Priority: 50,
|
|
}
|
|
return plugin.PluginDef{
|
|
Manifest: plugin.Manifest{
|
|
Schema: 1,
|
|
ID: IDDownloaderParallel,
|
|
Version: ver,
|
|
Name: plugin.LocalizedText{ZhCN: "多线程下载加速", EnUS: "Parallel Download Accelerator"},
|
|
Description: plugin.LocalizedText{
|
|
ZhCN: "内置纯 Go 多线程 HTTP 分段下载引擎(默认直连不走代理,无外部弹窗),加速 macOS 安装镜像与便携运行时下载",
|
|
EnUS: "Built-in pure-Go multi-connection HTTP Range downloader (direct connection by default, no external windows) for macOS installers and runtimes",
|
|
},
|
|
Publisher: "Reims-Go",
|
|
License: "Apache-2.0",
|
|
Permissions: []string{"net:download"},
|
|
DefaultEnabled: true,
|
|
Contributes: plugin.Contributes{
|
|
Downloaders: []plugin.DownloaderContribution{contrib},
|
|
},
|
|
Settings: &plugin.SettingsSchema{
|
|
Title: plugin.LocalizedText{ZhCN: "多线程下载加速", EnUS: "Parallel Download Accelerator"},
|
|
Group: "general",
|
|
Scope: "app",
|
|
Order: []string{"proxy_mode", "connections", "stall_timeout_sec"},
|
|
Properties: map[string]plugin.SettingProperty{
|
|
"proxy_mode": {
|
|
Type: "string",
|
|
Title: plugin.LocalizedText{ZhCN: "网络连接方式", EnUS: "Network mode"},
|
|
Description: plugin.LocalizedText{
|
|
ZhCN: "强制直连绕过系统代理直连 Apple CDN(不耗代理流量);智能分流仅 Apple 直连,其余跟随系统代理",
|
|
EnUS: "Direct bypasses proxies for full Apple CDN speed; Auto uses direct for Apple CDN and system proxy elsewhere",
|
|
},
|
|
Enum: []string{downloader.ProxyModeDirect, downloader.ProxyModeAuto, downloader.ProxyModeSystem},
|
|
EnumLabels: []plugin.LocalizedText{
|
|
{ZhCN: "强制直连", EnUS: "Direct"},
|
|
{ZhCN: "智能分流", EnUS: "Auto"},
|
|
{ZhCN: "系统代理", EnUS: "Proxy"},
|
|
},
|
|
Default: downloader.ProxyModeDirect,
|
|
Effect: "immediate",
|
|
Scope: "app",
|
|
Group: "general",
|
|
},
|
|
"connections": {
|
|
Type: "string",
|
|
Title: plugin.LocalizedText{ZhCN: "并发连接数", EnUS: "Concurrent connections"},
|
|
Description: plugin.LocalizedText{
|
|
ZhCN: "同时建立的独立 HTTP 分段连接数(类似 IDM/aria2 多线程)",
|
|
EnUS: "Number of independent concurrent HTTP Range connections",
|
|
},
|
|
Enum: []string{"4", "8", "16", "32"},
|
|
EnumLabels: []plugin.LocalizedText{
|
|
{ZhCN: "4 线程", EnUS: "4 connections"},
|
|
{ZhCN: "8 线程", EnUS: "8 connections"},
|
|
{ZhCN: "16 线程(推荐)", EnUS: "16 connections (recommended)"},
|
|
{ZhCN: "32 线程", EnUS: "32 connections"},
|
|
},
|
|
Default: "16",
|
|
Effect: "immediate",
|
|
Scope: "app",
|
|
Group: "general",
|
|
},
|
|
"stall_timeout_sec": {
|
|
Type: "integer",
|
|
Title: plugin.LocalizedText{ZhCN: "单连接卡顿超时 (秒)", EnUS: "Per-connection stall timeout (s)"},
|
|
Description: plugin.LocalizedText{
|
|
ZhCN: "某条连接超过该时间无数据流入时自动断开重建并重下该分片",
|
|
EnUS: "Abort and reconnect a worker automatically if no bytes arrive within this duration",
|
|
},
|
|
Default: 15,
|
|
Minimum: &minTimeout,
|
|
Maximum: &maxTimeout,
|
|
Effect: "immediate",
|
|
Scope: "app",
|
|
Group: "general",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Source: plugin.SourceBuiltin,
|
|
Trust: plugin.TrustOfficial,
|
|
Desired: desiredFn(IDDownloaderParallel, true),
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
prov := downloader.NewParallelDownloader(func() downloader.ParallelOptions {
|
|
return readParallelDownloaderOpts(cb)
|
|
})
|
|
sc.Registry().RegisterDownloader(sc, contrib, prov)
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func readParallelDownloaderOpts(cb HostCallbacks) downloader.ParallelOptions {
|
|
opts := downloader.ParallelOptions{
|
|
ID: IDDownloaderParallel,
|
|
Connections: downloader.DefaultConnections,
|
|
ProxyMode: downloader.ProxyModeDirect,
|
|
StallTimeout: downloader.DefaultStallTimeout,
|
|
}
|
|
if cb.GetSetting == nil {
|
|
return opts
|
|
}
|
|
if v, ok := cb.GetSetting(IDDownloaderParallel, "", "proxy_mode"); ok {
|
|
if s, ok := v.(string); ok && strings.TrimSpace(s) != "" {
|
|
opts.ProxyMode = strings.TrimSpace(s)
|
|
}
|
|
}
|
|
if v, ok := cb.GetSetting(IDDownloaderParallel, "", "connections"); ok {
|
|
switch x := v.(type) {
|
|
case string:
|
|
if n, err := strconv.Atoi(strings.TrimSpace(x)); err == nil && n > 0 {
|
|
opts.Connections = n
|
|
}
|
|
case float64:
|
|
if int(x) > 0 {
|
|
opts.Connections = int(x)
|
|
}
|
|
case int:
|
|
if x > 0 {
|
|
opts.Connections = x
|
|
}
|
|
}
|
|
}
|
|
if v, ok := cb.GetSetting(IDDownloaderParallel, "", "stall_timeout_sec"); ok {
|
|
switch x := v.(type) {
|
|
case float64:
|
|
if int(x) >= 5 {
|
|
opts.StallTimeout = time.Duration(int(x)) * time.Second
|
|
}
|
|
case int:
|
|
if x >= 5 {
|
|
opts.StallTimeout = time.Duration(x) * time.Second
|
|
}
|
|
}
|
|
}
|
|
return opts
|
|
}
|