|
|
|
@ -3,7 +3,6 @@ package main
|
|
|
|
|
import (
|
|
|
|
|
"code.google.com/p/go.crypto/ssh/terminal"
|
|
|
|
|
"crypto/rsa"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net"
|
|
|
|
@ -24,36 +23,50 @@ type ReadWriter struct {
|
|
|
|
|
io.Writer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connect() {
|
|
|
|
|
if !terminal.IsTerminal(0) {
|
|
|
|
|
exit(1, "yeah you have to run this from a tty")
|
|
|
|
|
type Client struct {
|
|
|
|
|
key *rsa.PrivateKey
|
|
|
|
|
host string
|
|
|
|
|
port int
|
|
|
|
|
nick string
|
|
|
|
|
conn net.Conn
|
|
|
|
|
}
|
|
|
|
|
f, err := os.Open(options.key)
|
|
|
|
|
|
|
|
|
|
func (c *Client) dial() error {
|
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", c.host, c.port))
|
|
|
|
|
if err != nil {
|
|
|
|
|
exit(1, "unable to open private key file at %s: %v", options.key, err)
|
|
|
|
|
return fmt.Errorf("client unable to connect: %v", err)
|
|
|
|
|
}
|
|
|
|
|
c.conn = conn
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
d1 := json.NewDecoder(f)
|
|
|
|
|
var key rsa.PrivateKey
|
|
|
|
|
if err := d1.Decode(&key); err != nil {
|
|
|
|
|
exit(1, "unable to decode key: %v", err)
|
|
|
|
|
func (c *Client) handshake() error {
|
|
|
|
|
r := &Auth{Nick: c.nick, Key: c.key.PublicKey}
|
|
|
|
|
return c.sendRequest(r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", options.host, options.port))
|
|
|
|
|
if err != nil {
|
|
|
|
|
exit(1, "unable to connect to server at %s:%d: %v", options.host, options.port, err)
|
|
|
|
|
func (c *Client) sendRequest(r request) error {
|
|
|
|
|
return writeRequest(c.conn, r)
|
|
|
|
|
}
|
|
|
|
|
auth := Auth{
|
|
|
|
|
Nick: options.nick,
|
|
|
|
|
Key: key.PublicKey,
|
|
|
|
|
|
|
|
|
|
func (c *Client) run() {
|
|
|
|
|
if err := c.dial(); err != nil {
|
|
|
|
|
exit(1, "%v", err)
|
|
|
|
|
}
|
|
|
|
|
defer c.conn.Close()
|
|
|
|
|
if err := c.handshake(); err != nil {
|
|
|
|
|
exit(1, "%v", err)
|
|
|
|
|
}
|
|
|
|
|
c.term()
|
|
|
|
|
}
|
|
|
|
|
encodeRequest(conn, &auth)
|
|
|
|
|
|
|
|
|
|
func (c *Client) term() {
|
|
|
|
|
old, err := terminal.MakeRaw(0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer terminal.Restore(0, old)
|
|
|
|
|
|
|
|
|
|
r := &ReadWriter{Reader: os.Stdin, Writer: os.Stdout}
|
|
|
|
|
term := terminal.NewTerminal(r, "> ")
|
|
|
|
|
|
|
|
|
@ -67,3 +80,22 @@ func connect() {
|
|
|
|
|
exit(1, "error on line read: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connect() {
|
|
|
|
|
if !terminal.IsTerminal(0) {
|
|
|
|
|
exit(1, "yeah, this only works from a TTY for now, sry.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key, err := privateKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
exit(1, "unable to open private key file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := &Client{
|
|
|
|
|
key: key,
|
|
|
|
|
host: options.host,
|
|
|
|
|
port: options.port,
|
|
|
|
|
nick: options.nick,
|
|
|
|
|
}
|
|
|
|
|
client.run()
|
|
|
|
|
}
|
|
|
|
|