This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
forked from barlowhaydnb/tpclash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
137 lines (118 loc) · 2.94 KB
/
static.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"compress/gzip"
"embed"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
)
//go:embed static
var static embed.FS
func mkHomeDir() {
info, err := os.Stat(conf.ClashHome)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(conf.ClashHome, 0755)
if err != nil {
logrus.Fatalf("failed to create clash home dir: %v", err)
}
} else {
logrus.Fatalf("failed to open clash home dir: %v", err)
}
} else if !info.IsDir() {
logrus.Fatalf("clash home path is not a dir")
}
}
func copyFiles() {
if conf.DisableExtract {
logrus.Warn("[static] skip copy static files...")
return
}
logrus.Info("[static] copy static files...")
dirEntries, err := static.ReadDir("static")
if err != nil {
logrus.Fatal(err)
}
err = extract(static, dirEntries, "static", conf.ClashHome)
if err != nil {
logrus.Fatal(err)
}
err = chmod(filepath.Join(conf.ClashHome, "xclash"))
if err != nil {
logrus.Fatal(err)
}
err = chownR(conf.ClashHome)
if err != nil {
logrus.Fatal(err)
}
}
func extract(efs embed.FS, dirEntries []fs.DirEntry, origin, target string) error {
for _, dirEntry := range dirEntries {
info, err := dirEntry.Info()
if err != nil {
return err
}
perm := info.Mode().Perm()
if dirEntry.IsDir() {
logrus.Debugf("[static] extract -> %s %s", filepath.Join(target, dirEntry.Name()), perm.String())
err := os.MkdirAll(filepath.Join(target, dirEntry.Name()), perm)
if err != nil {
return err
}
entries, err := efs.ReadDir(filepath.Join(origin, dirEntry.Name()))
if err != nil {
return err
}
err = extract(efs, entries, filepath.Join(origin, dirEntry.Name()), filepath.Join(target, dirEntry.Name()))
if err != nil {
return err
}
} else {
sf, err := static.Open(filepath.Join(origin, dirEntry.Name()))
if err != nil {
return err
}
defer func() { _ = sf.Close() }()
logrus.Debugf("[static] extract -> %s %s", filepath.Join(target, dirEntry.Name()), perm.String())
df, err := os.OpenFile(filepath.Join(target, dirEntry.Name()), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, perm)
if err != nil {
return err
}
defer func() { _ = df.Close() }()
_, err = io.Copy(df, sf)
if err != nil {
return err
}
}
}
return nil
}
func downloadClash(u, target string) error {
resp, err := http.Get(u)
if err != nil {
return fmt.Errorf("[static] failed to download clash: %s", err)
}
defer func() { _ = resp.Body.Close() }()
r, err := gzip.NewReader(resp.Body)
if err != nil {
return fmt.Errorf("[static] failed to create gzip reader: %s", err)
}
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("[static] failed to create xclash: %s", err)
}
defer func() { _ = f.Close() }()
_, err = io.Copy(f, r)
if err != nil {
return fmt.Errorf("[static] failed to create xclash: %s", err)
}
return nil
}
func ensureClashFiles() {
mkHomeDir()
copyFiles()
}