Files
xiaohongshu-mcp/cookies/cookies.go
lmxdawn a8a2743a51 feat: 支持返回登录二维码与 Docker 部署 (#155)
* feat: 支持返回登录二维码与 Docker 部署

* feat: 完善扫码登录功能

* fix: 修复当存在已经登录的情况,上层还会启动 goroutine的问题,并把 mcp 的返回增加为图片格式
2025-09-25 19:44:01 +08:00

67 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cookies
import (
"os"
"path/filepath"
"github.com/pkg/errors"
)
type Cookier interface {
LoadCookies() ([]byte, error)
SaveCookies(data []byte) error
}
type localCookie struct {
path string
}
func NewLoadCookie(path string) Cookier {
if path == "" {
panic("path is required")
}
return &localCookie{
path: path,
}
}
// LoadCookies 从文件中加载 cookies。
func (c *localCookie) LoadCookies() ([]byte, error) {
data, err := os.ReadFile(c.path)
if err != nil {
return nil, errors.Wrap(err, "failed to read cookies from tmp file")
}
return data, nil
}
// SaveCookies 保存 cookies 到文件中。
func (c *localCookie) SaveCookies(data []byte) error {
return os.WriteFile(c.path, data, 0644)
}
// GetCookiesFilePath 获取 cookies 文件路径。
// 为了向后兼容,如果旧路径 /tmp/cookies.json 存在,则继续使用;
// 否则使用当前目录下的 cookies.json
func GetCookiesFilePath() string {
// 旧路径:/tmp/cookies.json
tmpDir := os.TempDir()
oldPath := filepath.Join(tmpDir, "cookies.json")
// 检查旧路径文件是否存在
if _, err := os.Stat(oldPath); err == nil {
// 文件存在,使用旧路径(向后兼容)
return oldPath
}
path := os.Getenv("COOKIES_PATH") // 判断环境变量
if path == "" {
path = "cookies.json" // fallback本地调试时用当前目录
}
// 文件不存在,使用新路径(当前目录)
return path
}