Skip to content

Swift SMB client library and iOS/macOS file browser applications.

License

Notifications You must be signed in to change notification settings

zhouhesheng/SMBClient

 
 

Repository files navigation

SMBClient

Test

Swift SMB client library and iOS/macOS file browser applications. This library provides a high-level interface to the SMB protocol and allows you to access files on remote SMB servers. Written in Swift, no dependencies on external libraries.

Usage

SMBClient class hides the low-layer SMB protocol and provides a higher-layer interface suitable for common use cases. The following example demonstrates how to list files in a share drive on a remote SMB server.

import SMBClient

let client = SMBClient(host: "198.51.100.50")

try await client.login(username: "alice", password: "secret")
try await client.connectShare("Public")

let files = try await client.listDirectory("")
print(files.map { $0.fileName })

try await client.disconnectShare()
try await client.logoff()

If you want to use the low-layer SMB protocol directly, you can use the Session class. Session class provides a set of functions that correspond to SMB messages. You can get more fine-grained control over the SMB protocol.

import SMBClient

let session = Session(host: "198.51.100.50")

try await session.connect()
try await session.negotiate()

try await session.sessionSetup(username: "alice", password: "secret")
try await session.treeConnect(path: "Public")

let files = try await session.queryDirectory(path: "", pattern: "*")
print(files.map { $0.fileName })

try await session.treeDisconnect()
try await session.logoff()

Log in to the remote SMB server

let client = SMBClient(host: "198.51.100.50")
try await client.login(username: "alice", password: "secret")

List share drives

let shares = try await client.listShares()
print(shares.map { $0.name })

Connect to a share drive

try await client.connectShare("Public")

List files in a directory

let files = try await client.listDirectory("Documents/Presentations")
print(files.map { $0.fileName })

Download a file

let data = try await client.download(path: "Pictures/IMG_0001.jpg")

Upload a file/directory

try await client.upload(data: data, path: "Documents/Presentations/Keynote.key")

Example Applications

Network File Browser for macOS

macOS File Browser App

Network File Browser for iOS

iOS File Browser App iOS File Browser App

Installation

Add the following line to the dependencies in your Package.swift file:

dependencies: [
  .package(url: "https://github.com/kishikawakatsumi/SMBClient.git", .upToNextMajor(from: "0.1.0"))
]

Supported Platforms

  • macOS 10.15 or later
  • iOS 13.0 or later

Supported Protocol Version

SMB 2.0 (aka SMB2) is supported. See Header for protocol support, which follows SMB2 Packet Header open specification.

Supported SMB Messages

  • NEGOTIATE
  • SESSION_SETUP
  • LOGOFF
  • TREE_CONNECT
  • TREE_DISCONNECT
  • CREATE
  • CLOSE
  • FLUSH
  • READ
  • WRITE
  • LOCK
  • ECHO
  • CANCEL
  • IOCTL
  • QUERY_DIRECTORY
  • CHANGE_NOTIFY
  • QUERY_INFO
  • SET_INFO

FAQ

How to connedct to macOS file sharing?

To connect to macOS file sharing, you need to enable "Windows File Sharing" in the "Sharing" sytem settings in "File Sharing" section. This will enable NTLM v2 authentication on your macOS.

macOS File Sharing settings

Compatibility with SMB1 and AFP

This library does not support SMB1 and will not support SMB 1.0 in the near future, this is due to SMB1 and SMB2 are not compatible, with completely different packet structures.

Relatedly, connecting to macOS servers using pre-SMB, Apple Filing Protocol (AFP) is not supported.

OS X 10.9 Mavericks and later supports SMB as the primary file sharing protocol.

Supporters & Sponsors

Open source projects thrive on the generosity and support of people like you. If you find this project valuable, please consider extending your support. Contributing to the project not only sustains its growth, but also helps drive innovation and improve its features.

To support this project, you can become a sponsor through GitHub Sponsors. Your contribution will be greatly appreciated and will help keep the project alive and thriving. Thanks for your consideration! ❤️

About

Swift SMB client library and iOS/macOS file browser applications.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%