Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various QoL improvments #1311

Merged
merged 13 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

CfgNetworkServiceKey = "core/networkService"
defaultNetworkServiceMode bool

CfgLocaleKey = "core/localeID"
dhaavi marked this conversation as resolved.
Show resolved Hide resolved
)

func init() {
Expand Down Expand Up @@ -41,5 +43,32 @@
return err
}

if err := config.Register(&config.Option{
Name: "Locale",
Key: CfgLocaleKey,
Description: "Configures the locale for the user interface. This mainly affects rendering of dates, currency and numbers. Note that the Portmaster does not yet support different languages.",
OptType: config.OptTypeString,
ExpertiseLevel: config.ExpertiseLevelUser,
ReleaseLevel: config.ReleaseLevelStable,
DefaultValue: "en-GB",
PossibleValues: []config.PossibleValue{
{
Name: "en-GB",
Value: "en-GB",
},
{
Name: "en-US",
Value: "en-US",
},
},
dhaavi marked this conversation as resolved.
Show resolved Hide resolved
Annotations: config.Annotations{
config.CategoryAnnotation: "User Interface",
config.DisplayHintAnnotation: config.DisplayHintOneOf,
config.RequiresUIReloadAnnotation: true,

Check failure on line 67 in core/config.go

View workflow job for this annotation

GitHub Actions / Test

undefined: config.RequiresUIReloadAnnotation

Check failure on line 67 in core/config.go

View workflow job for this annotation

GitHub Actions / Test

undefined: config.RequiresUIReloadAnnotation

Check failure on line 67 in core/config.go

View workflow job for this annotation

GitHub Actions / Test

undefined: config.RequiresUIReloadAnnotation

Check failure on line 67 in core/config.go

View workflow job for this annotation

GitHub Actions / Test

undefined: config.RequiresUIReloadAnnotation
},
}); err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ require (
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.0 // indirect
github.com/fxamacker/cbor v1.5.1 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/golang/gddo v0.0.0-20180823221919-9d8ff1c67be5 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.1 // indirect
Expand All @@ -62,6 +64,7 @@ require (
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-server-timing v1.0.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUn
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/felixge/httpsnoop v1.0.0 h1:gh8fMGz0rlOv/1WmRZm7OgncIOTsAj21iNJot48omJQ=
github.com/felixge/httpsnoop v1.0.0/go.mod h1:3+D9sFq0ahK/JeJPhCBUV1xlf4/eIYrUQaxulT0VzX8=
github.com/florianl/go-conntrack v0.4.0 h1:TlYkxytdwgVayfU0cKwkHurQA0Rd1ZSEBRckRYDUu18=
github.com/florianl/go-conntrack v0.4.0/go.mod h1:iPDx4oIats2T7X7Jm3PFyRCJM1GfZhJaSHOWROYOrE8=
github.com/florianl/go-nfqueue v1.3.1 h1:khQ9fYCrjbu5CF8dZF55G2RTIEIQRI0Aj5k3msJR6Gw=
Expand All @@ -82,6 +84,8 @@ github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
github.com/gofrs/uuid v4.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/gddo v0.0.0-20180823221919-9d8ff1c67be5 h1:yrv1uUvgXH/tEat+wdvJMRJ4g51GlIydtDpU9pFjaaI=
github.com/golang/gddo v0.0.0-20180823221919-9d8ff1c67be5/go.mod h1:xEhNfoBDX1hzLm2Nf80qUvZ2sVwoMZ8d6IE2SrsQfh4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/glog v1.1.1 h1:jxpi2eWoU84wbX9iIEyAeeoac3FLuifZpY9tcNUD9kw=
Expand All @@ -94,6 +98,7 @@ github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl76
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -183,6 +188,8 @@ github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-server-timing v1.0.1 h1:f00/aIe8T3MrnLhQHu3tSWvnwc5GV/p5eutuu3hF/tE=
github.com/mitchellh/go-server-timing v1.0.1/go.mod h1:Mo6GKi9FSLwWFAMn3bqVPWe20y5ri5QGQuO9D9MCOxk=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
Expand Down
23 changes: 23 additions & 0 deletions netquery/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
writeConn *sqlite.Conn
}

BatchExecute struct {

Check failure on line 60 in netquery/database.go

View workflow job for this annotation

GitHub Actions / Linter

exported type `BatchExecute` should have comment or be unexported (golint)
ID string
SQL string
Params map[string]any
Result *[]map[string]any
}

// Conn is a network connection that is stored in a SQLite database and accepted
// by the *Database type of this package. This also defines, using the ./orm package,
// the table schema and the model that is exposed via the runtime database as well as
Expand Down Expand Up @@ -325,6 +332,22 @@
})
}

// ExecuteBatch executes multiple custom SQL query using a read-only connection against the SQLite
// database used by db.
func (db *Database) ExecuteBatch(ctx context.Context, batches []BatchExecute) error {
return db.withConn(ctx, func(conn *sqlite.Conn) error {
merr := new(multierror.Error)

for _, batch := range batches {
if err := orm.RunQuery(ctx, conn, batch.SQL, orm.WithNamedArgs(batch.Params), orm.WithResult(batch.Result)); err != nil {
merr.Errors = append(merr.Errors, fmt.Errorf("%s: %w", batch.ID, err))
}
}

return merr.ErrorOrNil()
})
}

