()
| 14 | ) |
| 15 | |
| 16 | func main() { |
| 17 | command := flag.String("c", "", "Set the command to run for each SSH session.") |
| 18 | var env args.ArrayArg |
| 19 | flag.Var(&env, "e", "Set environment variables for each SSH session.") |
| 20 | useOsEnv := flag.Bool("os-env", false, "Use the OS environment variables for the command, ignoring env passed by user.") |
| 21 | host := flag.String("h", "localhost", "Set the host for the server.") |
| 22 | port := flag.String("p", "2222", "Set the port for the server.") |
| 23 | hostKeyPath := flag.String("k", path.GetDefaultHostKeyPath(), "Set the path to the host key.") |
| 24 | |
| 25 | flag.Parse() |
| 26 | |
| 27 | if *command == "" { |
| 28 | log.Fatal("No command provided.") |
| 29 | } |
| 30 | |
| 31 | srv, err := server.CreateServer(server.CreateServerOptions{ |
| 32 | CommandProvider: func(s ssh.Session) *exec.Cmd { |
| 33 | argSession := args.NewSession(s) |
| 34 | fmtCmd := argSession.FormatArg(*command) |
| 35 | fmtEnv := argSession.FormatArgs(env) |
| 36 | |
| 37 | cmd := exec.CommandContext(s.Context(), "sh", "-c", fmtCmd) |
| 38 | if *useOsEnv { |
| 39 | cmd.Env = os.Environ() |
| 40 | } else { |
| 41 | cmd.Env = s.Environ() |
| 42 | } |
| 43 | cmd.Env = append(cmd.Env, fmtEnv...) |
| 44 | return cmd |
| 45 | }, |
| 46 | |
| 47 | Host: *host, |
| 48 | Port: *port, |
| 49 | HostKeyPath: *hostKeyPath, |
| 50 | |
| 51 | PublicKeyAuth: func(ctx ssh.Context, key ssh.PublicKey) bool { |
| 52 | log.Info("Public key auth", "remote", ctx.RemoteAddr(), "user", ctx.User(), "key", utils.StringifyPublicKey(key)) |
| 53 | return true |
| 54 | }, |
| 55 | PasswordAuth: func(ctx ssh.Context, password string) bool { |
| 56 | log.Info("Password auth", "remote", ctx.RemoteAddr(), "user", ctx.User(), "password", password) |
| 57 | return true |
| 58 | }, |
| 59 | }) |
| 60 | |
| 61 | if err != nil { |
| 62 | log.Fatalf("could not create server: %v", err) |
| 63 | } |
| 64 | |
| 65 | log.Info("Starting server...", "address", srv.Addr) |
| 66 | log.Fatal(srv.ListenAndServe()) |
| 67 | } |
nothing calls this directly
no test coverage detected