This repository was archived by the owner on Mar 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (119 loc) · 3.27 KB
/
Copy pathmain.go
File metadata and controls
132 lines (119 loc) · 3.27 KB
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
package main
import (
"flag"
"fmt"
"io"
"net"
"net/http"
"net/url"
"crypto/tls"
"os"
"strings"
"log"
"golang.org/x/net/websocket"
)
var (
origin string
headers string
version int
resolve_as string
)
func init() {
help = fmt.Sprintf(help, VERSION)
flag.StringVar(&origin, "o", "http://0.0.0.0/", "websocket origin")
flag.StringVar(&headers, "H", "", "a comma separated list of http headers")
flag.IntVar(&version, "v", websocket.ProtocolVersionHybi13, "websocket version")
flag.StringVar(&resolve_as, "r", "", "resolve the host as a specified ip, or ip:port")
flag.Parse()
log.SetFlags(log.LstdFlags|log.Lshortfile)
}
const VERSION = "0.1"
var help = `ws - %s
Usage:
ws [options] <url>
Use "ws --help" for help.
`
func parseHeaders(headers string) http.Header {
h := http.Header{}
for _, header := range strings.Split(headers, ",") {
parts := strings.SplitN(header, ":", 2)
if len(parts) != 2 {
continue
}
h.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
return h
}
func make_ip_port(u *url.URL) string{
if u==nil {return ""}
port := "80"
if u.Scheme == "wss" {
port = "443"
}
if _,_,err := net.SplitHostPort(u.Host); err!=nil { // not found a port
return net.JoinHostPort(u.Host,port)
}
return u.Host
}
func main() {
var (
client net.Conn
ws *websocket.Conn
err error
)
target := flag.Arg(0)
if target == "" {
log.Fatal(help)
}
config, err := websocket.NewConfig(target, origin)
if err != nil {
log.Fatal("%s\n", err)
}
if headers != "" {
config.Header = parseHeaders(headers)
}
config.Version = version
// i dont use the default DialConfig, or Dial, in order to control more and fetch more infomation
switch config.Location.Scheme {
case "ws":
if resolve_as == "" {
client,err = net.Dial("tcp", make_ip_port(config.Location) )
} else {
u,err := url.Parse(fmt.Sprintf("ws://%s",resolve_as))
if err != nil {
log.Fatal(err)
}
client,err = net.Dial("tcp",make_ip_port(u))
}
case "wss":
config.TlsConfig = &tls.Config{ServerName: config.Location.Host} // override by myself
if resolve_as == "" {
client,err = tls.Dial("tcp",make_ip_port(config.Location), config.TlsConfig)
} else {
u,err := url.Parse(fmt.Sprintf("wss://%s",resolve_as))
if err != nil {
log.Fatal(err)
}
client,err = tls.Dial("tcp",make_ip_port(u), config.TlsConfig)
}
default:
log.Fatal("invalid scheme. should be ws:// or wss://\n")
}
if err != nil {
log.Fatal(err)
}
ws, err = websocket.NewClient(config, client)
if err!= nil {
client.Close()
log.Fatal("Error dialing %s: %v\n", target, err)
}
fmt.Printf("Connected to %v\n", client.RemoteAddr() )
errc := make(chan error, 2)
cp := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src)
errc <- err
}
go cp(os.Stdout, ws)
go cp(ws, os.Stdin)
<-errc
}