Skip to content

Commit

Permalink
windows兼容,UI视觉差一些
Browse files Browse the repository at this point in the history
  • Loading branch information
shiguanghuxian01 committed Nov 20, 2021
1 parent 7d816fd commit a629490
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 95 deletions.
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 h1:cEhElsAv9LUt9ZUUocxzWe05oFLVd+AA2nstydTeI8g=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2 changes: 1 addition & 1 deletion lib/golib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ linux:
go build -o ${LIB_NAME}.so -buildmode=c-shared *.go

windows:
go build -o ${LIB_NAME}.dll -buildmode=c-shared *.go
go build -o libdns.dll -buildmode=c-shared .

clean:
@rm -f ./${LIB_NAME}*
Expand Down
7 changes: 5 additions & 2 deletions lib/golib/golib.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:ffi';
import 'dart:io';
import 'package:call/call.dart';
import 'package:ffi/ffi.dart';
import 'package:hosts_manage/golib/godart.dart';

/* 接入C函数,用于dart语言调用 */

// 加载动态库
DynamicLibrary _lib = DynamicLibrary.open(getLibPath());
DynamicLibrary _lib = Platform.isWindows
? getDyLibModule(getLibPath())
: DynamicLibrary.open(getLibPath());

/// 启动服务
typedef Start = void Function();
Expand Down Expand Up @@ -47,7 +50,7 @@ String getLibPath() {
if (Platform.isMacOS) {
return 'libdns.dylib';
} else if (Platform.isWindows) {
return 'libdns.dll';
return 'lib/golib/libdns.dll';
}
return 'libdns.so';
}
Binary file added lib/golib/libdns.dll
Binary file not shown.
80 changes: 80 additions & 0 deletions lib/golib/libdns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package golib/lib/golib */


#line 1 "cgo-builtin-export-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif

/* Start of preamble from import "C" comments. */




/* End of preamble from import "C" comments. */


/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue. */

#ifdef __cplusplus
extern "C" {
#endif

extern __declspec(dllexport) void Start();
extern __declspec(dllexport) void Stop();
extern __declspec(dllexport) GoInt32 GetIsStart();
extern __declspec(dllexport) char* GetErr();
extern __declspec(dllexport) void SetAddressBook(GoString* str);
extern __declspec(dllexport) void SetPublicDnsServer(GoString* str);

#ifdef __cplusplus
}
#endif
10 changes: 6 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class _MyAppState extends State<MyApp> {

final store = Store<ZState>(
appReducer,
initialState: ZState(locale: ModelConst.zhLang, lang: I18N()),
initialState: ZState(
locale: ModelConst.zhLang,
),
);

final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Expand All @@ -76,9 +78,9 @@ class _MyAppState extends State<MyApp> {
store.dispatch(UpdateLangAction(lang));
store.dispatch(UpdateThemeAction(theme));
store.dispatch(UpdateAutoDNSAction(autoDNS));
setState(() {
lang = lang;
});
// setState(() {
// lang = lang;
// });
}

@override
Expand Down
6 changes: 3 additions & 3 deletions lib/views/home/bloc/home_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
''');
Expand All @@ -235,7 +235,7 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
name: backupName,
check: true,
));
hostsFile.writeAsString(json.encode(hostsList)); // 写入默认列表
await hostsFile.writeAsString(json.encode(hostsList)); // 写入默认列表
}
} catch (e) {
log('初始化hosts配置列表错误2 ${e.toString()}');
Expand Down
18 changes: 17 additions & 1 deletion lib/views/home/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:hosts_manage/i18n/i18n.dart';
import 'package:hosts_manage/store/lang_store.dart';
import 'package:hosts_manage/store/store.dart';
import 'package:hosts_manage/views/home/bloc/home_bloc.dart';
import 'package:hosts_manage/views/home/widgets/home_edit.dart';
Expand All @@ -23,19 +26,32 @@ class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
_initHostsList();
}

final HomeBloc _homeBloc = HomeBloc();
I18N lang;

@override
void dispose() {
super.dispose();
}

_initHostsList() {
if (!Platform.isWindows) {
return;
}
// 等半秒钟,看一下是否启动错误
Future.delayed(const Duration(milliseconds: 500), () async {
_homeBloc.add(InitHostsListEvent(lang));
StoreProvider.of<ZState>(context).dispatch(UpdateLangAction(lang));
});
}

@override
Widget build(BuildContext context) {
return StoreBuilder<ZState>(builder: (context, store) {
I18N lang = StoreProvider.of<ZState>(context).state.lang;
lang = StoreProvider.of<ZState>(context).state.lang;
_homeBloc.add(InitHostsListEvent(lang));
return BlocProvider(
create: (context) {
Expand Down
6 changes: 5 additions & 1 deletion lib/views/home/widgets/home_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ class _HomeEditState extends State<HomeEdit> {
showSave = false;
} else if (state.showHosts.startsWith(ModelConst.systemShowHosts)) {
// 显示系统hosts
hostsPath = File('/etc/hosts');
String sysHostsPath = '/etc/hosts';
if (Platform.isWindows) {
sysHostsPath = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
}
hostsPath = File(sysHostsPath);
showSave = false;
} else {
hostsPath = await getHostsConfFile(state.showHosts);
Expand Down
Loading

0 comments on commit a629490

Please sign in to comment.