Browse Source

Added experimental auto renew session for smbfs

Toby Chui 3 years ago
parent
commit
c24ae81139
2 changed files with 32 additions and 5 deletions
  1. 32 3
      mod/filesystem/abstractions/smbfs/smbfs.go
  2. 0 2
      mod/filesystem/fssort/fssort.go

+ 32 - 3
mod/filesystem/abstractions/smbfs/smbfs.go

@@ -26,8 +26,10 @@ import (
 type ServerMessageBlockFileSystemAbstraction struct {
 type ServerMessageBlockFileSystemAbstraction struct {
 	UUID      string
 	UUID      string
 	Hierarchy string
 	Hierarchy string
+	root      string
 	ipaddr    string
 	ipaddr    string
 	user      string
 	user      string
+	pass      string
 	conn      *net.Conn
 	conn      *net.Conn
 	session   *smb2.Session
 	session   *smb2.Session
 	share     *smb2.Share
 	share     *smb2.Share
@@ -62,16 +64,43 @@ func NewServerMessageBlockFileSystemAbstraction(uuid string, hierarchy string, i
 		return ServerMessageBlockFileSystemAbstraction{}, err
 		return ServerMessageBlockFileSystemAbstraction{}, err
 	}
 	}
 
 
-	log.Println("[SMB-FS] Connected to remote: " + ipaddr)
-	return ServerMessageBlockFileSystemAbstraction{
+	fsAbstraction := ServerMessageBlockFileSystemAbstraction{
 		UUID:      uuid,
 		UUID:      uuid,
 		Hierarchy: hierarchy,
 		Hierarchy: hierarchy,
+		root:      rootShare,
 		ipaddr:    ipaddr,
 		ipaddr:    ipaddr,
 		user:      username,
 		user:      username,
+		pass:      password,
 		conn:      &conn,
 		conn:      &conn,
 		session:   s,
 		session:   s,
 		share:     fs,
 		share:     fs,
-	}, nil
+	}
+
+	//Create a ticker to check for renewing conncetion once an hour
+	ticker := time.NewTicker(1 * time.Hour)
+	done := make(chan bool)
+	go func(s *ServerMessageBlockFileSystemAbstraction) {
+		for {
+			select {
+			case <-done:
+				return
+			case <-ticker.C:
+				//Unable to list share due to session timeout. Close the handler and try connecting again.
+				s.share.Umount()
+				time.Sleep(300 * time.Millisecond)
+				fs, err := s.session.Mount(s.root)
+				if err != nil {
+					fmt.Println("[SMBFS] Unable to remount " + s.root)
+					return
+				}
+				fmt.Println("[SMBFS] Session Renewed for " + s.root)
+				s.share = fs
+			}
+		}
+	}(&fsAbstraction)
+
+	log.Println("[SMB-FS] Connected to remote: " + ipaddr)
+	return fsAbstraction, nil
 }
 }
 
 
 func (a ServerMessageBlockFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {
 func (a ServerMessageBlockFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {

+ 0 - 2
mod/filesystem/fssort/fssort.go

@@ -1,7 +1,6 @@
 package fssort
 package fssort
 
 
 import (
 import (
-	"fmt"
 	"io/fs"
 	"io/fs"
 	"path/filepath"
 	"path/filepath"
 	"sort"
 	"sort"
@@ -23,7 +22,6 @@ func SortFileList(filelistRealpath []string, fileInfos []fs.FileInfo, sortMode s
 	parsedFilelist := []*sortBufferedStructure{}
 	parsedFilelist := []*sortBufferedStructure{}
 	if len(filelistRealpath) != len(fileInfos) {
 	if len(filelistRealpath) != len(fileInfos) {
 		//Invalid usage
 		//Invalid usage
-		fmt.Println("[fssort] Invalid Usage!")
 		return filelistRealpath
 		return filelistRealpath
 	}
 	}
 	for i, file := range filelistRealpath {
 	for i, file := range filelistRealpath {