-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhou.go
217 lines (185 loc) · 4.3 KB
/
hou.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
package hou
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"github.com/argcv/stork/log"
"github.com/gin-gonic/gin"
"github.com/olekukonko/tablewriter"
)
type Hou struct {
Basedir string
Port int
DefaultFile string
IndexFile string
Proxy string
ProxyHeaders map[string]string
Debug bool
BodyNotFound string
}
func New() *Hou {
return &Hou{
Basedir: ".",
DefaultFile: "index.html",
BodyNotFound: "File Not Found",
Proxy: "",
}
}
func (h *Hou) GetIndexFile() string {
if len(h.IndexFile) == 0 {
return h.DefaultFile
} else {
return h.IndexFile
}
}
func (h *Hou) String() string {
lb := len(h.BodyNotFound)
if lb > 5 {
lb = 5
}
return fmt.Sprintf("Hou[:%v][Index(%v), Default(%v) Debug(%v) Not Found(%v...)]",
h.Port, h.GetIndexFile(), h.DefaultFile, h.Debug, h.BodyNotFound[:lb])
}
func (h Hou) ConfigTable() string {
buff := bytes.NewBuffer(nil)
lb := len(h.BodyNotFound)
if lb > 20 {
lb = 20
}
hostname, err := os.Hostname()
if err != nil {
hostname = "127.0.0.1"
}
data := [][]string{
{"Endpoint", fmt.Sprintf("http://%s:%d", hostname, h.Port)},
{"Debug", fmt.Sprint(h.Debug)},
{"Index", h.GetIndexFile()},
{"Default", h.DefaultFile},
{"Not Found", h.BodyNotFound[:lb]},
{"Proxy", h.Proxy},
}
if len(h.ProxyHeaders) > 0 {
for k, v := range h.ProxyHeaders {
data = append(data, []string{"Proxy Headers", strings.Join([]string{k, v}, ":")})
}
}
table := tablewriter.NewWriter(buff)
table.SetHeader([]string{"option", "value"})
table.SetAutoMergeCells(true)
table.AppendBulk(data)
table.Render() // Send output
return buff.String()
}
func (h *Hou) handlerLocal(r gin.IRouter) {
indexFile := h.GetIndexFile()
r.Any("/*file", func(c *gin.Context) {
file := path.Clean(path.Join(h.Basedir, path.Clean(c.Param("file"))))
log.Debugf("Requested Path: %s", file)
fileIn := ScanLocalValidFile(indexFile, file, h.DefaultFile)
if len(fileIn) > 0 {
// c.File(fileIn)
http.ServeFile(c.Writer, c.Request, fileIn)
// http.ServeContent(c.Writer, c.Request, "name", time.Now(), os.Open(fileIn))
} else {
c.String(404, h.BodyNotFound)
}
})
}
func (h *Hou) handlerRemote(r gin.IRouter) {
proxy := h.Proxy
if !strings.HasPrefix(proxy, "http://") && !strings.HasPrefix(proxy, "https://") {
proxy = "http://" + proxy
}
if strings.HasSuffix(proxy, "/") {
proxy = proxy[:len(proxy)-1]
}
host := ""
myURL, err := url.Parse(proxy)
if err == nil {
host = myURL.Host
}
buildRequest := func(c *gin.Context, file string) *http.Request {
targetURL := proxy + file
// log.Infof("[%v] requesting... %v", file, targetURL)
req := c.Request.Clone(c.Request.Context())
req.URL, _ = url.Parse(targetURL)
req.RequestURI = ""
for k, v := range h.ProxyHeaders {
req.Header.Set(k, v)
}
if host != "" {
req.Host = host
}
return req
}
copyHeader := func(c *gin.Context, header http.Header) {
for k, v := range header {
c.Writer.Header().Del(k)
for _, vx := range v {
c.Writer.Header().Add(k, vx)
}
}
}
log.Infof("using proxy %v", proxy)
client := httpClient
r.Any("/*file", func(c *gin.Context) {
file := c.Param("file")
resp, err := client.Do(buildRequest(c, file))
if err == nil {
c.Status(resp.StatusCode)
defer resp.Body.Close()
copyHeader(c, resp.Header)
_, err = io.Copy(c.Writer, resp.Body)
if err != nil {
log.Errorf("request %v failed: %v", file, err)
}
return
}
log.Warnf("request %v failed: %v", file, err)
c.Status(502)
return
})
}
func (h *Hou) Run() error {
if h.Debug {
// debug mode
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
if len(h.Proxy) > 0 {
h.handlerRemote(r)
} else {
h.handlerLocal(r)
}
return r.Run(fmt.Sprintf(":%v", h.Port))
}
func ScanLocalValidFile(index string, files ...string) string {
log.Debugf("Scanning...")
for _, f := range files {
log.Debugf("[%v] will be checked...", f)
}
for _, f := range files {
log.Debugf("[%v] checking...", f)
if stat, err := os.Stat(f); err == nil {
log.Debugf("[%v] exists...", f)
if stat.IsDir() {
log.Debugf("[%v] is dir...", f)
sf := ScanLocalValidFile(index, path.Join(f, index))
if len(sf) > 0 {
return sf
}
} else {
// is file
return f
}
}
}
return ""
}