Skip to content

Commit 24c48c1

Browse files
committed
Add more review suggestions, add -c for hostname
1 parent ed147cc commit 24c48c1

3 files changed

Lines changed: 36 additions & 27 deletions

File tree

cmd/root.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@ var signalChan chan os.Signal
3030

3131
var alreadyRunning bool
3232

33-
const http = "Expose an HTTP Port"
34-
const path = "Expose a local path"
35-
const webDAV = "Expose a local path with WebDAV"
33+
//Possible answers for prompts and error messages
34+
const (
35+
AnswerTunnelTypeHTTP string = "Expose an HTTP Port"
36+
AnswerTunnelTypePath string = "Expose a local path"
37+
AnswerTunnelTypeWebDAV string = "Expose a local path with WebDAV"
38+
AnswerYes string = "Yes"
39+
AnswerNo string = "No"
40+
PortRangeErrorMsg string = "port must be between 0-65535"
41+
PathValidityErrorMsg string = "enter an existing path without any quotation marks"
42+
)
3643

3744
var rootCmd = &cobra.Command{
3845
Use: "loophole",
@@ -53,14 +60,14 @@ func getPortPrompt() []*survey.Question {
5360
Prompt: &survey.Input{Message: "Please enter the http port you want to expose: "},
5461
Validate: func(val interface{}) error {
5562
if port, ok := val.(string); !ok {
56-
return errors.New("port must be between 0-65535")
63+
return errors.New(PortRangeErrorMsg)
5764
} else { //else is necessary here to keep access to port
5865
n, err := strconv.Atoi(port)
5966
if err != nil {
60-
return errors.New("port must be between 0-65535")
67+
return errors.New(PortRangeErrorMsg)
6168
}
6269
if (n < 0) || (n > 65535) {
63-
return errors.New("port must be between 0-65535")
70+
return errors.New(PortRangeErrorMsg)
6471
}
6572
}
6673

@@ -77,41 +84,41 @@ func getPathPrompt() []*survey.Question {
7784
Prompt: &survey.Input{Message: "Please enter the path you want to expose: "},
7885
Validate: func(val interface{}) error {
7986
if path, ok := val.(string); !ok {
80-
return errors.New("enter an existing path without any quotation marks")
87+
return errors.New(PathValidityErrorMsg)
8188
} else { //else is necessary here to keep access to path
8289
_, err := os.Stat(path)
8390
if err == nil {
8491
return nil
8592
}
86-
return errors.New("enter an existing path without any quotation marks")
93+
return errors.New(PathValidityErrorMsg)
8794
}
8895
},
8996
Transform: survey.TransformString(func(ans string) string {
90-
return fmt.Sprintf("'%s'", ans)
97+
return fmt.Sprintf("'%s'", ans) //adding quotation marks to the given answer to prevent issues with spaces in paths
9198
}),
9299
},
93100
}
94101
}
95102

96103
func getLastArgsPrompt(lastArgs string) *survey.Select {
97104
return &survey.Select{
98-
Message: "Your last settings were: '" + lastArgs + "', would you like to reuse them?",
99-
Options: []string{"Yes", "No"},
105+
Message: fmt.Sprintf("Your last settings were: '%s', would you like to reuse them?", lastArgs),
106+
Options: []string{AnswerYes, AnswerNo},
100107
}
101108
}
102109

103110
func getInitialPrompt() *survey.Select {
104111
return &survey.Select{
105112
Message: "Welcome to loophole. What do you want to do?",
106-
Options: []string{http, path, webDAV},
113+
Options: []string{AnswerTunnelTypeHTTP, AnswerTunnelTypePath, AnswerTunnelTypeWebDAV},
107114
}
108115
}
109116

110117
func askBasicAuth() string {
111118
res := ""
112119
prompt := &survey.Select{
113120
Message: "Do you want to secure your tunnel using a username and password?",
114-
Options: []string{"No", "Yes"},
121+
Options: []string{AnswerNo, AnswerYes},
115122
}
116123
var usernamePrompt = []*survey.Question{
117124
{
@@ -123,7 +130,7 @@ func askBasicAuth() string {
123130
if err != nil {
124131
signalChan <- nil
125132
}
126-
if res == "Yes" {
133+
if res == AnswerYes {
127134
err = survey.Ask(usernamePrompt, &res)
128135
if err != nil {
129136
os.Exit(0)
@@ -139,16 +146,16 @@ func askHostname() string {
139146
res := ""
140147
prompt := &survey.Select{
141148
Message: "Do you want to use a custom hostname?",
142-
Options: []string{"No", "Yes"},
149+
Options: []string{AnswerNo, AnswerYes},
143150
}
144151
var hostnamePrompt = []*survey.Question{
145152
{
146153
Name: "hostname",
147154
Prompt: &survey.Input{Message: "Please enter the hostname you want to use: "},
148155
Validate: func(val interface{}) error {
149-
var validChars = regexp.MustCompile(`^[a-z0-9]+$`).MatchString
150-
if hostname, ok := val.(string); !ok || len(hostname) > 31 || len(hostname) < 6 || !validChars(hostname) || !unicode.IsLetter(rune(hostname[0])) {
151-
return errors.New("hostname must be between 6-31 characters, may only contain lowercase letters and numbers and must start with a letter")
156+
var validChars = regexp.MustCompile(`^[a-z][a-z0-9]{0,30}$`).MatchString
157+
if hostname, ok := val.(string); !ok || len(hostname) > 31 || !validChars(hostname) || !unicode.IsLetter(rune(hostname[0])) {
158+
return errors.New("hostname must be up to 31 characters, may only contain lowercase letters and numbers and must start with a letter")
152159
}
153160

154161
return nil
@@ -159,7 +166,7 @@ func askHostname() string {
159166
if err != nil {
160167
signalChan <- nil
161168
}
162-
if res == "Yes" {
169+
if res == AnswerYes {
163170
err = survey.Ask(hostnamePrompt, &res)
164171
if err != nil {
165172
os.Exit(0)
@@ -198,7 +205,7 @@ func interactivePrompt() {
198205
if err != nil {
199206
signalChan <- nil
200207
}
201-
if res == "Yes" {
208+
if res == AnswerYes {
202209
cmd.SetArgs(strings.Split(lastArgs, " ")) //needs validation
203210
cmd.Execute()
204211
os.Exit(1)
@@ -210,19 +217,19 @@ func interactivePrompt() {
210217
signalChan <- nil
211218
}
212219
switch res {
213-
case http:
220+
case AnswerTunnelTypeHTTP:
214221
err = survey.Ask(portPrompt, &exposePort)
215222
if err != nil {
216223
signalChan <- nil
217224
}
218225
arguments = []string{"http", strconv.Itoa(exposePort)}
219-
case path:
226+
case AnswerTunnelTypePath:
220227
err = survey.Ask(pathPrompt, &exposePath)
221228
if err != nil {
222229
signalChan <- nil
223230
}
224231
arguments = []string{"path", exposePath}
225-
case webDAV:
232+
case AnswerTunnelTypeWebDAV:
226233
err = survey.Ask(pathPrompt, &exposePath)
227234
if err != nil {
228235
signalChan <- nil
@@ -236,10 +243,10 @@ func interactivePrompt() {
236243
}
237244
basicAuth := askBasicAuth()
238245
if basicAuth != "" {
239-
arguments = append(arguments, "-u", basicAuth)
246+
arguments = append(arguments, "--basic-auth-username", basicAuth)
240247
}
241-
closehandler.SaveArguments(arguments)
242248
cmd.SetArgs(arguments)
249+
closehandler.SaveArguments(arguments)
243250
cmd.Execute()
244251
}
245252

cmd/virtual-serve.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func initServeCommand(serveCmd *cobra.Command) {
3232

3333
serveCmd.PersistentFlags().StringVarP(&remoteEndpointSpecs.IdentityFile, "identity-file", "i", fmt.Sprintf("%s/id_rsa", sshDir), "private key path")
3434
serveCmd.MarkFlagFilename("identity-file")
35-
36-
serveCmd.PersistentFlags().StringVar(&remoteEndpointSpecs.SiteID, "hostname", "", "custom hostname you want to run service on")
35+
serveCmd.PersistentFlags().StringVarP(&remoteEndpointSpecs.SiteID, "hostname", "c", "", "custom hostname you want to run service on")
3736
serveCmd.PersistentFlags().BoolVar(&config.Config.Display.QR, "qr", false, "use if you want a QR version of your url to be shown")
3837

3938
serveCmd.PersistentFlags().StringVarP(&remoteEndpointSpecs.BasicAuthUsername, basicAuthUsernameFlagName, "u", "", "Basic authentication username to protect site with")

internal/pkg/closehandler/closehandler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func SetupCloseHandler(feedbackFormURL string) chan os.Signal {
3939
}
4040
if interactiveArgs != "" {
4141
communication.Info(fmt.Sprintf("Next time, add the following to loophole to start a tunnel with the same settings: %s", interactiveArgs))
42+
if strings.Contains(interactiveArgs, "--basic-auth-username") {
43+
communication.Info("If you want to provide the password as well instead of typing it into the terminal, also add --basic-auth-password <your password>")
44+
}
4245
argFile := cache.GetLocalStorageFile("lastArgs", "logs")
4346
ioutil.WriteFile(argFile, []byte(interactiveArgs), 0644)
4447
}

0 commit comments

Comments
 (0)