Skip to content

Commit

Permalink
scope, xml fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed May 3, 2023
1 parent 5990fa6 commit 2a545dd
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 41 deletions.
38 changes: 16 additions & 22 deletions cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ func (app *App) RemoveHandlerCb(cl cot.ClientHandler) {
if c := app.GetItem(uid); c != nil {
c.SetOffline()
}
app.SendToAllOther(cot.MakeOfflineMsg(uid, ""), cl.GetName())
msg := &cot.CotMessage{
From: cl.GetName(),
Scope: cl.GetScope(),
TakMessage: cot.MakeOfflineMsg(uid, ""),
}
app.SendToAllOther(msg)
}

app.RemoveClientHandler(cl.GetName())
Expand Down Expand Up @@ -401,7 +406,7 @@ func (app *App) route(msg *cot.CotMessage) {
app.SendToCallsign(s, msg.TakMessage)
}
} else {
app.SendToAllOther(msg.TakMessage, msg.From)
app.SendToAllOther(msg)
}
}

Expand Down Expand Up @@ -441,29 +446,17 @@ func (app *App) cleanOldUnits() {
}
}

func (app *App) SendToAllOther(msg *cotproto.TakMessage, author string) {
func (app *App) SendToAllOther(msg *cot.CotMessage) {
app.ForAllClients(func(ch cot.ClientHandler) bool {
if ch.GetName() != author {
if err := ch.SendMsg(msg); err != nil {
if ch.GetName() != msg.From && ch.CanSeeScope(msg.Scope) {
if err := ch.SendMsg(msg.TakMessage); err != nil {
app.Logger.Errorf("error sending to %s: %v", ch.GetName(), err)
}
}
return true
})
}

func (app *App) SendTo(addr string, msg *cotproto.TakMessage) {
app.ForAllClients(func(ch cot.ClientHandler) bool {
if ch.GetName() == addr {
if err := ch.SendMsg(msg); err != nil {
app.Logger.Errorf("error sending to %s: %v", ch.GetName(), err)
}
return false
}
return true
})
}

func (app *App) SendToCallsign(callsign string, msg *cotproto.TakMessage) {
app.ForAllClients(func(ch cot.ClientHandler) bool {
for _, c := range ch.GetUids() {
Expand All @@ -489,7 +482,10 @@ func (app *App) logMessage(c *model.ChatMessage) {
}

func loadCert(name string) ([]*x509.Certificate, error) {
pemBytes, err := os.ReadFile(viper.GetString(name))
if name == "" {
return nil, nil
}
pemBytes, err := os.ReadFile(name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -588,18 +584,16 @@ func main() {
panic(err)
}

cert, err := loadCert("ssl.cert")
ca, err := loadCert(viper.GetString("ssl.ca"))
if err != nil {
panic(err)
}

ca, err := loadCert("ssl.ca")
cert, err := loadCert(viper.GetString("ssl.cert"))
if err != nil {
panic(err)
}

ca = append(ca, cert...)

config := &AppConfig{
udpAddr: viper.GetString("udp_addr"),
tcpAddr: viper.GetString("tcp_addr"),
Expand Down
13 changes: 0 additions & 13 deletions cmd/goatak_server/marti_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,6 @@ func getMissionUploadHandler(app *App) func(req *air.Request, res *air.Response)
}
}

func getContentGetHandler(app *App) func(req *air.Request, res *air.Response) error {
return func(req *air.Request, res *air.Response) error {
app.Logger.Infof("%s %s", req.Method, req.Path)
uid := getStringParam(req, "uid")
if uid == "" {
res.Status = http.StatusNotAcceptable
return res.WriteString("no uid")
}

return nil
}
}

func getMetadataGetHandler(app *App) func(req *air.Request, res *air.Response) error {
return func(req *air.Request, res *air.Response) error {
app.Logger.Infof("%s %s", req.Method, req.Path)
Expand Down
5 changes: 5 additions & 0 deletions cmd/goatak_server/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type UserInfo struct {
Role string `yaml:"role"`
Typ string `yaml:"type"`
Password string `yaml:"password"`
Scope string `yaml:"scope"`
}

type UserManager struct {
Expand Down Expand Up @@ -65,6 +66,10 @@ func (um *UserManager) UserIsValid(user, sn string) bool {
return ok
}

func (um *UserManager) GetUser(username string) *UserInfo {
return um.users[username]
}

func (um *UserManager) GetProfile(user, uid string) []ZipFile {
if um == nil {
return nil
Expand Down
11 changes: 8 additions & 3 deletions cmd/goatak_server/tcpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ func (app *App) listenTls(addr string) error {
}

st := c1.ConnectionState()
user, serial := getUser(&st)
username, serial := getUser(&st)
var scope string
if user := app.userManager.GetUser(username); user != nil {
scope = user.Scope
}

name := "ssl:" + conn.RemoteAddr().String()
h := cot.NewConnClientHandler(name, conn, &cot.HandlerConfig{
Logger: app.Logger.With(zap.String("user", user), zap.String("addr", name)),
User: user,
Logger: app.Logger.With(zap.String("user", username), zap.String("addr", name)),
User: username,
Scope: scope,
Serial: serial,
MessageCb: app.NewCotMessage,
RemoveCb: app.RemoveHandlerCb})
Expand Down
9 changes: 9 additions & 0 deletions cmd/goatak_server/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func (w *WsClientHandler) GetName() string {
func (w *WsClientHandler) GetUser() string {
return ""
}

func (w *WsClientHandler) GetScope() string {
return ""
}

func (w *WsClientHandler) CanSeeScope(scope string) bool {
return true
}

func (w *WsClientHandler) GetVersion() int32 {
return 0
}
Expand Down
14 changes: 14 additions & 0 deletions cot/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (

type HandlerConfig struct {
User string
Scope string
Serial string
Uid string
IsClient bool
Expand All @@ -35,6 +36,8 @@ type ClientHandler interface {
GetName() string
GetUids() map[string]string
GetUser() string
GetScope() string
CanSeeScope(scope string) bool
GetVersion() int32
SendMsg(msg *cotproto.TakMessage) error
}
Expand All @@ -53,6 +56,7 @@ type ConnClientHandler struct {
sendChan chan []byte
active int32
user string
scope string
serial string
messageCb func(msg *CotMessage)
removeCb func(ch ClientHandler)
Expand All @@ -73,6 +77,7 @@ func NewConnClientHandler(name string, conn net.Conn, config *HandlerConfig) *Co

if config != nil {
c.user = config.User
c.scope = config.Scope
c.serial = config.Serial
c.localUid = config.Uid
if config.Logger != nil {
Expand All @@ -94,6 +99,14 @@ func (h *ConnClientHandler) GetUser() string {
return h.user
}

func (h *ConnClientHandler) GetScope() string {
return h.scope
}

func (h *ConnClientHandler) CanSeeScope(scope string) bool {
return h.scope == "" || h.scope == scope
}

func (h *ConnClientHandler) GetUids() map[string]string {
res := make(map[string]string)
h.uids.Range(func(key, value any) bool {
Expand Down Expand Up @@ -171,6 +184,7 @@ func (h *ConnClientHandler) handleRead() {

cotmsg := &CotMessage{
From: h.addr,
Scope: h.scope,
TakMessage: msg,
Detail: d,
}
Expand Down
1 change: 0 additions & 1 deletion cot/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func ProtoToEvent(msg *cotproto.TakMessage) *Event {
b := bytes.Buffer{}
b.WriteString("<detail>" + d.XmlDetail + "</detail>")
xml.NewDecoder(&b).Decode(&ev.Detail)
ev.Detail.Content = ""
}

if d.Contact != nil {
Expand Down
1 change: 0 additions & 1 deletion cot/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ func TestConvert(t *testing.T) {
if !reflect.DeepEqual(msg.CotEvent, msg1.CotEvent) {
t.Fail()
}

}
1 change: 1 addition & 0 deletions cot/cotmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type CotMessage struct {
From string
Scope string
TakMessage *cotproto.TakMessage
Detail *Node
}
Expand Down
2 changes: 1 addition & 1 deletion cot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Node struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr"`
Content string `xml:",innerxml"`
Content string `xml:",chardata"`
Nodes []*Node `xml:",any"`
}

Expand Down

0 comments on commit 2a545dd

Please sign in to comment.