Skip to content

Commit

Permalink
support a command to run that specifies the environment for the ssh c…
Browse files Browse the repository at this point in the history
…omand
  • Loading branch information
zwily committed Sep 17, 2014
1 parent d76db89 commit 20c8844
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TunnelDef struct {
JumpHost string `yaml:"jumpHost"`
RemoteHost string `yaml:"remoteHost"`
RemotePort int `yaml:"remotePort"`
EnvCommand string `yaml:"envCommand"`
}

type Config struct {
Expand All @@ -38,7 +39,7 @@ func main() {
goyaml.Unmarshal(configYaml, &config)

for _, t := range config.Tunnels {
s := server.New(t.Name, t.LocalPort, t.JumpHost, t.RemoteHost, t.RemotePort)
s := server.New(t.Name, t.LocalPort, t.JumpHost, t.RemoteHost, t.RemotePort, t.EnvCommand)
go s.Listen()
defer s.Close()
}
Expand Down
15 changes: 14 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"os/exec"
"strings"
"time"
)

Expand All @@ -16,18 +17,20 @@ type Server struct {
remoteHost string
remotePort int
proxyPort int
envCmd string

pubsub *pubsub.PubSub
cmd *exec.Cmd
}

func New(name string, localPort int, jumpHost string, remoteHost string, remotePort int) *Server {
func New(name string, localPort int, jumpHost string, remoteHost string, remotePort int, envCommand string) *Server {
s := &Server{
name: name,
localPort: localPort,
jumpHost: jumpHost,
remoteHost: remoteHost,
remotePort: remotePort,
envCmd: envCommand,
}

s.pubsub = pubsub.New(0)
Expand Down Expand Up @@ -131,6 +134,16 @@ func (s *Server) handlePending(in <-chan *net.TCPConn) {
s.jumpHost,
)

if s.envCmd != "" {
out, err := exec.Command("/bin/bash", "-c", s.envCmd).Output()
if err != nil {
log.Fatal(err)
}

log.Printf("%s: using custom environment %s", s.name, out)
cmd.Env = strings.Split(string(out), "\n")
}

log.Printf("%s: starting ssh\n", s.name)

err := cmd.Start()
Expand Down

0 comments on commit 20c8844

Please sign in to comment.