package logic

import (
	"errors"
	"fmt"
	"log"
	"net"
	"os"
	"regexp"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/samber/mo"
	"github.com/shirou/gopsutil/v4/process"
)

var (
	RegParsePid     = regexp.MustCompile(`^\[([0-9]+)\]:\s*(.*)$`)
	RegParseSession = regexp.MustCompile(`(?i)^session\s+(opened|closed)\s+for\s+local\s+user\s+(.*)\s+from\s+\[(.*)\]`)

	SftpLogMap  = make(map[int32]*SftpLogSet) // 全局的，用于记录sftp信息的map，key为进程id，value为日志集
	SftpLogLock = sync.RWMutex{}              // 保护SftpLogMap的锁
)

type GetSLA interface {
	GetFfileAction() SftpLogAction
	SetPid(pid int32)
	GetPid() int32
}

type SftpLogAction string

const (
	SLAUnknown      SftpLogAction = ""
	SLAOpen         SftpLogAction = "open "
	SLAClose        SftpLogAction = "close "
	SLARemove       SftpLogAction = "remove name "
	SLARename       SftpLogAction = "rename old "
	SLAForceClose   SftpLogAction = "forced close "
	SLAOpenSession  SftpLogAction = "session opened"
	SLACloseSession SftpLogAction = "session closed"
)

func parseSLA(s string, t time.Time) GetSLA {
	prev := ""
	if strings.HasPrefix(s, string(SLAOpen)) {
		fields := strings.Fields(s)
		result := SftpLogOpen{Time: t}
		for i, v := range fields[1:] {
			if i == 0 {
				result.Path = strings.Trim(v, `"`)
				continue
			}
			switch v {
			case "flags", "mode":
				prev = v
			default:
				switch prev {
				case "flags":
					result.Flags = strings.Split(v, ",")
				case "mode":
					p, err := strconv.ParseUint(v, 8, 32)
					if err == nil {
						result.Mode = mo.None[uint32]()
					} else {
						result.Mode = mo.Some(uint32(p))
					}
				}
			}
		}
		return &result
	}
	if strings.HasPrefix(s, string(SLAClose)) {
		fields := strings.Fields(s)
		result := SftpLogClose{Time: t}
		for k, v := range fields[1:] {
			if k == 0 {
				result.Path = strings.Trim(v, `"`)
			}
			switch v {
			case "bytes", "read", "written":
				prev = v
			default:
				switch prev {
				case "read":
					read, err := strconv.ParseUint(v, 10, 64)
					if err != nil {
						result.Read = mo.None[uint64]()
					} else {
						result.Read = mo.Some(read)
					}
				case "written":
					read, err := strconv.ParseUint(v, 10, 64)
					if err != nil {
						result.Write = mo.None[uint64]()
					} else {
						result.Write = mo.Some(read)
					}
				}
			}
		}
		return &result
	}
	if strings.HasPrefix(s, string(SLARemove)) {
		fields := strings.Fields(s)
		result := SftpLogRemove{Time: t}
		result.Path = fields[2]
		return &result
	}
	if strings.HasPrefix(s, string(SLARename)) {
		fields := strings.Fields(s)
		result := SftpLogRename{Time: t}
		for _, v := range fields[1:] {
			switch v {
			case "old", "new":
				prev = v
			default:
				switch prev {
				case "old":
					result.Old = strings.Trim(v, `"`)
				case "new":
					result.New = strings.Trim(v, `"`)
				}
			}
		}
		return &result
	}
	if strings.HasPrefix(s, string(SLAForceClose)) {
		fields := strings.Fields(s)
		result := SftpLogForceClose{Time: t}
		for k, v := range fields[2:] {
			if k == 0 {
				result.Path = strings.Trim(v, `"`)
			}
			switch v {
			case "bytes", "read", "written":
				prev = v
			default:
				switch prev {
				case "read":
					read, err := strconv.ParseUint(v, 10, 64)
					if err != nil {
						result.Read = mo.None[uint64]()
					} else {
						result.Read = mo.Some(read)
					}
				case "written":
					read, err := strconv.ParseUint(v, 10, 64)
					if err != nil {
						result.Write = mo.None[uint64]()
					} else {
						result.Write = mo.Some(read)
					}
				}
			}
		}
		return &result
	}
	if strings.HasPrefix(s, string(SLAOpenSession)) {
		result := SftpLogOpenSession{Time: t}
		items := RegParseSession.FindStringSubmatch(s)
		result.User = items[2]
		result.From = items[3]
		return &result
	}
	if strings.HasPrefix(s, string(SLACloseSession)) {
		result := SftpLogCloseSession{Time: t}
		items := RegParseSession.FindStringSubmatch(s)
		result.User = items[2]
		result.From = items[3]
		return &result
	}
	return nil
}

