-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartmirror.go
222 lines (202 loc) · 4.72 KB
/
smartmirror.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
217
218
219
220
221
222
/*
* Copyright (C) 2015 ~ 2017 Deepin Technology Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"fmt"
"internal/mirrors"
"net/http"
"os"
"strings"
"github.com/codegangsta/cli"
)
var CMDSmartMirror = cli.Command{
Name: "smartmirror",
Usage: `query of select mirrors`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "official",
Value: "http://mirrors.tuna.tsinghua.edu.cn/debian/",
Usage: "the official package repository",
},
cli.StringFlag{
Name: "mirrorlist,m",
Value: "/var/lib/lastore/mirrors.json",
Usage: "the list of mirrors, maintained by official",
},
cli.StringFlag{
Name: "db,d",
Value: "/var/lib/lastore/history.db",
Usage: "the db to store the history information",
},
cli.BoolFlag{
Name: "quiet,q",
Usage: "slient mode, only produces necessary output",
},
cli.IntFlag{
Name: "interval,i",
Value: 300,
Usage: "minimum interval in seconds allow for rechecking failed mirror",
},
cli.IntFlag{
Name: "parallel,p",
Value: 5,
Usage: "maximum http connections allow for detecting to take",
},
},
Subcommands: []cli.Command{
{
Name: "stats",
Usage: `show the history of serving
✓ and ★ indicate the candidates in next mirror selecting.
But ★ also indicate the mirror was unworkable in preview detecting.`,
Action: func(c *cli.Context) {
fmt.Println("Didn't support at this version")
},
},
{
Name: "server_stats",
Usage: "report the status of all knonw mirrors",
Action: SubmainMirrorSynProgress,
Flags: []cli.Flag{
cli.StringFlag{
Name: "index,i",
Value: ".__GUARD__INDEX__",
Usage: "the guard index path in official",
},
cli.BoolFlag{
Name: "list,l",
Usage: "only list knonwn mirrors",
},
cli.StringFlag{
Name: "export,e",
Value: "result.json",
Usage: "the file to save the results in json format",
},
},
},
},
}
func SubmainMirrorSynProgress(c *cli.Context) {
indexName := c.String("index")
exportPath := c.String("export")
official := c.Parent().String("official")
onlyList := c.Bool("list")
n := c.Parent().Int("parallel")
if onlyList {
mlist, _ := getMirrorList(c.Parent().String("mirrorlist"))
for _, m := range mlist {
fmt.Println(m)
}
return
}
mlist := c.Args()
if len(mlist) == 0 {
mlist, _ = getMirrorList(c.Parent().String("mirrorlist"))
}
infos := DetectServer(n, indexName, official, mlist)
ShowMirrorInfos(infos)
f, err := os.Create(exportPath)
if err != nil {
fmt.Println("E:", err)
return
}
err = SaveMirrorInfos(infos, f)
if err != nil {
fmt.Println("E:", err)
}
}
func ShowBest(url string) {
fmt.Println(url)
}
func ShowBestOnError(url string, err error) {
ShowBest(url)
fmt.Printf("E:%v\n", err)
os.Exit(-1)
}
func buildRequest(header map[string]string, method string, url string) *http.Request {
r, err := http.NewRequest(method, url, nil)
if err != nil {
return nil
}
for k, v := range header {
r.Header.Set(k, v)
}
return r
}
func handleRequest(c *http.Client, r *http.Request) (string, bool) {
if c == nil {
c = &http.Client{}
}
if r == nil {
return "", false
}
resp, err := c.Do(r)
if err != nil {
return "", false
}
resp.Body.Close()
switch resp.StatusCode / 100 {
case 4, 5:
return "", false
case 3:
u, err := resp.Location()
if err != nil {
return r.URL.String(), true
}
return u.String(), true
case 2, 1:
return r.URL.String(), true
default:
return "", true
}
}
func appendSuffix(r string, suffix string) string {
if strings.HasSuffix(r, suffix) {
return r
}
return r + suffix
}
func getMirrorList(p string) ([]string, error) {
if strings.HasPrefix(p, "http://") ||
strings.HasPrefix(p, "https://") {
ms, err := mirrors.LoadMirrorSources(p)
if err != nil {
return nil, err
}
var r []string
for _, m := range ms {
r = append(r, m.Url)
}
return r, nil
}
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
d := json.NewDecoder(f)
var raw []struct {
Url string `json:"url"`
}
err = d.Decode(&raw)
var r []string
for _, u := range raw {
r = append(r, u.Url)
}
return r, err
}