-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (152 loc) · 4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"os"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)
type Params struct {
typeP string
url string
query string
header string
message string
method string
file string
importPath string
proto string
methodName string
verbose bool
userAgent string
timeout int
}
func getProtocol(p Params) Protocol {
switch p.typeP {
case "ws":
return NewWebsocket(p)
case "gq":
return NewGrapQL(p)
case "http":
return NewHTTP(p)
case "grpc":
return NewGRPC(p)
}
return nil
}
var styleInfo = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")).
Bold(true).
Background(lipgloss.Color("#6699FF")).Padding(1).MarginBottom(1).MarginTop(1).MarginLeft(1)
var styleErr = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")).
Bold(true).
Background(lipgloss.Color("#e05074")).Padding(1).MarginBottom(1).MarginTop(1).MarginLeft(1)
var styleMessage = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#606683")).Padding(1).MarginBottom(1).MarginTop(1).MarginLeft(1)
func main() {
params := Params{}
flag.StringVar(¶ms.typeP, "t", "http", "type connection (ws, gq, http, grpc)")
flag.StringVar(¶ms.url, "u", "", "url to connect")
flag.StringVar(¶ms.message, "p", "", "data send to server")
flag.StringVar(¶ms.query, "q", "", "query params")
flag.BoolVar(¶ms.verbose, "v", false, "show response server headers")
flag.StringVar(¶ms.header, "h", "", "header params")
flag.StringVar(¶ms.method, "m", "GET", "method request")
flag.StringVar(¶ms.file, "f", "", "file path")
flag.StringVar(¶ms.userAgent, "a", "", "user agent header")
flag.IntVar(¶ms.timeout, "timeout", 0, "timeout in seconds")
// GRPC exclusive flags
flag.StringVar(¶ms.importPath, "import-path", "", "The path to a directory from which proto sources can be imported, for use with -proto flags")
flag.StringVar(¶ms.proto, "proto", "", "The proto file")
flag.StringVar(¶ms.methodName, "method", "", "The service method to call")
download := flag.Bool("d", false, "download content")
flag.Parse()
if params.url == "" {
if len(os.Args) > 1 {
params.url = os.Args[len(os.Args)-1]
}
}
if len(params.message) > 0 {
atSymbol := 64
if params.message[0] == byte(atSymbol) {
filePath := string(params.message[1:])
content, err := os.ReadFile(filePath)
if err != nil {
fmt.Println(styleErr.Render("Error reading file: " + err.Error()))
return
}
params.message = string(content)
}
}
validProtocos := []string{"ws", "gq", "http", "grpc"}
valid := false
for _, v := range validProtocos {
if params.typeP == v {
valid = true
break
}
}
if !valid {
fmt.Println(styleErr.Render("Invalid protocol, valid are: http, ws, gq"))
return
}
p := getProtocol(params)
defer p.Close()
if params.message == "" {
p.OnMessageReceived()
}
resp, err := p.RequestResponse()
if err != nil {
fmt.Println(styleErr.Render(err.Error()))
return
}
if *download {
err := p.Download()
if err != nil {
fmt.Println(styleErr.Render(err.Error()))
}
}
myRender, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(140),
)
if err != nil {
fmt.Println(styleErr.Render(err.Error()))
return
}
r, err := PrettyJSON(resp)
if err != nil {
dr, err := myRender.Render("```html\n" + resp + "\n```\n")
if err != nil {
fmt.Println(styleErr.Render(err.Error()))
return
}
fmt.Print(dr)
if params.verbose {
p.PrintHeaderResponse()
}
return
}
out, err := myRender.Render(r)
if err != nil {
fmt.Println(styleErr.Render(err.Error()))
os.Exit(0)
}
fmt.Print(out)
if params.verbose {
p.PrintHeaderResponse()
}
}
func PrettyJSON(str string) (string, error) {
var pretty bytes.Buffer
if err := json.Indent(&pretty, []byte(str), "", " "); err != nil {
return "", err
}
resp := "```json\n" + pretty.String() + "\n```\n"
return resp, nil
}