2017-02-12 04:13:29 +00:00
|
|
|
package connection
|
2017-02-02 03:12:13 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-03 03:28:25 +00:00
|
|
|
"encoding/hex"
|
2017-02-15 23:53:34 +00:00
|
|
|
"time"
|
2017-02-02 03:12:13 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection track websocket and faciliates in and out data
|
|
|
|
type Connection struct {
|
2017-02-16 02:06:26 +00:00
|
|
|
// The main connection table (should be just one of these created at startup)
|
2017-02-12 04:13:29 +00:00
|
|
|
connectionTable *Table
|
2017-02-02 03:12:13 +00:00
|
|
|
|
2017-02-16 02:06:26 +00:00
|
|
|
//used to track traffic for a domain. Not use for lookup or validation only for tracking
|
|
|
|
DomainTrack map[string]*DomainTrack
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
// The websocket connection.
|
|
|
|
conn *websocket.Conn
|
|
|
|
|
|
|
|
// Buffered channel of outbound messages.
|
2017-02-16 02:06:26 +00:00
|
|
|
send chan *SendTrack
|
2017-02-02 03:12:13 +00:00
|
|
|
|
|
|
|
// Address of the Remote End Point
|
|
|
|
source string
|
|
|
|
|
|
|
|
// bytes in
|
|
|
|
bytesIn int64
|
|
|
|
|
|
|
|
// bytes out
|
|
|
|
bytesOut int64
|
2017-02-12 20:39:50 +00:00
|
|
|
|
|
|
|
// communications channel between go routines
|
|
|
|
commCh chan bool
|
|
|
|
|
2017-02-15 23:53:34 +00:00
|
|
|
// Connect Time
|
|
|
|
connectTime time.Time
|
|
|
|
|
2017-02-12 20:39:50 +00:00
|
|
|
//initialDomains - a list of domains from the JWT
|
|
|
|
initialDomains []interface{}
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//NewConnection -- Constructor
|
2017-02-12 20:39:50 +00:00
|
|
|
func NewConnection(connectionTable *Table, conn *websocket.Conn, remoteAddress string, initialDomains []interface{}) (p *Connection) {
|
2017-02-12 04:13:29 +00:00
|
|
|
p = new(Connection)
|
|
|
|
p.connectionTable = connectionTable
|
|
|
|
p.conn = conn
|
|
|
|
p.source = remoteAddress
|
|
|
|
p.bytesIn = 0
|
|
|
|
p.bytesOut = 0
|
2017-02-16 02:06:26 +00:00
|
|
|
p.send = make(chan *SendTrack)
|
2017-02-12 20:39:50 +00:00
|
|
|
p.commCh = make(chan bool)
|
2017-02-15 23:53:34 +00:00
|
|
|
p.connectTime = time.Now()
|
2017-02-12 20:39:50 +00:00
|
|
|
p.initialDomains = initialDomains
|
2017-02-16 02:06:26 +00:00
|
|
|
p.DomainTrack = make(map[string]*DomainTrack)
|
|
|
|
|
|
|
|
for _, domain := range initialDomains {
|
|
|
|
p.AddTrackedDomain(string(domain.(string)))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddTrackedDomain -- Add a tracked domain
|
|
|
|
func (c *Connection) AddTrackedDomain(domain string) {
|
|
|
|
p := new(DomainTrack)
|
|
|
|
p.DomainName = domain
|
|
|
|
c.DomainTrack[domain] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
//InitialDomains -- Property
|
|
|
|
func (c *Connection) InitialDomains() (i []interface{}) {
|
|
|
|
i = c.initialDomains
|
2017-02-12 04:13:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-15 23:53:34 +00:00
|
|
|
//ConnectTime -- Property
|
|
|
|
func (c *Connection) ConnectTime() (t time.Time) {
|
|
|
|
t = c.connectTime
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//BytesIn -- Property
|
|
|
|
func (c *Connection) BytesIn() (b int64) {
|
|
|
|
b = c.bytesIn
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//BytesOut -- Property
|
|
|
|
func (c *Connection) BytesOut() (b int64) {
|
|
|
|
b = c.bytesOut
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-14 02:36:01 +00:00
|
|
|
//SendCh -- property to sending channel
|
2017-02-16 02:06:26 +00:00
|
|
|
func (c *Connection) SendCh() chan *SendTrack {
|
2017-02-14 02:36:01 +00:00
|
|
|
return c.send
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
func (c *Connection) addIn(num int64) {
|
|
|
|
c.bytesIn = c.bytesIn + num
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) addOut(num int64) {
|
|
|
|
c.bytesOut = c.bytesOut + num
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//ConnectionTable -- property
|
|
|
|
func (c *Connection) ConnectionTable() (table *Table) {
|
|
|
|
table = c.connectionTable
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:39:50 +00:00
|
|
|
//CommCh -- Property
|
|
|
|
func (c *Connection) CommCh() chan bool {
|
|
|
|
return c.commCh
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//Reader -- export the reader function
|
|
|
|
func (c *Connection) Reader() {
|
2017-02-02 03:12:13 +00:00
|
|
|
defer func() {
|
|
|
|
c.connectionTable.unregister <- c
|
|
|
|
c.conn.Close()
|
|
|
|
}()
|
2017-02-12 04:13:29 +00:00
|
|
|
c.conn.SetReadLimit(1024)
|
2017-02-02 03:12:13 +00:00
|
|
|
for {
|
|
|
|
_, message, err := c.conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
2017-02-06 03:19:04 +00:00
|
|
|
loginfo.Printf("error: %v", err)
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2017-02-03 03:28:25 +00:00
|
|
|
loginfo.Println(hex.Dump(message))
|
2017-02-02 03:12:13 +00:00
|
|
|
c.addIn(int64(len(message)))
|
|
|
|
loginfo.Println(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//Writer -- expoer the writer function
|
|
|
|
func (c *Connection) Writer() {
|
2017-02-02 03:12:13 +00:00
|
|
|
defer func() {
|
|
|
|
c.conn.Close()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case message := <-c.send:
|
|
|
|
w, err := c.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-02-16 02:06:26 +00:00
|
|
|
w.Write(message.data)
|
2017-02-02 03:12:13 +00:00
|
|
|
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:06:26 +00:00
|
|
|
messageLen := int64(len(message.data))
|
|
|
|
|
|
|
|
c.addOut(messageLen)
|
|
|
|
|
|
|
|
//Support for tracking outbound traffic based on domain.
|
|
|
|
if domainTrack, ok := c.DomainTrack[message.domain]; ok {
|
|
|
|
//if ok then add to structure, else warn there is something wrong
|
|
|
|
domainTrack.AddOut(messageLen)
|
|
|
|
loginfo.Println("adding ", messageLen, " to ", message.domain)
|
|
|
|
} else {
|
|
|
|
logdebug.Println("attempting to add bytes to ", message.domain, "it does not exist")
|
|
|
|
logdebug.Println(c.DomainTrack)
|
|
|
|
}
|
2017-02-14 02:53:27 +00:00
|
|
|
loginfo.Println(c)
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|