2017-03-03 00:47:59 +00:00
|
|
|
package genericlistener
|
2017-02-02 03:12:13 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-03 00:47:59 +00:00
|
|
|
"context"
|
2017-03-22 21:43:36 +00:00
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-03-03 03:32:53 +00:00
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2017-03-22 21:43:36 +00:00
|
|
|
|
|
|
|
"git.daplie.com/Daplie/go-rvpn-server/rvpn/packer"
|
2017-02-02 03:12:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
2017-03-02 03:02:20 +00:00
|
|
|
ReadBufferSize: 4096,
|
|
|
|
WriteBufferSize: 4096,
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connection track websocket and faciliates in and out data
|
|
|
|
type Connection struct {
|
2017-03-22 22:47:53 +00:00
|
|
|
mutex sync.Mutex
|
2017-02-19 20:05:06 +00:00
|
|
|
|
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
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
// WssState channel
|
|
|
|
// Must check state via channel before xmit
|
|
|
|
|
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
|
|
|
|
2017-03-20 00:04:47 +00:00
|
|
|
// requests
|
|
|
|
requests int64
|
|
|
|
|
|
|
|
// response
|
|
|
|
responses int64
|
|
|
|
|
2017-02-15 23:53:34 +00:00
|
|
|
// Connect Time
|
|
|
|
connectTime time.Time
|
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
//lastUpdate
|
|
|
|
lastUpdate time.Time
|
|
|
|
|
2017-02-12 20:39:50 +00:00
|
|
|
//initialDomains - a list of domains from the JWT
|
|
|
|
initialDomains []interface{}
|
2017-02-19 20:05:06 +00:00
|
|
|
|
2017-03-03 00:47:59 +00:00
|
|
|
connectionTrack *Tracking
|
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
///wssState tracks a highlevel status of the connection, false means do nothing.
|
|
|
|
wssState bool
|
2017-03-13 21:46:11 +00:00
|
|
|
|
|
|
|
//connectionID
|
|
|
|
connectionID int64
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//NewConnection -- Constructor
|
2017-03-03 00:47:59 +00:00
|
|
|
func NewConnection(connectionTable *Table, conn *websocket.Conn, remoteAddress string, initialDomains []interface{}, connectionTrack *Tracking) (p *Connection) {
|
2017-03-13 21:46:11 +00:00
|
|
|
connectionID = connectionID + 1
|
|
|
|
|
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-03-20 00:04:47 +00:00
|
|
|
p.requests = 0
|
|
|
|
p.responses = 0
|
2017-02-16 02:06:26 +00:00
|
|
|
p.send = make(chan *SendTrack)
|
2017-02-15 23:53:34 +00:00
|
|
|
p.connectTime = time.Now()
|
2017-02-12 20:39:50 +00:00
|
|
|
p.initialDomains = initialDomains
|
2017-03-03 00:47:59 +00:00
|
|
|
p.connectionTrack = connectionTrack
|
2017-02-16 02:06:26 +00:00
|
|
|
p.DomainTrack = make(map[string]*DomainTrack)
|
|
|
|
|
|
|
|
for _, domain := range initialDomains {
|
|
|
|
p.AddTrackedDomain(string(domain.(string)))
|
|
|
|
}
|
2017-02-19 20:05:06 +00:00
|
|
|
|
2017-03-22 23:45:47 +00:00
|
|
|
p.SetState(true)
|
2017-03-13 21:46:11 +00:00
|
|
|
p.connectionID = connectionID
|
2017-02-16 02:06:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddTrackedDomain -- Add a tracked domain
|
|
|
|
func (c *Connection) AddTrackedDomain(domain string) {
|
|
|
|
p := new(DomainTrack)
|
|
|
|
p.DomainName = domain
|
|
|
|
c.DomainTrack[domain] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
//InitialDomains -- Property
|
2017-03-22 22:33:09 +00:00
|
|
|
func (c *Connection) InitialDomains() []interface{} {
|
|
|
|
return c.initialDomains
|
2017-02-12 04:13:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 23:53:34 +00:00
|
|
|
//ConnectTime -- Property
|
2017-03-22 22:33:09 +00:00
|
|
|
func (c *Connection) ConnectTime() time.Time {
|
|
|
|
return c.connectTime
|
2017-02-15 23:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//BytesIn -- Property
|
2017-03-22 22:33:09 +00:00
|
|
|
func (c *Connection) BytesIn() int64 {
|
|
|
|
return c.bytesIn
|
2017-02-15 23:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//BytesOut -- Property
|
2017-03-22 22:33:09 +00:00
|
|
|
func (c *Connection) BytesOut() int64 {
|
|
|
|
return c.bytesOut
|
2017-02-15 23:53:34 +00:00
|
|
|
}
|
|
|
|
|
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-03-13 21:46:11 +00:00
|
|
|
//Source --
|
|
|
|
func (c *Connection) Source() string {
|
|
|
|
return c.source
|
|
|
|
}
|
|
|
|
|
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-03-20 00:04:47 +00:00
|
|
|
func (c *Connection) addRequests() {
|
|
|
|
c.requests = c.requests + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) addResponse() {
|
|
|
|
c.responses = c.responses + 1
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//ConnectionTable -- property
|
2017-03-22 22:33:09 +00:00
|
|
|
func (c *Connection) ConnectionTable() *Table {
|
|
|
|
return c.connectionTable
|
2017-02-12 04:13:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 23:45:47 +00:00
|
|
|
//State -- Get state of Socket...this is a high level state.
|
|
|
|
func (c *Connection) State() bool {
|
2017-02-19 20:05:06 +00:00
|
|
|
c.mutex.Lock()
|
2017-03-22 22:47:53 +00:00
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
return c.wssState
|
|
|
|
}
|
|
|
|
|
2017-03-22 23:45:47 +00:00
|
|
|
//SetState -- Set the set of the high level connection
|
|
|
|
func (c *Connection) SetState(state bool) {
|
2017-02-19 20:05:06 +00:00
|
|
|
c.mutex.Lock()
|
2017-03-22 22:47:53 +00:00
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
c.wssState = state
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update -- updates the lastUpdate property tracking idle time
|
|
|
|
func (c *Connection) Update() {
|
|
|
|
c.mutex.Lock()
|
2017-03-22 22:47:53 +00:00
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
c.lastUpdate = time.Now()
|
|
|
|
}
|
|
|
|
|
2017-03-18 19:28:54 +00:00
|
|
|
//LastUpdate -- retrieve last update
|
|
|
|
func (c *Connection) LastUpdate() time.Time {
|
|
|
|
return c.lastUpdate
|
|
|
|
}
|
|
|
|
|
2017-03-13 21:46:11 +00:00
|
|
|
//ConnectionID - Get
|
|
|
|
func (c *Connection) ConnectionID() int64 {
|
|
|
|
return c.connectionID
|
|
|
|
}
|
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
//NextWriter -- Wrapper to allow a high level state check before offering NextWriter
|
|
|
|
//The libary failes if client abends during write-cycle. a fast moving write is not caught before socket state bubbles up
|
|
|
|
//A synchronised state is maintained
|
2017-03-22 22:33:09 +00:00
|
|
|
func (c *Connection) NextWriter(wssMessageType int) (io.WriteCloser, error) {
|
2017-03-22 23:45:47 +00:00
|
|
|
if c.State() {
|
2017-03-22 22:33:09 +00:00
|
|
|
return c.conn.NextWriter(wssMessageType)
|
2017-02-19 20:05:06 +00:00
|
|
|
}
|
2017-03-22 22:33:09 +00:00
|
|
|
|
|
|
|
// Is returning a nil error actually the proper thing to do here?
|
|
|
|
loginfo.Println("NextWriter aborted, state is not true")
|
|
|
|
return nil, nil
|
2017-02-19 20:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Write -- Wrapper to allow a high level state check before allowing a write to the socket.
|
2017-03-22 22:33:09 +00:00
|
|
|
func (c *Connection) Write(w io.WriteCloser, message []byte) (int, error) {
|
2017-03-22 23:45:47 +00:00
|
|
|
if c.State() {
|
2017-03-22 22:33:09 +00:00
|
|
|
return w.Write(message)
|
2017-02-19 20:05:06 +00:00
|
|
|
}
|
2017-03-22 22:33:09 +00:00
|
|
|
|
|
|
|
// Is returning a nil error actually the proper thing to do here?
|
|
|
|
return 0, nil
|
2017-02-19 20:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//Reader -- export the reader function
|
2017-03-03 00:47:59 +00:00
|
|
|
func (c *Connection) Reader(ctx context.Context) {
|
|
|
|
connectionTrack := c.connectionTrack
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
defer func() {
|
|
|
|
c.connectionTable.unregister <- c
|
|
|
|
c.conn.Close()
|
2017-02-19 20:05:06 +00:00
|
|
|
loginfo.Println("reader defer", c)
|
2017-02-02 03:12:13 +00:00
|
|
|
}()
|
2017-02-19 20:05:06 +00:00
|
|
|
|
|
|
|
loginfo.Println("Reader Start ", c)
|
|
|
|
|
2017-03-24 22:45:54 +00:00
|
|
|
//c.conn.SetReadLimit(65535)
|
2017-02-02 03:12:13 +00:00
|
|
|
for {
|
2017-03-24 22:45:54 +00:00
|
|
|
_, message, err := c.conn.ReadMessage()
|
2017-02-19 20:05:06 +00:00
|
|
|
|
2017-03-24 22:45:54 +00:00
|
|
|
//loginfo.Println("ReadMessage", msgType, err)
|
2017-03-03 00:47:59 +00:00
|
|
|
|
2017-02-19 20:05:06 +00:00
|
|
|
c.Update()
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
if err != nil {
|
|
|
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
2017-03-22 23:45:47 +00:00
|
|
|
c.SetState(false)
|
2017-02-06 03:19:04 +00:00
|
|
|
loginfo.Printf("error: %v", err)
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2017-03-02 03:02:20 +00:00
|
|
|
|
|
|
|
// unpack the message.
|
2017-03-03 00:47:59 +00:00
|
|
|
p, err := packer.ReadMessage(message)
|
|
|
|
key := p.Header.Address().String() + ":" + strconv.Itoa(p.Header.Port)
|
2017-03-10 03:38:23 +00:00
|
|
|
track, err := connectionTrack.Lookup(key)
|
|
|
|
|
2017-03-24 22:45:54 +00:00
|
|
|
//loginfo.Println(hex.Dump(p.Data.Data()))
|
2017-03-03 00:47:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-03-24 22:45:54 +00:00
|
|
|
//loginfo.Println("Unable to locate Tracking for ", key)
|
2017-03-03 00:47:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-10 03:38:23 +00:00
|
|
|
//Support for tracking outbound traffic based on domain.
|
|
|
|
if domainTrack, ok := c.DomainTrack[track.domain]; ok {
|
|
|
|
//if ok then add to structure, else warn there is something wrong
|
2017-03-20 00:04:47 +00:00
|
|
|
domainTrack.AddOut(int64(len(message)))
|
|
|
|
domainTrack.AddResponses()
|
2017-03-10 03:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
track.conn.Write(p.Data.Data())
|
2017-03-02 03:02:20 +00:00
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
c.addIn(int64(len(message)))
|
2017-03-20 00:04:47 +00:00
|
|
|
c.addResponse()
|
2017-03-24 22:45:54 +00:00
|
|
|
//loginfo.Println("end of read")
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//Writer -- expoer the writer function
|
|
|
|
func (c *Connection) Writer() {
|
2017-03-22 22:47:53 +00:00
|
|
|
defer c.conn.Close()
|
2017-02-19 20:05:06 +00:00
|
|
|
|
|
|
|
loginfo.Println("Writer Start ", c)
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case message := <-c.send:
|
2017-02-19 20:05:06 +00:00
|
|
|
w, err := c.NextWriter(websocket.BinaryMessage)
|
|
|
|
loginfo.Println("next writer ", w)
|
2017-02-02 03:12:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-02-19 20:05:06 +00:00
|
|
|
|
|
|
|
c.Update()
|
|
|
|
|
|
|
|
_, err = c.Write(w, 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)
|
2017-03-20 00:04:47 +00:00
|
|
|
c.addRequests()
|
2017-02-16 02:06:26 +00:00
|
|
|
|
|
|
|
//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
|
2017-03-20 00:04:47 +00:00
|
|
|
domainTrack.AddIn(messageLen)
|
|
|
|
domainTrack.AddRequests()
|
2017-02-16 02:06:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|