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

[WIP] Add Zeroconf support. #77

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
83 changes: 83 additions & 0 deletions lib/pages/discover_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:async';

import 'package:bonsoir/bonsoir.dart';
import 'package:flutter/material.dart';
import 'package:rutorrentflutter/api/api_conf.dart';

class DiscoverPage extends StatefulWidget {
@override
_DiscoverPageState createState() => _DiscoverPageState();
}

class _DiscoverPageState extends State<DiscoverPage> {
final BonsoirDiscovery zeroconfService =
BonsoirDiscovery(type: "_rutorrent_mobile._tcp");
final List<ResolvedBonsoirService> urls = [];
StreamSubscription zeroconfSub;

@override
void dispose() {
zeroconfSub?.cancel();
if (zeroconfService.isReady && !zeroconfService.isStopped) {
zeroconfService.stop();
}
super.dispose();
}

static buildURL(ResolvedBonsoirService resolved) {
// TODO: Enable password authentication
var protocol = resolved.attributes["protocol"];
var port = resolved.port;
if (port == 80) {
return "http://${resolved.ip}/";
} else if (port == 443) {
return "https://${resolved.ip}/";
} else {
if (protocol != null) {
return "$protocol://${resolved.ip}:${resolved.port}/";
} else {
return "http://${resolved.ip}:${resolved.port}";
}
}
}

void setupListener() {
zeroconfSub = zeroconfService.eventStream.listen((event) {
if (event.isServiceResolved &&
event.type == BonsoirDiscoveryEventType.DISCOVERY_SERVICE_RESOLVED) {
var resolved = event.service as ResolvedBonsoirService;
setState(() {
urls.add(resolved);
});
}
});
zeroconfService.start();
}

@override
void initState() {
zeroconfService.ready.then((_) => setupListener());
super.initState();
}

@override
Widget build(BuildContext context) {
return Dialog(
child: Container(
child: ListView.builder(
itemBuilder: (context, index) => ListTile(
title: Text(urls[index].name),
subtitle: Text("IP address: ${urls[index].ip}"),
onTap: () {
var api = Api();
api.setUrl(buildURL(urls[index]));
api.setUsername("");
api.setPassword("");
Navigator.of(context).pop(api);
},
),
itemCount: urls.length,
)),
);
}
}
42 changes: 42 additions & 0 deletions lib/screens/configurations_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:provider/provider.dart';
import 'package:rutorrentflutter/components/data_input.dart';
import 'package:rutorrentflutter/components/password_input.dart';
import 'package:rutorrentflutter/models/mode.dart';
import 'package:rutorrentflutter/pages/discover_page.dart';
import 'package:rutorrentflutter/screens/main_screen.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:rutorrentflutter/utilities/preferences.dart';
Expand Down Expand Up @@ -222,6 +223,47 @@ class _ConfigurationsScreenState extends State<ConfigurationsScreen> {
passwordFocus: passwordFocus,
),
),
Padding(
padding:
const EdgeInsets.symmetric(vertical: 16, horizontal: 32),
child: Container(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
side:
BorderSide(color: Theme.of(context).primaryColor),
),
primary: Provider.of<Mode>(context).isLightMode
? Colors.white
: Colors.black,
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 28, vertical: 16),
child: Text(
'Discover ruTorrent servers in LAN',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 18),
),
),
onPressed: () async {
var received = await showDialog<Api>(
context: context, builder: (ctx) => DiscoverPage());
if (received != null) {
Api api = Provider.of<Api>(context,
listen: false); // One call to provider
api.setUrl(api.url);
api.setUsername(api.username);
api.setPassword(api.password);
_validateConfigurationDetails(api);
}
},
),
),
),
Padding(
padding:
const EdgeInsets.symmetric(vertical: 16, horizontal: 32),
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
bonsoir:
dependency: "direct main"
description:
name: bonsoir
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3+2"
boolean_selector:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies:
wakelock: ^0.5.0+2
flutter_slidable: ^0.6.0-nullsafety.0
flutter_svg: ^0.21.0-nullsafety.0
bonsoir: ^0.1.3+2

dev_dependencies:
flutter_test:
Expand Down