Skip to content

Commit

Permalink
Switching to use my own version of certstream-go to catch panic cause…
Browse files Browse the repository at this point in the history
… by jsonq lib
  • Loading branch information
santrancisco committed Sep 10, 2018
1 parent 56a2a27 commit bf224ec
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
67 changes: 67 additions & 0 deletions certstream-go/certstream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package certstream

import (
"log"
"time"

"github.com/gorilla/websocket"
"github.com/jmoiron/jsonq"
"github.com/pkg/errors"
)

func NewQueryCatchPanic(v interface{}) *jsonq.JsonQuery {
defer func() {
if r := recover(); r != nil {
log.Println("[ERROR] Recovered from ", r)
}
}()
jq := jsonq.NewQuery(v)
return jq
}

func CertStreamEventStream(skipHeartbeats bool) (chan jsonq.JsonQuery, chan error) {
outputStream := make(chan jsonq.JsonQuery)
errStream := make(chan error)

go func() {
MAINLOOP:
for {
c, _, err := websocket.DefaultDialer.Dial("wss://certstream.calidog.io", nil)

if err != nil {
errStream <- errors.Wrap(err, "Error connecting to certstream! Sleeping a few seconds and reconnecting... ")
time.Sleep(5 * time.Second)
continue
}

defer c.Close()
defer close(outputStream)

for {
var v interface{}
err = c.ReadJSON(&v)
if err != nil {
errStream <- errors.Wrap(err, "Error decoding json frame!")
}
var jq *jsonq.JsonQuery
jq = NewQueryCatchPanic(v)
if jq == nil {
continue MAINLOOP
}

res, err := jq.String("message_type")
if err != nil {
errStream <- errors.Wrap(err, "Error creating jq object!")
}

if skipHeartbeats && res == "heartbeat" {
continue
}

outputStream <- *jq
}
}
}()

return outputStream, errStream
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"syscall"

"github.com/CaliDog/certstream-go"
"github.com/santrancisco/certstream_screenshot/certstream-go"
"github.com/santrancisco/certstream_screenshot/screenshotworker"
"github.com/santrancisco/logutils"
)
Expand Down

0 comments on commit bf224ec

Please sign in to comment.