|
| 1 | +package ssh |
| 2 | + |
| 3 | +import "strings" |
| 4 | + |
| 5 | +type SSHPassthrough struct { |
| 6 | + Options []string |
| 7 | + RemoteCommand []string |
| 8 | +} |
| 9 | + |
| 10 | +// ParseOpenSSHPassThrough walks pass-through args (e.g. args[1:] from "pangolin ssh <res> ..."). |
| 11 | +func ParseOpenSSHPassThrough(args []string) SSHPassthrough { |
| 12 | + if len(args) == 0 { |
| 13 | + return SSHPassthrough{} |
| 14 | + } |
| 15 | + var opts []string |
| 16 | + i := 0 |
| 17 | + for i < len(args) { |
| 18 | + a := args[i] |
| 19 | + if a == "--" { |
| 20 | + opts = append(opts, a) |
| 21 | + i++ |
| 22 | + return SSHPassthrough{Options: opts, RemoteCommand: cloneStringSliceOrNil(args[i:])} |
| 23 | + } |
| 24 | + if !strings.HasPrefix(a, "-") { |
| 25 | + break |
| 26 | + } |
| 27 | + ex := openSSHOptionExtras(a, args, i) |
| 28 | + end := i + 1 + ex |
| 29 | + if end > len(args) { |
| 30 | + end = len(args) |
| 31 | + } |
| 32 | + opts = append(opts, args[i:end]...) |
| 33 | + i = end |
| 34 | + } |
| 35 | + return SSHPassthrough{Options: opts, RemoteCommand: cloneStringSliceOrNil(args[i:])} |
| 36 | +} |
| 37 | + |
| 38 | +func cloneStringSliceOrNil(s []string) []string { |
| 39 | + if len(s) == 0 { |
| 40 | + return nil |
| 41 | + } |
| 42 | + return append([]string{}, s...) |
| 43 | +} |
| 44 | + |
| 45 | +// openSSHOptionExtras returns how many args after the current token should be part of the same |
| 46 | +// option (0 = only the current token, e.g. -N; 1 = one following value, e.g. -F path). |
| 47 | +func openSSHOptionExtras(a string, args []string, i int) int { |
| 48 | + if a == "" || a == "--" { |
| 49 | + return 0 |
| 50 | + } |
| 51 | + // long options |
| 52 | + if len(a) > 1 && a[0] == '-' && a[1] == '-' { |
| 53 | + if strings.Contains(a, "=") { |
| 54 | + return 0 |
| 55 | + } |
| 56 | + if i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") && longOpenSSHWithArg(a) { |
| 57 | + return 1 |
| 58 | + } |
| 59 | + return 0 |
| 60 | + } |
| 61 | + if !strings.HasPrefix(a, "-") || a == "-" { |
| 62 | + return 0 |
| 63 | + } |
| 64 | + // exactly two runes: e.g. -N, -L, -1, -2 |
| 65 | + if len(a) == 2 { |
| 66 | + switch a[1] { |
| 67 | + case '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 68 | + 'N', 'G', 'T', 'C', 'f', 'g', 'n', 'q', 's', 't', 'v', 'x', 'X', 'Y', 'A', 'a', 'M', 'Q', 'V', 'y': |
| 69 | + return 0 |
| 70 | + case 'B', 'b', 'c', 'e', 'E', 'F', 'h', 'I', 'J', 'K', 'L', 'm', 'O', 'o', 'P', 'R', 'S', 'W', 'D', 'i', 'l', 'p', 'U': |
| 71 | + return 1 |
| 72 | + } |
| 73 | + return 0 |
| 74 | + } |
| 75 | + // combined short token |
| 76 | + if a[0] == '-' { |
| 77 | + c := a[1] |
| 78 | + switch c { |
| 79 | + case 'D': |
| 80 | + // -D, -D1080, -D[bind]:port |
| 81 | + if len(a) == 2 { |
| 82 | + return 1 |
| 83 | + } |
| 84 | + return 0 |
| 85 | + case 'L', 'R': |
| 86 | + // -L, -L8080:host:port |
| 87 | + if len(a) == 2 { |
| 88 | + return 1 |
| 89 | + } |
| 90 | + if i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") { |
| 91 | + // e.g. -L with space then spec; if no ':' in a[2:] treat next as spec |
| 92 | + if !strings.Contains(a[2:], ":") { |
| 93 | + return 1 |
| 94 | + } |
| 95 | + } |
| 96 | + return 0 |
| 97 | + case 'O': |
| 98 | + // -O with command (single token) or -O and next |
| 99 | + if len(a) == 2 && i+1 < len(args) { |
| 100 | + return 1 |
| 101 | + } |
| 102 | + return 0 |
| 103 | + } |
| 104 | + } |
| 105 | + return 0 |
| 106 | +} |
| 107 | + |
| 108 | +func longOpenSSHWithArg(s string) bool { |
| 109 | + known := map[string]struct{}{ |
| 110 | + "--bind-address": {}, |
| 111 | + "--ciphers": {}, |
| 112 | + "--kex": {}, |
| 113 | + "--kexalgorithms": {}, |
| 114 | + "--log-level": {}, |
| 115 | + "--macs": {}, |
| 116 | + "--keygen": {}, |
| 117 | + "--user": {}, |
| 118 | + } |
| 119 | + _, ok := known[strings.ToLower(s)] |
| 120 | + return ok |
| 121 | +} |
0 commit comments