-
Notifications
You must be signed in to change notification settings - Fork 19
/
websocket.go
130 lines (114 loc) · 3.41 KB
/
websocket.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
// Copyright (C) 2020-2021, IrineSistiana
//
// This file is part of simple-tls.
//
// simple-tls is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// simple-tls is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package core
import (
"context"
"crypto/tls"
"errors"
"fmt"
"golang.org/x/net/http2"
"net"
"net/http"
"net/url"
"nhooyr.io/websocket"
"time"
)
type WebsocketTransport struct {
u string
op *websocket.DialOptions
}
func NewWebsocketTransport(serverAddr, serverName, urlPath string, tlsConfig *tls.Config, dialer *net.Dialer) *WebsocketTransport {
u := url.URL{
Scheme: "https",
Host: serverName,
Path: urlPath,
}
t := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, serverAddr)
},
TLSClientConfig: tlsConfig,
TLSHandshakeTimeout: time.Second * 5,
DisableCompression: true,
ResponseHeaderTimeout: time.Second * 5,
ExpectContinueTimeout: 0,
WriteBufferSize: 32 * 1024,
ReadBufferSize: 32 * 1024,
ForceAttemptHTTP2: true,
}
t2, _ := http2.ConfigureTransports(t)
t2.ReadIdleTimeout = time.Second * 30
t2.PingTimeout = time.Second * 10
return &WebsocketTransport{
u: u.String(),
op: &websocket.DialOptions{
HTTPClient: &http.Client{
Transport: t,
Timeout: time.Second * 10,
},
CompressionMode: websocket.CompressionDisabled,
},
}
}
func (p *WebsocketTransport) Dial(ctx context.Context) (net.Conn, error) {
wsConn, _, err := websocket.Dial(ctx, p.u, p.op)
if err != nil {
return nil, err
}
return websocket.NetConn(context.Background(), wsConn, websocket.MessageBinary), nil
}
type wsHttpHandler struct {
nextHandler TransportHandler
path string
}
var errInvalidPath = errors.New("invalid request path")
func (h *wsHttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if len(h.path) != 0 && r.URL.Path != h.path {
w.WriteHeader(http.StatusNotFound)
logRequestErr(r, errInvalidPath)
return
}
wsConn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
CompressionMode: websocket.CompressionDisabled,
})
if err != nil {
logRequestErr(r, fmt.Errorf("cannot accept websocket connection: %w", err))
return
}
clientConn := websocket.NetConn(context.Background(), wsConn, websocket.MessageBinary)
defer clientConn.Close()
if err := h.nextHandler.Handle(clientConn); err != nil {
logRequestErr(r, err)
return
}
return
}
func ListenWebsocket(l net.Listener, path string, nextHandler TransportHandler) error {
httpServer := &http.Server{
Handler: &wsHttpHandler{
nextHandler: nextHandler,
path: path,
},
ReadTimeout: time.Second * 10,
ReadHeaderTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
http2.ConfigureServer(httpServer, &http2.Server{
IdleTimeout: time.Second * 45,
})
return httpServer.Serve(l)
}