Skip to content

Commit

Permalink
uid blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Sep 19, 2024
1 parent b51dd8b commit db235f0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions cmd/goatak_server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (c *AppConfig) LogExclude() []string {
return c.k.Strings("log_exclude")
}

func (c *AppConfig) BlacklistedUID() []string {
return c.k.Strings("blacklist")
}

func (c *AppConfig) processCerts() error {
for _, name := range []string{"ssl.ca", "ssl.cert", "ssl.key"} {
if c.k.String(name) == "" {
Expand Down
11 changes: 11 additions & 0 deletions cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,17 @@ func (app *App) sendToUID(uid string, msg *cot.CotMessage) {
})
}

func (app *App) checkUID(uid string) bool {
u := strings.ToLower(uid)
for _, s := range app.config.BlacklistedUID() {
if u == strings.ToLower(s) {
return false
}
}

return true
}

func getDatabase() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/goatak_server/tcpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (app *App) ListenTCP(ctx context.Context, addr string) (err error) {
RemoveCb: app.RemoveHandlerCb,
NewContactCb: app.NewContactCb,
DropMetric: dropMetric,
UidChecker: app.checkUID,
})
app.AddClientHandler(h)
h.Start()
Expand Down Expand Up @@ -113,6 +114,7 @@ func (app *App) processTLSConn(ctx context.Context, conn *tls.Conn) {
RemoveCb: app.RemoveHandlerCb,
NewContactCb: app.NewContactCb,
DropMetric: dropMetric,
UidChecker: app.checkUID,
})
app.AddClientHandler(h)
h.Start()
Expand Down
14 changes: 5 additions & 9 deletions cmd/goatak_server/templates/points.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
<tr v-for="c in all_conns">
<td>{{ c.addr }}</td>
<td>
<span v-for="(k, v) in c.uids">
{{ k }}: {{ v }}
</span>
<span v-for="(k, v) in c.uids">{{ k }}: {{ v }}</span>
</td>
<td>{{ c.user }}</td>
<td>{{ c.scope }}</td>
Expand All @@ -46,13 +44,12 @@
<th>TAK ver.</th>
</tr>
<tr v-for="u in byCategory('contact')">
<td>
<img :src="getImg(u, 18)"/>
</td>
<td><img :src="getImg(u, 18)"/></td>
<td>{{ u.callsign }}</td>
<td>{{ u.scope }}</td>
<td><span class="badge"
:class="u.status=='Online' ? 'text-bg-success' : 'text-bg-secondary'">{{ u.status }}</span>
:class="u.status=='Online' ? 'text-bg-success' : 'text-bg-secondary'">
{{ u.status }}</span>
</td>
<td>{{ printCoords(u.lat, u.lon) }}</td>
<td>{{ u.tak_version }}</td>
Expand All @@ -76,8 +73,7 @@
<th>stale time</th>
</tr>
<tr v-for="u in byCategory('unit')">
<td>
<img :src="getImg(u, 18)"/>
<td><img :src="getImg(u, 18)"/>
</td>
<td>{{ u.type }}</td>
<td>{{ u.callsign }}</td>
Expand Down
8 changes: 8 additions & 0 deletions internal/client/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type HandlerConfig struct {
NewContactCb func(uid, callsign string)
Logger *slog.Logger
DropMetric *prometheus.CounterVec
UidChecker func(uid string) bool
}

type ClientHandler interface {
Expand Down Expand Up @@ -69,6 +70,7 @@ type ConnClientHandler struct {
newContactCb func(uid, callsign string)
logger *slog.Logger
dropMetric *prometheus.CounterVec
uidChecker func(uid string) bool
}

func NewConnClientHandler(name string, conn net.Conn, config *HandlerConfig) *ConnClientHandler {
Expand All @@ -91,6 +93,7 @@ func NewConnClientHandler(name string, conn net.Conn, config *HandlerConfig) *Co
c.removeCb = config.RemoveCb
c.newContactCb = config.NewContactCb
c.dropMetric = config.DropMetric
c.uidChecker = config.UidChecker

params := []any{"client", name}

Expand Down Expand Up @@ -238,6 +241,11 @@ func (h *ConnClientHandler) handleRead(ctx context.Context) {
uid := msg.GetUID()
uid = strings.TrimSuffix(uid, "-ping")

if h.uidChecker != nil && !h.uidChecker(uid) {
h.logger.Warn(fmt.Sprintf("blacklisted uid %s - dropped", uid))
return
}

if _, present := h.uids.Swap(uid, msg.GetCallsign()); !present {
if h.newContactCb != nil {
h.newContactCb(uid, msg.GetCallsign())
Expand Down

0 comments on commit db235f0

Please sign in to comment.