-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
executable file
·48 lines (42 loc) · 1.2 KB
/
local.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
package websocket
import (
"net/url"
"github.com/gorilla/websocket"
"github.com/yinqiwen/gsnova/common/channel"
"github.com/yinqiwen/gsnova/common/logger"
"github.com/yinqiwen/gsnova/common/mux"
"github.com/yinqiwen/pmux"
)
type WebsocketProxy struct {
}
func (p *WebsocketProxy) Features() channel.FeatureSet {
return channel.FeatureSet{
AutoExpire: true,
Pingable: true,
}
}
func (ws *WebsocketProxy) CreateMuxSession(server string, conf *channel.ProxyChannelConfig) (mux.MuxSession, error) {
u, err := url.Parse(server)
if nil != err {
return nil, err
}
u.Path = "/ws"
wsDialer := &websocket.Dialer{}
wsDialer.NetDial = channel.NewDialByConf(conf, u.Scheme)
wsDialer.TLSClientConfig = channel.NewTLSConfig(conf)
c, _, err := wsDialer.Dial(u.String(), nil)
if err != nil {
logger.Notice("dial websocket error:%v", err)
return nil, err
}
logger.Debug("Connect %s success.", server)
ps, err := pmux.Client(&mux.WsConn{Conn: c}, channel.InitialPMuxConfig(&conf.Cipher))
if nil != err {
return nil, err
}
return &mux.ProxyMuxSession{Session: ps}, nil
}
func init() {
channel.RegisterLocalChannelType("ws", &WebsocketProxy{})
channel.RegisterLocalChannelType("wss", &WebsocketProxy{})
}