mirror of
https://github.com/kodjodevf/mangayomi.git
synced 2026-03-11 17:25:32 +00:00
fix bt-server
This commit is contained in:
parent
a43498d757
commit
9072986e41
29 changed files with 670 additions and 270 deletions
Binary file not shown.
|
|
@ -11,19 +11,21 @@ class MainActivity: FlutterActivity() {
|
||||||
|
|
||||||
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
|
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
|
||||||
super.configureFlutterEngine(flutterEngine)
|
super.configureFlutterEngine(flutterEngine)
|
||||||
val taskQueue =
|
|
||||||
flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()
|
|
||||||
MethodChannel(
|
MethodChannel(
|
||||||
flutterEngine.dartExecutor.binaryMessenger,
|
flutterEngine.dartExecutor.binaryMessenger,
|
||||||
"com.kodjodevf.mangayomi.libmtorrentserver",
|
"com.kodjodevf.mangayomi.libmtorrentserver",
|
||||||
StandardMethodCodec.INSTANCE,
|
StandardMethodCodec.INSTANCE,
|
||||||
taskQueue
|
flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()
|
||||||
).setMethodCallHandler { call, result ->
|
).setMethodCallHandler { call, result ->
|
||||||
when (call.method) {
|
when (call.method) {
|
||||||
"start" -> {
|
"start" -> {
|
||||||
val config = call.argument<String>("config")
|
val config = call.argument<String>("config")
|
||||||
Libmtorrentserver.start(config)
|
try {
|
||||||
result.success("ok")
|
val port = Libmtorrentserver.start(config)
|
||||||
|
result.success(port)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
result.error("ERROR", e.message, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
result.notImplemented()
|
result.notImplemented()
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,20 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"C"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"server"
|
"server"
|
||||||
)
|
)
|
||||||
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
//export Start
|
//export Start
|
||||||
func Start(mcfg *C.char) {
|
func Start(mcfg *C.char) (int, *C.char) {
|
||||||
var config server.Config
|
var config server.Config
|
||||||
json.Unmarshal([]byte(C.GoString(mcfg)), &config)
|
json.Unmarshal([]byte(C.GoString(mcfg)), &config)
|
||||||
server.Start(&config)
|
port, err := server.Start(&config)
|
||||||
|
if err != nil {
|
||||||
|
return 0, C.CString(err.Error())
|
||||||
|
}
|
||||||
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
//export Start
|
//export Start
|
||||||
func Start(mcfg string) {
|
func Start(mcfg string) (int, error) {
|
||||||
var config server.Config
|
var config server.Config
|
||||||
json.Unmarshal([]byte(mcfg), &config)
|
json.Unmarshal([]byte(mcfg), &config)
|
||||||
server.Start(&config)
|
return server.Start(&config)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
go/server.go
18
go/server.go
|
|
@ -23,7 +23,7 @@ import (
|
||||||
var torrentCli *torrent.Client
|
var torrentCli *torrent.Client
|
||||||
var torrentcliCfg *torrent.ClientConfig
|
var torrentcliCfg *torrent.ClientConfig
|
||||||
|
|
||||||
func Start(config *Config) {
|
func Start(config *Config) (int, error) {
|
||||||
|
|
||||||
torrentcliCfg = torrent.NewDefaultClientConfig()
|
torrentcliCfg = torrent.NewDefaultClientConfig()
|
||||||
|
|
||||||
|
|
@ -70,9 +70,21 @@ func Start(config *Config) {
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("[INFO] Listening on %s\n", config.Address)
|
listener, err := net.Listen("tcp", config.Address)
|
||||||
log.Fatalln(http.ListenAndServe(config.Address, c.Handler(mux)))
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
addr := listener.Addr().(*net.TCPAddr)
|
||||||
|
|
||||||
|
log.Printf("[INFO] Listening on %s\n", addr.AddrPort())
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := http.Serve(listener, c.Handler(mux)); err != nil && err != http.ErrServerClosed {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return addr.Port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func safenDisplayPath(displayPath string) string {
|
func safenDisplayPath(displayPath string) string {
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,6 @@
|
||||||
#include "Universe.objc.h"
|
#include "Universe.objc.h"
|
||||||
|
|
||||||
|
|
||||||
FOUNDATION_EXPORT void LibmtorrentserverStart(NSString* _Nullable mcfg);
|
FOUNDATION_EXPORT BOOL LibmtorrentserverStart(NSString* _Nullable mcfg, long* _Nullable ret0_, NSError* _Nullable* _Nullable error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
||||||
#include "Universe.objc.h"
|
#include "Universe.objc.h"
|
||||||
|
|
||||||
|
|
||||||
FOUNDATION_EXPORT void LibmtorrentserverStart(NSString* _Nullable mcfg);
|
FOUNDATION_EXPORT BOOL LibmtorrentserverStart(NSString* _Nullable mcfg, long* _Nullable ret0_, NSError* _Nullable* _Nullable error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
||||||
#include "Universe.objc.h"
|
#include "Universe.objc.h"
|
||||||
|
|
||||||
|
|
||||||
FOUNDATION_EXPORT void LibmtorrentserverStart(NSString* _Nullable mcfg);
|
FOUNDATION_EXPORT BOOL LibmtorrentserverStart(NSString* _Nullable mcfg, long* _Nullable ret0_, NSError* _Nullable* _Nullable error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
||||||
#include "Universe.objc.h"
|
#include "Universe.objc.h"
|
||||||
|
|
||||||
|
|
||||||
FOUNDATION_EXPORT void LibmtorrentserverStart(NSString* _Nullable mcfg);
|
FOUNDATION_EXPORT BOOL LibmtorrentserverStart(NSString* _Nullable mcfg, long* _Nullable ret0_, NSError* _Nullable* _Nullable error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
||||||
#include "Universe.objc.h"
|
#include "Universe.objc.h"
|
||||||
|
|
||||||
|
|
||||||
FOUNDATION_EXPORT void LibmtorrentserverStart(NSString* _Nullable mcfg);
|
FOUNDATION_EXPORT BOOL LibmtorrentserverStart(NSString* _Nullable mcfg, long* _Nullable ret0_, NSError* _Nullable* _Nullable error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
||||||
#include "Universe.objc.h"
|
#include "Universe.objc.h"
|
||||||
|
|
||||||
|
|
||||||
FOUNDATION_EXPORT void LibmtorrentserverStart(NSString* _Nullable mcfg);
|
FOUNDATION_EXPORT BOOL LibmtorrentserverStart(NSString* _Nullable mcfg, long* _Nullable ret0_, NSError* _Nullable* _Nullable error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -16,7 +16,13 @@ import Libmtorrentserver
|
||||||
case "start":
|
case "start":
|
||||||
let args = call.arguments as? Dictionary<String, Any>
|
let args = call.arguments as? Dictionary<String, Any>
|
||||||
let config = args?["config"] as? String
|
let config = args?["config"] as? String
|
||||||
LibmtorrentserverStart(config)
|
var error: NSError?
|
||||||
|
let mPort = UnsafeMutablePointer<Int>.allocate(capacity: MemoryLayout<Int>.stride)
|
||||||
|
if LibmtorrentserverStart(config, mPort, &error){
|
||||||
|
result(mPort.pointee)
|
||||||
|
}else{
|
||||||
|
result(FlutterError(code: "ERROR", message: error.debugDescription, details: nil))
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
result(FlutterMethodNotImplemented)
|
result(FlutterMethodNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -863,7 +863,7 @@ class TorrentLibrary {
|
||||||
late final __FCmulcr =
|
late final __FCmulcr =
|
||||||
__FCmulcrPtr.asFunction<_Fcomplex Function(_Fcomplex, double)>();
|
__FCmulcrPtr.asFunction<_Fcomplex Function(_Fcomplex, double)>();
|
||||||
|
|
||||||
void Start(
|
Start_return Start(
|
||||||
ffi.Pointer<ffi.Char> mcfg,
|
ffi.Pointer<ffi.Char> mcfg,
|
||||||
) {
|
) {
|
||||||
return _Start(
|
return _Start(
|
||||||
|
|
@ -872,10 +872,10 @@ class TorrentLibrary {
|
||||||
}
|
}
|
||||||
|
|
||||||
late final _StartPtr =
|
late final _StartPtr =
|
||||||
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Char>)>>(
|
_lookup<ffi.NativeFunction<Start_return Function(ffi.Pointer<ffi.Char>)>>(
|
||||||
'Start');
|
'Start');
|
||||||
late final _Start =
|
late final _Start =
|
||||||
_StartPtr.asFunction<void Function(ffi.Pointer<ffi.Char>)>();
|
_StartPtr.asFunction<Start_return Function(ffi.Pointer<ffi.Char>)>();
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef va_list = ffi.Pointer<ffi.Char>;
|
typedef va_list = ffi.Pointer<ffi.Char>;
|
||||||
|
|
@ -959,6 +959,14 @@ typedef GoInt = GoInt64;
|
||||||
typedef GoInt64 = ffi.LongLong;
|
typedef GoInt64 = ffi.LongLong;
|
||||||
typedef DartGoInt64 = int;
|
typedef DartGoInt64 = int;
|
||||||
|
|
||||||
|
/// Return type for Start
|
||||||
|
final class Start_return extends ffi.Struct {
|
||||||
|
@GoInt()
|
||||||
|
external int r0;
|
||||||
|
|
||||||
|
external ffi.Pointer<ffi.Char> r1;
|
||||||
|
}
|
||||||
|
|
||||||
const int _VCRT_COMPILER_PREPROCESSOR = 1;
|
const int _VCRT_COMPILER_PREPROCESSOR = 1;
|
||||||
|
|
||||||
const int _SAL_VERSION = 20;
|
const int _SAL_VERSION = 20;
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,13 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern __declspec(dllexport) void Start(char* mcfg);
|
|
||||||
|
/* Return type for Start */
|
||||||
|
struct Start_return {
|
||||||
|
GoInt r0;
|
||||||
|
char* r1;
|
||||||
|
};
|
||||||
|
extern __declspec(dllexport) struct Start_return Start(char* mcfg);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:async';
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
|
@ -10,7 +11,16 @@ import 'generated_bindings.dart';
|
||||||
/// For very short-lived functions, it is fine to call them on the main isolate.
|
/// For very short-lived functions, it is fine to call them on the main isolate.
|
||||||
/// They will block the Dart execution while running the native function, so
|
/// They will block the Dart execution while running the native function, so
|
||||||
/// only do this for native functions which are guaranteed to be short-lived.
|
/// only do this for native functions which are guaranteed to be short-lived.
|
||||||
void start(String mcfg) => _bindings.Start(mcfg.toNativeUtf8().cast());
|
Future<int> start(String mcfg) async {
|
||||||
|
var completer = Completer<int>();
|
||||||
|
var res = _bindings.Start(mcfg.toNativeUtf8().cast());
|
||||||
|
if (res.r1 != nullptr) {
|
||||||
|
completer.completeError(Exception(res.r1.cast<Utf8>().toDartString()));
|
||||||
|
} else {
|
||||||
|
completer.complete(res.r0);
|
||||||
|
}
|
||||||
|
return completer.future;
|
||||||
|
}
|
||||||
|
|
||||||
const String _libName = 'libmtorrentserver';
|
const String _libName = 'libmtorrentserver';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -159,6 +159,10 @@ class Settings {
|
||||||
|
|
||||||
int? aniSkipTimeoutLength;
|
int? aniSkipTimeoutLength;
|
||||||
|
|
||||||
|
String? btServerAddress;
|
||||||
|
|
||||||
|
int? btServerPort;
|
||||||
|
|
||||||
Settings(
|
Settings(
|
||||||
{this.id = 227,
|
{this.id = 227,
|
||||||
this.displayType = DisplayType.compactGrid,
|
this.displayType = DisplayType.compactGrid,
|
||||||
|
|
@ -228,7 +232,9 @@ class Settings {
|
||||||
this.updateProgressAfterReading = true,
|
this.updateProgressAfterReading = true,
|
||||||
this.enableAniSkip,
|
this.enableAniSkip,
|
||||||
this.enableAutoSkip,
|
this.enableAutoSkip,
|
||||||
this.aniSkipTimeoutLength});
|
this.aniSkipTimeoutLength,
|
||||||
|
this.btServerAddress = "127.0.0.1",
|
||||||
|
this.btServerPort});
|
||||||
|
|
||||||
Settings.fromJson(Map<String, dynamic> json) {
|
Settings.fromJson(Map<String, dynamic> json) {
|
||||||
animatePageTransitions = json['animatePageTransitions'];
|
animatePageTransitions = json['animatePageTransitions'];
|
||||||
|
|
@ -354,6 +360,8 @@ class Settings {
|
||||||
enableAniSkip = json['enableAniSkip'];
|
enableAniSkip = json['enableAniSkip'];
|
||||||
enableAutoSkip = json['enableAutoSkip'];
|
enableAutoSkip = json['enableAutoSkip'];
|
||||||
aniSkipTimeoutLength = json['aniSkipTimeoutLength'];
|
aniSkipTimeoutLength = json['aniSkipTimeoutLength'];
|
||||||
|
btServerAddress = json['btServerAddress'];
|
||||||
|
btServerPort = json['btServerPort'];
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {
|
Map<String, dynamic> toJson() => {
|
||||||
|
|
@ -447,7 +455,9 @@ class Settings {
|
||||||
'updateProgressAfterReading': updateProgressAfterReading,
|
'updateProgressAfterReading': updateProgressAfterReading,
|
||||||
'enableAniSkip': enableAniSkip,
|
'enableAniSkip': enableAniSkip,
|
||||||
'enableAutoSkip': enableAutoSkip,
|
'enableAutoSkip': enableAutoSkip,
|
||||||
'aniSkipTimeoutLength': aniSkipTimeoutLength
|
'aniSkipTimeoutLength': aniSkipTimeoutLength,
|
||||||
|
'btServerAddress': btServerAddress,
|
||||||
|
'btServerPort': btServerPort
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -2,6 +2,8 @@ import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:mangayomi/main.dart';
|
||||||
|
import 'package:mangayomi/models/settings.dart';
|
||||||
import 'package:mangayomi/models/video.dart';
|
import 'package:mangayomi/models/video.dart';
|
||||||
import 'package:mangayomi/providers/storage_provider.dart';
|
import 'package:mangayomi/providers/storage_provider.dart';
|
||||||
import 'package:mangayomi/utils/extensions/string_extensions.dart';
|
import 'package:mangayomi/utils/extensions/string_extensions.dart';
|
||||||
|
|
@ -11,9 +13,6 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
part 'torrent_server.g.dart';
|
part 'torrent_server.g.dart';
|
||||||
|
|
||||||
class MTorrentServer {
|
class MTorrentServer {
|
||||||
final _baseUrl =
|
|
||||||
Platform.isLinux ? "http://127.0.0.1:8090" : "http://127.0.0.1:3535";
|
|
||||||
|
|
||||||
Future<bool> removeTorrent(String? inforHash) async {
|
Future<bool> removeTorrent(String? inforHash) async {
|
||||||
if (inforHash == null || inforHash.isEmpty) return false;
|
if (inforHash == null || inforHash.isEmpty) return false;
|
||||||
try {
|
try {
|
||||||
|
|
@ -58,22 +57,20 @@ class MTorrentServer {
|
||||||
Future<(List<Video>, String?)> getTorrentPlaylist(String url) async {
|
Future<(List<Video>, String?)> getTorrentPlaylist(String url) async {
|
||||||
final isRunning = await check();
|
final isRunning = await check();
|
||||||
if (!isRunning) {
|
if (!isRunning) {
|
||||||
final address =
|
|
||||||
_baseUrl.replaceAll("http://", "").replaceAll("https://", "");
|
|
||||||
final path = (await StorageProvider().getBtDirectory())!.path;
|
final path = (await StorageProvider().getBtDirectory())!.path;
|
||||||
final config = jsonEncode({"path": path, "address": address});
|
final config = jsonEncode({"path": path, "address": "127.0.0.1:0"});
|
||||||
|
int port = 0;
|
||||||
if (Platform.isAndroid || Platform.isIOS) {
|
if (Platform.isAndroid || Platform.isIOS) {
|
||||||
const channel =
|
const channel =
|
||||||
MethodChannel('com.kodjodevf.mangayomi.libmtorrentserver');
|
MethodChannel('com.kodjodevf.mangayomi.libmtorrentserver');
|
||||||
channel.invokeMethod('start', {"config": config});
|
port = await channel.invokeMethod('start', {"config": config});
|
||||||
} else {
|
} else {
|
||||||
await Isolate.run(() async {
|
port = await Isolate.run(() async {
|
||||||
libmtorrentserver_ffi.start(config);
|
return libmtorrentserver_ffi.start(config);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
_setBtServerPort(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isMagnet = !url.startsWith("http");
|
bool isMagnet = !url.startsWith("http");
|
||||||
String finalUrl = "";
|
String finalUrl = "";
|
||||||
String? infohash;
|
String? infohash;
|
||||||
|
|
@ -99,6 +96,18 @@ class MTorrentServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String get _baseUrl {
|
||||||
|
final settings = isar.settings.getSync(227);
|
||||||
|
final port = settings!.btServerPort ?? 0;
|
||||||
|
final address = settings.btServerAddress ?? "127.0.0.1";
|
||||||
|
return "http://$address:$port";
|
||||||
|
}
|
||||||
|
|
||||||
|
void _setBtServerPort(int newPort) {
|
||||||
|
isar.writeTxnSync(() => isar.settings
|
||||||
|
.putSync(isar.settings.getSync(227)!..btServerPort = newPort));
|
||||||
|
}
|
||||||
|
|
||||||
@riverpod
|
@riverpod
|
||||||
Future<bool> mTorrentIsRunning(MTorrentIsRunningRef ref) async {
|
Future<bool> mTorrentIsRunning(MTorrentIsRunningRef ref) async {
|
||||||
return await MTorrentServer().check();
|
return await MTorrentServer().check();
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,13 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern void Start(char* mcfg);
|
|
||||||
|
/* Return type for Start */
|
||||||
|
struct Start_return {
|
||||||
|
GoInt r0;
|
||||||
|
char* r1;
|
||||||
|
};
|
||||||
|
extern struct Start_return Start(char* mcfg);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -74,7 +74,13 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern __declspec(dllexport) void Start(char* mcfg);
|
|
||||||
|
/* Return type for Start */
|
||||||
|
struct Start_return {
|
||||||
|
GoInt r0;
|
||||||
|
char* r1;
|
||||||
|
};
|
||||||
|
extern __declspec(dllexport) struct Start_return Start(char* mcfg);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue