-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (137 loc) · 3.58 KB
/
main.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
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/gorilla/mux"
"golang.org/x/crypto/acme/autocert"
)
var imgs []string
var showid int
var gImgID int
var (
optDomain string
optSecure bool
optPort string
optImgsDir string
optSuffix string
)
var epoch = time.Unix(0, 0).Format(time.RFC1123)
var rd = rand.New(rand.NewSource(time.Now().UnixNano()))
// Taken from https://github.com/mytrile/nocache
var noCacheHeaders = map[string]string{
"Expires": epoch,
"Cache-Control": "no-cache, private, max-age=0",
"Pragma": "no-cache",
"X-Accel-Expires": "0",
}
var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
func NoCache(w *http.ResponseWriter, r *http.Request) {
return
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
// Delete any ETag headers that may have been set
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}
// Set our NoCache headers
for k, v := range noCacheHeaders {
w.Header().Set(k, v)
}
http.ServeFile(w, r, "form.html")
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(`echo ...`)
w.Write([]byte("Hello world"))
}
func imgHandler(w http.ResponseWriter, r *http.Request) {
//imgid := rd.Intn(len(imgs) - 1)
imgid := gImgID % len(imgs)
imgDirPath := optImgsDir + `\` + imgs[imgid]
log.Println(`imgDirPath `, imgDirPath)
gImgID = gImgID + 1
// Delete any ETag headers that may have been set
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}
// Set our NoCache headers
for k, v := range noCacheHeaders {
w.Header().Set(k, v)
}
http.ServeFile(w, r, imgDirPath)
}
func main() {
flag.BoolVar(&optSecure, "s", false, "run in secure mode (https)")
flag.StringVar(&optDomain, "n", "sohu.com", "cookie domain")
flag.StringVar(&optImgsDir, "d", "D:\\imgs", "-d : 图片的路径.")
flag.StringVar(&optPort, "p", "80", "listen port")
flag.StringVar(&optSuffix, "f", "jpg;", "img suffix name")
flag.Parse()
gImgID = 0
imgs = LoadImgs(optImgsDir, optSuffix)
if len(imgs) == 0 {
fmt.Println(`指定目录下没有img文件,退出`)
return
}
for i, img := range imgs {
fmt.Println(i, img)
}
rt := mux.NewRouter()
rt.HandleFunc("/", helloHandler)
rt.HandleFunc("/echo", echoHandler)
rt.HandleFunc("/img", imgHandler)
if optSecure {
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(optDomain, "www."+optDomain),
Cache: autocert.DirCache("certcache"),
}
go func() {
log.Fatal(http.ListenAndServe(":http",
m.HTTPHandler(nil)))
}()
s := &http.Server{
Addr: ":443",
Handler: rt,
ReadTimeout: 20 * time.Second,
WriteTimeout: 20 * time.Second,
MaxHeaderBytes: 1 << 20,
TLSConfig: &tls.Config{
GetCertificate: m.GetCertificate,
},
}
log.Println("Img server started at 443(https) domain", optDomain, " ...")
err := s.ListenAndServeTLS("", "")
if err != nil {
log.Fatalln(err)
}
} else {
s := &http.Server{
Addr: ":" + optPort,
Handler: rt,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Println("Img server started at", optPort, ", domain", optDomain, " ...")
err := s.ListenAndServe()
if err != nil {
log.Fatalln(err)
}
}
}