mirror of
https://github.com/kodjodevf/mangayomi.git
synced 2026-01-11 22:40:36 +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) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
val taskQueue =
|
||||
flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()
|
||||
MethodChannel(
|
||||
flutterEngine.dartExecutor.binaryMessenger,
|
||||
"com.kodjodevf.mangayomi.libmtorrentserver",
|
||||
StandardMethodCodec.INSTANCE,
|
||||
taskQueue
|
||||
flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()
|
||||
).setMethodCallHandler { call, result ->
|
||||
when (call.method) {
|
||||
"start" -> {
|
||||
val config = call.argument<String>("config")
|
||||
Libmtorrentserver.start(config)
|
||||
result.success("ok")
|
||||
try {
|
||||
val port = Libmtorrentserver.start(config)
|
||||
result.success(port)
|
||||
} catch (e: Exception) {
|
||||
result.error("ERROR", e.message, null)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
|
|
|
|||
|
|
@ -1,17 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
"encoding/json"
|
||||
"server"
|
||||
)
|
||||
|
||||
import "C"
|
||||
|
||||
//export Start
|
||||
func Start(mcfg *C.char) {
|
||||
func Start(mcfg *C.char) (int, *C.char) {
|
||||
var config server.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() {}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,8 @@ import (
|
|||
)
|
||||
|
||||
//export Start
|
||||
func Start(mcfg string) {
|
||||
func Start(mcfg string) (int, error) {
|
||||
var config server.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 torrentcliCfg *torrent.ClientConfig
|
||||
|
||||
func Start(config *Config) {
|
||||
func Start(config *Config) (int, error) {
|
||||
|
||||
torrentcliCfg = torrent.NewDefaultClientConfig()
|
||||
|
||||
|
|
@ -70,9 +70,21 @@ func Start(config *Config) {
|
|||
AllowCredentials: true,
|
||||
})
|
||||
|
||||
log.Printf("[INFO] Listening on %s\n", config.Address)
|
||||
log.Fatalln(http.ListenAndServe(config.Address, c.Handler(mux)))
|
||||
listener, err := net.Listen("tcp", config.Address)
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,6 @@
|
|||
#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
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
|||
#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
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
|||
#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
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
|||
#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
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
|||
#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
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -11,6 +11,6 @@
|
|||
#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
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -16,7 +16,13 @@ import Libmtorrentserver
|
|||
case "start":
|
||||
let args = call.arguments as? Dictionary<String, Any>
|
||||
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:
|
||||
result(FlutterMethodNotImplemented)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -863,7 +863,7 @@ class TorrentLibrary {
|
|||
late final __FCmulcr =
|
||||
__FCmulcrPtr.asFunction<_Fcomplex Function(_Fcomplex, double)>();
|
||||
|
||||
void Start(
|
||||
Start_return Start(
|
||||
ffi.Pointer<ffi.Char> mcfg,
|
||||
) {
|
||||
return _Start(
|
||||
|
|
@ -872,10 +872,10 @@ class TorrentLibrary {
|
|||
}
|
||||
|
||||
late final _StartPtr =
|
||||
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Char>)>>(
|
||||
_lookup<ffi.NativeFunction<Start_return Function(ffi.Pointer<ffi.Char>)>>(
|
||||
'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>;
|
||||
|
|
@ -959,6 +959,14 @@ typedef GoInt = GoInt64;
|
|||
typedef GoInt64 = ffi.LongLong;
|
||||
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 _SAL_VERSION = 20;
|
||||
|
|
|
|||
|
|
@ -74,7 +74,13 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
|||
extern "C" {
|
||||
#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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import 'dart:async';
|
||||
import 'dart:ffi';
|
||||
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.
|
||||
/// 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.
|
||||
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';
|
||||
|
||||
|
|
|
|||
|
|
@ -159,6 +159,10 @@ class Settings {
|
|||
|
||||
int? aniSkipTimeoutLength;
|
||||
|
||||
String? btServerAddress;
|
||||
|
||||
int? btServerPort;
|
||||
|
||||
Settings(
|
||||
{this.id = 227,
|
||||
this.displayType = DisplayType.compactGrid,
|
||||
|
|
@ -228,7 +232,9 @@ class Settings {
|
|||
this.updateProgressAfterReading = true,
|
||||
this.enableAniSkip,
|
||||
this.enableAutoSkip,
|
||||
this.aniSkipTimeoutLength});
|
||||
this.aniSkipTimeoutLength,
|
||||
this.btServerAddress = "127.0.0.1",
|
||||
this.btServerPort});
|
||||
|
||||
Settings.fromJson(Map<String, dynamic> json) {
|
||||
animatePageTransitions = json['animatePageTransitions'];
|
||||
|
|
@ -354,6 +360,8 @@ class Settings {
|
|||
enableAniSkip = json['enableAniSkip'];
|
||||
enableAutoSkip = json['enableAutoSkip'];
|
||||
aniSkipTimeoutLength = json['aniSkipTimeoutLength'];
|
||||
btServerAddress = json['btServerAddress'];
|
||||
btServerPort = json['btServerPort'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
|
|
@ -447,7 +455,9 @@ class Settings {
|
|||
'updateProgressAfterReading': updateProgressAfterReading,
|
||||
'enableAniSkip': enableAniSkip,
|
||||
'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:isolate';
|
||||
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/providers/storage_provider.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';
|
||||
|
||||
class MTorrentServer {
|
||||
final _baseUrl =
|
||||
Platform.isLinux ? "http://127.0.0.1:8090" : "http://127.0.0.1:3535";
|
||||
|
||||
Future<bool> removeTorrent(String? inforHash) async {
|
||||
if (inforHash == null || inforHash.isEmpty) return false;
|
||||
try {
|
||||
|
|
@ -58,22 +57,20 @@ class MTorrentServer {
|
|||
Future<(List<Video>, String?)> getTorrentPlaylist(String url) async {
|
||||
final isRunning = await check();
|
||||
if (!isRunning) {
|
||||
final address =
|
||||
_baseUrl.replaceAll("http://", "").replaceAll("https://", "");
|
||||
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) {
|
||||
const channel =
|
||||
MethodChannel('com.kodjodevf.mangayomi.libmtorrentserver');
|
||||
channel.invokeMethod('start', {"config": config});
|
||||
port = await channel.invokeMethod('start', {"config": config});
|
||||
} else {
|
||||
await Isolate.run(() async {
|
||||
libmtorrentserver_ffi.start(config);
|
||||
port = await Isolate.run(() async {
|
||||
return libmtorrentserver_ffi.start(config);
|
||||
});
|
||||
}
|
||||
_setBtServerPort(port);
|
||||
}
|
||||
|
||||
bool isMagnet = !url.startsWith("http");
|
||||
String finalUrl = "";
|
||||
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
|
||||
Future<bool> mTorrentIsRunning(MTorrentIsRunningRef ref) async {
|
||||
return await MTorrentServer().check();
|
||||
|
|
|
|||
|
|
@ -74,7 +74,13 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
|||
extern "C" {
|
||||
#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
|
||||
}
|
||||
|
|
|
|||
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" {
|
||||
#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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue