feat: trust peer
This commit is contained in:
@@ -44,11 +44,7 @@ func (s *Service) SendFile(target *discovery.Peer, targetIP string, filePath str
|
||||
|
||||
task := NewTransfer(
|
||||
taskID,
|
||||
NewSender(
|
||||
s.discoveryService.GetID(),
|
||||
s.config.GetHostName(),
|
||||
WithReceiverIP(targetIP, s.discoveryService),
|
||||
),
|
||||
s.discoveryService.GetSelf(),
|
||||
WithFileName(filepath.Base(filePath)),
|
||||
WithFileSize(stat.Size()),
|
||||
WithType(TransferTypeSend),
|
||||
@@ -111,11 +107,7 @@ func (s *Service) SendFolder(target *discovery.Peer, targetIP string, folderPath
|
||||
|
||||
task := NewTransfer(
|
||||
taskID,
|
||||
NewSender(
|
||||
s.discoveryService.GetID(),
|
||||
s.config.GetHostName(),
|
||||
WithReceiverIP(targetIP, s.discoveryService),
|
||||
),
|
||||
s.discoveryService.GetSelf(),
|
||||
WithFileName(filepath.Base(folderPath)),
|
||||
WithFileSize(size),
|
||||
WithType(TransferTypeSend),
|
||||
@@ -164,11 +156,7 @@ func (s *Service) SendText(target *discovery.Peer, targetIP string, text string)
|
||||
r := bytes.NewReader([]byte(text))
|
||||
task := NewTransfer(
|
||||
taskID,
|
||||
NewSender(
|
||||
s.discoveryService.GetID(),
|
||||
s.config.GetHostName(),
|
||||
WithReceiverIP(targetIP, s.discoveryService),
|
||||
),
|
||||
s.discoveryService.GetSelf(),
|
||||
WithFileSize(int64(len(text))),
|
||||
WithType(TransferTypeSend),
|
||||
WithContentType(ContentTypeText),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package transfer
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"mesh-drop/internal/discovery"
|
||||
"time"
|
||||
)
|
||||
@@ -37,7 +36,7 @@ const (
|
||||
type Transfer struct {
|
||||
ID string `json:"id" binding:"required"` // 传输会话 ID
|
||||
CreateTime int64 `json:"create_time"` // 创建时间
|
||||
Sender Sender `json:"sender" binding:"required"` // 发送者
|
||||
Sender discovery.Peer `json:"sender" binding:"required"` // 发送者
|
||||
FileName string `json:"file_name"` // 文件名
|
||||
FileSize int64 `json:"file_size"` // 文件大小 (字节)
|
||||
SavePath string `json:"savePath"` // 保存路径
|
||||
@@ -53,7 +52,7 @@ type Transfer struct {
|
||||
|
||||
type TransferOption func(*Transfer)
|
||||
|
||||
func NewTransfer(id string, sender Sender, opts ...TransferOption) *Transfer {
|
||||
func NewTransfer(id string, sender discovery.Peer, opts ...TransferOption) *Transfer {
|
||||
t := &Transfer{
|
||||
ID: id,
|
||||
CreateTime: time.Now().UnixMilli(),
|
||||
@@ -122,41 +121,6 @@ func WithToken(token string) TransferOption {
|
||||
}
|
||||
}
|
||||
|
||||
type Sender struct {
|
||||
ID string `json:"id" binding:"required"` // 发送者 ID
|
||||
Name string `json:"name" binding:"required"` // 发送者名称
|
||||
IP string `json:"ip" binding:"required"` // 发送者 IP
|
||||
}
|
||||
|
||||
type NewSenderOption func(*Sender)
|
||||
|
||||
func NewSender(id string, name string, opts ...NewSenderOption) Sender {
|
||||
s := &Sender{
|
||||
ID: id,
|
||||
Name: name,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(s)
|
||||
}
|
||||
return *s
|
||||
}
|
||||
|
||||
func WithIP(ip string) NewSenderOption {
|
||||
return func(s *Sender) {
|
||||
s.IP = ip
|
||||
}
|
||||
}
|
||||
|
||||
func WithReceiverIP(ip string, discoveryService *discovery.Service) NewSenderOption {
|
||||
return func(s *Sender) {
|
||||
ip, ok := discoveryService.GetLocalIPInSameSubnet(ip)
|
||||
if !ok {
|
||||
slog.Error("Failed to get local IP in same subnet", "ip", ip, "component", "transfer-client")
|
||||
}
|
||||
s.IP = ip
|
||||
}
|
||||
}
|
||||
|
||||
// Progress 用户前端传输进度
|
||||
type Progress struct {
|
||||
Current int64 `json:"current"` // 当前进度
|
||||
|
||||
@@ -43,7 +43,13 @@ func (s *Service) handleAsk(c *gin.Context) {
|
||||
task.DecisionChan = make(chan Decision, 1)
|
||||
s.StoreTransferToList(&task)
|
||||
|
||||
if s.config.GetAutoAccept() {
|
||||
// 从本地获取 peer 检查是否 mismatch
|
||||
peer, ok := s.discoveryService.GetPeerByID(task.Sender.ID)
|
||||
if ok {
|
||||
task.Sender.TrustMismatch = peer.TrustMismatch
|
||||
}
|
||||
|
||||
if s.config.GetAutoAccept() || (s.config.IsTrustedPeer(task.Sender.ID) && !task.Sender.TrustMismatch) {
|
||||
task.DecisionChan <- Decision{
|
||||
ID: task.ID,
|
||||
Accepted: true,
|
||||
@@ -54,7 +60,7 @@ func (s *Service) handleAsk(c *gin.Context) {
|
||||
_ = s.notifier.SendNotification(notifications.NotificationOptions{
|
||||
ID: uuid.New().String(),
|
||||
Title: "File Transfer Request",
|
||||
Body: fmt.Sprintf("%s(%s) wants to transfer %s", task.Sender.Name, task.Sender.IP, task.FileName),
|
||||
Body: fmt.Sprintf("%s wants to transfer %s", task.Sender.Name, task.FileName),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -74,6 +80,11 @@ func (s *Service) handleAsk(c *gin.Context) {
|
||||
})
|
||||
} else {
|
||||
task.Status = TransferStatusRejected
|
||||
c.JSON(http.StatusOK, TransferAskResponse{
|
||||
ID: task.ID,
|
||||
Accepted: false,
|
||||
Message: "Transfer rejected",
|
||||
})
|
||||
}
|
||||
case <-c.Request.Context().Done():
|
||||
// 发送端放弃
|
||||
|
||||
Reference in New Issue
Block a user