📝 Add docstrings to test
Docstrings generation was requested by @sky22333. * https://github.com/sky22333/hubproxy/pull/13#issuecomment-2965775900 The following files were modified: * `src/config.go` * `src/main.go` * `src/proxysh.go` * `src/skopeo_service.go`
This commit is contained in:
committed by
GitHub
parent
a8498ad24a
commit
ac0cab22ef
@@ -197,7 +197,9 @@ func LoadConfig() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 🔥 启用Viper自动热重载
|
||||
// enableViperHotReload enables hot reloading of the configuration file using Viper.
|
||||
// If hot reload is already enabled, the function returns immediately.
|
||||
// On detecting changes to the configuration file, it triggers a reload of the application configuration.
|
||||
func enableViperHotReload() {
|
||||
if isViperEnabled {
|
||||
return
|
||||
|
||||
@@ -152,6 +152,9 @@ func proxy(c *gin.Context, u string) {
|
||||
}
|
||||
|
||||
|
||||
// proxyWithRedirect forwards an HTTP request to the specified URL, handling redirects, file size limits, and intelligent content processing.
|
||||
//
|
||||
// If the response is a redirect, the function recursively follows it up to a maximum of 20 times. It enforces a file size limit based on configuration and processes the response body using a smart processor. If processing fails, it gracefully falls back to direct proxying of the original response. Security-related headers are removed from the proxied response. The function rewrites redirect locations for known URL patterns to relative paths when appropriate. Processed content is streamed to the client with appropriate headers.
|
||||
func proxyWithRedirect(c *gin.Context, u string, redirectCount int) {
|
||||
// 限制最大重定向次数,防止无限递归
|
||||
const maxRedirects = 20
|
||||
@@ -267,6 +270,7 @@ func proxyWithRedirect(c *gin.Context, u string, redirectCount int) {
|
||||
}
|
||||
}
|
||||
|
||||
// checkURL returns the first set of submatches for the input URL against known regex patterns, or nil if no pattern matches.
|
||||
func checkURL(u string) []string {
|
||||
for _, exp := range exps {
|
||||
if matches := exp.FindStringSubmatch(u); matches != nil {
|
||||
|
||||
@@ -57,7 +57,7 @@ var (
|
||||
cleanupOnce sync.Once
|
||||
)
|
||||
|
||||
// GetSmartProcessor 获取智能处理器实例
|
||||
// GetSmartProcessor returns the singleton instance of SmartProcessor, initializing it with GitHub URL detection, script pattern recognition, buffer pooling, and background cache cleanup if not already created.
|
||||
func GetSmartProcessor() *SmartProcessor {
|
||||
smartOnce.Do(func() {
|
||||
smartProcessor = &SmartProcessor{
|
||||
@@ -82,7 +82,9 @@ func GetSmartProcessor() *SmartProcessor {
|
||||
return smartProcessor
|
||||
}
|
||||
|
||||
// ProcessSmart 智能处理函数
|
||||
// ProcessSmart reads content from the input, detects and rewrites GitHub URLs in script-like text using a proxy host, and returns the processed content as an io.Reader along with its length.
|
||||
// If the processor is disabled, the input is returned unmodified. Large files and non-script content are bypassed. Results are cached for performance.
|
||||
// Returns an error if reading the input fails.
|
||||
func ProcessSmart(input io.ReadCloser, isCompressed bool, host string) (io.Reader, int64, error) {
|
||||
processor := GetSmartProcessor()
|
||||
|
||||
|
||||
@@ -125,7 +125,8 @@ func initSkopeoRoutes(router *gin.Engine) {
|
||||
go cleanupExpiredTasks()
|
||||
}
|
||||
|
||||
// 处理WebSocket连接
|
||||
// handleWebSocket upgrades an HTTP connection to a WebSocket for real-time task progress updates.
|
||||
// It registers the client, starts heartbeat and message handling goroutines, sends the current task status if available, and manages connection liveness with ping/pong and timeout handlers.
|
||||
func handleWebSocket(c *gin.Context) {
|
||||
taskID := c.Param("taskId")
|
||||
|
||||
@@ -350,7 +351,7 @@ func generateTaskID() string {
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// 初始化任务并启动进度处理器
|
||||
// initTask initializes a DownloadTask by setting up its progress update channels, starting a periodic status updater to keep WebSocket connections alive, and launching a goroutine to process progress updates and maintain task and image status.
|
||||
func initTask(task *DownloadTask) {
|
||||
// 创建进度更新通道和控制通道
|
||||
task.updateChan = make(chan *ProgressUpdate, 100)
|
||||
@@ -1127,7 +1128,8 @@ func createZipArchive(task *DownloadTask) (string, error) {
|
||||
return zipFilePath, nil
|
||||
}
|
||||
|
||||
// 发送任务更新到WebSocket
|
||||
// sendTaskUpdate sends the current status and progress of a download task to the associated WebSocket client, if the client is active.
|
||||
// It serializes a snapshot of the task state and attempts to deliver it within a timeout, logging if the send fails.
|
||||
func sendTaskUpdate(task *DownloadTask) {
|
||||
// 复制任务状态避免序列化时锁定
|
||||
taskCopy := &DownloadTask{
|
||||
@@ -1397,7 +1399,7 @@ func checkForCompletionMarkers(output string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// cleanupWebSocketConnections 定期清理无效的WebSocket连接
|
||||
// cleanupWebSocketConnections periodically removes inactive or unresponsive WebSocket clients from the global clients map.
|
||||
func cleanupWebSocketConnections() {
|
||||
ticker := time.NewTicker(2 * time.Minute) // 增加清理频率
|
||||
defer ticker.Stop()
|
||||
|
||||
Reference in New Issue
Block a user