type SftpLogOpen struct {
	Path  string
	Flags []string
	Mode  mo.Option[uint32]
	Time  time.Time
	Pid   int32
}

func (slo *SftpLogOpen) GetFfileAction() SftpLogAction {
	return SLAOpen
}

func (slo *SftpLogOpen) GetPid() int32 {
	return slo.Pid
}

func (slo *SftpLogOpen) SetPid(pid int32) {
	slo.Pid = pid
}

type SftpLogClose struct {
	Path        string
	Read, Write mo.Option[uint64]
	Time        time.Time
	Pid         int32
}

func (s *SftpLogClose) GetFfileAction() SftpLogAction {
	return SLAClose
}

func (s *SftpLogClose) GetPid() int32 {
	return s.Pid
}

func (s *SftpLogClose) SetPid(pid int32) {
	s.Pid = pid
}

type SftpLogRemove struct {
	Path string
	Time time.Time
	Pid  int32
}

func (slr *SftpLogRemove) GetFfileAction() SftpLogAction {
	return SLARemove
}

func (s *SftpLogRemove) GetPid() int32 {
	return s.Pid
}

func (s *SftpLogRemove) SetPid(pid int32) {
	s.Pid = pid
}

type SftpLogRename struct {
	Old, New string
	Time     time.Time
	Pid      int32
}

func (slr *SftpLogRename) GetFfileAction() SftpLogAction {
	return SLARename
}

func (s *SftpLogRename) GetPid() int32 {
	return s.Pid
}

func (s *SftpLogRename) SetPid(pid int32) {
	s.Pid = pid
}

type SftpLogForceClose struct {
	Path        string
	Read, Write mo.Option[uint64]
	Time        time.Time
	Pid         int32
}

func (slfc *SftpLogForceClose) GetFfileAction() SftpLogAction {
	return SLAForceClose
}

func (s *SftpLogForceClose) GetPid() int32 {
	return s.Pid
}

func (s *SftpLogForceClose) SetPid(pid int32) {
	s.Pid = pid
}

type SftpLogOpenSession struct {
	Time time.Time
	User string
	From string
	Pid  int32
}

func (slos *SftpLogOpenSession) GetFfileAction() SftpLogAction {
	return SLAOpenSession
}

func (s *SftpLogOpenSession) GetPid() int32 {
	return s.Pid
}

func (s *SftpLogOpenSession) SetPid(pid int32) {
	s.Pid = pid
}

type SftpLogCloseSession struct {
	Time time.Time
	User string
	From string
	Pid  int32
}

func (slos *SftpLogCloseSession) GetFfileAction() SftpLogAction {
	return SLACloseSession
}

func (s *SftpLogCloseSession) GetPid() int32 {
	return s.Pid
}

func (s *SftpLogCloseSession) SetPid(pid int32) {
	s.Pid = pid
}

// SftpLogSet 存储一个sftp进程的相关日志信息
type SftpLogSet struct {
	Pid          int32                // 进程pid
	User         string               // 用户名或uid
	From         string               // 连接地址
	SessionStart mo.Option[time.Time] // 对话开始时间
	SessionClose mo.Option[time.Time] // 会话断开时间
	OpenedFile   map[string]*FileInfo // 文件日志
	IsTabby      mo.Option[bool]      // 进程是否为tabby
	IsAlive      bool                 // 进程是否存活
	Lock         sync.RWMutex         // 保护OpenedFile读写的锁
}

func NewSftpLogSet(pid int32, user *string, startTime *time.Time) *SftpLogSet {
	result := &SftpLogSet{
		Pid:          pid,
		User:         "",
		SessionStart: mo.None[time.Time](),
		SessionClose: mo.None[time.Time](),
		OpenedFile:   make(map[string]*FileInfo),
		IsTabby:      mo.None[bool](),
		IsAlive:      true,
		Lock:         sync.RWMutex{},
	}
	if user != nil {
		result.User = *user
	} else {
		p, err := process.NewProcess(pid)
		if err == nil {
			user, err := p.Username()
			if err == nil {
				result.User = user
			}
		}
	}
	if startTime != nil {
		result.SessionStart = mo.Some(*startTime)
	}
	return result
}

