Skip to content

Commit 5944d85

Browse files
brcodinglonng
authored andcommitted
Added TLS / secure cert for Websockets (#42)
* Added TLS / secure cert for Websockets * Added exclusion of current id to group broadcast. * bugfix for listen interface * Revert broadcastExcludeId, change not needed.
1 parent f6bcf0f commit 5944d85

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

app.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ import (
3737
var running int32
3838

3939
func listen(addr string, isWs bool, opts ...Option) {
40+
listenTLS(addr, isWs, "", "", opts...);
41+
}
42+
43+
func listenTLS(addr string, isWs bool, certificate string, key string, opts ...Option) {
4044
// mark application running
4145
if atomic.AddInt32(&running, 1) != 1 {
4246
logger.Println("Nano has running")
@@ -62,7 +66,11 @@ func listen(addr string, isWs bool, opts ...Option) {
6266

6367
go func() {
6468
if isWs {
65-
listenAndServeWS(addr)
69+
if len(certificate) != 0 {
70+
listenAndServeWSTLS(addr, certificate, key)
71+
} else {
72+
listenAndServeWS(addr)
73+
}
6674
} else {
6775
listenAndServe(addr)
6876
}
@@ -128,3 +136,25 @@ func listenAndServeWS(addr string) {
128136
logger.Fatal(err.Error())
129137
}
130138
}
139+
140+
func listenAndServeWSTLS(addr string, certificate string, key string) {
141+
var upgrader = websocket.Upgrader{
142+
ReadBufferSize: 1024,
143+
WriteBufferSize: 1024,
144+
CheckOrigin: env.checkOrigin,
145+
}
146+
147+
http.HandleFunc("/"+strings.TrimPrefix(env.wsPath, "/"), func(w http.ResponseWriter, r *http.Request) {
148+
conn, err := upgrader.Upgrade(w, r, nil)
149+
if err != nil {
150+
logger.Println(fmt.Sprintf("Upgrade failure, URI=%s, Error=%s", r.RequestURI, err.Error()))
151+
return
152+
}
153+
154+
handler.handleWS(conn)
155+
})
156+
157+
if err := http.ListenAndServeTLS(addr, certificate, key, nil); err != nil {
158+
logger.Fatal(err.Error())
159+
}
160+
}

group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,4 @@ func (c *Group) Close() error {
224224
// release all reference
225225
c.sessions = make(map[int64]*session.Session)
226226
return nil
227-
}
227+
}

interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ func ListenWS(addr string, opts ...Option) {
4242
listen(addr, true, opts...)
4343
}
4444

45+
// ListenWS listens on the TCP network address addr
46+
// and then upgrades the HTTP server connection to the WebSocket protocol
47+
// to handle requests on incoming connections.
48+
func ListenWSTLS(addr string, certificate string, key string, opts ...Option) {
49+
listenTLS(addr, true, certificate, key, opts...)
50+
}
51+
4552
// Register register a component with options
4653
func Register(c component.Component, options ...component.Option) {
4754
comps = append(comps, regComp{c, options})

0 commit comments

Comments
 (0)