Skip to content

Commit

Permalink
socks5功能实现,UI待更新
Browse files Browse the repository at this point in the history
  • Loading branch information
shiguanghuxian committed Nov 28, 2021
1 parent 4d3ad34 commit df04520
Show file tree
Hide file tree
Showing 17 changed files with 727 additions and 16 deletions.
9 changes: 9 additions & 0 deletions lib/assets/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
"server_tooltip": "Click restart DNS to save this configuration",
"tray_dns_proxy": "DNS Proxy"
},
"socks5": {
"title": "Local Socks5 proxy",
"start": "Start",
"stop": "Stop",
"create_ca": "Generate certificate",
"open_ca_path": "Open certificate directory",
"create_ca_message": "Please open the certificate directory, install the certificate and trust it",
"open_ca_path_btn": "Open"
},
"settings": {
"title": "Settings",
"theme_title": "Theme",
Expand Down
9 changes: 9 additions & 0 deletions lib/assets/locale/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
"server_tooltip": "点击重启dns后会保存此配置",
"tray_dns_proxy": "DNS代理"
},
"socks5": {
"title": "Socks5代理",
"start": "启动",
"stop": "停止",
"create_ca": "生成证书",
"open_ca_path": "打开证书目录",
"create_ca_message": "请打开证书目录,安装证书并信任",
"open_ca_path_btn": "打开目录"
},
"settings": {
"title": "设置",
"theme_title": "主题:",
Expand Down
24 changes: 24 additions & 0 deletions lib/golib/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-----BEGIN CERTIFICAET-----
MIID/zCCAuegAwIBAgIQSlmBzXZHfh37jTz0xO/n+jANBgkqhkiG9w0BAQsFADB6
MQswCQYDVQQGEwJDTjEQMA4GA1UECBMHQmVpamluZzEQMA4GA1UEBxMHQmVpamlu
ZzEXMBUGA1UEChMOc2hpZ3VhbmdodXhpYW4xFzAVBgNVBAsTDnNoaWd1YW5naHV4
aWFuMRUwEwYDVQQDEwxHaXRIdWIgUHJveHkwHhcNMjExMTI0MTA1ODIxWhcNMjIx
MTI0MTA1ODIxWjB6MQswCQYDVQQGEwJDTjEQMA4GA1U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H
rXY7aQTkHRrUje5T2hYPhPeU4A==
-----END CERTIFICAET-----
37 changes: 37 additions & 0 deletions lib/golib/golib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@ final GetErr getErr = _lib
.lookup<NativeFunction<Pointer<Int8> Function()>>('GetErr')
.asFunction();

/* 以下socks5代理 */

/// 启动socks5服务
typedef Socks5Start = void Function();
final Socks5Start socks5Start =
_lib.lookup<NativeFunction<Void Function()>>('Socks5Start').asFunction();

/// 停止socks5服务
typedef Socks5Stop = void Function();
final Socks5Stop socks5Stop =
_lib.lookup<NativeFunction<Void Function()>>('Socks5Stop').asFunction();

/// 获取当前socks5服务是否启动 1启动 0未启动
typedef Socks5GetIsStart = int Function();
final Socks5GetIsStart socks5GetIsStart = _lib
.lookup<NativeFunction<Int32 Function()>>('Socks5GetIsStart')
.asFunction();

/// 获取socks5启动或停止错误
typedef Socks5GetErr = Pointer<Int8> Function();
final Socks5GetErr socks5GetErr = _lib
.lookup<NativeFunction<Pointer<Int8> Function()>>('Socks5GetErr')
.asFunction();

/// 设置证书根路径
typedef Socks5SetCertPath = void Function(Pointer<GoString>);
final Socks5SetCertPath socks5SetCertPath = _lib
.lookup<NativeFunction<Void Function(Pointer<GoString>)>>(
'Socks5SetCertPath')
.asFunction();

/// 获取socks5启动或停止错误
typedef Socks5GenCaCert = Pointer<Int8> Function();
final Socks5GenCaCert socks5GenCaCert = _lib
.lookup<NativeFunction<Pointer<Int8> Function()>>('Socks5GenCaCert')
.asFunction();

// 根据系统平台加载不同的库文件
String getLibPath() {
if (Platform.isMacOS) {
Expand Down
44 changes: 44 additions & 0 deletions lib/golib/golib.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

/* DNS服务相关,暂不改名,socks5服务增加前缀 */

//export Start
func Start() {
go DnsProxyHandle.Start()
Expand Down Expand Up @@ -77,6 +79,48 @@ func SetPublicDnsServer(str *string) {
DnsProxyHandle.SetPublicDnsServer(addrs)
}

/* socks5代理 */

//export Socks5Start
func Socks5Start() {
go Socks5ProxyHandle.Start()
}

//export Socks5Stop
func Socks5Stop() {
Socks5ProxyHandle.Stop()
}

//export Socks5GetIsStart
func Socks5GetIsStart() int32 {
if Socks5ProxyHandle.GetIsStart() {
return 1
}
return 0
}

//export Socks5GetErr
func Socks5GetErr() *C.char {
errStr := Socks5ProxyHandle.GetErr()
return C.CString(errStr)
}

//export Socks5GenCaCert
func Socks5GenCaCert() *C.char {
err := Socks5ProxyHandle.GenCaCert()
outStr := ""
if err != nil {
outStr = err.Error()
}
return C.CString(outStr)
}

//export Socks5SetCertPath
func Socks5SetCertPath(str *string) {
log.Println("证书根路径", *str)
Socks5ProxyHandle.SetCertPath(*str)
}

func main() {
// DnsProxyHandle.publicDnsServer = []string{"8.8.8.8"}
// DnsProxyHandle.Start()
Expand Down
27 changes: 27 additions & 0 deletions lib/golib/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAv+krnOsoykqJdh4NT5YxN39nL9Lo67RhdaD7b3YBKH6kgUZ8
MvJjXCoUV7jzYk1YVQv0Ry/BVg8MS3y0+9uEKJHHxFI43ZM7j7mLYQQ93AgZddSl
L6FuZmLM2MdjQjD5hZmMKm099hOmfUP7N1LPkjqJx4QhOY+IkWBN8qnSlL6ccD2Z
rSTy6Hc1LcVLnHpp5Y2e/DvmbFED/lf8Vmaef3bzE1WNIeQJK1BGmuPGJTKiw9Uj
jvg0xy4lWfjmI0qiae8iJ3Fl04zbTLlm/AJ8XzQpItpgF0Sl970sPIcls5qjhF8o
kBBd85+H3wlllpx1ussdbAweqTiYyNt5+9Z2lwIDAQABAoIBAQCJIyZ3S9QqOnlg
XrlEVh91eSVTl3O6e3vpgtAqJjlNprIHb0bxYSR/h9JhnVjOtlaY76Hv/MA7ryUq
DEOIdqVKGGKYd+kK7VtyqTRgb5eE1ITdHser8b/iile30UOs2TJ1tfPjnYzZe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J98biahO3++xV2nvDTOrDnixevRBQm9Qd3Ig==
-----END RSA PRIVATE KEY-----
20 changes: 13 additions & 7 deletions lib/golib/libdns.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package golib/lib/golib */
/* package command-line-arguments */


#line 1 "cgo-builtin-export-prolog"
Expand Down Expand Up @@ -68,12 +68,18 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
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);
extern void Start();
extern void Stop();
extern GoInt32 GetIsStart();
extern char* GetErr();
extern void SetAddressBook(GoString* str);
extern void SetPublicDnsServer(GoString* str);
extern void Socks5Start();
extern void Socks5Stop();
extern GoInt32 Socks5GetIsStart();
extern char* Socks5GetErr();
extern char* Socks5GenCaCert();
extern void Socks5SetCertPath(GoString* str);

#ifdef __cplusplus
}
Expand Down
52 changes: 45 additions & 7 deletions lib/golib/socks5_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,58 @@ const (
caKey = "key.pem"
)

func init() {
func (sp *Socks5Proxy) initCa() {
// read ca cert
certPEMBlock, err := ioutil.ReadFile(caCert)
certPEMBlock, err := ioutil.ReadFile(strings.Join([]string{sp.certPath, caCert}, string(os.PathSeparator)))
if err != nil {
log.Fatal(err)
log.Println("证书错误1", err)
return
}
certDERBlock, _ := pem.Decode(certPEMBlock)
caParent, err = x509.ParseCertificate(certDERBlock.Bytes)
if err != nil {
log.Fatal(err)
log.Println("证书错误2", err)
return
}

keyPEMBlock, err := ioutil.ReadFile(caKey)
keyPEMBlock, err := ioutil.ReadFile(strings.Join([]string{sp.certPath, caKey}, string(os.PathSeparator)))
if err != nil {
log.Fatal(err)
log.Println("证书错误3", err)
return
}
keyDERBlock, _ := pem.Decode(keyPEMBlock)
caPriKey, err = x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
if err != nil {
log.Fatal(err)
log.Println("证书错误5", err)
return
}
}

func init() {
// 初始化定时更新ip
go func() {
t := time.NewTicker(10 * 60 * time.Second)
for {
<-t.C
Socks5ProxyHandle.cronIpConnTime()
}
}()

}

// GetIsStart 获取启动状态
func (sp *Socks5Proxy) GetIsStart() bool {
return sp.isStart
}

// GetErr 获取启动或停止错误
func (sp *Socks5Proxy) GetErr() string {
if sp.err == nil {
return ""
}
return sp.err.Error()
}

// SetPublicDnsServer 设置公网dns服务
func (sp *Socks5Proxy) SetPublicDnsServer(addrs []string) {
sp.publicDnsServer = addrs
Expand All @@ -140,6 +169,7 @@ func (sp *Socks5Proxy) SetSpeedUpHosts(speedUpHosts []string) {
// 设置证书生成根路径
func (sp *Socks5Proxy) SetCertPath(certPath string) {
sp.certPath = certPath
sp.initCa()
}

// 获取主机对于ip列表,从多个dns获取ip
Expand Down Expand Up @@ -288,7 +318,14 @@ func (sp *Socks5Proxy) sortIpsConnTime(host string) {
v.ConnTime = connTime
}
}
// <0的往后排
sort.Slice(hostIpAddrs, func(i, j int) bool {
if hostIpAddrs[i].ConnTime < 0 {
return false
}
if hostIpAddrs[j].ConnTime < 0 {
return true
}
return hostIpAddrs[i].ConnTime < hostIpAddrs[j].ConnTime
})
sp.hostIpAddrsLock.Lock()
Expand Down Expand Up @@ -541,6 +578,7 @@ func (sp *Socks5Proxy) GenCaCert() error {
if err != nil {
return err
}
sp.initCa()
return nil
}

Expand Down
2 changes: 0 additions & 2 deletions lib/views/home/widgets/hosts_list_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import 'package:hosts_manage/components/macos_alert_dialog.dart';
import 'package:hosts_manage/i18n/i18n.dart';
import 'package:hosts_manage/models/hosts_info_model.dart';
import 'package:hosts_manage/store/store.dart';
import 'package:hosts_manage/views/common/common.dart';
import 'package:hosts_manage/views/home/bloc/home_bloc.dart';
import 'package:hosts_manage/views/home/widgets/home_show_sandbox.dart';
import 'package:macos_ui/macos_ui.dart';

// hosts配置列表中一个元素
Expand Down
14 changes: 14 additions & 0 deletions lib/views/main/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:hosts_manage/views/about/about_page.dart';
import 'package:hosts_manage/views/dns/dns_page.dart';
import 'package:hosts_manage/views/home/home_page.dart';
import 'package:hosts_manage/views/settings/settings_page.dart';
import 'package:hosts_manage/views/socks5/socks5_page.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:proste_indexed_stack/proste_indexed_stack.dart';
import 'package:url_launcher/url_launcher.dart';
Expand Down Expand Up @@ -41,6 +42,9 @@ class _MainPageState extends State<MainPage> {
IndexedStackChild(
child: const DNSPage(),
),
IndexedStackChild(
child: const Socks5Page(),
),
IndexedStackChild(
child: const SettingsPage(),
),
Expand Down Expand Up @@ -110,6 +114,16 @@ class _MainPageState extends State<MainPage> {
),
label: Text(lang.get('dns.title')),
),
SidebarItem(
leading: MacosIcon(
CupertinoIcons.lock_circle,
color: pageIndex == 1
? Colors.white
: MacosTheme.of(context).typography.largeTitle.color,
size: 18,
),
label: Text(lang.get('socks5.title')),
),
SidebarItem(
leading: MacosIcon(
CupertinoIcons.settings,
Expand Down
26 changes: 26 additions & 0 deletions lib/views/socks5/bloc/socks5_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

part 'socks5_event.dart';
part 'socks5_state.dart';

class Socks5Bloc extends Bloc<Socks5Event, Socks5State> {
Socks5Bloc() : super(const Socks5State());

@override
Stream<Socks5State> mapEventToState(Socks5Event event) async* {
if (event is ChangeLocalSocks5AddrEvent) {
yield _mapChangeLocalSocks5Addr(event, state);
}
}

// 设置本机dns代理地址
Socks5State _mapChangeLocalSocks5Addr(
ChangeLocalSocks5AddrEvent event, Socks5State state) {
return state.copyWith(
localSocks5Addr: event.localSocks5Addr,
);
}

}
Loading

0 comments on commit df04520

Please sign in to comment.