// CheckAlive 检查进程是否存活
func (sls *SftpLogSet) CheckAlive() (bool, error) {
	p, err := process.NewProcess(sls.Pid)
	if err != nil {
		sls.IsAlive = false
		return false, err
	}
	if p == nil {
		sls.IsAlive = false
		return false, nil
	}
	cmd, err := p.Cmdline()
	if err != nil {
		sls.IsAlive = false
		return false, err
	}
	alive := strings.Contains(cmd, "sftp-server")
	sls.IsAlive = alive
	return alive, nil
}

type FileInfo struct {
	Path    string
	NameSet map[string]bool // 可用名称
	Log     []GetSLA        // 文件操作日志
	LogSet  *SftpLogSet
}

func NewFileInfo(path string, ls *SftpLogSet) *FileInfo {
	return &FileInfo{
		Path:    path,
		NameSet: make(map[string]bool),
		Log:     make([]GetSLA, 0, 4),
		LogSet:  ls,
	}
}

// CheckNeedScan 检查是否需要扫描文件
func (fi *FileInfo) CheckNeedScan() {
	// 检查最后一个日志的种类
	l := len(fi.Log)
	if l == 0 {
		return
	}
	last := fi.Log[l-1]
	switch act := last.(type) {
	case *SftpLogClose:
		if strings.HasSuffix(act.Path, "tabby-upload") {
			return
		}
		log.Println("scan file: ", act.Path)
		have, _ := ScanFile(act.Path)
		if have {
			log.Println(act.Path, "有病毒")
		} else {
			log.Println(act.Path, "没病毒")
		}
	case *SftpLogRename:
		if strings.HasSuffix(act.Old, "tabby-upload") {
			log.Println("scan file: ", act.New)
			have, _ := ScanFile(act.New)
			if have {
				log.Println(act.New, "有病毒")
			} else {
				log.Println(act.New, "没病毒")
			}
		}
	case *SftpLogForceClose:
		log.Println("scan file: ", act.Path)
		have, _ := ScanFile(act.Path)
		if have {
			log.Println(act.Path, "有病毒")
		} else {
			log.Println(act.Path, "没病毒")
		}
	}
}

