Skip to content

Commit

Permalink
Updates to code style
Browse files Browse the repository at this point in the history
Replaced panic with log.Fatal
Removed old commented code line
Cleaned up declaration of flag variables
Fixed rootDir flag variable - was pointing to debugLevelStr
Log any final error returned by SFTP subsystem
  • Loading branch information
kothar committed Nov 8, 2015
1 parent 9287f24 commit cf6c57c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
39 changes: 21 additions & 18 deletions examples/sftp-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"

Expand All @@ -17,19 +18,20 @@ import (
// Based on example server code from golang.org/x/crypto/ssh and server_standalone
func main() {

readOnly := false
debugLevelStr := "none"
debugLevel := 0
debugStderr := false
rootDir := ""
var (
readOnly bool
debugLevelStr string
debugLevel int
debugStderr bool
rootDir string
)

flag.BoolVar(&readOnly, "R", false, "read-only server")
flag.BoolVar(&debugStderr, "e", false, "debug to stderr")
flag.StringVar(&debugLevelStr, "l", "none", "debug level")
flag.StringVar(&debugLevelStr, "root", "", "root directory")
flag.StringVar(&rootDir, "root", "", "root directory")
flag.Parse()

// term := terminal.NewTerminal(channel, "> ")
debugStream := ioutil.Discard
if debugStderr {
debugStream = os.Stderr
Expand All @@ -52,12 +54,12 @@ func main() {

privateBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
panic("Failed to load private key")
log.Fatal("Failed to load private key", err)
}

private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
log.Fatal("Failed to parse private key", err)
}

config.AddHostKey(private)
Expand All @@ -66,20 +68,20 @@ func main() {
// accepted.
listener, err := net.Listen("tcp", "0.0.0.0:2022")
if err != nil {
panic("failed to listen for connection")
log.Fatal("failed to listen for connection", err)
}
fmt.Printf("Listening on %v\n", listener.Addr())

nConn, err := listener.Accept()
if err != nil {
panic("failed to accept incoming connection")
log.Fatal("failed to accept incoming connection", err)
}

// Before use, a handshake must be performed on the incoming
// net.Conn.
_, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
panic("failed to handshake")
log.Fatal("failed to handshake", err)
}
fmt.Fprintf(debugStream, "SSH server established\n")

Expand All @@ -89,9 +91,8 @@ func main() {
// Service the incoming Channel channel.
for newChannel := range chans {
// Channels have a type, depending on the application level
// protocol intended. In the case of a shell, the type is
// "session" and ServerShell may be used to present a simple
// terminal interface.
// protocol intended. In the case of an SFTP session, this is "subsystem"
// with a payload string of "<length=4>sftp"
fmt.Fprintf(debugStream, "Incoming channel: %s\n", newChannel.ChannelType())
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
Expand All @@ -100,7 +101,7 @@ func main() {
}
channel, requests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
log.Fatal("could not accept channel.", err)
}
fmt.Fprintf(debugStream, "Channel accepted\n")

Expand All @@ -125,8 +126,10 @@ func main() {

server, err := sftp.NewServer(channel, channel, debugStream, debugLevel, readOnly, rootDir)
if err != nil {
panic(err)
log.Fatal(err)
}
if err := server.Serve(); err != nil {
log.Fatal("sftp server completed with error:", err)
}
go server.Serve()
}
}
11 changes: 7 additions & 4 deletions server_standalone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
)

func main() {
readOnly := false
debugLevelStr := "none"
debugLevel := 0
debugStderr := false
var (
readOnly bool
debugLevelStr string
debugLevel int
debugStderr bool
)

flag.BoolVar(&readOnly, "R", false, "read-only server")
flag.BoolVar(&debugStderr, "e", false, "debug to stderr")
flag.StringVar(&debugLevelStr, "l", "none", "debug level")
Expand Down

0 comments on commit cf6c57c

Please sign in to comment.