-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
155 lines (126 loc) · 3.27 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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"os"
"sync"
"github.com/fatih/color"
"golang.org/x/net/websocket"
)
// Version is the current version.
const Version = "0.1.0"
var (
origin string
url string
protocol string
displayHelp bool
displayVersion bool
insecureSkipVerify bool
red = color.New(color.FgRed).SprintFunc()
magenta = color.New(color.FgMagenta).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
cyan = color.New(color.FgCyan).SprintFunc()
wg sync.WaitGroup
)
func init() {
flag.StringVar(&origin, "origin", "http://localhost/", "origin of WebSocket client")
flag.StringVar(&url, "url", "ws://localhost:1337/ws", "WebSocket server address to connect to")
flag.StringVar(&protocol, "protocol", "", "WebSocket subprotocol")
flag.BoolVar(&insecureSkipVerify, "insecureSkipVerify", false, "Skip TLS certificate verification")
flag.BoolVar(&displayHelp, "help", false, "Display help information about wsd")
flag.BoolVar(&displayVersion, "version", false, "Display version number")
}
func inLoop(ws *websocket.Conn, errors chan<- error, in chan<- []byte) {
var msg = make([]byte, 512)
for {
var n int
var err error
n, err = ws.Read(msg)
if err != nil {
errors <- err
continue
}
in <- msg[:n]
}
}
func printErrors(errors <-chan error) {
for err := range errors {
if err == io.EOF {
fmt.Printf("\r✝ %v - connection closed by remote\n", magenta(err))
os.Exit(0)
} else {
fmt.Printf("\rerr %v\n> ", red(err))
}
}
}
func printReceivedMessages(in <-chan []byte) {
for msg := range in {
fmt.Printf("\r< %s\n> ", cyan(string(msg)))
}
}
func outLoop(ws *websocket.Conn, out <-chan []byte, errors chan<- error) {
for msg := range out {
_, err := ws.Write(msg)
if err != nil {
errors <- err
}
}
}
func dial(url, protocol, origin string) (ws *websocket.Conn, err error) {
config, err := websocket.NewConfig(url, origin)
if err != nil {
return nil, err
}
if protocol != "" {
config.Protocol = []string{protocol}
}
config.TlsConfig = &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
}
return websocket.DialConfig(config)
}
func main() {
flag.Parse()
if displayVersion {
fmt.Fprintf(os.Stdout, "%s version %s\n", os.Args[0], Version)
os.Exit(0)
}
if displayHelp {
fmt.Fprintf(os.Stdout, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
ws, err := dial(url, protocol, origin)
if protocol != "" {
fmt.Printf("connecting to %s via %s from %s...\n", yellow(url), yellow(protocol), yellow(origin))
} else {
fmt.Printf("connecting to %s from %s...\n", yellow(url), yellow(origin))
}
defer ws.Close()
if err != nil {
panic(err)
}
fmt.Printf("successfully connected to %s\n\n", green(url))
wg.Add(3)
errors := make(chan error)
in := make(chan []byte)
out := make(chan []byte)
defer close(errors)
defer close(out)
defer close(in)
go inLoop(ws, errors, in)
go printReceivedMessages(in)
go printErrors(errors)
go outLoop(ws, out, errors)
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("> ")
for scanner.Scan() {
out <- []byte(scanner.Text())
fmt.Print("> ")
}
wg.Wait()
}