smbfs.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package smbfs
  2. import (
  3. "fmt"
  4. "io"
  5. "io/fs"
  6. "log"
  7. "net"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/hirochachacha/go-smb2"
  13. "imuslab.com/arozos/mod/filesystem/fsdef"
  14. )
  15. /*
  16. Server Message Block.go
  17. This is a file abstraction that mount SMB folders onto ArozOS as virtual drive
  18. */
  19. type ServerMessageBlockFileSystemAbstraction struct {
  20. UUID string
  21. Hierarchy string
  22. ipaddr string
  23. user string
  24. conn *net.Conn
  25. session *smb2.Session
  26. share *smb2.Share
  27. }
  28. func NewServerMessageBlockFileSystemAbstraction(uuid string, hierarchy string, ipaddr string, rootShare string, username string, password string) (ServerMessageBlockFileSystemAbstraction, error) {
  29. log.Println("[SMB-FS] Connecting to " + uuid + ":/ (" + ipaddr + ")")
  30. nd := net.Dialer{Timeout: 10 * time.Second}
  31. conn, err := nd.Dial("tcp", ipaddr)
  32. if err != nil {
  33. log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
  34. return ServerMessageBlockFileSystemAbstraction{}, err
  35. }
  36. d := &smb2.Dialer{
  37. Initiator: &smb2.NTLMInitiator{
  38. User: username,
  39. Password: password,
  40. },
  41. }
  42. s, err := d.Dial(conn)
  43. if err != nil {
  44. log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
  45. return ServerMessageBlockFileSystemAbstraction{}, err
  46. }
  47. //Mound remote storage
  48. fs, err := s.Mount(rootShare)
  49. if err != nil {
  50. log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
  51. return ServerMessageBlockFileSystemAbstraction{}, err
  52. }
  53. log.Println("[SMB-FS] Connected to remote: " + ipaddr)
  54. return ServerMessageBlockFileSystemAbstraction{
  55. UUID: uuid,
  56. Hierarchy: hierarchy,
  57. ipaddr: ipaddr,
  58. user: username,
  59. conn: &conn,
  60. session: s,
  61. share: fs,
  62. }, nil
  63. }
  64. func (a ServerMessageBlockFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {
  65. filename = filterFilepath(filename)
  66. filename = toWinPath(filename)
  67. return a.share.Chmod(filename, mode)
  68. }
  69. func (a ServerMessageBlockFileSystemAbstraction) Chown(filename string, uid int, gid int) error {
  70. return fsdef.ErrOperationNotSupported
  71. }
  72. func (a ServerMessageBlockFileSystemAbstraction) Chtimes(filename string, atime time.Time, mtime time.Time) error {
  73. filename = filterFilepath(filename)
  74. filename = toWinPath(filename)
  75. return a.share.Chtimes(filename, atime, mtime)
  76. }
  77. func (a ServerMessageBlockFileSystemAbstraction) Create(filename string) (*os.File, error) {
  78. return nil, fsdef.ErrOperationNotSupported
  79. }
  80. func (a ServerMessageBlockFileSystemAbstraction) Mkdir(filename string, mode os.FileMode) error {
  81. filename = filterFilepath(filename)
  82. filename = toWinPath(filename)
  83. return a.share.Mkdir(filename, mode)
  84. }
  85. func (a ServerMessageBlockFileSystemAbstraction) MkdirAll(filename string, mode os.FileMode) error {
  86. filename = filterFilepath(filename)
  87. filename = toWinPath(filename)
  88. return a.share.MkdirAll(filename, mode)
  89. }
  90. func (a ServerMessageBlockFileSystemAbstraction) Name() string {
  91. return ""
  92. }
  93. func (a ServerMessageBlockFileSystemAbstraction) Open(filename string) (*os.File, error) {
  94. return nil, fsdef.ErrOperationNotSupported
  95. }
  96. func (a ServerMessageBlockFileSystemAbstraction) OpenFile(filename string, flag int, perm os.FileMode) (*os.File, error) {
  97. return nil, fsdef.ErrOperationNotSupported
  98. }
  99. func (a ServerMessageBlockFileSystemAbstraction) Remove(filename string) error {
  100. filename = filterFilepath(filename)
  101. filename = toWinPath(filename)
  102. return a.share.Remove(filename)
  103. }
  104. func (a ServerMessageBlockFileSystemAbstraction) RemoveAll(filename string) error {
  105. filename = filterFilepath(filename)
  106. filename = toWinPath(filename)
  107. return a.share.RemoveAll(filename)
  108. }
  109. func (a ServerMessageBlockFileSystemAbstraction) Rename(oldname, newname string) error {
  110. oldname = toWinPath(filterFilepath(oldname))
  111. newname = toWinPath(filterFilepath(newname))
  112. return a.share.Rename(oldname, newname)
  113. }
  114. func (a ServerMessageBlockFileSystemAbstraction) Stat(filename string) (os.FileInfo, error) {
  115. filename = toWinPath(filterFilepath(filename))
  116. return a.share.Stat(filename)
  117. }
  118. func (a ServerMessageBlockFileSystemAbstraction) Close() error {
  119. a.share.Umount()
  120. a.session.Logoff()
  121. conn := *(a.conn)
  122. conn.Close()
  123. return nil
  124. }
  125. /*
  126. Abstraction Utilities
  127. */
  128. func (a ServerMessageBlockFileSystemAbstraction) VirtualPathToRealPath(subpath string, username string) (string, error) {
  129. if strings.HasPrefix(subpath, a.UUID+":") {
  130. //This is full virtual path. Trim the uuid and correct the subpath
  131. subpath = strings.TrimPrefix(subpath, a.UUID+":")
  132. }
  133. subpath = filterFilepath(subpath)
  134. if a.Hierarchy == "user" {
  135. return toWinPath(filepath.ToSlash(filepath.Clean(filepath.Join("users", username, subpath)))), nil
  136. } else if a.Hierarchy == "public" {
  137. return toWinPath(filepath.ToSlash(filepath.Clean(subpath))), nil
  138. }
  139. return "", fsdef.ErrVpathResolveFailed
  140. }
  141. func (a ServerMessageBlockFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
  142. fullpath = strings.TrimPrefix(fullpath, "\\")
  143. vpath := a.UUID + ":/" + filterFilepath(fullpath)
  144. return vpath, nil
  145. }
  146. func (a ServerMessageBlockFileSystemAbstraction) FileExists(realpath string) bool {
  147. realpath = toWinPath(filterFilepath(realpath))
  148. _, err := a.share.Open(realpath)
  149. return err == nil
  150. }
  151. func (a ServerMessageBlockFileSystemAbstraction) IsDir(realpath string) bool {
  152. realpath = filterFilepath(realpath)
  153. realpath = toWinPath(realpath)
  154. stx, err := a.share.Stat(realpath)
  155. if err != nil {
  156. return false
  157. }
  158. return stx.IsDir()
  159. }
  160. func (a ServerMessageBlockFileSystemAbstraction) Glob(realpathWildcard string) ([]string, error) {
  161. realpathWildcard = toWinPath(filterFilepath(realpathWildcard))
  162. return a.share.Glob(realpathWildcard)
  163. }
  164. func (a ServerMessageBlockFileSystemAbstraction) GetFileSize(realpath string) int64 {
  165. realpath = toWinPath(filterFilepath(realpath))
  166. stat, err := a.share.Stat(realpath)
  167. if err != nil {
  168. return 0
  169. }
  170. return stat.Size()
  171. }
  172. func (a ServerMessageBlockFileSystemAbstraction) GetModTime(realpath string) (int64, error) {
  173. realpath = toWinPath(filterFilepath(realpath))
  174. stat, err := a.share.Stat(realpath)
  175. if err != nil {
  176. return 0, nil
  177. }
  178. return stat.ModTime().Unix(), nil
  179. }
  180. func (a ServerMessageBlockFileSystemAbstraction) WriteFile(filename string, content []byte, mode os.FileMode) error {
  181. filename = toWinPath(filterFilepath(filename))
  182. return a.share.WriteFile(filename, content, mode)
  183. }
  184. func (a ServerMessageBlockFileSystemAbstraction) ReadFile(filename string) ([]byte, error) {
  185. filename = toWinPath(filterFilepath(filename))
  186. return a.share.ReadFile(filename)
  187. }
  188. func (a ServerMessageBlockFileSystemAbstraction) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  189. filename = toWinPath(filterFilepath(filename))
  190. f, err := a.share.OpenFile(filename, os.O_CREATE|os.O_WRONLY, mode)
  191. if err != nil {
  192. return err
  193. }
  194. p := make([]byte, 1024)
  195. for {
  196. _, err := stream.Read(p)
  197. if err != nil {
  198. if err == io.EOF {
  199. break
  200. } else {
  201. return err
  202. }
  203. }
  204. _, err = f.Write(p)
  205. if err != nil {
  206. return err
  207. }
  208. }
  209. return nil
  210. }
  211. func (a ServerMessageBlockFileSystemAbstraction) ReadStream(filename string) (io.ReadCloser, error) {
  212. filename = toWinPath(filterFilepath(filename))
  213. f, err := a.share.OpenFile(filename, os.O_RDONLY, 0755)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return f, nil
  218. }
  219. func (a ServerMessageBlockFileSystemAbstraction) Walk(root string, walkFn filepath.WalkFunc) error {
  220. root = toWinPath(filterFilepath(root))
  221. err := fs.WalkDir(a.share.DirFS(root), ".", func(path string, d fs.DirEntry, err error) error {
  222. statInfo, _ := d.Info()
  223. walkFn(path, statInfo, err)
  224. return nil
  225. })
  226. return err
  227. }
  228. /*
  229. Optional Functions
  230. */
  231. func (a *ServerMessageBlockFileSystemAbstraction) CapacityInfo() {
  232. fsinfo, err := a.share.Statfs(".")
  233. if err != nil {
  234. return
  235. }
  236. fmt.Println(fsinfo)
  237. }
  238. /*
  239. Helper Functions
  240. */
  241. func toWinPath(filename string) string {
  242. backslashed := strings.ReplaceAll(filename, "/", "\\")
  243. return strings.TrimPrefix(backslashed, "\\")
  244. }
  245. func filterFilepath(rawpath string) string {
  246. rawpath = filepath.ToSlash(filepath.Clean(rawpath))
  247. rawpath = strings.TrimSpace(rawpath)
  248. if strings.HasPrefix(rawpath, "./") {
  249. return rawpath[1:]
  250. } else if rawpath == "." || rawpath == "" {
  251. return "/"
  252. }
  253. return rawpath
  254. }