Sfoglia il codice sorgente

Added experimental smb auto reconnection

Toby Chui 2 anni fa
parent
commit
5b4ed98a2f
1 ha cambiato i file con 46 aggiunte e 1 eliminazioni
  1. 46 1
      mod/filesystem/abstractions/smbfs/smbfs.go

+ 46 - 1
mod/filesystem/abstractions/smbfs/smbfs.go

@@ -88,13 +88,58 @@ func NewServerMessageBlockFileSystemAbstraction(uuid string, hierarchy string, i
 				return
 			case <-ticker.C:
 				//Ping the smb server to make sure it knows we are alive
-				s.share.Stat("")
+				_, err := s.share.Stat("")
+				if err != nil {
+					//Disconnect the connection and re-establish connection
+					s.reconnect()
+
+				}
 			}
 		}
 	}(&fsAbstraction)
 	return fsAbstraction, nil
 }
 
+func (a *ServerMessageBlockFileSystemAbstraction) reconnect() error {
+	//Close existing connection
+	a.Close()
+
+	//Restart connection
+	nd := net.Dialer{Timeout: 10 * time.Second}
+	conn, err := nd.Dial("tcp", a.ipaddr)
+	if err != nil {
+		log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
+		return err
+	}
+
+	d := &smb2.Dialer{
+		Initiator: &smb2.NTLMInitiator{
+			User:     a.user,
+			Password: a.pass,
+		},
+	}
+
+	s, err := d.Dial(conn)
+	if err != nil {
+		log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
+		return err
+	}
+
+	//Mound remote storage
+	fs, err := s.Mount(a.root)
+	if err != nil {
+		log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
+		return err
+	}
+
+	a.conn = &conn
+	a.session = s
+	a.share = fs
+
+	fmt.Println("[SMB-FS] Connection restarted")
+	return nil
+}
+
 func (a ServerMessageBlockFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {
 	filename = filterFilepath(filename)
 	filename = toWinPath(filename)