Skip to content

Instantly share code, notes, and snippets.

@xor-gate
Last active November 26, 2015 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xor-gate/60d2c1e1d1e75f485376 to your computer and use it in GitHub Desktop.
Save xor-gate/60d2c1e1d1e75f485376 to your computer and use it in GitHub Desktop.
http-sftp-share

HTTP-SFTP file server in golang

package main
import (
"log"
"os"
"io/ioutil"
"net/http"
"github.com/mdlayher/sshttp"
"golang.org/x/crypto/ssh"
)
func main() {
// Set up a http.FileSystem pointed at a user's home directory on
// a remote server.
pemBytes, err := ioutil.ReadFile(os.Getenv("HOME") + "/.ssh/id_rsa")
if err != nil {
log.Fatal(err)
}
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
log.Fatalf("parse key failed:%v", err)
}
fs, err := sshttp.NewFileSystem("sftp://localhost:22/", &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
})
if err != nil {
log.Fatal(err)
}
// Bind HTTP server, provide link for user to browse files
host := ":8080"
log.Printf("starting listener: http://localhost%s/", host)
if err := http.ListenAndServe(":8080", http.FileServer(fs)); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment