sshttp

package module
v0.0.0-...-b6785ee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2015 License: MIT Imports: 13 Imported by: 0

README

sshttp Build Status GoDoc

Package sshttp provides functionality that enables some functionality of Go's net/http package to be used with SSH servers using SFTP. MIT Licensed.

Examples

Currently, sshttp provides two types which can be used with net/http.

FileSystem

sshttp.FileSystem can be used as a http.FileSystem which enables a user to browse and access files on a remote machine, using a local HTTP server.

package main

import (
	"log"
	"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.
	fs, err := sshttp.NewFileSystem("sftp://192.168.1.1:22/home/user", &ssh.ClientConfig{
		User: "user",
		Auth: []ssh.AuthMethod{
			ssh.Password("password"),
		},
	})
	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)
	}
}

RoundTripper

sshttp.RoundTripper can be used as a http.RoundTripper which enables a user to use Go's net/http library directly over SSH with SFTP. In the future, this method will support adding, updating, and deleting remote files.

package main

import (
	"io"
	"log"
	"net/http"
	"os"

	"github.com/mdlayher/sshttp"
	"golang.org/x/crypto/ssh"
)

func main() {
	log.SetOutput(os.Stderr)

	// Set up a sshttp.RoundTripper with a default set of credentials.  These
	// credentials will be used whenever a SSH host is dialed, unless another
	// set is provided directly via the RoundTripper.Dial method.
	rt := sshttp.NewRoundTripper(&ssh.ClientConfig{
		User: "user",
		Auth: []ssh.AuthMethod{
			ssh.Password("password"),
		},
	})

	/*
		// If needed, more hosts can be dialed with different credentials.
		// The sshttp.RoundTripper will automatically use these credentials
		// for future interactions with this host.
		if err := rt.Dial("192.168.1.2:22", &ssh.ClientConfig{
			User: "user2",
			Auth: []ssh.AuthMethod{
				ssh.Password("password2"),
			},
		}); err != nil {
			log.Fatal(err)
		}
	*/

	// Set up a http.Client with sshttp.RoundTripper registered to handle SFTP
	// URLs.
	t := &http.Transport{}
	t.RegisterProtocol(sshttp.Protocol, rt)
	c := &http.Client{Transport: t}

	// Perform a HTTP GET request over SFTP, to download a file.
	res, err := c.Get("sftp://192.168.1.1:22/home/user/hello.txt")
	if err != nil {
		log.Fatal(err)
	}

	// Copy file directly to stdout.
	if _, err := io.Copy(os.Stdout, res.Body); err != nil {
		log.Fatal(err)
	}

	// Close all open sshttp.RoundTripper connections.
	if err := rt.Close(); err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Package sshttp provides functionality that enables some functionality of Go's net/http package to be used with SSH servers using SFTP. MIT Licensed.

Index

Constants

View Source
const (
	// Protocol is the protocol which identifies SFTP as the proper scheme for
	// a URL used by this package.
	Protocol = "sftp"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	// Embed for interface implementation
	*sftp.File
	// contains filtered or unexported fields
}

File implements http.File using remote files over SFTP, and is returned by FileSystem's Open method.

func (*File) Readdir

func (f *File) Readdir(count int) ([]os.FileInfo, error)

Readdir is used to implement http.File for remote files over SFTP. It behaves in the same manner as os.File.Readdir: https://godoc.org/os#File.Readdir.

type FileSystem

type FileSystem struct {
	// contains filtered or unexported fields
}

FileSystem implements http.FileSystem for remote files over SFTP.

func NewFileSystem

func NewFileSystem(host string, config *ssh.ClientConfig) (*FileSystem, error)

NewFileSystem creates a new FileSystem which can access remote files over SFTP. The resulting FileSystem can be used by net/http to provide access to remote files over SFTP, as if they were local. The host parameter specifies the URI to dial and access, and the configuration parameter is used to configure the underlying SSH connection.

A host must be a complete URI, including a protocol segment. For example, sftp://127.0.0.1:22/home/foo dials 127.0.0.1 on port 22, and accesses the /home/foo directory on the host.

func (*FileSystem) Close

func (fs *FileSystem) Close() error

Close closes open SFTP and SSH connections for this FileSystem.

func (*FileSystem) Open

func (fs *FileSystem) Open(name string) (http.File, error)

Open attempts to access a file under the directory specified in NewFileSystem, and attempts to return a http.File for use with net/http.

type RoundTripper

type RoundTripper struct {
	// contains filtered or unexported fields
}

RoundTripper implements http.RoundTripper, and handles performing a HTTP request over SSH, using SFTP to send a file in response. A RoundTripper can automatically dial SSH hosts when RoundTrip is called, assuming the correct default credentials are provided. If more control is needed, use the Dial method to configure each host on an individual basis.

func NewRoundTripper

func NewRoundTripper(config *ssh.ClientConfig) *RoundTripper

NewRoundTripper accepts a ssh.ClientConfig struct and returns a RoundTripper which can be used by net/http. The configuration parameter is used as the default for any SSH hosts which are not explicitly configured using the Dial method.

func (*RoundTripper) Close

func (rt *RoundTripper) Close() error

Close closes all open SFTP and SSH connections for this RoundTripper.

func (*RoundTripper) Dial

func (rt *RoundTripper) Dial(host string, config *ssh.ClientConfig) error

Dial attempts to dial a SSH connection to the specified host, using the specified SSH client configuration. If the config parameter is nil, the default set by NewRoundTripper will be used.

Dial should be used if more than a single host is being dialed by RoundTripper, so that various SSH client configurations may be used, if needed. For a single host, allowing RoundTripper to lazily dial a host using the default SSH client configuration is typically acceptable.

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper, and performs a HTTP request over SSH, using SFTP to coordinate the response. If a SSH connection is not already open to the host specified in r.URL.Host, RoundTrip will attempt to lazily dial the host using the default configuration from NewRoundTripper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL