-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
130 lines (102 loc) · 3.22 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
)
type RemoteShellOptions struct {
username string
port int
timeLimit time.Duration
idleTimeout time.Duration
connectionGrace time.Duration
homeDir string
shellCommand string
insecureMode bool
// TODO: should we allow multiple sessions?
allowMultipleSessions bool
currentUserName string
}
var (
globalOptions RemoteShellOptions
)
func init() {
globalOptions = parseCommandFlags()
}
func determineDefaultShell() string {
defaultShells := [5]string{"bash", "sh", "dash", "zsh", "rbash"}
for _, shellExec := range defaultShells {
shellPath, err := exec.LookPath(shellExec)
if err != nil {
continue
}
return shellPath
}
return "/tmp/no-shell-was-found"
}
func parseCommandFlags() RemoteShellOptions {
var help = flag.Bool("help", false, "Show help")
var version = flag.Bool("version", false, "Show version")
var usernameFlag string
var userHomeFlag string
var shellCommandFlag string
var portFlag int
var graceFlag time.Duration
var idleTimeFlag time.Duration
var timeLimitFlag time.Duration
var insecureModeFlag bool
fallbackHomeDir, err := os.UserHomeDir()
if err != nil {
fallbackHomeDir = fetchEnvValue("HOME", fetchEnvValue("PWD", "/"))
}
fallbackUsername := fetchEnvValue("C87RS_USER", "")
fallbackShell := fetchEnvValue("C87RS_SHELL", "automatic")
fallBackPort := fetchEnvValueInt("C87RS_PORT", 8722)
fallBackGrace := fetchEnvValueDuration("C87RS_GRACE", 30*time.Minute)
fallBackIdle := fetchEnvValueDuration("C87RS_IDLETIME", 0*time.Second)
fallBackTimeLimit := fetchEnvValueDuration("C87RS_MAXTIME", 12*time.Hour)
flag.StringVar(&usernameFlag, "user", fallbackUsername, "GitHub username")
flag.StringVar(&userHomeFlag, "home", fallbackHomeDir, "Home Directory")
flag.StringVar(&shellCommandFlag, "shell", fallbackShell, "Shell Command")
flag.IntVar(&portFlag, "port", fallBackPort, "SSH port to use")
flag.DurationVar(&graceFlag, "grace", fallBackGrace, "How many seconds to wait for a connection before we assume abandonment")
flag.DurationVar(&idleTimeFlag, "idletime", fallBackIdle, "Idle timeout")
flag.DurationVar(&timeLimitFlag, "maxtime", fallBackTimeLimit, "Max session duration")
flag.BoolVar(&insecureModeFlag, "insecure", false, "Skip SSL verification")
flag.Parse()
if *help {
flag.Usage()
os.Exit(0)
}
if *version {
fmt.Printf("%s@%s\n", buildVersion, buildSha)
os.Exit(0)
}
if usernameFlag == "" && !isAuthKeyFromEnv() {
log.Fatal("You must provide a GitHub username")
}
userHomePath, err := filepath.Abs(userHomeFlag)
check(err)
if shellCommandFlag == "automatic" {
shellCommandFlag = determineDefaultShell()
}
shellCommand, err := exec.LookPath(shellCommandFlag)
check(err)
curUserName := fetchEnvValue("USER", fetchEnvValue("LOGNAME", "root"))
return RemoteShellOptions{
username: usernameFlag,
homeDir: userHomePath,
shellCommand: shellCommand,
port: portFlag,
idleTimeout: idleTimeFlag,
timeLimit: timeLimitFlag,
connectionGrace: graceFlag,
insecureMode: insecureModeFlag,
allowMultipleSessions: false,
currentUserName: curUserName,
}
}