Skip to content

Commit

Permalink
bruig: expose auto handshake and idle user removal cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
miki authored and miki-totefu committed Aug 25, 2023
1 parent 5658267 commit 3382d2c
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 3 deletions.
14 changes: 13 additions & 1 deletion bruig/flutterui/bruig/lib/config.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:args/args.dart';
import 'package:bruig/util.dart';
import "package:ini/ini.dart" as ini;
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
Expand Down Expand Up @@ -80,6 +81,8 @@ class Config {
late final int circuitLimit;
late final bool noLoadChatHistory;
late final bool syncFreeList;
late final int autoHandshakeInterval;
late final int autoRemoveIdleUsersInterval;

Config();
Config.filled(
Expand All @@ -106,7 +109,9 @@ class Config {
this.proxyPassword: "",
this.circuitLimit: 32,
this.noLoadChatHistory: true,
this.syncFreeList: true});
this.syncFreeList: true,
this.autoHandshakeInterval: 21 * 24 * 60 * 60,
this.autoRemoveIdleUsersInterval: 60 * 24 * 60 * 60});
factory Config.newWithRPCHost(
Config cfg, String rpcHost, String tlsCert, String macaroonPath) =>
Config.filled(
Expand Down Expand Up @@ -134,6 +139,8 @@ class Config {
circuitLimit: cfg.circuitLimit,
noLoadChatHistory: cfg.noLoadChatHistory,
syncFreeList: cfg.syncFreeList,
autoHandshakeInterval: cfg.autoHandshakeInterval,
autoRemoveIdleUsersInterval: cfg.autoRemoveIdleUsersInterval,
);

Future<void> saveConfig(String filepath) async {
Expand Down Expand Up @@ -244,6 +251,11 @@ Future<Config> loadConfig(String filepath) async {
c.noLoadChatHistory = getBool("default", "noloadchathistory");
c.syncFreeList = getBoolDefaultTrue("default", "syncfreelist");

c.autoHandshakeInterval =
parseDurationSeconds(f.get("default", "autohandshakeinterval") ?? "21d");
c.autoRemoveIdleUsersInterval = parseDurationSeconds(
f.get("default", "autoremoveidleusersinterval") ?? "60d");

if (c.walletType != "disabled") {
c.lnRPCHost = f.get("payment", "lnrpchost") ?? "localhost:10009";
c.lnTLSCert =
Expand Down
4 changes: 3 additions & 1 deletion bruig/flutterui/bruig/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ class _AppState extends State<App> with WindowListener {
cfg.proxyUsername,
cfg.proxyPassword,
cfg.circuitLimit,
cfg.noLoadChatHistory);
cfg.noLoadChatHistory,
cfg.autoHandshakeInterval,
cfg.autoRemoveIdleUsersInterval);
await Golib.initClient(initArgs);

navkey.currentState!.pushReplacementNamed(OverviewScreen.routeName);
Expand Down
90 changes: 90 additions & 0 deletions bruig/flutterui/bruig/lib/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,93 @@ String humanReadableSize(int size) {
}
return "$size B";
}

// Parse a go-like string duration to nanoseconds. Supports day and week.
int parseDuration(String s) {
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
var orig = s;
int d = 0; // In Nanoseconds.
bool isNeg = false;
if (s != "") {
if (s[0] == "+" || s[0] == "-") {
isNeg = s[0] == "-";
s = s.substring(1);
}
}

if (s == "0") {
return 0;
}

if (s == "") {
throw "invalid duration: '$orig'";
}

var fullMatch = RegExp(r'^([0-9]*(\.[0-9]*)?([a-z]+))+$').allMatches(s);
if (fullMatch.isEmpty) {
throw "invalid duration: '$orig'";
}

var matches = RegExp(r'([0-9]*(?:\.[0-9]*)?)([a-z]+)').allMatches(s);

for (var el in matches) {
/*
for (var i = 0; i <= el.groupCount; i++) {
print("$i ${el.group(i)}");
}
*/

if (el.groupCount != 2) {
throw "invalid duration: '$orig'";
}
double v = double.parse(el.group(1)!);
String label = el.group(2)!;
int unit;
switch (label) {
case "ns":
unit = 1;
break;
case "us":
unit = 1e3.toInt();
break;
case "µs":
case "μs":
unit = 1e6.toInt();
break;
case "ms":
unit = 1e9.toInt();
break;
case "s":
unit = 1e12.toInt();
break;
case "m":
unit = 1e12.toInt() * 60;
break;
case "h":
unit = 1e12.toInt() * 60 * 60;
break;
case "d":
unit = 1e12.toInt() * 60 * 60 * 24;
break;
case "w":
unit = 1e12.toInt() * 60 * 60 * 24 * 7;
break;
default:
throw "invalid duration: '$orig'";
}

if (v > ((1 << 63) - 1) / unit) {
throw ("invalid duration (overflow): '$orig'");
}

v *= unit;
d += v.truncate();
}

if (isNeg) {
d *= -1;
}
return d;
}

int parseDurationSeconds(String s) => (parseDuration(s) / 1e12).truncate();
8 changes: 7 additions & 1 deletion bruig/flutterui/plugin/lib/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class InitClient {
final int circuitLimit;
@JsonKey(name: 'no_load_chat_history')
final bool noLoadChatHistory;
@JsonKey(name: 'auto_handshake_interval')
final int autoHandshakeInterval;
@JsonKey(name: 'auto_remove_idle_users_interval')
final int autoRemoveIdleUsersInterval;

InitClient(
this.dbRoot,
Expand All @@ -76,7 +80,9 @@ class InitClient {
this.proxyUsername,
this.proxyPassword,
this.circuitLimit,
this.noLoadChatHistory);
this.noLoadChatHistory,
this.autoHandshakeInterval,
this.autoRemoveIdleUsersInterval);

Map<String, dynamic> toJson() => _$InitClientToJson(this);
}
Expand Down
4 changes: 4 additions & 0 deletions bruig/flutterui/plugin/lib/definitions.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bruig/golib/command_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func handleInitClient(handle uint32, args initClient) error {
ResourcesProvider: resRouter,
NoLoadChatHistory: args.NoLoadChatHistory,

AutoHandshakeInterval: time.Duration(args.AutoHandshakeInterval) * time.Second,
AutoRemoveIdleUsersInterval: time.Duration(args.AutoRemoveIdleUsersInterval) * time.Second,

CertConfirmer: func(ctx context.Context, cs *tls.ConnectionState,
svrID *zkidentity.PublicIdentity) error {

Expand Down
3 changes: 3 additions & 0 deletions bruig/golib/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type initClient struct {
ProxyPassword string `json:"proxy_password"`
TorIsolation bool `json:"torisolation"`
CircuitLimit uint32 `json:"circuit_limit"`

AutoHandshakeInterval int64 `json:"auto_handshake_interval"`
AutoRemoveIdleUsersInterval int64 `json:"auto_remove_idle_users_interval"`
}

type iDInit struct {
Expand Down

0 comments on commit 3382d2c

Please sign in to comment.