Documentation
¶
Overview ¶
Package localshare serves a filesystem that lives in this process, a volume lent by its owner, over SMB, and optionally mounts it in the file manager of this machine.
It is the generic half of go-theca/theca's former share package: any program that holds a disk image or an unlocked encrypted container and wants to show it in the Finder, Files or Explorer can link it.
- One random account is drawn per share (macOS replays a stale credential for a fixed name on 127.0.0.1, which made the second mount of a session fail with errno 80), and one random 24-byte password. The password is held as bytes, never as a string the package keeps; it reaches the owner through a Credential sink and the OS mount service. The copy the Share keeps for RevealPassword (a "show password" button) is wiped at Stop.
- The address must be loopback unless Options.Network is set. Every session signs; beyond loopback, signing and encryption are both required (MS-SMB2 3.3.5.4 and 3.3.5.5, 3.3.5.2.11).
- Stop is ordered: unmount (its own 30 s budget, on a live context), then server close, then filesystem close. The volume is never closed by this package: ServeDevice takes a volume.Borrowed, which has no Close, and the owner commits and wipes after Stop returns.
- The owner supplies the commit: Options.Commit is called every Options.Every, and a failure ends the share.
The decrypted bytes never leave the process: this is a library linked into the owner, not a daemon the owner hands data to.
Index ¶
- Constants
- Variables
- func CheckAddr(addr string, network bool) error
- type Credential
- type Options
- type Share
- func (s *Share) Addr() string
- func (s *Share) Done() <-chan struct{}
- func (s *Share) Err() error
- func (s *Share) MountPath() string
- func (s *Share) Reveal() error
- func (s *Share) RevealPassword(fn func(password []byte))
- func (s *Share) Stop() error
- func (s *Share) URL() string
- func (s *Share) User() string
Constants ¶
const DefaultAddr = "127.0.0.1:0"
DefaultAddr is the address served when Options.Addr is empty: a free port on the loopback interface.
const DefaultServerName = "LOCALSHARE"
DefaultServerName is the NetBIOS-style name the server shows clients when Options.ServerName is empty.
const DefaultUserPrefix = "share-"
DefaultUserPrefix starts the account name when Options.UserPrefix is empty.
Variables ¶
var ErrNoFilesystem = errors.New("the device holds no exFAT filesystem")
ErrNoFilesystem is returned (wrapped) by ServeDevice when the default opener finds no exFAT filesystem on the device.
var ErrNotMounted = automount.ErrNotMounted
ErrNotMounted is returned by Share.Reveal when the share was not mounted (Options.Mount false) or has been stopped. It is automount.ErrNotMounted, so errors.Is matches either.
Functions ¶
func CheckAddr ¶
CheckAddr refuses a malformed address, and unless network is set one that is not on the loopback interface: the share carries bytes that exist decrypted only in this process, and beyond loopback it is served only with signing and encryption required (Options.Network). "localhost" counts as loopback.
Types ¶
type Credential ¶
type Credential struct {
// URL is smb://host:port/name as a client on this machine reaches it. With
// an unspecified listen address (0.0.0.0, [::]) it names 127.0.0.1 on the
// same port; a client elsewhere uses this machine's address and that port.
URL *url.URL
// User is the account name: the prefix and 8 random characters.
User string
// Password is the account password, 32 characters of the URL-safe base64
// alphabet. It is wiped (overwritten with zeros) as soon as Serve returns,
// so a sink that needs it later must copy it, into memory it can wipe.
Password []byte
}
Credential is what a share's account is, handed once to Options.Credential. It never contains user information in the URL.
type Options ¶
type Options struct {
// Addr is the address to listen on. Empty means DefaultAddr. Anything but
// a loopback address needs Network.
Addr string
// Name is the share name (required, no path separator: the SMB server
// refuses an empty one or one with a separator): the last element of the
// URL.
Name string
// ServerName is the name the server gives itself in the NTLM challenge.
// Empty means DefaultServerName.
ServerName string
// UserPrefix starts the account name. Empty means DefaultUserPrefix.
UserPrefix string
// Network allows an Addr that is not loopback. It is not a plain switch:
// the server then REQUIRES message signing and encryption of every session
// and of the share (smb RequireSigning, EncryptData and RequireEncryption),
// so a client that cannot do both is refused. Anyone who can reach the
// address and holds the password can read and write the volume's files.
// False: Addr must be loopback.
Network bool
// Mount mounts the share in the file manager of this machine, with no
// password dialog (go-fileshare/automount).
Mount bool
// ReadOnly serves the share read-only, and mounts it read-only where the
// platform can (macOS; elsewhere the mount is refused rather than done
// read-write).
ReadOnly bool
// Every is how often Commit is called; zero means never.
Every time.Duration
// Commit makes what was written durable: the owner's own commit (a journal
// write, an fsync). It is called every Every, from one goroutine at a
// time, and an error ends the share (Done closes, Err returns it). It is
// required with Every, and refused without it. ServeDevice defaults it to
// the device's Sync.
Commit func() error
// Credential, if set, receives the account once, before the server
// accepts a connection and before the mount. An error from it aborts
// Serve with nothing left running.
Credential func(Credential) error
// Open opens the filesystem on a device for ServeDevice. Nil means exFAT
// only, so a device with anything else on it is refused (ErrNoFilesystem):
// the decrypted bytes of a vault are not served as whatever they look like.
// Pass github.com/go-filesystems/detect's Open, wrapped, to serve any
// registered type.
Open func(r io.ReaderAt, size int64) (filesystem.Filesystem, error)
}
Options configures Serve and ServeDevice. The zero value is not usable: Name is required.
type Share ¶
type Share struct {
// contains filtered or unexported fields
}
Share is a running share. Create one with Serve or ServeDevice; end it with Stop.
func Serve ¶
func Serve(ctx context.Context, fsys filesystem.Filesystem, o Options) (*Share, error)
Serve serves fsys over SMB on o.Addr, and with o.Mount mounts it. ctx bounds the mount and, when it ends, ends the share (Done closes, Err is nil); Stop must still be called.
Serve takes ownership of fsys: Stop closes it, and so does every error return. Closing it must not close what it was opened over (the contract of go-filesystems' OpenReader constructors).
func ServeDevice ¶
ServeDevice opens the filesystem on dev (Options.Open, default exFAT only) and serves it as Serve does. dev is never closed: a Borrowed has no Close, and the filesystem opened over it is closed at Stop without reaching it. With Every set and no Commit, the device's Sync is the commit.
A device with no filesystem the opener accepts is refused with ErrNoFilesystem. On any error nothing is left running and dev is untouched.
func (*Share) Done ¶
func (s *Share) Done() <-chan struct{}
Done is closed when the share ends by itself: its context ended, the server failed or a commit failed. Stop closes it too.
func (*Share) Err ¶
Err is the error that ended the share, nil while it runs or when it ended without one. Read it after Done is closed.
func (*Share) Reveal ¶ added in v0.2.0
Reveal opens the mounted share in the operating system's file manager (go-fileshare/automount Mounted.Reveal: argv only, no shell). It returns ErrNotMounted when Options.Mount was false or the share has been stopped.
func (*Share) RevealPassword ¶ added in v0.2.0
RevealPassword calls fn with a copy of the account password, for a "show password" button. The copy is wiped as soon as fn returns, so fn must not keep or retain it, and must convert it to a string only for as long as it is shown. fn is not called when the share has been stopped (the password is wiped at Stop), and Stop waits for a running fn. Calling Stop from fn deadlocks.
func (*Share) Stop ¶
Stop unmounts (own 30 s context, so a cancelled ctx does not cut it short), closes the server, then closes the filesystem. It never closes the volume: the owner commits and wipes after Stop returns. It returns the error that ended the share, if any, joined with the stop errors; calling it again returns the same result.