-
Notifications
You must be signed in to change notification settings - Fork 3
/
disk.go
210 lines (197 loc) · 4.25 KB
/
disk.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"time"
"github.com/suconghou/netdisk/commands"
"github.com/suconghou/netdisk/config"
"github.com/suconghou/netdisk/middleware"
"github.com/suconghou/netdisk/route"
"github.com/suconghou/netdisk/util"
"github.com/suconghou/utilgo"
)
const (
index = "index.html"
)
var (
startTime = time.Now()
port = 6060
doc = "./"
)
var sysStatus struct {
Uptime string
GoVersion string
Hostname string
MemAllocated uint64 // bytes allocated and still in use
MemTotal uint64 // bytes allocated (even if freed)
MemSys uint64 // bytes obtained from system
NumGoroutine int
CPUNum int
Pid int
}
func main() {
if len(os.Args) > 1 {
cli()
} else {
err := daemon()
if err != nil {
util.Log.Print(err)
}
}
}
func cli() {
switch os.Args[1] {
case "ls":
commands.Ls()
case "cd":
commands.Cd()
case "pwd":
commands.Pwd()
case "cp":
commands.Cp()
case "mv":
commands.Mv()
case "mkdir":
commands.Mkdir()
case "rm":
commands.Rm()
case "get":
commands.Get()
case "put":
commands.Put()
case "wget":
commands.Wget()
case "info":
commands.Info()
case "hash", "sha1", "sha1sum":
commands.Hash("sha1")
case "sha256":
commands.Hash("sha256")
case "md5", "md5sum":
commands.Hash("md5")
case "crc32":
commands.Hash("crc32")
case "play":
commands.Play()
case "help":
commands.Help()
case "task":
commands.Task()
case "search":
commands.Search()
case "empty":
commands.Empty()
case "serve":
commands.Serve()
case "proxy":
commands.Proxy()
case "reverse":
commands.HTTPProxy()
case "nc":
commands.Nc()
case "fwd":
commands.Fwd()
case "network":
commands.Network()
default:
commands.Usage()
}
}
func daemon() error {
d := os.Getenv("DOC")
if d != "" {
doc = d
}
p := os.Getenv("PORT")
if p != "" {
po, err := strconv.Atoi(p)
if err != nil {
return err
}
port = po
}
pwd, err := utilgo.PathMustHave(doc)
if err != nil {
return err
}
http.HandleFunc("/status", status)
http.HandleFunc("/config", configs)
http.HandleFunc("/", routeMatch)
util.Log.Printf("Starting up on port %d\nDocument root %s", port, pwd)
return http.ListenAndServe(fmt.Sprintf("%s:%d", os.Getenv("HOST"), port), nil)
}
func status(w http.ResponseWriter, r *http.Request) {
memStat := new(runtime.MemStats)
runtime.ReadMemStats(memStat)
sysStatus.Uptime = time.Since(startTime).String()
sysStatus.NumGoroutine = runtime.NumGoroutine()
sysStatus.MemAllocated = memStat.Alloc / 1024 // 当前内存使用量
sysStatus.MemTotal = memStat.TotalAlloc / 1024 // 所有被分配的内存
sysStatus.MemSys = memStat.Sys / 1024 // 内存占用量
sysStatus.CPUNum = runtime.NumCPU()
sysStatus.GoVersion = runtime.Version()
sysStatus.Hostname, _ = os.Hostname()
sysStatus.Pid = os.Getpid()
if bs, err := json.Marshal(&sysStatus); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
utilgo.JSONPut(w, bs, true, 60)
}
}
func routeMatch(w http.ResponseWriter, r *http.Request) {
for _, p := range route.RoutePath {
if p.Reg.MatchString(r.URL.Path) {
if err := p.Handler(w, r, p.Reg.FindStringSubmatch(r.URL.Path)); err != nil {
util.Log.Print(err)
}
return
}
}
fallback(w, r)
}
func fallback(w http.ResponseWriter, r *http.Request) {
files := []string{index}
if r.URL.Path != "/" {
files = []string{r.URL.Path, path.Join(r.URL.Path, index)}
}
if !tryFiles(files, w, r) {
if utilgo.IsURL(r.RequestURI, true) {
middleware.Proxy(w, r)
} else {
http.NotFound(w, r)
}
}
}
func tryFiles(files []string, w http.ResponseWriter, r *http.Request) bool {
for _, file := range files {
realpath := filepath.Join(doc, file)
if f, err := os.Stat(realpath); err == nil {
if f.Mode().IsRegular() {
http.ServeFile(w, r, realpath)
return true
}
}
}
return false
}
func configs(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Auth") == os.Getenv("AUTH") {
token := r.Header.Get("Token")
if r.Method == http.MethodGet {
h := w.Header()
if token != "" {
h.Add("Token", config.Cfg.Token)
}
} else if r.Method == http.MethodPost {
if token != "" {
config.Cfg.Token = token
}
}
}
}