Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Aug 20, 2016
0 parents commit 8329e06
Show file tree
Hide file tree
Showing 8 changed files with 558 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

*.sw?

# Project specific
.uptodate
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Paul Bellamy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SSH

Easier ssh server wrapper, net/http-style.

Work-in-progress
97 changes: 97 additions & 0 deletions example/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path/filepath"

"github.com/paulbellamy/ssh"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)

func main() {
addr := flag.String("addr", ":12345", "address to listen on")
flag.Parse()

// Configure the server
server := &ssh.Server{
Addr: *addr,
Handler: ssh.HandlerFunc(handle),

// ConnState specifies an optional callback function that is
// called when a client connection changes state. See the
// ConnState type and associated constants for details.
ConnState: func(conn net.Conn, state ssh.ConnState) {
log.Printf("[ConnState] %v: %s", conn.RemoteAddr(), state)
},

ServerConfig: &gossh.ServerConfig{
PasswordCallback: func(c gossh.ConnMetadata, pass []byte) (*gossh.Permissions, error) {
// Should use constant-time compare (or better, salt+hash) in
// a production setting.
if c.User() == "testuser" && string(pass) == "tiger" {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %q", c.User())
},
},
}

// Generate a random key for now
tempDir, err := ioutil.TempDir("", "")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
privKeyPath := filepath.Join(tempDir, "key")
out, err := exec.Command("ssh-keygen", "-f", privKeyPath, "-t", "rsa", "-N", "").CombinedOutput()
if err != nil {
log.Fatalf("Fail to generate private key: %v - %q", err, out)
}
privKeyFile, err := os.Open(privKeyPath)
if err != nil {
log.Fatal(err)
}
defer privKeyFile.Close()
server.AddHostKey(privKeyFile)

// Start the server
log.Println("Listening on:", *addr)
log.Fatal(server.ListenAndServe())
}

func handle(p *ssh.Permissions, c ssh.Channel, r <-chan *ssh.Request) {
term := terminal.NewTerminal(c, "> ")

go func() {
defer c.Close()
for {
line, err := term.ReadLine()
if err != nil {
break
}
fmt.Println(line)
}
}()

for req := range r {
ok := false
switch req.Type {
case "shell":
ok = true
if len(req.Payload) > 0 {
// We don't accept any
// commands, only the
// default shell.
ok = false
}
}
req.Reply(ok, nil)
}
}
23 changes: 23 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ssh

import "golang.org/x/crypto/ssh"

var DefaultHandler = HandlerFunc(func(p *Permissions, c Channel, r <-chan *Request) {
ssh.DiscardRequests(unwrapRequests(r))
})

// Handler handles ssh connections
type Handler interface {
ServeSSH(*Permissions, Channel, <-chan *Request) // TODO: Finish filling this in
}

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(*Permissions, Channel, <-chan *Request)

// ServeSSH calls f(p, c, r).
func (f HandlerFunc) ServeSSH(p *Permissions, c Channel, r <-chan *Request) {
f(p, c, r)
}
Loading

0 comments on commit 8329e06

Please sign in to comment.