Example #1
0
func main() {
	server, err := ssh2docker.NewServer()
	if err != nil {
		logrus.Fatalf("Cannot create server: %v", err)
	}

	err = server.AddHostKeyFile("/Users/moul/Git/moul/ssh2docker/host_rsa")
	if err != nil {
		logrus.Fatalf("Cannot add host key file: %v", err)
	}

	listener, err := net.Listen("tcp", ":2222")
	if err != nil {
		logrus.Fatalf("Failed to start listener: %v", err)
	}
	logrus.Infof("Listening on port 2222")

	for {
		conn, err := listener.Accept()
		if err != nil {
			logrus.Error("Accept failed: %v", err)
			continue
		}
		go server.Handle(conn)
	}
}
Example #2
0
// Action is the default cli action to execute
func Action(c *cli.Context) {
	// Initialize the SSH server
	server, err := ssh2docker.NewServer()
	if err != nil {
		log.Fatalf("Cannot create server: %v", err)
	}

	// Restrict list of allowed images
	if c.String("allowed-images") != "" {
		server.AllowedImages = strings.Split(c.String("allowed-images"), ",")
	}

	// Configure server
	server.DefaultShell = c.String("shell")
	server.DockerRunArgsInline = c.String("docker-run-args")
	server.DockerExecArgsInline = c.String("docker-exec-args")
	server.NoJoin = c.Bool("no-join")
	server.CleanOnStartup = c.Bool("clean-on-startup")
	server.PasswordAuthScript = c.String("password-auth-script")
	server.PublicKeyAuthScript = c.String("publickey-auth-script")
	server.LocalUser = c.String("local-user")
	server.Banner = c.String("banner")

	// Register the SSH host key
	hostKey := c.String("host-key")
	switch hostKey {
	case "built-in":
		hostKey = DefaultHostKey
	case "system":
		hostKey = "/etc/ssh/ssh_host_rsa_key"
	}
	err = server.AddHostKey(hostKey)
	if err != nil {
		log.Fatalf("Cannot add host key: %v", err)
	}

	// Bind TCP socket
	bindAddress := c.String("bind")
	listener, err := net.Listen("tcp", bindAddress)
	if err != nil {
		log.Fatalf("Failed to start listener on %q: %v", bindAddress, err)
	}
	log.Infof("Listening on %q", bindAddress)

	// Initialize server
	if err = server.Init(); err != nil {
		log.Fatalf("Failed to initialize the server: %v", err)
	}

	// Accept new clients
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Errorf("Accept failed: %v", err)
			continue
		}
		go server.Handle(conn)
	}
}