/
rcurl.go
112 lines (100 loc) · 3.3 KB
/
rcurl.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
// Copyright (c) 2015 Christopher Cooper
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// RCURL (remote curl) creates an ssh session and then curls a remote host. Good for environments with VPCs and VPNs
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
smssh "sessionm/shared/net/ssh"
"golang.org/x/crypto/ssh"
)
var (
sshHost = flag.String("H", "", "the remote host with which to begin an ssh session")
remoteUrl = flag.String("u", "", "the host url that you would like to curl")
options = flag.String("o", "", `the curl options you would like to use, surrounded in quotes, ex: -o=""`)
privateKeyFile = flag.String("P", "", "if specified, the location of the private key file to use for the ssh session - if not provided, the system default path will be attempted - MacOS(/Users/{user}/.ssh/id_rsa), Linux(/home/{user}/.ssh/id_rsa)")
)
var (
NoSshHostGiven = errors.New("-H option (remote host) is required.")
NoRemoteUrl = errors.New("-u option (remote url) is required.")
)
func main() {
parseFlags()
conn, err := setupConn()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
var data []byte
if *options != "" {
data, err = smssh.CurlFromRemote(conn, *remoteUrl, strings.Split(*options, " ")...)
} else {
data, err = smssh.CurlFromRemote(conn, *remoteUrl)
}
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
fmt.Println(string(data))
}
func parseFlags() {
flag.Parse()
if *sshHost == "" {
fmt.Printf(NoSshHostGiven.Error())
}
if *remoteUrl == "" {
fmt.Printf(NoRemoteUrl.Error())
}
}
func setupConn() (*ssh.Client, error) {
if !strings.Contains(*sshHost, ":") {
split := strings.Split(*sshHost, "/")
if len(split) == 1 {
*sshHost = split[0] + ":" + "22"
} else {
*sshHost = split[0] + ":" + "22"
for i, s := range split {
if i != 0 {
*sshHost = fmt.Sprintf("%s/%s", *sshHost, s)
}
}
}
}
var config *ssh.ClientConfig
if *privateKeyFile != "" {
auth, err := smssh.ParsePrivateKey(*privateKeyFile)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
config = &ssh.ClientConfig{User: smssh.CurrentUser, Auth: []ssh.AuthMethod{auth}}
} else {
err := smssh.SetupDefaultClientConfig()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
config = smssh.DefaultClientConfig
}
return smssh.GetSshConn(*sshHost, config)
}