-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (147 loc) · 4.91 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"net/url"
"os"
"strings"
)
type Flags struct {
Url string
Version bool
ShowResHeaders bool
Head bool
KeepAlive bool
Headers []string
PostBody string
}
// CliFlags Parses CLI Flags
func CliFlags() Flags {
var returnFlags Flags
// Short Flags
flag.StringVar(&returnFlags.Url, "u", "", "URL to Request")
flag.BoolVar(&returnFlags.Version, "v", false, "Display version information")
flag.BoolVar(&returnFlags.ShowResHeaders, "i", false, "Show response headers")
flag.BoolVar(&returnFlags.KeepAlive, "k", false, "Set connection to \"keep-alive\"")
flag.StringVar(&returnFlags.PostBody, "p", "", "Set request type to POST")
flag.BoolVar(&returnFlags.Head, "he", false, "Set request type to HEAD")
// Append each header to the returnFlags.Headers array
flag.Func("H", "Add a header to the request", func(val string) error {
returnFlags.Headers = append(returnFlags.Headers, val)
return nil
})
// Long flags
flag.StringVar(&returnFlags.Url, "url", "", "URL to Request")
flag.BoolVar(&returnFlags.Version, "version", false, "Display version information")
flag.BoolVar(&returnFlags.ShowResHeaders, "show-headers", false, "Show response headers")
flag.BoolVar(&returnFlags.KeepAlive, "keepalive", false, "Set connection to \"keep-alive\"")
flag.StringVar(&returnFlags.PostBody, "post", "", "Set request type to POST")
flag.BoolVar(&returnFlags.Head, "head", false, "Set request type to HEAD")
// Append each header to the returnFlags.Headers array
flag.Func("header", "Add a header to the request", func(val string) error {
returnFlags.Headers = append(returnFlags.Headers, val)
return nil
})
// Custom help message for -h/--help
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -h, --help Display this Message\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -u, --url URL to Request\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -v, --version Display version information\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -i, --show-headers Display response headers\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -k, --keepalive Set connection to \"keep-alive\"\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -H, --header Add a header. To add another, use this flag again.\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -p, --post Sets request type to POST. Pass this flag your request body.\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " -he, --head Sets request type to HEAD\n")
}
flag.Parse()
return returnFlags
}
// SetupRequest General setup that all request types will use
func SetupRequest(flags *Flags) (Connection net.Conn, Host string, Path string, Error error) {
// Parse URL
parsedURL, err := url.Parse(flags.Url)
if err != nil {
return nil, "", "", err
}
// If the URL was parsed w/o a protocol prepend "http://"
if parsedURL.Host == "" {
parsedURL, err = url.Parse("http://" + flags.Url)
if err != nil {
return nil, "", "", err
}
}
var host string
if parsedURL.Scheme == "http" && parsedURL.Port() == "" {
// HTTP on Port 80
host = parsedURL.Hostname()
host += ":80"
} else if parsedURL.Scheme == "https" && parsedURL.Port() == "" {
// HTTPS on Port 443
host = parsedURL.Hostname()
host += ":443"
} else {
// Otherwise use user specified port
host = parsedURL.Hostname() + ":" + parsedURL.Port()
}
// Make a TCP Connection
conn, err := net.Dial("tcp", host)
if err != nil {
return nil, "", "", err
}
// Use TLS if the request is HTTPS
var client net.Conn
if parsedURL.Scheme == "https" {
client = tls.Client(conn, &tls.Config{
ServerName: parsedURL.Hostname(),
})
} else {
client = conn
}
return client, host, parsedURL.Path, nil
}
func main() {
// Read CLI Flags
cliFlags := CliFlags()
// Print version info if -v/--version is set
if cliFlags.Version {
versionData := "Go URL by arithefirst\n"
versionData += "gURL Version beta+0.2\n"
versionData += "---------------------\n"
versionData += "github.com/arithefirst/gurl\n"
fmt.Print(versionData)
return
}
// Check to see if the URl is empty
if cliFlags.Url == "" {
fmt.Println("Usage: gurl -u <target URL>")
os.Exit(0)
}
// Pass to get.go/post.go
var res []byte
var err error
// Send to get.go if post body not empty
if cliFlags.PostBody == "" {
if cliFlags.Head {
res, err = Head(&cliFlags)
} else {
res, err = Get(&cliFlags)
}
} else {
res, err = Post(&cliFlags)
}
if err != nil {
log.Fatalf("Error: %s", err.Error())
}
if !cliFlags.ShowResHeaders && !cliFlags.Head {
// Print the response w/o headers
resStr := string(res)
fmt.Print(resStr[strings.Index(resStr, "\r\n\r\n")+4:])
} else {
// Print the response w/ headers
fmt.Print(string(res))
}
}