yanic/respond/respond.go

82 lines
1.7 KiB
Go
Raw Normal View History

2016-03-09 03:49:27 +01:00
package respond
import (
2020-01-07 15:27:08 +01:00
"bytes"
"compress/flate"
"encoding/json"
"io"
2016-03-09 03:49:27 +01:00
"net"
2020-01-07 15:27:08 +01:00
"github.com/bdlm/log"
"github.com/tidwall/gjson"
"github.com/FreifunkBremen/yanic/data"
2016-03-09 03:49:27 +01:00
)
const (
// default multicast group used by announced
2020-01-07 15:27:08 +01:00
MulticastAddressDefault = "ff05:0:0:0:0:0:2:1001"
2016-03-09 03:49:27 +01:00
// default udp port used by announced
2020-01-07 15:27:08 +01:00
PortDefault = 1001
2016-03-09 03:49:27 +01:00
// maximum receivable size
2020-01-07 15:27:08 +01:00
MaxDataGramSize = 8192
2016-03-09 03:49:27 +01:00
)
// Response of the respond request
2016-03-09 03:49:27 +01:00
type Response struct {
Address *net.UDPAddr
2016-03-09 03:49:27 +01:00
Raw []byte
}
2020-01-07 15:27:08 +01:00
func NewRespone(res *data.ResponseData, addr *net.UDPAddr) (*Response, error) {
buf := new(bytes.Buffer)
flater, err := flate.NewWriter(buf, flate.BestCompression)
if err != nil {
return nil, err
}
defer flater.Close()
if err = json.NewEncoder(flater).Encode(res); err != nil {
return nil, err
}
err = flater.Flush()
return &Response{
Raw: buf.Bytes(),
Address: addr,
}, err
}
func (res *Response) parse(customFields []CustomFieldConfig) (*data.ResponseData, error) {
// Deflate
deflater := flate.NewReader(bytes.NewReader(res.Raw))
defer deflater.Close()
2023-09-18 10:55:05 +02:00
jsonData, err := io.ReadAll(deflater)
2020-01-07 15:27:08 +01:00
if err != nil && err != io.ErrUnexpectedEOF {
return nil, err
}
// Unmarshal
rdata := &data.ResponseData{}
err = json.Unmarshal(jsonData, rdata)
rdata.CustomFields = make(map[string]interface{})
if !gjson.Valid(string(jsonData)) {
log.WithField("jsonData", jsonData).Info("JSON data is invalid")
} else {
jsonParsed := gjson.Parse(string(jsonData))
for _, customField := range customFields {
field := jsonParsed.Get(customField.Path)
if field.Exists() {
rdata.CustomFields[customField.Name] = field.String()
}
}
}
return rdata, err
}