// CountRows returns the number of rows stored in the database.
func (db *Database) CountRows(ctx context.Context) (int, error) {
var result []struct {
Expand Down
23 changes: 21 additions & 2 deletions netquery/module_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/hashicorp/go-multierror"
servertiming "github.com/mitchellh/go-server-timing"

"github.com/safing/portbase/api"
"github.com/safing/portbase/config"
Expand Down Expand Up @@ -81,6 +82,11 @@ func (m *module) prepare() error {
IsDevMode: config.Concurrent.GetAsBool(config.CfgDevModeKey, false),
}

batchHander := &BatchQueryHandler{
Database: m.Store,
IsDevMode: config.Concurrent.GetAsBool(config.CfgDevModeKey, false),
}

chartHandler := &ChartHandler{
Database: m.Store,
}
Expand All @@ -93,7 +99,20 @@ func (m *module) prepare() error {
Read: api.PermitUser, // Needs read+write as the query is sent using POST data.
Write: api.PermitUser, // Needs read+write as the query is sent using POST data.
BelongsTo: m.Module,
HandlerFunc: queryHander.ServeHTTP,
HandlerFunc: servertiming.Middleware(queryHander, nil).ServeHTTP,
}); err != nil {
return fmt.Errorf("failed to register API endpoint: %w", err)
}

if err := api.RegisterEndpoint(api.Endpoint{
Name: "Batch Query Connections",
Description: "Batch query the in-memory sqlite connection database.",
Path: "netquery/query/batch",
MimeType: "application/json",
Read: api.PermitUser, // Needs read+write as the query is sent using POST data.
Write: api.PermitUser, // Needs read+write as the query is sent using POST data.
BelongsTo: m.Module,
HandlerFunc: servertiming.Middleware(batchHander, nil).ServeHTTP,
}); err != nil {
return fmt.Errorf("failed to register API endpoint: %w", err)
}
Expand All @@ -105,7 +124,7 @@ func (m *module) prepare() error {
MimeType: "application/json",
Write: api.PermitUser,
BelongsTo: m.Module,
HandlerFunc: chartHandler.ServeHTTP,
HandlerFunc: servertiming.Middleware(chartHandler, nil).ServeHTTP,
}); err != nil {
return fmt.Errorf("failed to register API endpoint: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions netquery/orm/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
// if we have the column definition available we
// use the target go type from there.
if colDef != nil {
valueKind = normalizeKind(colDef.GoType.Kind())
valueKind = NormalizeKind(colDef.GoType.Kind())

// if we have a column definition we try to convert the value to
// the actual Go-type that was used in the model.
Expand Down Expand Up @@ -458,10 +458,10 @@
// to their base type.
func getKind(val reflect.Value) reflect.Kind {
kind := val.Kind()
return normalizeKind(kind)
return NormalizeKind(kind)
}

func normalizeKind(kind reflect.Kind) reflect.Kind {
func NormalizeKind(kind reflect.Kind) reflect.Kind {

Check failure on line 464 in netquery/orm/decoder.go

View workflow job for this annotation

GitHub Actions / Linter

exported function `NormalizeKind` should have comment or be unexported (golint)
switch {
case kind >= reflect.Int && kind <= reflect.Int64:
return reflect.Int
Expand Down
17 changes: 16 additions & 1 deletion netquery/orm/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
val = val.Elem()
}

switch normalizeKind(kind) { //nolint:exhaustive
switch NormalizeKind(kind) { //nolint:exhaustive
case reflect.String,
reflect.Float64,
reflect.Bool,
Expand Down Expand Up @@ -156,6 +156,8 @@
val = reflect.Indirect(val)
}

normalizedKind := NormalizeKind(valType.Kind())

// we only care about "time.Time" here
var t time.Time
switch {
Expand All @@ -179,6 +181,19 @@
return nil, false, fmt.Errorf("failed to parse time as RFC3339: %w", err)
}

case (normalizedKind == reflect.Int || normalizedKind == reflect.Uint || normalizedKind == reflect.Float64) && colDef.IsTime:
seconds := int64(0)
switch normalizedKind {

Check failure on line 186 in netquery/orm/encoder.go

View workflow job for this annotation

GitHub Actions / Linter

missing cases in switch of type reflect.Kind: reflect.Invalid, reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Complex64, reflect.Complex128, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer|reflect.Ptr, reflect.Slice, reflect.String, reflect.Struct, reflect.UnsafePointer (exhaustive)
case reflect.Int:
seconds = val.Int()
case reflect.Uint:
seconds = int64(val.Uint())
case reflect.Float64:
seconds = int64(val.Float())
}

t = time.Unix(seconds, 0)

default:
// we don't care ...
return nil, false, nil
Expand Down
2 changes: 1 addition & 1 deletion netquery/orm/schema_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func getColumnDef(fieldType reflect.StructField) (*ColumnDef, error) {
}

def.GoType = ft
kind := normalizeKind(ft.Kind())
kind := NormalizeKind(ft.Kind())

switch kind { //nolint:exhaustive
case reflect.Int, reflect.Uint:
Expand Down
Loading
Loading