Skip to content

Commit

Permalink
[bruig] add rpclient to bruig
Browse files Browse the repository at this point in the history
  • Loading branch information
vctt94 committed Oct 18, 2024
1 parent 160c17b commit 94f608e
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 39 deletions.
38 changes: 37 additions & 1 deletion bruig/flutterui/bruig/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ class Config {
late final bool sendRecvReceipts;
late final bool autoSubPosts;
late final bool logPings;
late final List<String> jsonrpclisten;
late final String fundsAccount;
late final String rpccertpath;
late final String rpckeypath;
late final bool rpcissueclientcert;
late final String rpcclientcapath;
late final String rpcUser;
late final String rpcPass;
late final bool requireRPCAuth;

Config();
Config.filled(
Expand Down Expand Up @@ -135,7 +144,16 @@ class Config {
this.autoRemoveIgnoreList = defaultAutoRemoveIgnoreList,
this.sendRecvReceipts = true,
this.autoSubPosts = true,
this.logPings = false});
this.logPings = false,
this.jsonrpclisten = const [""],
this.fundsAccount = "",
this.rpccertpath = "",
this.rpckeypath = "",
this.rpcissueclientcert = false,
this.rpcclientcapath = "",
this.rpcUser = "",
this.rpcPass = "",
this.requireRPCAuth = true});
factory Config.newWithRPCHost(
Config cfg, String rpcHost, String tlsCert, String macaroonPath) =>
Config.filled(
Expand Down Expand Up @@ -173,6 +191,15 @@ class Config {
sendRecvReceipts: cfg.sendRecvReceipts,
autoSubPosts: cfg.autoSubPosts,
logPings: cfg.logPings,
jsonrpclisten: cfg.jsonrpclisten,
fundsAccount: cfg.fundsAccount,
rpccertpath: cfg.rpccertpath,
rpckeypath: cfg.rpckeypath,
rpcissueclientcert: cfg.rpcissueclientcert,
rpcclientcapath: cfg.rpcclientcapath,
rpcUser: cfg.rpcUser,
rpcPass: cfg.rpcPass,
requireRPCAuth: cfg.requireRPCAuth
);

// Save a new config from scratch.
Expand Down Expand Up @@ -394,6 +421,15 @@ Future<Config> loadConfig(String filepath) async {
c.simpleStoreShipCharge =
double.tryParse(f.get("resources", "shipcharge") ?? "0") ?? 0;

c.jsonrpclisten = getCommaList("clientrpc", "jsonrpclisten") ?? [""]; // default to [""]
c.rpccertpath = f.get("clientrpc", "rpccertpath") ?? "";
c.rpckeypath = f.get("clientrpc", "rpckeypath") ?? "";
c.rpcissueclientcert = f.get("clientrpc", "rpcissueclientcert") == "true";
c.rpcclientcapath = f.get("clientrpc", "rpcclientcapath") ?? "";
c.rpcUser = f.get("clientrpc", "rpcuser") ?? "";
c.rpcPass = f.get("clientrpc", "rpcpass") ?? "";
c.requireRPCAuth = getBoolDefaultTrue("clientrpc", "rpcrequireauth");

return c;
}

Expand Down
69 changes: 38 additions & 31 deletions bruig/flutterui/bruig/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,37 +271,44 @@ class _AppState extends State<App> with WindowListener {
try {
var cfg = widget.cfg;
InitClient initArgs = InitClient(
cfg.dbRoot,
cfg.downloadsDir,
cfg.embedsDir,
cfg.serverAddr,
cfg.lnRPCHost,
cfg.lnTLSCert,
cfg.lnMacaroonPath,
cfg.logFile,
cfg.msgRoot,
cfg.debugLevel,
true,
cfg.resourcesUpstream,
cfg.simpleStorePayType,
cfg.simpleStoreAccount,
cfg.simpleStoreShipCharge,
cfg.proxyaddr,
cfg.torIsolation,
cfg.proxyUsername,
cfg.proxyPassword,
cfg.circuitLimit,
cfg.noLoadChatHistory,
cfg.autoHandshakeInterval,
cfg.autoRemoveIdleUsersInterval,
cfg.autoRemoveIgnoreList,
cfg.sendRecvReceipts,
cfg.autoSubPosts,
cfg.logPings,
Platform.isAndroid || Platform.isIOS // Use longer interval on mobile
? 210 * 1000 // 210 = 3m30s
: 0, // Use whatever is default
);
cfg.dbRoot,
cfg.downloadsDir,
cfg.embedsDir,
cfg.serverAddr,
cfg.lnRPCHost,
cfg.lnTLSCert,
cfg.lnMacaroonPath,
cfg.logFile,
cfg.msgRoot,
cfg.debugLevel,
true,
cfg.resourcesUpstream,
cfg.simpleStorePayType,
cfg.simpleStoreAccount,
cfg.simpleStoreShipCharge,
cfg.proxyaddr,
cfg.torIsolation,
cfg.proxyUsername,
cfg.proxyPassword,
cfg.circuitLimit,
cfg.noLoadChatHistory,
cfg.autoHandshakeInterval,
cfg.autoRemoveIdleUsersInterval,
cfg.autoRemoveIgnoreList,
cfg.sendRecvReceipts,
cfg.autoSubPosts,
cfg.logPings,
Platform.isAndroid || Platform.isIOS // Use longer interval on mobile
? 210 * 1000 // 210 = 3m30s
: 0, // Use whatever is default
cfg.jsonrpclisten,
cfg.rpccertpath,
cfg.rpckeypath,
cfg.rpcissueclientcert,
cfg.rpcclientcapath,
cfg.rpcUser,
cfg.rpcPass,
cfg.requireRPCAuth);
await Golib.initClient(initArgs);
} catch (exception) {
if ("$exception".contains("client already initialized")) {
Expand Down
26 changes: 26 additions & 0 deletions bruig/flutterui/plugin/lib/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ class InitClient {
final bool logPings;
@JsonKey(name: 'ping_interval_ms')
final int pingIntervalMs;
// rpc fields
@JsonKey(name: 'json_rpc_listen')
final List<String> jsonrpclisten;
@JsonKey(name: 'rpc_cert_path')
final String rpccertpath;
@JsonKey(name: 'rpc_key_path')
final String rpckeypath;
@JsonKey(name: 'rpc_issue_client_cert')
final bool rpcissueclientcert;
@JsonKey(name: 'rpc_client_ca_path')
final String rpcclientcapath;
@JsonKey(name: 'rpc_user')
final String rpcUser;
@JsonKey(name: 'rpc_pass')
final String rpcPass;
@JsonKey(name: 'require_rpc_auth')
final bool requireRPCAuth;


InitClient(
this.dbRoot,
Expand Down Expand Up @@ -102,6 +120,14 @@ class InitClient {
this.autoSubPosts,
this.logPings,
this.pingIntervalMs,
this.jsonrpclisten,
this.rpccertpath,
this.rpckeypath,
this.rpcissueclientcert,
this.rpcclientcapath,
this.rpcUser,
this.rpcPass,
this.requireRPCAuth
);

Map<String, dynamic> toJson() => _$InitClientToJson(this);
Expand Down
18 changes: 18 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.

141 changes: 134 additions & 7 deletions bruig/golib/command_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"github.com/companyzero/bisonrelay/client/clientintf"
"github.com/companyzero/bisonrelay/client/resources"
"github.com/companyzero/bisonrelay/client/resources/simplestore"
"github.com/companyzero/bisonrelay/client/rpcserver"
"github.com/companyzero/bisonrelay/clientrpc/types"
"github.com/companyzero/bisonrelay/embeddeddcrlnd"
"github.com/companyzero/bisonrelay/internal/tlsconn"
"github.com/companyzero/bisonrelay/lockfile"
"github.com/companyzero/bisonrelay/rates"
"github.com/companyzero/bisonrelay/rpc"
Expand All @@ -40,17 +42,23 @@ import (
lpclient "github.com/decred/dcrlnlpd/client"
"github.com/decred/go-socks/socks"
"github.com/decred/slog"
"github.com/prometheus/common/version"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)

const (
appName = "bruig"
)

type clientCtx struct {
c *client.Client
lnpc *client.DcrlnPaymentClient
ctx context.Context
cancel func()
runMtx sync.Mutex
runErr error
c *client.Client
lnpc *client.DcrlnPaymentClient
rpcserver *rpcserver.Server

Check failure on line 57 in bruig/golib/command_handlers.go

View workflow job for this annotation

GitHub Actions / Go CI (1.22)

field `rpcserver` is unused (unused)

Check failure on line 57 in bruig/golib/command_handlers.go

View workflow job for this annotation

GitHub Actions / Go CI (1.23)

field `rpcserver` is unused (unused)
ctx context.Context
cancel func()
runMtx sync.Mutex
runErr error

log slog.Logger
logBknd *logBackend
Expand Down Expand Up @@ -169,7 +177,16 @@ func handleInitClient(handle uint32, args initClient) error {

ntfns := client.NewNotificationManager()
ntfns.Register(client.OnPMNtfn(func(user *client.RemoteUser, msg rpc.RMPrivateMessage, ts time.Time) {
// TODO: replace PM{} for types.ReceivedPM{}.
// pm := &types.ReceivedPM{
// Uid: user.ID().Bytes(),
// Msg: &types.RMPrivateMessage{
// Message: msg.Message,
// Mode: types.MessageMode(msg.Mode),
// },
// TimestampMs: ts.Unix(),
// Nick: user.Nick(),
// }

pm := pm{
UID: user.ID(),
Msg: msg.Message,
Expand Down Expand Up @@ -742,12 +759,122 @@ func handleInitClient(handle uint32, args initClient) error {
rates: r,
}

var rpcServer *rpcserver.Server
if len(args.JSONRPCListen) > 0 {
rpcsLog := logBknd.logger("RPCS")
tlsConnCfg := tlsconn.TLSListenersConfig{
Addresses: args.JSONRPCListen,
CertPath: args.RPCCertPath,
KeyPath: args.RPCKeyPath,
CreateCertPairIfNotExists: true,
ClientCAPath: args.RPCClientCAPath,
ClientCertPath: filepath.Join(filepath.Dir(args.RPCClientCAPath), "rpc-client.cert"),
ClientKeyPath: filepath.Join(filepath.Dir(args.RPCClientCAPath), "rpc-client.key"),
CreateClientCertIfNotExists: args.RPCIssueClientCert,
Log: rpcsLog,
}
jsonListeners, err := tlsconn.TLSListeners(tlsConnCfg)
if err != nil {
return err
}
rpcServer = rpcserver.New(rpcserver.Config{
JSONRPCListeners: jsonListeners,
Log: rpcsLog,
RPCUser: args.RPCUser,
RPCPass: args.RPCPass,
RequireAuth: args.RequireRPCAuth,
})
rpcServer.InitVersionService(appName, version.Version)
chatRPCServerCfg := rpcserver.ChatServerCfg{
Log: logBknd.logger("RPCS"),
Client: c,
PayClient: cctx.lnpc,
RootReplayMsgLogs: filepath.Join(args.DBRoot, "replaymsglog"),

// Following are handlers called when the rpc server receives
// a request to perform an action.

OnPM: func(ctx context.Context, uid client.UserID, pm *types.PMRequest) error {

// notify(NTPM, pm.Msg, nil)
return nil
},
OnGCM: func(ctx context.Context, gcid client.GCID, gcm *types.GCMRequest) error {
notify(NTGCMessage, gcm, nil)
return nil
},
}
err = rpcServer.InitChatService(chatRPCServerCfg)
if err != nil {
return err
}

postsRPCServerCfg := rpcserver.PostsServerCfg{
Log: logBknd.logger("RPCS"),
Client: c,
RootReplayMsgLogs: filepath.Join(args.DBRoot, "replaymsglog"),
}
err = rpcServer.InitPostsService(postsRPCServerCfg)
if err != nil {
return err
}

payRPCServerCfg := rpcserver.PaymentsServerCfg{
Log: logBknd.logger("RPCS"),
Client: c,
RootReplayMsgLogs: filepath.Join(args.DBRoot, "replaymsglog"),
}
err = rpcServer.InitPaymentsService(payRPCServerCfg)
if err != nil {
return err
}

gcRPCServerCfg := rpcserver.GCServerCfg{
Log: logBknd.logger("RPCS"),
Client: c,
RootReplayMsgLogs: filepath.Join(args.DBRoot, "replaymsglog"),
}
err = rpcServer.InitGCService(gcRPCServerCfg)
if err != nil {
return err
}

resServerCfg := rpcserver.ResourcesServerCfg{
Log: logBknd.logger("RPCS"),
Client: c,
}
if args.ResourcesUpstream == "clientrpc" {
resServerCfg.Router = resRouter
}
err = rpcServer.InitResourcesService(resServerCfg)
if err != nil {
return err
}

contentServerCfg := rpcserver.ContentServerCfg{
Log: logBknd.logger("RPCS"),
Client: c,
RootReplayMsgLogs: filepath.Join(args.DBRoot, "replaymsglog"),
}
err = rpcServer.InitContentService(contentServerCfg)
if err != nil {
return err
}
}
cs[handle] = cctx

if sstore != nil {
go sstore.Run(ctx)
}

if rpcServer != nil {
go func() {
err := rpcServer.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
cctx.log.Errorf("RPCServer Run() error: %v", err)
}
}()
}
go func() {
err := c.Run(ctx)
if errors.Is(err, context.Canceled) {
Expand Down
Loading

0 comments on commit 94f608e

Please sign in to comment.