-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathproxy.go
164 lines (153 loc) · 4.07 KB
/
proxy.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
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You under the Apache License, Version 2.0
* * (the "License"); you may not use this file except in compliance with
* * the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package websocketproxy
import (
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
)
const (
WsScheme = "ws"
WssScheme = "wss"
BufSize = 1024 * 32
)
var ErrFormatAddr = errors.New("remote websockets addr format error")
type WebsocketProxy struct {
// ws, wss
scheme string
// The target address: host:port
remoteAddr string
// path
defaultPath string
tlsc *tls.Config
logger *log.Logger
// Send handshake before callback
beforeHandshake func(r *http.Request) error
}
type Options func(wp *WebsocketProxy)
// You must carry a port number,ws://ip:80/ssss, wss://ip:443/aaaa
// ex: ws://ip:port/ajaxchattest
func NewProxy(addr string, beforeCallback func(r *http.Request) error, options ...Options) (*WebsocketProxy, error) {
u, err := url.Parse(addr)
if err != nil {
return nil, ErrFormatAddr
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
return nil, ErrFormatAddr
}
if u.Scheme != WsScheme && u.Scheme != WssScheme {
return nil, ErrFormatAddr
}
wp := &WebsocketProxy{
scheme: u.Scheme,
remoteAddr: fmt.Sprintf("%s:%s", host, port),
defaultPath: u.Path,
beforeHandshake: beforeCallback,
logger: log.New(os.Stderr, "", log.LstdFlags),
}
if u.Scheme == WssScheme {
wp.tlsc = &tls.Config{InsecureSkipVerify: true}
}
for op := range options {
options[op](wp)
}
return wp, nil
}
func (wp *WebsocketProxy) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
wp.Proxy(writer, request)
}
func (wp *WebsocketProxy) Proxy(writer http.ResponseWriter, request *http.Request) {
if strings.ToLower(request.Header.Get("Connection")) != "upgrade" ||
strings.ToLower(request.Header.Get("Upgrade")) != "websocket" {
_, _ = writer.Write([]byte(`Must be a websocket request`))
return
}
hijacker, ok := writer.(http.Hijacker)
if !ok {
return
}
conn, _, err := hijacker.Hijack()
if err != nil {
return
}
defer conn.Close()
req := request.Clone(request.Context())
req.URL.Path, req.URL.RawPath, req.RequestURI = wp.defaultPath, wp.defaultPath, wp.defaultPath
req.Host = wp.remoteAddr
if wp.beforeHandshake != nil {
// Add headers, permission authentication + masquerade sources
err = wp.beforeHandshake(req)
if err != nil {
_, _ = writer.Write([]byte(err.Error()))
return
}
}
var remoteConn net.Conn
switch wp.scheme {
case WsScheme:
remoteConn, err = net.Dial("tcp", wp.remoteAddr)
case WssScheme:
remoteConn, err = tls.Dial("tcp", wp.remoteAddr, wp.tlsc)
}
if err != nil {
_, _ = writer.Write([]byte(err.Error()))
return
}
defer remoteConn.Close()
err = req.Write(remoteConn)
if err != nil {
wp.logger.Println("remote write err:", err)
return
}
errChan := make(chan error, 2)
copyConn := func(a, b net.Conn) {
buf := ByteSliceGet(BufSize)
defer ByteSlicePut(buf)
_, err := io.CopyBuffer(a, b, buf)
errChan <- err
}
go copyConn(conn, remoteConn) // response
go copyConn(remoteConn, conn) // request
select {
case err = <-errChan:
if err != nil {
log.Println(err)
}
}
}
func SetTLSConfig(tlsc *tls.Config) Options {
return func(wp *WebsocketProxy) {
wp.tlsc = tlsc
}
}
func SetLogger(l *log.Logger) Options {
return func(wp *WebsocketProxy) {
if l != nil {
wp.logger = l
}
}
}