This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
216 lines (184 loc) · 3.64 KB
/
config.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"encoding/json"
"net"
"os"
"path/filepath"
"runtime"
"time"
)
type Config struct {
// DaemonPath string
DaemonControlAddr string
DownloadsPath string
ListenAddr string
}
func LoadConfig(dir string) (*Config, error) {
path := dir + "/config.json"
_, err := os.Stat(path)
if os.IsNotExist(err) {
cfg := &Config{
DaemonControlAddr: "127.0.0.1:15555",
DownloadsPath: downloadsPath(),
ListenAddr: ":13333",
}
ip, seed := checkCanSeed()
if seed {
cfg.ListenAddr = ip + cfg.ListenAddr
}
err = cfg.SaveConfig(dir)
if err != nil {
return nil, err
}
return cfg, nil
} else if err == nil {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg Config
err = json.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
return nil, err
}
func (cfg *Config) SaveConfig(dir string) error {
path := dir + "/config.json"
data, err := json.MarshalIndent(cfg, "", "\t")
if err != nil {
return err
}
err = os.WriteFile(path, data, 0766)
if err != nil {
return err
}
return nil
}
func downloadsPath() string {
homeDir, err := os.UserHomeDir()
if err != nil {
wd, err := os.Getwd()
if err != nil {
return "./"
}
return wd
}
return filepath.Join(homeDir, "Downloads")
}
func checkIPAddress(ip string) string {
p := net.ParseIP(ip)
if p == nil {
println("bad ip", len(p))
return ""
}
p = p.To4()
if p == nil {
println("bad ip, not v4", len(p))
return ""
}
println("ip ok", p.String())
return p.String()
}
func checkCanSeed() (string, bool) {
ch := make(chan bool, 1)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
ip := ""
go func() {
defer func() {
ch <- ip != ""
}()
listen, err := net.Listen("tcp", "0.0.0.0:18889")
if err != nil {
println("listen err", err.Error())
return
}
defer listen.Close()
conn, err := listen.Accept()
if err != nil {
println("accept err", err.Error())
return
}
ipData := make([]byte, 256)
n, err := conn.Read(ipData)
if err != nil {
println("read err", err.Error())
return
}
ip = string(ipData[:n])
println("got from server:", "'"+ip+"'")
ip = checkIPAddress(ip)
_ = conn.Close()
}()
ips, err := net.LookupIP("tonutils.com")
if err != nil || len(ips) == 0 {
return "", false
}
println("port checker at:", ips[0].String())
conn, err := net.Dial("tcp", ips[0].String()+":9099")
if err != nil {
return "", false
}
_, err = conn.Write([]byte("ME"))
if err != nil {
return "", false
}
ok := false
select {
case k := <-ch:
ok = k
println("port result:", ok, "public ip:", ip)
case <-ctx.Done():
println("port check timeout, will use client mode")
}
return ip, ok
}
var CustomRoot = ""
func PrepareRootPath() (string, error) {
if CustomRoot != "" {
return CustomRoot, nil
}
switch runtime.GOOS {
case "darwin":
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
path := home + "/Library/Application Support/org.tonutils.tontorrent"
_, err = os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(path, 0766)
}
if err != nil {
return "", err
}
}
return path, nil
case "windows":
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
path := home + "\\AppData\\Roaming\\TON Torrent.exe"
_, err = os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(path, 0766)
}
if err != nil {
return "", err
}
}
return path, nil
}
ex, err := os.Executable()
if err != nil {
panic(err)
}
return filepath.Dir(ex), nil
}