forked from pwaller/wscat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (137 loc) · 2.89 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
package main
import (
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"regexp"
"github.com/urfave/cli"
"github.com/gorilla/websocket"
)
func main() {
app := cli.NewApp()
app.Name = "wscat"
app.Usage = "cat, but for websockets"
app.Action = ActionMain
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "origin",
Value: "samehost",
Usage: "URL to use for the origin header ('samehost' is special)",
EnvVar: "WSCAT_ORIGIN",
},
cli.StringSliceFlag{
Name: "header, H",
Usage: "headers to pass to the remote",
Value: &cli.StringSlice{},
EnvVar: "WSCAT_HEADER",
},
}
app.Run(os.Args)
}
var RegexParseHeader = regexp.MustCompile("^\\s*([^\\:]+)\\s*:\\s*(.*)$")
func MustParseHeader(header string) (string, string) {
if !RegexParseHeader.MatchString(header) {
log.Fatalf("Unable to parse header: %v (re: %v)", header,
RegexParseHeader.String())
return "", ""
}
parts := RegexParseHeader.FindStringSubmatch(header)
return parts[1], parts[2]
}
func MustParseHeaders(c *cli.Context) http.Header {
headers := http.Header{}
for _, h := range c.StringSlice("header") {
key, value := MustParseHeader(h)
headers.Set(key, value)
}
return headers
}
func MustParseURL(u string) *url.URL {
tgt, err := url.ParseRequestURI(u)
if err != nil {
log.Fatalf("Unable to parse URL: %v: %v", u, err)
}
switch tgt.Scheme {
case "http":
tgt.Scheme = "ws"
case "https":
tgt.Scheme = "wss"
}
return tgt
}
func ActionMain(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
log.Fatalf("usage: wscat <url>")
}
urlString := args.First()
u := MustParseURL(urlString)
headers := MustParseHeaders(c)
origin := c.String("origin")
if origin == "samehost" {
origin = "//" + u.Host
}
headers.Set("Origin", origin)
if u.User != nil {
userPassBytes := []byte(u.User.String() + ":")
token := base64.StdEncoding.EncodeToString(userPassBytes)
headers.Set("Authorization", fmt.Sprintf("Basic %v", token))
u.User = nil
}
conn, resp, err := websocket.DefaultDialer.Dial(u.String(), headers)
if err != nil {
if resp != nil {
err = fmt.Errorf("%v: response: %v", err, resp.Status)
}
log.Fatalf("Error dialing: %v", err)
}
defer conn.Close()
errc := make(chan error)
go func() {
// _, err := io.Copy(os.Stdout, conn)
var (
err error
r io.Reader
)
for {
_, r, err = conn.NextReader()
if err != nil {
break
}
_, err = io.Copy(os.Stdout, r)
if err != nil {
break
}
}
if err != io.EOF {
log.Printf("Error copying to stdout: %v", err)
}
errc <- err
}()
go func() {
var (
err error
w io.Writer
)
for {
w, err = conn.NextWriter(websocket.BinaryMessage)
if err != nil {
break
}
_, err = io.Copy(w, os.Stdin)
if err != nil {
break
}
break
}
if err != nil && err != io.EOF {
log.Printf("Error copying from stdin: %v", err)
}
errc <- err
}()
<-errc
}