Skip to content

Commit

Permalink
connections
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Mar 21, 2022
1 parent e6e5662 commit b4f33ce
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 150 deletions.
23 changes: 21 additions & 2 deletions cmd/goatak_server/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,35 @@ type ClientHandler struct {
app *App
sendChan chan []byte
active int32
ssl bool
user string
mx sync.RWMutex
}

func NewClientHandler(conn net.Conn, app *App) *ClientHandler {
func NewClientHandler(app *App, conn net.Conn, user string) *ClientHandler {
c := &ClientHandler{
conn: conn,
app: app,
ver: 0,
sendChan: make(chan []byte, 10),
active: 1,
ssl: false,
user: user,
mx: sync.RWMutex{},
}

return c
}

func NewSSLClientHandler(app *App, conn net.Conn, user string) *ClientHandler {
c := &ClientHandler{
conn: conn,
app: app,
ver: 0,
sendChan: make(chan []byte, 10),
active: 1,
ssl: true,
user: user,
mx: sync.RWMutex{},
}

Expand Down Expand Up @@ -184,7 +203,7 @@ func (h *ClientHandler) checkFirstMsg(msg *cot.Msg) {
}
if h.GetCallsign() == "" && msg.TakMessage.GetCotEvent().GetDetail().GetContact() != nil {
h.callsign = msg.TakMessage.GetCotEvent().GetDetail().GetContact().Callsign
h.app.AddContact(msg.TakMessage.CotEvent.Uid, model.ContactFromEvent(msg))
h.app.AddContact(msg.TakMessage.CotEvent.Uid, model.ContactFromMsg(msg))
}
}

Expand Down
29 changes: 29 additions & 0 deletions cmd/goatak_server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
//go:embed templates
var templates embed.FS

type Connection struct {
Uid string `json:"uid"`
User string `json:"user"`
Ssl bool `json:"ssl"`
Ver int32 `json:"ver"`
}

type HttpServer struct {
app *App
air *air.Air
Expand All @@ -36,6 +43,7 @@ func NewHttp(app *App, address string, apiAddress string) *HttpServer {
a.GET("/map", getMapHandler(app, renderer))
a.GET("/config", getConfigHandler(app))
a.GET("/units", getUnitsHandler(app))
a.GET("/connections", getConnHandler(app))

a.GET("/stack", getStackHandler())

Expand Down Expand Up @@ -154,3 +162,24 @@ func getUnits(app *App) []*model.WebUnit {

return units
}

func getConnHandler(app *App) func(req *air.Request, res *air.Response) error {
return func(req *air.Request, res *air.Response) error {
conn := make([]*Connection, 0)

app.handlers.Range(func(key, value interface{}) bool {
if v, ok := value.(*ClientHandler); ok {
c := &Connection{
Uid: v.uid,
User: v.user,
Ssl: v.ssl,
Ver: v.ver,
}
conn = append(conn, c)
}
return true
})

return res.WriteJSON(conn)
}
}
34 changes: 26 additions & 8 deletions cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,29 @@ func (app *App) GetContact(uid string) *model.Contact {
return nil
}

func (app *App) ProcessContact(msg *cot.Msg) {
if c := app.GetContact(msg.GetUid()); c != nil {
app.Logger.Infof("update contact %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
c.Update(msg)
} else {
app.Logger.Infof("new contact %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
if msg.GetUid() == app.uid {
return
}
app.units.Store(msg.GetUid(), model.ContactFromMsg(msg))
}
}

func (app *App) ProcessUnit(msg *cot.Msg) {
if u := app.GetUnit(msg.GetUid()); u != nil {
app.Logger.Infof("update unit %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
u.Update(msg)
} else {
app.Logger.Infof("new unit %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
app.units.Store(msg.GetUid(), model.UnitFromMsg(msg))
}
}

func (app *App) AddPoint(uid string, p *model.Point) {
if p == nil {
return
Expand Down Expand Up @@ -219,7 +242,7 @@ func (app *App) EventProcessor() {
uid = uid[:len(uid)-5]
}
if c := app.GetContact(uid); c != nil {
c.SetLastSeenNow(nil)
c.Update(nil)
}
app.SendTo(uid, cot.MakePong())
break
Expand All @@ -235,14 +258,9 @@ func (app *App) EventProcessor() {
msg.GetType(),
msg.GetStale().Sub(time.Now()))
if msg.IsContact() {
if c := app.GetContact(msg.GetUid()); c != nil {
c.SetLastSeenNow(msg)
} else {
app.Logger.Infof("new contact: uid %s, callsign %s", msg.GetUid(), msg.GetCallsign())
app.AddContact(msg.GetUid(), model.ContactFromEvent(msg))
}
app.ProcessContact(msg)
} else {
app.AddUnit(msg.GetUid(), model.UnitFromEvent(msg))
app.ProcessUnit(msg)
}
// b-m-p-s-p-i digital pointer
// b-m-p-s-m point
Expand Down
4 changes: 2 additions & 2 deletions cmd/goatak_server/tcpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (app *App) ListenTCP(addr string) (err error) {
return err
}
app.Logger.Infof("TCP connection")
NewClientHandler(conn, app).Start()
NewClientHandler(app, conn, "").Start()
}
}

Expand Down Expand Up @@ -77,7 +77,7 @@ func (app *App) ListenSSl(certFile, keyFile, caFile, addr string) error {
user := getUser(c1)

app.Logger.Infof("user: %s", user)
NewClientHandler(c1, app).Start()
NewSSLClientHandler(app, conn, user).Start()
}
}

Expand Down
44 changes: 31 additions & 13 deletions cmd/goatak_server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,63 @@ <h5>ATAK Server</h5>
<a href="/map">Map</a>
<div class="row">
<div class="col-6">

<div class="overflow-auto">
<div class="alert alert-secondary info" role="alert" v-if="alert != null">
{{alert}}
</div>
<div class="card mb-2">
<div class="card-header">Contacts</div>
<div class="card-header">Connections</div>
<div class="card-body">
<table class="table table-hover table-sm units">
<tr v-for="u in byCategory('contact')">
<td>
<img v-bind:src="getImg(u)"/>
</td>
<td>{{u.callsign}}</td>
<td>{{u.status}}</td>
<td>{{printCoords(u.lat, u.lon)}}</td>
<td>{{u.tak_version}}</td>
<tr>
<th>UID</th>
<th>username</th>
<th>SSL</th>
<th>ver.</th>
</tr>
<tr v-for="c in all_conns">
<td>{{c.uid}}</td>
<td>{{c.user}}</td>
<td>{{c.ssl}}</td>
<td>{{c.ver}}</td>
</tr>
</table>
</div>
</div>
<div class="card mb-2">
<div class="card-header">Units</div>
<div class="card-header">Contacts</div>
<div class="card-body">
<table class="table table-hover table-sm units">
<tr v-for="u in byCategory('unit')">
<tr v-for="u in byCategory('contact')">
<td>
<img v-bind:src="getImg(u)"/>
</td>
<td>{{u.callsign}}</td>
<td>{{u.status}}</td>
<td>{{printCoords(u.lat, u.lon)}}</td>
<td>{{dt(u.stale_time)}}</td>
<td>{{u.tak_version}}</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="col-6">
<div class="card mb-2">
<div class="card-header">Units</div>
<div class="card-body">
<table class="table table-hover table-sm units">
<tr v-for="u in byCategory('unit')">
<td>
<img v-bind:src="getImg(u)"/>
</td>
<td>{{u.callsign}}</td>
<td>{{printCoords(u.lat, u.lon)}}</td>
<td>{{dt(u.stale_time)}}</td>
</tr>
</table>
</div>
</div>
<div class="card mb-2">
<div class="card-header">Points</div>
<div class="card-body">
Expand Down
78 changes: 48 additions & 30 deletions cmd/webclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,11 @@ func (app *App) sendPing() {
}

func (app *App) ProcessEvent(msg *cot.Msg) {

switch {
case msg.GetType() == "t-x-c-t":
app.Logger.Debugf("ping from %s", msg.GetUid())
if c := app.GetContact(msg.GetUid()); c != nil {
c.SetLastSeenNow(nil)
c.Update(nil)
}
case msg.GetType() == "t-x-c-t-r":
app.Logger.Debugf("pong")
Expand All @@ -328,17 +327,11 @@ func (app *App) ProcessEvent(msg *cot.Msg) {
app.Logger.Info("my own info")
break
}
if c := app.GetContact(msg.GetUid()); c != nil {
app.Logger.Infof("update pos %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
c.SetLastSeenNow(msg)
} else {
app.Logger.Infof("new contact %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
app.AddContact(msg.GetUid(), model.ContactFromEvent(msg))
}
app.ProcessContact(msg)
} else {
app.Logger.Infof("new unit %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
app.AddUnit(msg.GetUid(), model.UnitFromEvent(msg))
app.ProcessUnit(msg)
}
return
case strings.HasPrefix(msg.GetType(), "b-"):
app.Logger.Infof("point %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
app.AddPoint(msg.GetUid(), model.PointFromEvent(msg))
Expand All @@ -347,11 +340,27 @@ func (app *App) ProcessEvent(msg *cot.Msg) {
}
}

func (app *App) AddUnit(uid string, u *model.Unit) {
if u == nil {
return
func (app *App) ProcessContact(msg *cot.Msg) {
if c := app.GetContact(msg.GetUid()); c != nil {
app.Logger.Infof("update contact %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
c.Update(msg)
} else {
app.Logger.Infof("new contact %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
if msg.GetUid() == app.uid {
return
}
app.units.Store(msg.GetUid(), model.ContactFromMsg(msg))
}
}

func (app *App) ProcessUnit(msg *cot.Msg) {
if u := app.GetUnit(msg.GetUid()); u != nil {
app.Logger.Infof("update unit %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
u.Update(msg)
} else {
app.Logger.Infof("new unit %s (%s) %s", msg.GetUid(), msg.GetCallsign(), msg.GetType())
app.units.Store(msg.GetUid(), model.UnitFromMsg(msg))
}
app.units.Store(uid, u)
}

func (app *App) AddPoint(uid string, u *model.Point) {
Expand All @@ -361,17 +370,6 @@ func (app *App) AddPoint(uid string, u *model.Point) {
app.units.Store(uid, u)
}

func (app *App) GetUnit(uid string) *model.Unit {
if v, ok := app.units.Load(uid); ok {
if unit, ok := v.(*model.Unit); ok {
return unit
} else {
app.Logger.Errorf("invalid object for uid %s: %v", uid, v)
}
}
return nil
}

func (app *App) Remove(uid string) {
if _, ok := app.units.Load(uid); ok {
app.units.Delete(uid)
Expand All @@ -390,7 +388,18 @@ func (app *App) GetContact(uid string) *model.Contact {
if contact, ok := v.(*model.Contact); ok {
return contact
} else {
app.Logger.Errorf("invalid object for uid %s: %v", uid, v)
app.Logger.Warnf("invalid object for uid %s: %s", uid, v)
}
}
return nil
}

func (app *App) GetUnit(uid string) *model.Unit {
if v, ok := app.units.Load(uid); ok {
if contact, ok := v.(*model.Unit); ok {
return contact
} else {
app.Logger.Warnf("invalid unit for uid %s: %s", uid, v)
}
}
return nil
Expand All @@ -399,13 +408,22 @@ func (app *App) GetContact(uid string) *model.Contact {
func (app *App) removeByLink(msg *cot.Msg) {
if msg.Detail != nil && msg.Detail.HasChild("link") {
uid := msg.Detail.GetFirstChild("link").GetAttr("uid")
typ := msg.Detail.GetFirstChild("link").GetAttr("type")
if uid == "" {
app.Logger.Errorf("invalid remove message: %s", msg.Detail)
return
}
app.Logger.Debugf("remove %s by message", uid)
if c := app.GetContact(uid); c != nil {
c.SetOffline()
if v, ok := app.units.Load(uid); ok {
switch vv := v.(type) {
case *model.Contact:
app.Logger.Debugf("remove %s by message", uid)
vv.SetOffline()
return
case *model.Unit, *model.Point:
app.Logger.Debugf("remove unit/point %s type %s by message", uid, typ)
//app.units.Delete(uid)
return
}
}
}
}
Expand Down
Loading

0 comments on commit b4f33ce

Please sign in to comment.