├── .github └── workflows │ ├── main.yml │ └── release.yml ├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── cmd └── sshwebdav │ └── main.go ├── go.mod ├── go.sum └── pkg ├── sshwebdav └── sshwebdav.go └── version └── version.go /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | push: 4 | branches: 5 | - master 6 | - 'release/**' 7 | pull_request: 8 | jobs: 9 | main: 10 | strategy: 11 | matrix: 12 | platform: [ubuntu-20.04, macos-12] 13 | runs-on: ${{ matrix.platform }} 14 | steps: 15 | - uses: actions/setup-go@v2 16 | with: 17 | go-version: 1.20.x 18 | - uses: actions/checkout@v2 19 | with: 20 | fetch-depth: 1 21 | - run: make 22 | - run: sudo make install 23 | - run: go test -covermode=atomic -race -v github.com/lima-vm/sshwebdav/... 24 | - run: make artifacts 25 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | # Forked from https://github.com/containerd/nerdctl/blob/v0.8.1/.github/workflows/release.yml 2 | # Apache License 2.0 3 | 4 | name: Release 5 | on: 6 | push: 7 | tags: 8 | - 'v*' 9 | - 'test-action-release-*' 10 | env: 11 | GO111MODULE: on 12 | jobs: 13 | release: 14 | runs-on: ubuntu-20.04 15 | timeout-minutes: 20 16 | steps: 17 | - uses: actions/setup-go@v2 18 | with: 19 | go-version: 1.20.x 20 | - uses: actions/checkout@v2 21 | - name: "Compile binaries" 22 | run: make artifacts 23 | - name: "SHA256SUMS" 24 | run: | 25 | ( cd _artifacts; sha256sum *.tar.gz ) | tee /tmp/SHA256SUMS 26 | mv /tmp/SHA256SUMS _artifacts/SHA256SUMS 27 | - name: "The sha256sum of the SHA256SUMS file" 28 | run: (cd _artifacts; sha256sum SHA256SUMS) 29 | - name: "Prepare the release note" 30 | run: | 31 | tag="${GITHUB_REF##*/}" 32 | shasha=$(sha256sum _artifacts/SHA256SUMS | awk '{print $1}') 33 | cat <<-EOF | tee /tmp/release-note.txt 34 | ${tag} 35 | 36 | (Changes to be documented) 37 | - - - 38 | The binaries were built automatically on GitHub Actions. 39 | The build log is available for 90 days: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} 40 | 41 | The sha256sum of the SHA256SUMS file itself is \`${shasha}\` . 42 | EOF 43 | - name: "Create release" 44 | env: 45 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 46 | run: | 47 | tag="${GITHUB_REF##*/}" 48 | gh release create -F /tmp/release-note.txt --draft --title "${tag}" "${tag}" _artifacts/* 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /_output/ 2 | /_artifacts/ 3 | /sshwebdav 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Files are installed under $(DESTDIR)/$(PREFIX) 2 | PREFIX ?= /usr/local 3 | DEST := $(shell echo "$(DESTDIR)/$(PREFIX)" | sed 's:///*:/:g; s://*$$::') 4 | 5 | GO ?= go 6 | 7 | TAR ?= tar 8 | 9 | PACKAGE := github.com/lima-vm/sshwebdav 10 | 11 | VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags) 12 | VERSION_TRIMMED := $(VERSION:v%=%) 13 | 14 | GO_BUILD := CGO_ENABLED=0 $(GO) build -ldflags="-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION)" 15 | 16 | .PHONY: all 17 | all: binaries 18 | 19 | .PHONY: binaries 20 | binaries: _output/bin/sshwebdav 21 | 22 | .PHONY: _output/bin/sshwebdav 23 | _output/bin/sshwebdav: 24 | $(GO_BUILD) -o $@ ./cmd/sshwebdav 25 | 26 | .PHONY: install 27 | install: 28 | mkdir -p "$(DEST)" 29 | cp -av _output/* "$(DEST)" 30 | 31 | .PHONY: uninstall 32 | uninstall: 33 | rm -rf "$(DEST)/bin/sshwebdav" 34 | 35 | .PHONY: clean 36 | clean: 37 | rm -rf _output 38 | 39 | .PHONY: artifacts 40 | artifacts: 41 | mkdir -p _artifacts 42 | GOOS=darwin GOARCH=amd64 make clean binaries 43 | $(TAR) -C _output/ -czvf _artifacts/sshwebdav-$(VERSION_TRIMMED)-Darwin-x86_64.tar.gz ./ 44 | GOOS=darwin GOARCH=arm64 make clean binaries 45 | $(TAR) -C _output -czvf _artifacts/sshwebdav-$(VERSION_TRIMMED)-Darwin-arm64.tar.gz ./ 46 | GOOS=linux GOARCH=amd64 make clean binaries 47 | $(TAR) -C _output/ -czvf _artifacts/sshwebdav-$(VERSION_TRIMMED)-Linux-x86_64.tar.gz ./ 48 | GOOS=linux GOARCH=arm64 make clean binaries 49 | $(TAR) -C _output/ -czvf _artifacts/sshwebdav-$(VERSION_TRIMMED)-Linux-aarch64.tar.gz ./ 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sshwebdav: WebDAV server for SSH 2 | 3 | `sshwebdav` provides a WebDAV server for a remote SSH host. 4 | 5 | `sshwebdav` is similar to `sshfs` but does not require proprietary MacFUSE on macOS. 6 | 7 | `sshwebdav` is planned to be integrated into [Lima](https://github.com/lima-vm/lima), for exposing 8 | Linux (guest) filesystem to macOS (host). 9 | 10 | ## Install 11 | 12 | ``` 13 | make 14 | make install 15 | ``` 16 | 17 | ## Usage 18 | 19 | ``` 20 | sshwebdav ssh://foo@example.com:22/home/foo http://127.0.0.1:8080/ 21 | ``` 22 | 23 | Open `Go` menu of macOS Finder, choose `Connect to Server`, and connect to http://127.0.0.1:8080 . 24 | 25 | Current limitations: 26 | - No support for write operations 27 | - No support for HTTP auth 28 | - No support for HTTPS, so make sure to serve only for 127.0.0.1 29 | -------------------------------------------------------------------------------- /cmd/sshwebdav/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "os" 7 | 8 | "github.com/lima-vm/sshwebdav/pkg/sshwebdav" 9 | "github.com/lima-vm/sshwebdav/pkg/version" 10 | "github.com/sirupsen/logrus" 11 | "github.com/urfave/cli/v2" 12 | ) 13 | 14 | func main() { 15 | if err := newApp().Run(os.Args); err != nil { 16 | logrus.Fatal(err) 17 | } 18 | } 19 | 20 | const usageText = "sshwebdav ssh://foo@example.com:22/home/foo http://127.0.0.1:8080/" 21 | 22 | func newApp() *cli.App { 23 | app := cli.NewApp() 24 | app.Name = "sshwebdav" 25 | app.Version = version.Version 26 | app.Usage = "WebDAV server for SSH" 27 | app.UsageText = usageText 28 | app.Flags = []cli.Flag{ 29 | &cli.BoolFlag{ 30 | Name: "debug", 31 | Usage: "debug mode", 32 | }, 33 | &cli.StringFlag{ 34 | Name: "ssh-config", 35 | Aliases: []string{"F"}, 36 | Usage: "ssh config file", 37 | }, 38 | &cli.StringFlag{ 39 | Name: "ssh-identity", 40 | Aliases: []string{"i"}, 41 | Usage: "ssh identify file (private key)", 42 | }, 43 | &cli.StringSliceFlag{ 44 | Name: "ssh-option", 45 | Aliases: []string{"o"}, 46 | Usage: "ssh option (KEY=VAL)", 47 | }, 48 | } 49 | app.HideHelpCommand = true 50 | app.Before = func(clicontext *cli.Context) error { 51 | if clicontext.Bool("debug") { 52 | logrus.SetLevel(logrus.DebugLevel) 53 | } 54 | return nil 55 | } 56 | app.Action = action 57 | return app 58 | } 59 | 60 | func action(clicontext *cli.Context) error { 61 | if clicontext.NArg() < 2 { 62 | return fmt.Errorf("missing args (usage: %s)", usageText) 63 | } 64 | if clicontext.NArg() > 2 { 65 | return fmt.Errorf("too many args (usage: %s)", usageText) 66 | } 67 | sshURLStr := clicontext.Args().Get(0) 68 | sshURL, err := url.Parse(sshURLStr) 69 | if err != nil { 70 | return fmt.Errorf("failed to parse SSH URL %q: %w", sshURLStr, err) 71 | } 72 | if sshURL.Scheme != "ssh" { 73 | return fmt.Errorf("the first argument should be like \"ssh://foo@example.com:22/home/foo\", got %q", sshURLStr) 74 | } 75 | 76 | webdavURLStr := clicontext.Args().Get(1) 77 | webdavURL, err := url.Parse(webdavURLStr) 78 | if err != nil { 79 | return fmt.Errorf("failed to parse WebDAV URL %q: %w", webdavURLStr, err) 80 | } 81 | if webdavURL.Scheme != "http" { 82 | // TODO: https 83 | return fmt.Errorf("the second argument should be like \"http://127.0.0.1:8080\", got %q", webdavURLStr) 84 | } 85 | 86 | x, err := sshwebdav.New(sshURL, webdavURL, 87 | sshwebdav.WithSSHConfig(clicontext.String("ssh-config")), 88 | sshwebdav.WithSSHIdentity(clicontext.String("ssh-identity")), 89 | sshwebdav.WithSSHOptions(clicontext.StringSlice("ssh-options"))) 90 | if err != nil { 91 | return err 92 | } 93 | 94 | logrus.Info("Hint: open Finder => `Go` => `Connect to Server` to connect.") 95 | return x.Serve() 96 | } 97 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/lima-vm/sshwebdav 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/pkg/sftp v1.13.2 7 | github.com/sirupsen/logrus v1.8.1 8 | github.com/urfave/cli/v2 v2.3.0 9 | golang.org/x/net v0.23.0 10 | ) 11 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= 3 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 4 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 6 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 7 | github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= 8 | github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= 9 | github.com/pkg/sftp v1.13.2 h1:taJnKntsWgU+qae21Rx52lIwndAdKrj0mfUNQsz1z4Q= 10 | github.com/pkg/sftp v1.13.2/go.mod h1:LzqnAvaD5TWeNBsZpfKxSYn1MbjWwOsCIAFFJbpIsK8= 11 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 12 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 13 | github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= 14 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 15 | github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= 16 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 17 | github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= 18 | github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= 19 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 20 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 21 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 22 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 23 | github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= 24 | github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= 25 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 26 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 27 | golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= 28 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 29 | golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= 30 | golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= 31 | golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= 32 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 33 | golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 34 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 35 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 36 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 37 | golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 38 | golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= 39 | golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= 40 | golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= 41 | golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= 42 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 43 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 44 | golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 45 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 46 | golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 47 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 48 | golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 49 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 50 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 51 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 52 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 53 | golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 54 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 55 | golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= 56 | golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 57 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 58 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 59 | golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 60 | golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= 61 | golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= 62 | golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= 63 | golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= 64 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 65 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 66 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 67 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 68 | golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= 69 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 70 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 71 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 72 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 73 | golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= 74 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 75 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 76 | gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 77 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 78 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 79 | -------------------------------------------------------------------------------- /pkg/sshwebdav/sshwebdav.go: -------------------------------------------------------------------------------- 1 | package sshwebdav 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | "io/fs" 8 | iofs "io/fs" 9 | "net" 10 | "net/http" 11 | "net/url" 12 | "os" 13 | "os/exec" 14 | "path" 15 | 16 | "github.com/pkg/sftp" 17 | "github.com/sirupsen/logrus" 18 | "golang.org/x/net/webdav" 19 | ) 20 | 21 | type Opt func(*SSHWebDAV) error 22 | 23 | func WithSSHConfig(sshConfig string) Opt { 24 | return func(x *SSHWebDAV) error { 25 | x.sshConfig = sshConfig 26 | return nil 27 | } 28 | } 29 | 30 | func WithSSHIdentity(sshIdentity string) Opt { 31 | return func(x *SSHWebDAV) error { 32 | x.sshIdentity = sshIdentity 33 | return nil 34 | } 35 | } 36 | 37 | func WithSSHOptions(sshOptions []string) Opt { 38 | return func(x *SSHWebDAV) error { 39 | x.sshOptions = sshOptions 40 | return nil 41 | } 42 | } 43 | 44 | func New(sshURL, webdavURL *url.URL, opts ...Opt) (*SSHWebDAV, error) { 45 | if sshURL.Scheme != "ssh" { 46 | return nil, fmt.Errorf("expected ssh url, got %q", sshURL.String()) 47 | } 48 | if webdavURL.Scheme != "http" { 49 | // TODO: https 50 | return nil, fmt.Errorf("expected http url, got %q", webdavURL.String()) 51 | } 52 | 53 | x := &SSHWebDAV{ 54 | sshURL: sshURL, 55 | webdavURL: webdavURL, 56 | } 57 | for _, f := range opts { 58 | if err := f(x); err != nil { 59 | return nil, err 60 | } 61 | } 62 | return x, nil 63 | } 64 | 65 | type SSHWebDAV struct { 66 | sshURL *url.URL 67 | webdavURL *url.URL 68 | sshConfig string 69 | sshIdentity string 70 | sshOptions []string 71 | } 72 | 73 | func (x *SSHWebDAV) Serve() error { 74 | sshArgs, err := sshArgs(x.sshURL, x.sshConfig, x.sshIdentity, x.sshOptions) 75 | if err != nil { 76 | return err 77 | } 78 | sshCmd := exec.Command("ssh", sshArgs...) 79 | sshCmd.Stderr = os.Stderr 80 | sshW, err := sshCmd.StdinPipe() 81 | if err != nil { 82 | return err 83 | } 84 | sshR, err := sshCmd.StdoutPipe() 85 | if err != nil { 86 | return err 87 | } 88 | if err := sshCmd.Start(); err != nil { 89 | return err 90 | } 91 | defer func() { _ = sshCmd.Wait() }() 92 | sftpClient, err := sftp.NewClientPipe(sshR, sshW) 93 | if err != nil { 94 | return err 95 | } 96 | defer sftpClient.Close() 97 | 98 | fs, ls, err := NewFileSystem(sftpClient, x.sshURL.Path) 99 | if err != nil { 100 | return err 101 | } 102 | 103 | handler := &webdav.Handler{ 104 | Prefix: x.webdavURL.Path, 105 | FileSystem: fs, 106 | LockSystem: ls, 107 | Logger: func(r *http.Request, e error) { 108 | if e != nil { 109 | logrus.WithField("request", r).WithError(e).Errorf("handler: %+v", e) 110 | } else { 111 | logrus.WithField("request", r).WithError(e).Debug("handler") 112 | } 113 | }, 114 | } 115 | 116 | srv := &http.Server{ 117 | Addr: x.webdavURL.Host, 118 | Handler: handler, 119 | } 120 | defer srv.Close() 121 | 122 | return srv.ListenAndServe() 123 | } 124 | 125 | func sshArgs(sshURL *url.URL, sshConfig, sshIdentity string, sshOptions []string) ([]string, error) { 126 | if sshURL.Scheme != "ssh" { 127 | return nil, fmt.Errorf("expected ssh url, got %q", sshURL.String()) 128 | } 129 | var args []string 130 | if sshConfig != "" { 131 | args = append(args, "-F", sshConfig) 132 | } 133 | if sshIdentity != "" { 134 | args = append(args, "-i", sshIdentity) 135 | } 136 | for _, f := range sshOptions { 137 | args = append(args, "-o", f) 138 | } 139 | if u := sshURL.User; u != nil { 140 | if _, ok := u.Password(); ok { 141 | return nil, fmt.Errorf("plain-text SSH passsword is not supported, use public key") 142 | } 143 | if username := u.Username(); username != "" { 144 | args = append(args, "-l", username) 145 | } 146 | } 147 | host := sshURL.Host 148 | if h, p, err := net.SplitHostPort(sshURL.Host); err == nil { 149 | args = append(args, "-p", p) 150 | host = h 151 | } 152 | args = append(args, host) 153 | args = append(args, "-s", "sftp") 154 | return args, nil 155 | } 156 | 157 | func NewFileSystem(sftpClient *sftp.Client, root string) (webdav.FileSystem, webdav.LockSystem, error) { 158 | fs := &fileSystem{ 159 | sftpClient: sftpClient, 160 | root: path.Clean(root), 161 | } 162 | ls := webdav.NewMemLS() 163 | return fs, ls, nil 164 | } 165 | 166 | type fileSystem struct { 167 | sftpClient *sftp.Client // thread-safe 168 | root string 169 | } 170 | 171 | var ErrReadOnlyFS = errors.New("read-only file system") 172 | 173 | func (fs *fileSystem) Mkdir(_ context.Context, name string, perm os.FileMode) (retErr error) { 174 | logrus.WithField("name", name).Debug("> *fileSystem.Mkdir") 175 | defer func() { 176 | logrus.WithField("name", name).WithError(retErr).Debug("< *fileSystem.Mkdir") 177 | }() 178 | return ErrReadOnlyFS 179 | } 180 | 181 | func (fs *fileSystem) OpenFile(_ context.Context, name string, flag int, perm os.FileMode) (res webdav.File, retErr error) { 182 | logrus.WithField("name", name).WithField("flag", flag).WithField("perm", perm).Debug("> *fileSystem.OpenFile") 183 | defer func() { 184 | logrus.WithField("name", name).WithField("flag", flag).WithField("perm", perm).WithError(retErr).Debug("< *fileSystem.OpenFile") 185 | }() 186 | remotePath := fs.remotePath(name) 187 | st, err := fs.sftpClient.Stat(remotePath) 188 | if err != nil { 189 | // even on err we need to return a file without err, otherwise `ls` result becomes empty 190 | return &errFile{ 191 | err: err, 192 | st: func() (iofs.FileInfo, error) { 193 | return st, err 194 | }}, nil 195 | } 196 | 197 | if st.Mode()&(iofs.ModeNamedPipe|iofs.ModeDevice|iofs.ModeCharDevice) != 0 { 198 | // don't open pipe, it hangs 199 | return &errFile{ 200 | err: fmt.Errorf("can't be opened"), 201 | st: func() (iofs.FileInfo, error) { 202 | return st, nil 203 | }}, nil 204 | } 205 | 206 | f, err := fs.sftpClient.Open(remotePath) // read-only 207 | if err != nil { 208 | // even on err we need to return a file without err, otherwise `ls` result becomes empty 209 | return &errFile{ 210 | err: err, 211 | st: func() (iofs.FileInfo, error) { 212 | return st, nil 213 | }}, nil 214 | } 215 | ff := &file{ 216 | File: f, 217 | sftpClient: fs.sftpClient, 218 | remotePath: remotePath, 219 | } 220 | return ff, nil 221 | } 222 | 223 | func (fs *fileSystem) RemoveAll(_ context.Context, name string) (retErr error) { 224 | logrus.WithField("name", name).Debug("> *fileSystem.RemoveAll") 225 | defer func() { 226 | logrus.WithField("name", name).WithError(retErr).Debug("< *fileSystem.RemoveAll") 227 | }() 228 | return ErrReadOnlyFS 229 | } 230 | 231 | func (fs *fileSystem) Rename(_ context.Context, oldName, newName string) (retErr error) { 232 | logrus.WithField("oldName", oldName).WithField("newName", newName).Debug("> *fileSystem.Rename") 233 | defer func() { 234 | logrus.WithField("oldName", oldName).WithField("newName", newName).WithError(retErr).Debug("< *fileSystem.Rename") 235 | }() 236 | return ErrReadOnlyFS 237 | } 238 | 239 | func (fs *fileSystem) Stat(_ context.Context, name string) (fi os.FileInfo, retErr error) { 240 | logrus.WithField("name", name).Debug("> *fileSystem.Stat") 241 | defer func() { 242 | logrus.WithField("name", name).WithField("fi", fi).WithError(retErr).Debug("< *fileSystem.Stat") 243 | }() 244 | return fs.sftpClient.Stat(fs.remotePath(name)) 245 | } 246 | 247 | func (fs *fileSystem) remotePath(name string) string { 248 | remotePath := path.Join(fs.root, path.Clean(name)) 249 | // logrus.Debugf("remotePath(%q) => %q", name, remotePath) 250 | return remotePath 251 | } 252 | 253 | type file struct { 254 | *sftp.File 255 | sftpClient *sftp.Client 256 | remotePath string 257 | } 258 | 259 | func (f *file) Readdir(count int) (fis []iofs.FileInfo, retErr error) { 260 | logrus.WithField("count", count).WithField("f.remotePath", f.remotePath).Debug("> *file.Readdir") 261 | defer func() { 262 | logrus.WithField("count", count).WithField("f.remotePath", f.remotePath).WithField("nfis", len(fis)).WithError(retErr).Debug("< *file.Readdir") 263 | }() 264 | if count > 0 { 265 | return nil, fmt.Errorf("unsupported call: Readdir(%d)", count) 266 | } 267 | return f.sftpClient.ReadDir(f.remotePath) 268 | } 269 | 270 | func (f *file) Stat() (fi fs.FileInfo, retErr error) { 271 | logrus.Debug("> *file.Stat") 272 | defer func() { 273 | logrus.WithField("fi", fi).WithError(retErr).Debug("< *file.Stat") 274 | }() 275 | return f.File.Stat() 276 | } 277 | 278 | type errFile struct { 279 | err error 280 | st func() (iofs.FileInfo, error) 281 | } 282 | 283 | func (f *errFile) Close() error { 284 | return nil 285 | } 286 | 287 | func (f *errFile) Read(p []byte) (n int, err error) { 288 | return 0, f.err 289 | } 290 | 291 | func (f *errFile) Seek(offset int64, whence int) (int64, error) { 292 | return 0, f.err 293 | } 294 | 295 | func (f *errFile) Readdir(count int) (fis []iofs.FileInfo, retErr error) { 296 | logrus.WithField("count", count).Debug("> *errFile.Readdir") 297 | defer func() { 298 | logrus.WithField("count", count).WithError(retErr).Debug("< *errFile.Readdir") 299 | }() 300 | return nil, f.err 301 | } 302 | 303 | func (f *errFile) Stat() (fi fs.FileInfo, retErr error) { 304 | logrus.Debug("> *errFile.Stat") 305 | defer func() { 306 | logrus.WithField("fi", fi).WithError(retErr).Debug("< *errFile.Stat") 307 | }() 308 | return f.st() 309 | } 310 | 311 | func (f *errFile) Write(p []byte) (n int, err error) { 312 | return 0, f.err 313 | } 314 | -------------------------------------------------------------------------------- /pkg/version/version.go: -------------------------------------------------------------------------------- 1 | package version 2 | 3 | var ( 4 | // Version is filled on compilation time 5 | Version = "" 6 | ) 7 | --------------------------------------------------------------------------------