func InsertAction(action GetSLA) {
	if action == nil {
		return
	}
	pid := action.GetPid()
	SftpLogLock.Lock()
	defer SftpLogLock.Unlock()
	switch act := action.(type) {
	case *SftpLogOpenSession:
		// 新建
		sls := NewSftpLogSet(pid, &act.User, &act.Time)
		sls.From = act.From
		SftpLogMap[pid] = sls
		return
	case *SftpLogCloseSession:
		delete(SftpLogMap, pid)
		return
	case *SftpLogOpen:
		ls, have := SftpLogMap[pid]
		if !have {
			ls = NewSftpLogSet(pid, nil, &act.Time)
			SftpLogMap[pid] = ls
		}
		istabby := strings.HasSuffix(act.Path, ".tabby-upload")
		ls.IsTabby = mo.Some(istabby)
		ls.Lock.Lock()
		finfo, have := ls.OpenedFile[act.Path]
		if !have {
			finfo = NewFileInfo(act.Path, ls)
			ls.OpenedFile[act.Path] = finfo
			finfo.Log = append(finfo.Log, act)
		} else {
			// 发生重复了？？？
		}
		ls.Lock.Unlock()
		return
	case *SftpLogClose:
		// 关闭文件
		ls, have := SftpLogMap[pid]
		if !have {
			ls = NewSftpLogSet(pid, nil, &act.Time)
			SftpLogMap[pid] = ls
		}
		ls.Lock.Lock()
		finfo, have := ls.OpenedFile[act.Path]
		if !have {
			finfo = NewFileInfo(act.Path, ls)
			ls.OpenedFile[act.Path] = finfo
		}
		finfo.Log = append(finfo.Log, act)
		ls.Lock.Unlock()
		finfo.CheckNeedScan()
		return
	case *SftpLogRemove:
		// 查看是否为tabby，如果是tabby，需要检查是否为上传的文件
		// 如果没有，不创建日志
		ls, have := SftpLogMap[pid]
		if !have {
			ls = NewSftpLogSet(pid, nil, &act.Time)
			SftpLogMap[pid] = ls
		}
		a, b := ls.IsTabby.Get()
		if a && b {
			// 是tabby上传的文件，需要记录日志
			ls.Lock.Lock()
			finfo, have := ls.OpenedFile[act.Path]
			if !have {
				finfo = NewFileInfo(act.Path, ls)
				ls.OpenedFile[act.Path] = finfo
			}
			finfo.Log = append(finfo.Log, act)
			ls.Lock.Unlock()
		} else {
			delete(ls.OpenedFile, act.Path)
		}
		return
	case *SftpLogRename:
		// 查看是否为tabby，如果是tabby，需要检查是否为上传的文件
		ls, have := SftpLogMap[pid]
		if !have {
			ls = NewSftpLogSet(pid, nil, &act.Time)
			SftpLogMap[pid] = ls
		}
		istabby := strings.HasSuffix(act.Old, ".tabby-upload")
		ls.IsTabby = mo.Some(istabby)
		if istabby {
			// 是tabby上传的文件，需要记录日志
			ls.Lock.Lock()
			finfo, have := ls.OpenedFile[act.Old]
			if !have {
				finfo = NewFileInfo(act.Old, ls)
				ls.OpenedFile[act.Old] = finfo
			}
			finfo.Log = append(finfo.Log, act)
			ls.Lock.Unlock()
			finfo.CheckNeedScan()
		}
		return
	case *SftpLogForceClose:
		// 是强制中断，需要检查是否为上传的文件
		ls, have := SftpLogMap[pid]
		if !have {
			ls = NewSftpLogSet(pid, nil, &act.Time)
			SftpLogMap[pid] = ls
		}
		ls.Lock.Lock()
		finfo, have := ls.OpenedFile[act.Path]
		if !have {
			finfo = NewFileInfo(act.Path, ls)
			ls.OpenedFile[act.Path] = finfo
		}
		finfo.Log = append(finfo.Log, act)
		ls.Lock.Unlock()
		finfo.CheckNeedScan()
		return
	}
}

func ParseSftpLog(s string) (GetSLA, error) {
	if !strings.Contains(s, "sftp-server") {
		// 不是sftp日志
		return nil, nil
	}
	items := strings.SplitN(s, "sftp-server", 2)
	if len(items) != 2 {
		return nil, errors.New("parse error")
	}
	fs := strings.Split(strings.Trim(items[0], " "), " ")
	if len(fs) != 4 {
		return nil, errors.New("parse error")
	}
	t, err := time.Parse(time.Stamp, strings.Join(fs[:3], " "))
	t = t.AddDate(time.Now().Year(), 0, 0)
	if err != nil {
		return nil, err
	}
	if !RegParsePid.MatchString(items[1]) {
		return nil, errors.New("parse error")
	}
	fs = RegParsePid.FindStringSubmatch(items[1])
	if len(fs) != 3 {
		return nil, errors.New("parse error")
	}
	pid, err := strconv.ParseInt(fs[1], 10, 32)
	if err != nil {
		return nil, err
	}
	fta := parseSLA(fs[2], t)
	if fta == nil {
		return nil, nil
	}
	fta.SetPid(int32(pid))
	return fta, nil
}

func StartSftpMonitor() {

	regRemove := regexp.MustCompile(`^<\d+>(.*)$`)
	os.Remove("/tmp/rsyslog.sock")
	conn, err := net.ListenPacket("unixgram", "/tmp/rsyslog.sock")
	if err != nil {
		log.Fatal(err)
	}
	buffer := make([]byte, 16384)
	for {
		n, _, err := conn.ReadFrom(buffer)
		if err != nil {
			break
		}
		content := string(buffer[:n])
		items := regRemove.FindStringSubmatch(content)
		if len(items) == 0 {
			continue
		}
		if strings.Contains(items[1], "sftp-server") {
			fmt.Println(items[1])
			l, err := ParseSftpLog(items[1])
			if err != nil {
				log.Println(err)
			} else {
				InsertAction(l)
			}
		}
	}
	os.Remove("/tmp/rsyslog.sock")
}
