Skip to content

Commit 7592ecb

Browse files
author
yiguo
committed
add desktop bin
1 parent 90a700a commit 7592ecb

File tree

9 files changed

+102
-46
lines changed

9 files changed

+102
-46
lines changed

build/app/build.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Builder(object):
1010
def __init__(self, build_dir: str):
1111
self.build_dir = build_dir
1212
self.lib_dir = os.path.join(self.build_dir, "..")
13+
self.bin_file = "xray"
1314

1415
def clean_lib_files(self, files: list[str]):
1516
for file in files:
@@ -23,7 +24,7 @@ def clean_lib_dirs(self, dirs: list[str]):
2324

2425
def download_geo(self):
2526
os.chdir(self.lib_dir)
26-
main_path = os.path.join("main", "main.go")
27+
main_path = os.path.join("download_geo", "main.go")
2728
ret = subprocess.run(["go", "run", main_path])
2829
if ret.returncode != 0:
2930
raise Exception("download_geo failed")
@@ -101,3 +102,24 @@ def reset_package_name(self, file_name: str):
101102
new_lines.append(new_line)
102103
with open(file_path, "w") as f:
103104
f.writelines(new_lines)
105+
106+
107+
def build_desktop_bin(self):
108+
output_file = os.path.join(self.lib_dir, self.bin_file)
109+
run_env = os.environ.copy()
110+
run_env["CGO_ENABLED"] = "0"
111+
112+
cmd = [
113+
"go",
114+
"build",
115+
"-trimpath",
116+
"-ldflags",
117+
"-s -w",
118+
f"-o={output_file}",
119+
"./desktop_bin",
120+
]
121+
os.chdir(self.lib_dir)
122+
print(cmd)
123+
ret = subprocess.run(cmd, env=run_env)
124+
if ret.returncode != 0:
125+
raise Exception(f"build_desktop_bin failed")

build/app/linux.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def __init__(self, build_dir: str):
1313
create_dir_if_not_exists(self.framework_dir)
1414
self.lib_file = "libXray.so"
1515
self.lib_header_file = "libXray.h"
16-
self.bin_file = "route"
1716

1817
def before_build(self):
1918
super().before_build()
@@ -24,7 +23,7 @@ def build(self):
2423
self.build_linux()
2524
self.after_build()
2625

27-
self.build_linux_bin()
26+
self.build_desktop_bin()
2827

2928
def build_linux(self):
3029
output_dir = self.framework_dir
@@ -54,22 +53,4 @@ def after_build(self):
5453
super().after_build()
5554
self.reset_files()
5655

57-
def build_linux_bin(self):
58-
output_file = os.path.join(self.lib_dir, self.bin_file)
59-
run_env = os.environ.copy()
60-
run_env["CGO_ENABLED"] = "0"
61-
62-
cmd = [
63-
"go",
64-
"build",
65-
"-trimpath",
66-
"-ldflags",
67-
"-s -w",
68-
f"-o={output_file}",
69-
"./linux_route",
70-
]
71-
os.chdir(self.lib_dir)
72-
print(cmd)
73-
ret = subprocess.run(cmd, env=run_env)
74-
if ret.returncode != 0:
75-
raise Exception(f"build_linux_bin failed")
56+

build/app/windows.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self, build_dir: str):
1313
create_dir_if_not_exists(self.framework_dir)
1414
self.lib_file = "libXray.dll"
1515
self.lib_header_file = "libXray.h"
16+
self.bin_file = "xray.exe"
1617

1718
def before_build(self):
1819
super().before_build()
@@ -23,6 +24,8 @@ def build(self):
2324
self.build_windows()
2425
self.after_build()
2526

27+
self.build_desktop_bin()
28+
2629
def build_windows(self):
2730
output_dir = self.framework_dir
2831
create_dir_if_not_exists(output_dir)

desktop_bin/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/signal"
7+
"runtime"
8+
"runtime/debug"
9+
"syscall"
10+
11+
"github.com/xtls/libxray/xray"
12+
)
13+
14+
func main() {
15+
configPath := os.Args[1]
16+
fmt.Println("configPath:", configPath)
17+
err := runXray(configPath)
18+
if err != nil {
19+
os.Exit(1)
20+
}
21+
defer xray.StopXray()
22+
// Explicitly triggering GC to remove garbage from config loading.
23+
runtime.GC()
24+
debug.FreeOSMemory()
25+
26+
{
27+
osSignals := make(chan os.Signal, 1)
28+
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
29+
<-osSignals
30+
}
31+
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@ package main
44

55
import (
66
"net"
7-
"strconv"
87

98
"github.com/vishvananda/netlink"
109
)
1110

1211
// sudo ip route add default dev tun0 metric 20
1312
// sudo ip -6 route add default dev tun0 metric 20
14-
func initIpRoute(tunName string, tunPriority string) error {
15-
priority, err := strconv.Atoi(tunPriority)
16-
if err != nil {
17-
return err
18-
}
13+
func initIpRoute(tunName string, tunPriority int) error {
1914
link, err := netlink.LinkByName(tunName)
2015
if err != nil {
2116
return err
2217
}
23-
err = addRoute(link.Attrs().Index, "0.0.0.0/0", netlink.FAMILY_V4, priority)
18+
err = addRoute(link.Attrs().Index, "0.0.0.0/0", netlink.FAMILY_V4, tunPriority)
2419
if err != nil {
2520
return err
2621
}
27-
err = addRoute(link.Attrs().Index, "::/0", netlink.FAMILY_V6, priority)
22+
err = addRoute(link.Attrs().Index, "::/0", netlink.FAMILY_V6, tunPriority)
2823
if err != nil {
2924
return err
3025
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
package main
44

5-
func initIpRoute(_ string, _ string) error {
5+
func initIpRoute(_ string, _ int) error {
66
return nil
77
}

desktop_bin/run.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
"github.com/xtls/libxray/dns"
8+
"github.com/xtls/libxray/xray"
9+
)
10+
11+
type runXrayConfig struct {
12+
// tun
13+
TunName string `json:"tunName,omitempty"`
14+
TunPriority int `json:"tunPriority,omitempty"`
15+
// dns
16+
Dns string `json:"dns,omitempty"`
17+
BindInterface string `json:"bindInterface,omitempty"`
18+
// xray
19+
DatDir string `json:"datDir,omitempty"`
20+
ConfigPath string `json:"configPath,omitempty"`
21+
}
22+
23+
func runXray(configPath string) error {
24+
configBytes, err := os.ReadFile(configPath)
25+
if err != nil {
26+
return err
27+
}
28+
var config runXrayConfig
29+
err = json.Unmarshal(configBytes, &config)
30+
if err != nil {
31+
return err
32+
}
33+
err = initIpRoute(config.TunName, config.TunPriority)
34+
if err != nil {
35+
return err
36+
}
37+
dns.InitDns(config.Dns, config.BindInterface)
38+
return xray.RunXray(config.DatDir, config.ConfigPath)
39+
}
File renamed without changes.

linux_route/main.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)