2020-05-01 05:47:46 +00:00
|
|
|
package api
|
2017-03-03 00:47:59 +00:00
|
|
|
|
2017-03-19 14:57:28 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-05-01 05:47:46 +00:00
|
|
|
"log"
|
2017-03-19 14:57:28 +00:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
)
|
2017-03-03 00:47:59 +00:00
|
|
|
|
2017-03-10 03:38:23 +00:00
|
|
|
//Track -- used to track connection + domain
|
|
|
|
type Track struct {
|
|
|
|
conn net.Conn
|
|
|
|
domain string
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewTrack -- Constructor
|
|
|
|
func NewTrack(conn net.Conn, domain string) (p *Track) {
|
|
|
|
p = new(Track)
|
|
|
|
p.conn = conn
|
|
|
|
p.domain = domain
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:47:59 +00:00
|
|
|
//Tracking --
|
|
|
|
type Tracking struct {
|
2017-03-22 22:47:53 +00:00
|
|
|
mutex sync.Mutex
|
2017-03-10 03:38:23 +00:00
|
|
|
connections map[string]*Track
|
|
|
|
register chan *Track
|
2017-03-03 00:47:59 +00:00
|
|
|
unregister chan net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewTracking -- Constructor
|
|
|
|
func NewTracking() (p *Tracking) {
|
|
|
|
p = new(Tracking)
|
2017-03-10 03:38:23 +00:00
|
|
|
p.connections = make(map[string]*Track)
|
|
|
|
p.register = make(chan *Track)
|
2017-03-03 00:47:59 +00:00
|
|
|
p.unregister = make(chan net.Conn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//Run -
|
|
|
|
func (p *Tracking) Run(ctx context.Context) {
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("Tracking Running")
|
2017-03-03 00:47:59 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("Cancel signal hit")
|
2017-03-03 00:47:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case connection := <-p.register:
|
2017-03-19 14:57:28 +00:00
|
|
|
p.mutex.Lock()
|
2017-03-10 03:38:23 +00:00
|
|
|
key := connection.conn.RemoteAddr().String()
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("register fired", key)
|
2017-03-03 00:47:59 +00:00
|
|
|
p.connections[key] = connection
|
|
|
|
p.list()
|
2017-03-19 14:57:28 +00:00
|
|
|
p.mutex.Unlock()
|
2017-03-03 00:47:59 +00:00
|
|
|
|
|
|
|
case connection := <-p.unregister:
|
2017-03-19 14:57:28 +00:00
|
|
|
p.mutex.Lock()
|
2017-03-03 00:47:59 +00:00
|
|
|
key := connection.RemoteAddr().String()
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println("unregister fired", key)
|
2017-03-03 00:47:59 +00:00
|
|
|
if _, ok := p.connections[key]; ok {
|
|
|
|
delete(p.connections, key)
|
|
|
|
}
|
|
|
|
p.list()
|
2017-03-19 14:57:28 +00:00
|
|
|
p.mutex.Unlock()
|
2017-03-03 00:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Tracking) list() {
|
|
|
|
for c := range p.connections {
|
2020-05-01 05:47:46 +00:00
|
|
|
log.Println(c)
|
2017-03-03 00:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Lookup --
|
|
|
|
// - get connection from key
|
2017-03-22 22:33:09 +00:00
|
|
|
func (p *Tracking) Lookup(key string) (*Track, error) {
|
2017-03-19 14:57:28 +00:00
|
|
|
p.mutex.Lock()
|
2017-03-22 22:47:53 +00:00
|
|
|
defer p.mutex.Unlock()
|
2017-03-19 14:57:28 +00:00
|
|
|
|
2017-03-03 00:47:59 +00:00
|
|
|
if _, ok := p.connections[key]; ok {
|
2017-03-22 22:33:09 +00:00
|
|
|
return p.connections[key], nil
|
2017-03-03 00:47:59 +00:00
|
|
|
}
|
2017-03-22 22:33:09 +00:00
|
|
|
return nil, fmt.Errorf("Lookup failed for %s", key)
|
2017-03-03 00:47:59 +00:00
|
|
|
}
|