Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Dec 6, 2023
1 parent 6df6f90 commit b4ff85d
Show file tree
Hide file tree
Showing 32 changed files with 3,162 additions and 3,026 deletions.
2 changes: 1 addition & 1 deletion cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (app *App) SendToCallsign(callsign string, msg *cot.CotMessage) {

func (app *App) SendToUid(uid string, msg *cot.CotMessage) {
app.ForAllClients(func(ch client.ClientHandler) bool {
if ch.HasUid(uid) {
if ch.HasUID(uid) {
if err := ch.SendMsg(msg); err != nil {
app.Logger.Errorf("error: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/goatak_server/metrics.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:gochecknoglobals
package main

import (
Expand Down
40 changes: 20 additions & 20 deletions cmd/goatak_server/templates/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@
<li v-for="u in byCategory('contact')">
<a class="dropdown-item" href="#" v-on:click="setCurrentUnitUid(u.uid, true)">
<img v-bind:src="getImg(u)"/>
<span v-if="u.lat ===0 && u.lon ===0">* </span>{{u.callsign}}<span
v-if="u.status != ''"> ({{u.status}})</span>
<span v-if="u.lat ===0 && u.lon ===0">* </span>{{ u.callsign }}<span
v-if="u.status != ''"> ({{ u.status }})</span>
</a>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink2" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
Units <span class="badge rounded-pill bg-success">{{countByCategory('unit')}}</span>
Units <span class="badge rounded-pill bg-success">{{ countByCategory('unit') }}</span>
</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink2">
<li v-for="u in byCategory('unit')">
<a class="dropdown-item" href="#" v-on:click="setCurrentUnitUid(u.uid, true)">
{{getUnitName(u)}}
{{ getUnitName(u) }}
</a>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink3" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
Points <span class="badge rounded-pill bg-success">{{countByCategory('point')}}</span>
Points <span class="badge rounded-pill bg-success">{{ countByCategory('point') }}</span>
</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink3">
<li v-for="u in byCategory('point')">
<a class="dropdown-item" href="#" v-on:click="setCurrentUnitUid(u.uid, true)">
{{getUnitName(u)}}
{{ getUnitName(u) }}
</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#" data-bs-toggle="modal" data-bs-target="#messages">Chat <span
class="badge rounded-pill bg-success">{{msgNum()}}</span></a>
class="badge rounded-pill bg-success">{{ msgNum() }}</span></a>
</li>
</ul>
</div>
Expand All @@ -61,14 +61,14 @@
</div>
<div class="col-3">
<div class="card mb-1" v-if="config != null && ne(config.callsign)">
<div class="card-header">Me ({{config.callsign}})</div>
<div class="card-header">Me ({{ config.callsign }})</div>
<div class="card-body">
<b>UID:</b> {{config.uid}}<br/>
<b>UID:</b> {{ config.uid }}<br/>
<div v-if="ne(config.team)">
<b>Team:</b> {{config.team}}, <b>Role:</b>{{config.role}}
<b>Team:</b> {{ config.team }}, <b>Role:</b>{{ config.role }}
</div>
<div>
<b>coords:</b> {{printCoords(config.lat, config.lon)}}
<b>coords:</b> {{ printCoords(config.lat, config.lon) }}
<span class="badge rounded-pill bg-success" style="cursor:default;"
v-on:click="map.setView([config.lat, config.lon])"><i
class="bi bi-geo"></i></span>
Expand All @@ -88,21 +88,21 @@
<label class="btn btn-outline-primary btn-sm" for="redx">Red-x</label>
</div>
<div v-if="getTool('redx') != null" class="mt-1">
<span class="badge bg-danger">RedX</span>: {{printCoordsll(getTool('redx').getLatLng())}}
<span class="badge bg-danger">RedX</span>: {{ printCoordsll(getTool('redx').getLatLng()) }}
<span class="badge rounded-pill bg-success" style="cursor:default;"
v-on:click="map.setView(getTool('redx').getLatLng())"><i class="bi bi-geo"></i></span>
<span class="badge rounded-pill bg-danger" style="cursor:default;"
v-on:click="removeTool('redx')">X</span>
</div>
<div v-if="getTool('dp1') != null" class="mt-1">
<span class="badge bg-success">DP1</span>: {{printCoordsll(getTool('dp1').getLatLng())}}
<span class="badge bg-success">DP1</span>: {{ printCoordsll(getTool('dp1').getLatLng()) }}
<span class="badge rounded-pill bg-success" style="cursor:default;"
v-on:click="map.setView(getTool('dp1').getLatLng())"><i class="bi bi-geo"></i></span>
<span class="badge rounded-pill bg-danger" style="cursor:default;"
v-on:click="removeTool('dp1')">X</span>
</div>
<div v-if="coords != null" class="mt-1">
<span class="badge bg-secondary">cursor</span>: {{printCoordsll(coords)}} <span
<span class="badge bg-secondary">cursor</span>: {{ printCoordsll(coords) }} <span
v-if="getTool('redx') != null">({{ distBea(getTool('redx').getLatLng(), coords) }} from RedX)</span>
</div>
</div>
Expand All @@ -111,8 +111,8 @@
<div class="card-header">
<div class="row">
<h5 v-on:click="mapToUnit(current_unit)" class="col-9">
<img v-bind:src="milImg(current_unit)"/> {{getUnitName(current_unit)}} <span
v-if="ne(current_unit.status)"> ({{current_unit.status}})</span>
<img v-bind:src="milImg(current_unit)"/> {{ getUnitName(current_unit) }} <span
v-if="ne(current_unit.status)"> ({{ current_unit.status }})</span>
<img height="24" src="/static/icons/coord_unlock.png"
v-if="current_unit.category !== 'point' && locked_unit_uid != current_unit.uid"
v-on:click="locked_unit_uid=current_unit.uid"/>
Expand All @@ -131,13 +131,13 @@ <h5 v-on:click="mapToUnit(current_unit)" class="col-9">
</div>
</div>
<div class="card-body">
<b>UID:</b> {{current_unit.uid}}<br/>
<b>UID:</b> {{ current_unit.uid }}<br/>
<div v-if="ne(current_unit.team)">
<b>Team:</b> {{current_unit.team}},
<b>Role:</b> {{current_unit.role}}
<b>Team:</b> {{ current_unit.team }},
<b>Role:</b> {{ current_unit.role }}
</div>
<div>
<b>Type:</b> {{current_unit.type}} <b>SIDC:</b> {{current_unit.sidc}}
<b>Type:</b> {{ current_unit.type }} <b>SIDC:</b> {{ current_unit.sidc }}
</div>
<div>
<b>coords:</b> {{ printCoords(current_unit.lat, current_unit.lon) }}
Expand Down
2 changes: 1 addition & 1 deletion cmd/goatak_server/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (w *WsClientHandler) GetUids() map[string]string {
return res
}

func (w *WsClientHandler) HasUid(uid string) bool {
func (w *WsClientHandler) HasUID(uid string) bool {
_, ok := w.uids.Load(uid)
return ok
}
Expand Down
107 changes: 49 additions & 58 deletions cmd/webclient/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Enroller struct {
logger *zap.SugaredLogger
host string
port int
cl *http.Client
client *http.Client
user string
passwd string
save bool
Expand All @@ -55,38 +55,61 @@ func NewEnroller(logger *zap.SugaredLogger, host, user, passwd string, save bool
passwd: passwd,
port: 8446,
save: save,
cl: &http.Client{Timeout: time.Second * 30, Transport: &http.Transport{TLSClientConfig: tlsConf}},
client: &http.Client{Timeout: time.Second * 30, Transport: &http.Transport{TLSClientConfig: tlsConf}},
}
}

func (e *Enroller) baseUrl() string {
return fmt.Sprintf("https://%s:%d", e.host, e.port)
func (e *Enroller) getUrl(path string) string {
return fmt.Sprintf("https://%s:%d%s", e.host, e.port, path)
}

func (e *Enroller) getConfig() (*CertificateConfig, error) {
e.logger.Infof("getting tls config")
req, err := http.NewRequest(http.MethodGet, e.baseUrl()+"/Marti/api/tls/config", http.NoBody)
func (e *Enroller) request(method, path string, args map[string]string, body io.Reader) (io.ReadCloser, error) {
url := e.getUrl(path)
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Del("User-Agent")

req.SetBasicAuth(e.user, e.passwd)
res, err := e.cl.Do(req)
req.Header.Del("User-Agent")

if args != nil && len(args) > 0 {
q := req.URL.Query()

for k, v := range args {
q.Add(k, v)

}
req.URL.RawQuery = q.Encode()
}

res, err := e.client.Do(req)
if err != nil {
return nil, err
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status: %d", res.StatusCode)
return nil, fmt.Errorf("status is %s", res.Status)
}

if res.Body == nil {
return nil, nil
return nil, fmt.Errorf("null body")
}

defer res.Body.Close()
return res.Body, nil
}

dec := xml.NewDecoder(res.Body)
func (e *Enroller) getConfig() (*CertificateConfig, error) {
e.logger.Infof("getting tls config")
body, err := e.request(http.MethodGet, "/Marti/api/tls/config", nil, nil)

if err != nil {
return nil, err
}

defer body.Close()

dec := xml.NewDecoder(body)
conf := new(CertificateConfig)
err = dec.Decode(conf)

Expand All @@ -97,6 +120,7 @@ func (e *Enroller) getOrEnrollCert(uid, version string) (*tls.Certificate, []*x5
fname := fmt.Sprintf("%s_%s.p12", e.host, e.user)
if cert, cas, err := loadP12(fname, viper.GetString("ssl.password")); err == nil {
e.logger.Infof("loading cert from file %s", fname)

return cert, cas, nil
}

Expand All @@ -121,45 +145,27 @@ func (e *Enroller) getOrEnrollCert(uid, version string) (*tls.Certificate, []*x5
}

csr, key := makeCsr(subj)

e.logger.Infof("signing cert on server")
req, err := http.NewRequest(http.MethodPost, e.baseUrl()+"/Marti/api/tls/signClient/v2", strings.NewReader(csr))
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("clientUID", uid)
q.Add("version", version)
req.URL.RawQuery = q.Encode()
args := map[string]string{"clientUID": uid, "version": version}
body, err := e.request(http.MethodPost, "/Marti/api/tls/signClient/v2", args, strings.NewReader(csr))

req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Del("User-Agent")
req.SetBasicAuth(e.user, e.passwd)
res, err := e.cl.Do(req)
if err != nil {
return nil, nil, err
}

if res.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("bad status: %d", res.StatusCode)
}
defer body.Close()

var certs map[string]string

if err := json.NewDecoder(res.Body).Decode(&certs); err != nil {
if err := json.NewDecoder(body).Decode(&certs); err != nil {
return nil, nil, err
}

if res.Body == nil {
return nil, nil, fmt.Errorf("empty response")
}

var cert *x509.Certificate

ca := make([]*x509.Certificate, 0)

defer res.Body.Close()

for name, c := range certs {
crt, err := tlsutil.ParseCert(c)
if err != nil {
Expand Down Expand Up @@ -199,38 +205,23 @@ func (e *Enroller) getOrEnrollCert(uid, version string) (*tls.Certificate, []*x5
}

func (e *Enroller) getProfile(uid string) error {
req, err := http.NewRequest(http.MethodGet, e.baseUrl()+"/Marti/api/tls/profile/enrollment", nil)
if err != nil {
return err
}
args := map[string]string{"clientUID": uid}
body, err := e.request(http.MethodGet, "/Marti/api/tls/profile/enrollment", args, nil)

q := req.URL.Query()
q.Add("clientUID", uid)
req.URL.RawQuery = q.Encode()
req.Header.Del("User-Agent")
req.SetBasicAuth(e.user, e.passwd)
res, err := e.cl.Do(req)
if err != nil {
return err
}

if res.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %d", res.StatusCode)
}

if res.Body == nil {
return fmt.Errorf("null body")
}

defer res.Body.Close()
defer body.Close()

f, err := os.Create(e.host + ".zip")
if err != nil {
return err
}

_, err = io.Copy(f, res.Body)
f.Close()
defer f.Close()

_, err = io.Copy(f, body)
return err
}

Expand All @@ -244,7 +235,7 @@ func makeCsr(subj *pkix.Name) (string, *rsa.PrivateKey) {

csrBytes, _ := x509.CreateCertificateRequest(rand.Reader, &template, keyBytes)

csr := string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes}))
csr := string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes, Headers: nil}))

csr = strings.ReplaceAll(csr, "-----BEGIN CERTIFICATE REQUEST-----\n", "")
csr = strings.ReplaceAll(csr, "\n-----END CERTIFICATE REQUEST-----", "")
Expand Down
2 changes: 1 addition & 1 deletion cmd/webclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (app *App) Init(cancel context.CancelFunc) {
app.remoteApi = NewRemoteApi(app.host)

if app.tls {
app.remoteApi.SetTls(app.getTlsConfig())
app.remoteApi.SetTls(app.getTLSConfig())
}

app.ch = make(chan []byte, 20)
Expand Down
6 changes: 3 additions & 3 deletions cmd/webclient/tcp_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (app *App) connect() (net.Conn, error) {
addr := fmt.Sprintf("%s:%s", app.host, app.tcpPort)
if app.tls {
app.Logger.Infof("connecting with SSL to %s...", addr)
conn, err := tls.Dial("tcp", addr, app.getTlsConfig())
conn, err := tls.Dial("tcp", addr, app.getTLSConfig())
if err != nil {
return nil, err
}
Expand All @@ -34,8 +34,8 @@ func (app *App) connect() (net.Conn, error) {
}
}

func (app *App) getTlsConfig() *tls.Config {
conf := &tls.Config{
func (app *App) getTLSConfig() *tls.Config {
conf := &tls.Config{ //nolint:exhaustruct
Certificates: []tls.Certificate{*app.tlsCert},
RootCAs: app.cas,
ClientCAs: app.cas,
Expand Down
2 changes: 2 additions & 0 deletions cmd/webclient/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ func (app *App) layout(g *gocui.Gui) error {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}

v.Frame = true
}

if v, err := g.SetView("log", maxX/2, 0, maxX-1, maxY-1); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}

v.Frame = true
v.Title = "Log"
}
Expand Down
Loading

0 comments on commit b4ff85d

Please sign in to comment.