2014-12-29 04:06:09 +00:00
|
|
|
package sshrpc
|
|
|
|
|
|
|
|
import (
|
2015-04-06 11:08:26 +00:00
|
|
|
"log"
|
2014-12-29 04:06:09 +00:00
|
|
|
"net/rpc"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2014-12-29 04:17:16 +00:00
|
|
|
// Client represents an RPC client using an SSH backed connection.
|
2014-12-29 04:06:09 +00:00
|
|
|
type Client struct {
|
|
|
|
*rpc.Client
|
2015-04-05 10:41:07 +00:00
|
|
|
Config *ssh.ClientConfig
|
|
|
|
ChannelName string
|
|
|
|
sshClient *ssh.Client
|
2015-04-06 11:08:26 +00:00
|
|
|
RPCServer *rpc.Server
|
2014-12-29 04:06:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 04:17:16 +00:00
|
|
|
// NewClient returns a new Client to handle RPC requests.
|
2014-12-29 04:06:09 +00:00
|
|
|
func NewClient() *Client {
|
|
|
|
|
|
|
|
config := &ssh.ClientConfig{
|
2014-12-29 04:17:16 +00:00
|
|
|
User: "sshrpc",
|
2014-12-29 04:06:09 +00:00
|
|
|
Auth: []ssh.AuthMethod{
|
2014-12-29 04:17:16 +00:00
|
|
|
ssh.Password("sshrpc"),
|
2014-12-29 04:06:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-04-06 11:08:26 +00:00
|
|
|
return &Client{nil, config, DefaultRPCChannel, nil, rpc.NewServer()}
|
2014-12-29 04:06:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-29 04:17:16 +00:00
|
|
|
// Connect starts a client connection to the given SSH/RPC server.
|
2014-12-29 04:06:09 +00:00
|
|
|
func (c *Client) Connect(address string) {
|
|
|
|
|
|
|
|
sshClient, err := ssh.Dial("tcp", address, c.Config)
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to dial: " + err.Error())
|
|
|
|
}
|
2015-04-05 10:41:07 +00:00
|
|
|
c.sshClient = sshClient
|
2014-12-29 04:06:09 +00:00
|
|
|
|
2015-04-06 11:08:26 +00:00
|
|
|
c.openRPCServerChannel(c.ChannelName + "-reverse")
|
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
// Each ClientConn can support multiple channels
|
2015-04-06 11:08:26 +00:00
|
|
|
channel, err := openRPCClientChannel(c.sshClient.Conn, c.ChannelName)
|
2014-12-29 04:06:09 +00:00
|
|
|
if err != nil {
|
2015-04-05 10:41:07 +00:00
|
|
|
panic("Failed to create channel: " + err.Error())
|
2014-12-29 04:06:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
c.Client = rpc.NewClient(channel)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-06 11:08:26 +00:00
|
|
|
// openRPCClientChannel opens an SSH RPC channel and makes an ssh subsystem request to trigger remote RPC server start
|
|
|
|
func openRPCClientChannel(conn ssh.Conn, channelName string) (ssh.Channel, error) {
|
|
|
|
channel, in, err := conn.OpenChannel(channelName, nil)
|
2014-12-29 04:06:09 +00:00
|
|
|
if err != nil {
|
2015-04-05 10:41:07 +00:00
|
|
|
return nil, err
|
2014-12-29 04:06:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
// SSH Documentation states that this go channel of requests needs to be serviced
|
2015-04-06 11:08:26 +00:00
|
|
|
go ssh.DiscardRequests(in)
|
2015-04-05 10:41:07 +00:00
|
|
|
|
|
|
|
var msg struct {
|
|
|
|
Subsystem string
|
|
|
|
}
|
|
|
|
msg.Subsystem = RPCSubsystem
|
2014-12-29 04:06:09 +00:00
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
ok, err := channel.SendRequest("subsystem", true, ssh.Marshal(&msg))
|
|
|
|
if err == nil && !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel, nil
|
2014-12-29 04:06:09 +00:00
|
|
|
}
|
2015-04-06 11:08:26 +00:00
|
|
|
|
|
|
|
func (c *Client) openRPCServerChannel(channelName string) error {
|
|
|
|
subChannel := c.sshClient.HandleChannelOpen(channelName)
|
|
|
|
go func(chans <-chan ssh.NewChannel) {
|
|
|
|
|
|
|
|
for newChannel := range chans {
|
|
|
|
|
|
|
|
/* Don't need to check, already know what channel type is coming in
|
|
|
|
// Check the type of channel
|
|
|
|
if t := newChannel.ChannelType(); t != channelName {
|
|
|
|
newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
acceptRPCServerRequest(c.RPCServer, newChannel)
|
|
|
|
}
|
|
|
|
}(subChannel)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func acceptRPCServerRequest(rpcServer *rpc.Server, newChannel ssh.NewChannel) {
|
|
|
|
channel, requests, err := newChannel.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not accept channel (%s)", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("Accepted channel")
|
|
|
|
|
|
|
|
// Channels can have out-of-band requests
|
|
|
|
go func(in <-chan *ssh.Request) {
|
|
|
|
for req := range in {
|
|
|
|
ok := false
|
|
|
|
switch req.Type {
|
|
|
|
|
|
|
|
case "subsystem":
|
|
|
|
ok = true
|
|
|
|
log.Printf("subsystem '%s'", req.Payload)
|
|
|
|
switch string(req.Payload[4:]) {
|
|
|
|
//RPCSubsystem Request made indicates client desires RPC Server access
|
|
|
|
case RPCSubsystem:
|
|
|
|
go rpcServer.ServeConn(channel)
|
|
|
|
log.Printf("Started SSH RPC")
|
|
|
|
default:
|
|
|
|
log.Printf("Unknown subsystem: %s", req.Payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
log.Printf("declining %s request...", req.Type)
|
|
|
|
}
|
|
|
|
req.Reply(ok, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
}(requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait allows clients also acting as an RPC server to detect when the ssh connection ends
|
|
|
|
func (c *Client) Wait() error {
|
|
|
|
return c.sshClient.Conn.Wait()
|
|
|
|
}
|