|
| 1 | +// Copyright 2025. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ui |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strconv" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/Adembc/lazyssh/internal/core/domain" |
| 23 | +) |
| 24 | + |
| 25 | +// ParseSSHCommand parses an SSH command string and returns ServerFormData |
| 26 | +// Supported formats: |
| 27 | +// - ssh user@host |
| 28 | +// - ssh host |
| 29 | +// - ssh user@host -p port |
| 30 | +// - ssh user@host -i keyfile |
| 31 | +// - ssh -p port user@host |
| 32 | +// - ssh -i keyfile user@host |
| 33 | +func ParseSSHCommand(cmd string) (ServerFormData, error) { |
| 34 | + data := ServerFormData{ |
| 35 | + User: "root", |
| 36 | + Port: "22", |
| 37 | + Key: "~/.ssh/id_ed25519", |
| 38 | + } |
| 39 | + |
| 40 | + // Trim whitespace and split into tokens |
| 41 | + cmd = strings.TrimSpace(cmd) |
| 42 | + tokens := strings.Fields(cmd) |
| 43 | + |
| 44 | + if len(tokens) == 0 { |
| 45 | + return data, fmt.Errorf("empty command") |
| 46 | + } |
| 47 | + |
| 48 | + // Check if it starts with ssh (optional) |
| 49 | + i := 0 |
| 50 | + if tokens[0] == "ssh" { |
| 51 | + i = 1 |
| 52 | + } |
| 53 | + |
| 54 | + if i >= len(tokens) { |
| 55 | + return data, fmt.Errorf("no host specified") |
| 56 | + } |
| 57 | + |
| 58 | + // Parse arguments and host |
| 59 | + var hostPart string |
| 60 | + for i < len(tokens) { |
| 61 | + token := tokens[i] |
| 62 | + |
| 63 | + // Handle arguments |
| 64 | + if strings.HasPrefix(token, "-") { |
| 65 | + switch token { |
| 66 | + case "-p": |
| 67 | + // Handle port |
| 68 | + if i+1 < len(tokens) { |
| 69 | + i++ |
| 70 | + port, err := strconv.Atoi(tokens[i]) |
| 71 | + if err != nil || port < 1 || port > 65535 { |
| 72 | + return data, fmt.Errorf("invalid port: %s", tokens[i]) |
| 73 | + } |
| 74 | + data.Port = tokens[i] |
| 75 | + } else { |
| 76 | + return data, fmt.Errorf("missing port value after -p") |
| 77 | + } |
| 78 | + case "-i": |
| 79 | + // Handle key file |
| 80 | + if i+1 < len(tokens) { |
| 81 | + i++ |
| 82 | + data.Key = tokens[i] |
| 83 | + } else { |
| 84 | + return data, fmt.Errorf("missing key file after -i") |
| 85 | + } |
| 86 | + default: |
| 87 | + // Ignore other arguments |
| 88 | + } |
| 89 | + } else if hostPart == "" { |
| 90 | + // This should be host or user@host |
| 91 | + hostPart = token |
| 92 | + } |
| 93 | + i++ |
| 94 | + } |
| 95 | + |
| 96 | + if hostPart == "" { |
| 97 | + return data, fmt.Errorf("no host specified") |
| 98 | + } |
| 99 | + |
| 100 | + // Parse user@host |
| 101 | + if strings.Contains(hostPart, "@") { |
| 102 | + parts := strings.SplitN(hostPart, "@", 2) |
| 103 | + if len(parts) == 2 && parts[0] != "" && parts[1] != "" { |
| 104 | + data.User = parts[0] |
| 105 | + data.Host = parts[1] |
| 106 | + } else { |
| 107 | + return data, fmt.Errorf("invalid user@host format: %s", hostPart) |
| 108 | + } |
| 109 | + } else { |
| 110 | + data.Host = hostPart |
| 111 | + } |
| 112 | + |
| 113 | + // Generate default alias |
| 114 | + if data.Host != "" { |
| 115 | + // If it's an IP address, use it directly |
| 116 | + alias := data.Host |
| 117 | + // Check if it's an IP address |
| 118 | + if !strings.Contains(alias, ":") && strings.Count(alias, ".") == 3 { |
| 119 | + // Might be IPv4, check if all parts are numbers |
| 120 | + parts := strings.Split(alias, ".") |
| 121 | + isIP := true |
| 122 | + for _, part := range parts { |
| 123 | + if _, err := strconv.Atoi(part); err != nil { |
| 124 | + isIP = false |
| 125 | + break |
| 126 | + } |
| 127 | + } |
| 128 | + if !isIP { |
| 129 | + // Not an IP, remove domain suffix to create short alias |
| 130 | + if idx := strings.Index(alias, "."); idx > 0 { |
| 131 | + alias = alias[:idx] |
| 132 | + } |
| 133 | + } |
| 134 | + } else if !strings.Contains(alias, ":") { |
| 135 | + // Not an IP, remove domain suffix to create short alias |
| 136 | + if idx := strings.Index(alias, "."); idx > 0 { |
| 137 | + alias = alias[:idx] |
| 138 | + } |
| 139 | + } |
| 140 | + data.Alias = alias |
| 141 | + } |
| 142 | + |
| 143 | + return data, nil |
| 144 | +} |
| 145 | + |
| 146 | +// BuildServerFromSSHCommand builds a Server object from an SSH command |
| 147 | +func BuildServerFromSSHCommand(cmd string) (domain.Server, error) { |
| 148 | + data, err := ParseSSHCommand(cmd) |
| 149 | + if err != nil { |
| 150 | + return domain.Server{}, err |
| 151 | + } |
| 152 | + |
| 153 | + // Validate form data |
| 154 | + if errMsg := validateServerForm(data); errMsg != "" { |
| 155 | + return domain.Server{}, fmt.Errorf("%s", errMsg) |
| 156 | + } |
| 157 | + |
| 158 | + // Convert port |
| 159 | + port := 22 |
| 160 | + if data.Port != "" { |
| 161 | + if n, err := strconv.Atoi(data.Port); err == nil && n > 0 { |
| 162 | + port = n |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Process tags |
| 167 | + var tags []string |
| 168 | + if data.Tags != "" { |
| 169 | + for _, t := range strings.Split(data.Tags, ",") { |
| 170 | + if s := strings.TrimSpace(t); s != "" { |
| 171 | + tags = append(tags, s) |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return domain.Server{ |
| 177 | + Alias: data.Alias, |
| 178 | + Host: data.Host, |
| 179 | + User: data.User, |
| 180 | + Port: port, |
| 181 | + Key: data.Key, |
| 182 | + Tags: tags, |
| 183 | + }, nil |
| 184 | +} |
0 commit comments