2020-05-01 05:47:46 +00:00
|
|
|
package api
|
2017-02-02 03:12:13 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-03 00:47:59 +00:00
|
|
|
"context"
|
2017-04-17 20:57:32 +00:00
|
|
|
"fmt"
|
2017-03-22 21:43:36 +00:00
|
|
|
"io"
|
2020-05-01 05:47:46 +00:00
|
|
|
"log"
|
2017-03-22 21:43:36 +00:00
|
|
|
"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
|
|
|
|
2020-04-30 05:52:44 +00:00
|
|
|
"git.coolaj86.com/coolaj86/go-telebitd/packer"
|
2017-02-02 03:12:13 +00:00
|
|
|
)
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
var connectionID int64 = 0
|
|
|
|
|
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
|
|
|
|
|
2017-03-25 22:22:28 +00:00
|
|
|
// serverName -- Name of the server, at this point 1st domain registered. Will likely change with JWT
|
|
|
|
serverName string
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
// bytes in
|
|
|
|
bytesIn int64
|
|
|
|
|
|
|
|
// bytes out
|
|
|
|
bytesOut int64
|
2017-02-12 20:39:50 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
// Requests
|
|
|
|
Requests int64 // TODO atomic
|
2017-03-20 00:04:47 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
// Response
|
|
|
|
Responses int64 // TODO atomic
|
2017-03-20 00:04:47 +00:00
|
|
|
|
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
|
2020-04-30 11:11:03 +00:00
|
|
|
initialDomains []string
|
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-25 22:22:28 +00:00
|
|
|
func NewConnection(connectionTable *Table, conn *websocket.Conn, remoteAddress string,
|
2020-04-30 11:11:03 +00:00
|
|
|
initialDomains []string, connectionTrack *Tracking, serverName string) (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
|
2017-03-25 22:22:28 +00:00
|
|
|
p.serverName = serverName
|
2017-02-12 04:13:29 +00:00
|
|
|
p.bytesIn = 0
|
|
|
|
p.bytesOut = 0
|
2020-05-01 05:47:46 +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)
|
2017-03-26 01:33:09 +00:00
|
|
|
p.lastUpdate = time.Now()
|
2017-02-16 02:06:26 +00:00
|
|
|
|
|
|
|
for _, domain := range initialDomains {
|
2020-04-30 11:11:03 +00:00
|
|
|
p.AddTrackedDomain(domain)
|
2017-02-16 02:06:26 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-25 22:22:28 +00:00
|
|
|
//ServerName -- Property
|
|
|
|
func (c *Connection) ServerName() string {
|
|
|
|
return c.serverName
|
|
|
|
}
|
|
|
|
|
|
|
|
//SetServerName -- Setter
|
|
|
|
func (c *Connection) SetServerName(serverName string) {
|
|
|
|
c.serverName = serverName
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:06:26 +00:00
|
|
|
//InitialDomains -- Property
|
2020-04-30 11:11:03 +00:00
|
|
|
func (c *Connection) InitialDomains() []string {
|
2017-03-22 22:33:09 +00:00
|
|
|
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() {
|
2020-05-01 05:47:46 +00:00
|
|
|
// TODO atomic
|
|
|
|
c.Requests++
|
2017-03-20 00:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) addResponse() {
|
2020-05-01 05:47:46 +00:00
|
|
|
// TODO atomic
|
|
|
|
c.Responses++
|
2017-03-20 00:04:47 +00:00
|
|
|
}
|
|
|
|
|
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?
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("NextWriter aborted, state is not true")
|
2017-03-22 22:33:09 +00:00
|
|
|
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()
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("reader defer", c)
|
2017-02-02 03:12:13 +00:00
|
|
|
}()
|
2017-02-19 20:05:06 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("Reader Start ", c)
|
2017-02-19 20:05:06 +00:00
|
|
|
|
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
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
//log.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)
|
2020-05-01 05:47:46 +00:00
|
|
|
log.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)
|
2020-06-03 06:35:56 +00:00
|
|
|
if nil != err {
|
|
|
|
fmt.Println("error during msg parse:", err)
|
|
|
|
continue
|
|
|
|
}
|
2017-04-17 20:57:32 +00:00
|
|
|
key := fmt.Sprintf("%s:%d", p.Address(), p.Port())
|
2017-03-10 03:38:23 +00:00
|
|
|
track, err := connectionTrack.Lookup(key)
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
//log.Println(hex.Dump(p.Data.Data()))
|
2017-03-03 00:47:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-01 05:47:46 +00:00
|
|
|
//log.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()
|
2020-05-01 05:47:46 +00:00
|
|
|
//log.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
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("Writer Start ", c)
|
2017-02-19 20:05:06 +00:00
|
|
|
|
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)
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("next writer ", w)
|
2017-02-02 03:12:13 +00:00
|
|
|
if err != nil {
|
2017-03-25 20:01:57 +00:00
|
|
|
c.SetState(false)
|
2017-02-02 03:12:13 +00:00
|
|
|
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()
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("adding ", messageLen, " to ", message.domain)
|
2017-02-16 02:06:26 +00:00
|
|
|
} else {
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("attempting to add bytes to ", message.domain, "it does not exist")
|
2020-05-01 08:18:47 +00:00
|
|
|
log.Println("dt", c.DomainTrack)
|
2017-02-16 02:06:26 +00:00
|
|
|
}
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println(c)
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|