forked from Gavinpub/ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (107 loc) · 3.04 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
package main
import (
"fmt"
"net/url"
"os"
"os/user"
"path/filepath"
"regexp"
"time"
"github.com/chzyer/readline"
"github.com/spf13/cobra"
)
var (
version = "local build"
options struct {
origin string
printVersion bool
insecure bool
subProtocals string
initMsg string
authHeader string
timestamp bool
binAsText bool
pingPong bool
compression bool
pingInterval time.Duration
filter *regexp.Regexp
}
filter string
)
func main() {
rootCmd := &cobra.Command{
Use: "ws URL",
Short: fmt.Sprintf("ws is a websocket client v.%s", version),
Run: root,
}
rootCmd.Flags().StringVarP(&options.origin, "origin", "o", "", "websocket origin (default value is formed from URL)")
rootCmd.Flags().BoolVarP(&options.printVersion, "version", "v", false, "print version")
rootCmd.Flags().BoolVarP(&options.insecure, "insecure", "k", false, "skip ssl certificate check")
rootCmd.Flags().StringVarP(&options.subProtocals, "subprotocal", "s", "", "sec-websocket-protocal field")
rootCmd.Flags().StringVarP(&options.authHeader, "auth", "a", "", "auth header value, like 'Bearer $TOKEN'")
rootCmd.Flags().BoolVarP(&options.timestamp, "timestamp", "t", false, "print timestamps for sent and received messages")
rootCmd.Flags().BoolVarP(&options.binAsText, "bin2text", "b", false, "print binary message as text")
rootCmd.Flags().BoolVarP(&options.pingPong, "pingPong", "p", false, "print out ping/pong messages")
rootCmd.Flags().DurationVarP(&options.pingInterval, "interval", "i", 0, "send ping each interval (ex: 20s)")
rootCmd.Flags().StringVarP(&options.initMsg, "init", "m", "", "connection init message")
rootCmd.Flags().BoolVarP(&options.compression, "compression", "c", false, "enable compression")
rootCmd.Flags().StringVarP(&filter, "filter", "f", "", "only messages that match regexp will be printed")
rootCmd.Execute()
}
func root(cmd *cobra.Command, args []string) {
if options.printVersion {
fmt.Printf("ws v.%s\n", version)
os.Exit(0)
}
if len(args) != 1 {
cmd.Help()
os.Exit(1)
}
dest, err := url.Parse(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if options.origin == "" {
originURL := *dest
switch dest.Scheme {
case "wss":
originURL.Scheme = "https"
case "ws":
originURL.Scheme = "http"
default:
fmt.Printf("unsupported scheme: %s\n", dest.Scheme)
os.Exit(1)
}
options.origin = originURL.String()
}
if len(filter) > 0 {
options.filter, err = regexp.Compile(filter)
if err != nil {
fmt.Fprintf(os.Stderr, "compiling regexp '%s' error: %v", filter, err)
os.Exit(1)
}
}
var historyFile string
user, err := user.Current()
if err == nil {
historyFile = filepath.Join(user.HomeDir, ".ws_history")
}
rl, err := readline.NewEx(&readline.Config{
Prompt: "> ",
HistoryFile: historyFile,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
s := &Session{rl: rl}
errs := s.connect(dest.String())
if len(errs) > 0 {
fmt.Println()
for _, err := range errs {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}
}