Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a custom port for ssh connection #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

dist/
.DS_Store
.DS_Store
2 changes: 1 addition & 1 deletion cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ It assumes that your VPS is already configured and that your application is read
)

go func() {
sshClient, err := utils.Login(viper.GetString("serverAddress"), "sidekick")
sshClient, err := utils.Login(viper.GetString("serverAddress"), viper.GetString("sshPort"), "sidekick")
if err != nil {
p.Send(render.ErrorMsg{})
}
Expand Down
21 changes: 19 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ var InitCmd = &cobra.Command{
if serverFlagErr != nil {
fmt.Println(serverFlagErr)
}

port, portFlagErr := cmd.Flags().GetString("port")
if portFlagErr != nil {
fmt.Println(portFlagErr)
}
certEmail, emailFlagError := cmd.Flags().GetString("email")
if emailFlagError != nil {
fmt.Println(emailFlagError)
Expand All @@ -74,6 +79,16 @@ var InitCmd = &cobra.Command{
}
}

if port == "" {
portTextInput := pterm.DefaultInteractiveTextInput
portTextInput.DefaultText = "Please enter the SSH Port of your VPS"
port, _ = portTextInput.Show()
if !utils.IsValidPort(port) {
pterm.Error.Printfln("You entered an incorrect SSH Port - %s", port)
os.Exit(0)
}
}

if certEmail == "" {
certEmailTextInput := pterm.DefaultInteractiveTextInput
certEmailTextInput.DefaultText = "Please enter an email for use with TLS certs"
Expand Down Expand Up @@ -104,6 +119,7 @@ var InitCmd = &cobra.Command{
}

viper.Set("serverAddress", server)
viper.Set("sshPort", port)
viper.Set("certEmail", certEmail)

pterm.Println()
Expand All @@ -116,7 +132,7 @@ var InitCmd = &cobra.Command{
users := []string{"root", "sidekick"}
canConnect := false
for _, user := range users {
sshClient, initSessionErr = utils.Login(server, user)
sshClient, initSessionErr = utils.Login(server, port, user)
if initSessionErr != nil {
continue
}
Expand Down Expand Up @@ -197,7 +213,7 @@ var InitCmd = &cobra.Command{
stage0Spinner.Success(utils.UsersetupStage.SpinnerSuccessMessage)

sidekickLoginSpinner.Sequence = []string{"▀ ", " ▀", " ▄", "▄ "}
sidekickSshClient, err := utils.Login(server, "sidekick")
sidekickSshClient, err := utils.Login(server, port, "sidekick")
if err != nil {
sidekickLoginSpinner.Fail("Something went wrong logging in to your VPS")
log.Fatal(err)
Expand Down Expand Up @@ -313,6 +329,7 @@ func init() {
rootCmd.AddCommand(InitCmd)

InitCmd.Flags().StringP("server", "s", "", "Set the IP address of your Server")
InitCmd.Flags().StringP("port", "p", "", "Set the SSH Port of your server")
InitCmd.Flags().StringP("email", "e", "", "An email address to be used for SSL certs")
InitCmd.Flags().BoolP("yes", "y", false, "Skip all validation prompts")
}
2 changes: 1 addition & 1 deletion cmd/launch/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var LaunchCmd = &cobra.Command{
})

go func() {
sshClient, err := utils.Login(viper.GetString("serverAddress"), "sidekick")
sshClient, err := utils.Login(viper.GetString("serverAddress"), viper.GetString("sshPort"), "sidekick")
if err != nil {
p.Send(render.ErrorMsg{ErrorStr: "Something went wrong logging in to your VPS"})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var PreviewCmd = &cobra.Command{
})

go func() {
sshClient, err := utils.Login(viper.GetString("serverAddress"), "sidekick")
sshClient, err := utils.Login(viper.GetString("serverAddress"), viper.GetString("sshPort"), "sidekick")
if err != nil {
p.Send(render.ErrorMsg{})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/preview/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func deletePreviewEnv(hash string) {
if appConfigErr != nil {
log.Fatalf("Unable to load your config file. Might be corrupted")
}
sshClient, err := utils.Login(viper.GetString("serverAddress"), "sidekick")
sshClient, err := utils.Login(viper.GetString("serverAddress"), viper.GetString("sshPort"), "sidekick")
if err != nil {
log.Fatal("Unable to login to your VPS")
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/charmbracelet/bubbletea v1.1.1
github.com/charmbracelet/lipgloss v0.13.0
github.com/charmbracelet/log v0.4.0
github.com/joho/godotenv v1.5.1
github.com/skeema/knownhosts v1.3.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand All @@ -32,7 +33,6 @@ require (
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down
8 changes: 4 additions & 4 deletions utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func inspectServerPublicKey(key ssh.PublicKey, hostname string) {

}

func GetSshClient(server string, sshUser string) (*ssh.Client, error) {
sshPort := "22"
func GetSshClient(server string, sshPort string, sshUser string) (*ssh.Client, error) {
//sshPort := "22"
sshAgentSock := os.Getenv("SSH_AUTH_SOCK")
if sshAgentSock == "" {
log.Fatal("No SSH SOCK AVAILABLE")
Expand Down Expand Up @@ -143,8 +143,8 @@ func GetSshClient(server string, sshUser string) (*ssh.Client, error) {
return client, nil
}

func Login(server string, user string) (*ssh.Client, error) {
sshClient, err := GetSshClient(server, user)
func Login(server string, port string, user string) (*ssh.Client, error) {
sshClient, err := GetSshClient(server, port, user)
if err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -114,6 +115,22 @@ func IsValidIPAddress(ip string) bool {

}

func IsValidPort(port string) bool {
const portPattern = `^\d{1,5}$`

re := regexp.MustCompile(portPattern)
if !re.MatchString(port) {
return false
}

portNumber, err := strconv.Atoi(port)
if err != nil || portNumber < 1 || portNumber > 65535 {
return false
}

return true
}

func FileExists(filename string) bool {
_, err := os.Stat(filename)
if err == nil {
Expand Down