-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy.go
179 lines (167 loc) · 4.25 KB
/
proxy.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
package main
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/admpub/log"
"github.com/webx-top/reverseproxy"
)
const ProxyPort = "8080"
var errAppQuit = errors.New("== App quit unexpetedly")
type Proxy struct {
App *App
appOldPort string
ReserveProxy reverseproxy.ReverseProxy
Watcher *Watcher
FirstRequest *sync.Once
upgraded int64
Port string
AdminPwd string
AdminIPs []string
Engine string
AutoRestartMaxTimes int
autoRestartTimes int
waiting *sync.Once
ctx context.Context
}
func NewProxy(ctx context.Context, app *App, watcher *Watcher) (proxy Proxy) {
proxy.App = app
proxy.Watcher = watcher
proxy.Port = ProxyPort
proxy.AdminIPs = []string{`127.0.0.1`, `::1`}
proxy.AutoRestartMaxTimes = 3
proxy.waiting = &sync.Once{}
proxy.ctx = ctx
return
}
func (this *Proxy) authAdmin(ctx reverseproxy.Context) bool {
pwd := ctx.QueryValue(`pwd`)
valid := false
if len(pwd) > 0 && pwd == this.AdminPwd {
valid = true
} else {
clientIP := ctx.RemoteAddr()
if p := strings.LastIndex(clientIP, `]:`); p > -1 {
clientIP = clientIP[0:p]
clientIP = strings.TrimPrefix(clientIP, `[`)
} else if p := strings.LastIndex(clientIP, `:`); p > -1 {
clientIP = clientIP[0:p]
}
for _, ip := range this.AdminIPs {
if ip == clientIP {
valid = true
break
}
}
}
return valid
}
func (this *Proxy) Listen() error {
if this.App.DisabledVisitPort() || len(this.Port) == 0 {
<-make(chan int)
return nil
}
this.FirstRequest = &sync.Once{}
router := &ProxyRouter{Proxy: this}
router.dst = "http://localhost:" + app.Port
var engine string
if strings.ToLower(this.Engine) == `fast` {
this.ReserveProxy = &reverseproxy.FastReverseProxy{PassingBrowsingURL: true}
engine = `FastHTTP`
} else {
this.ReserveProxy = &reverseproxy.NativeReverseProxy{PassingBrowsingURL: true}
engine = `Standard`
}
listenAddr := this.Port
if !strings.Contains(listenAddr, `:`) {
listenAddr = `:` + listenAddr
}
config := reverseproxy.ReverseProxyConfig{
Listen: listenAddr,
Router: router,
RequestIDHeader: "X-Request-ID",
ResponseBefore: func(ctx reverseproxy.Context) bool {
switch ctx.RequestPath() {
case "/tower-proxy/watch/restart":
this.handleWatchRestart(ctx)
return true
case "/tower-proxy/watch/pause":
this.handleWatchPause(ctx)
return true
case "/tower-proxy/watch/begin":
this.handleWatchBegin(ctx)
return true
case "/tower-proxy/watch":
this.handleWatchStatus(ctx)
return true
}
this.App.LastError = ""
if this.upgraded > 0 {
timeout := time.Now().Unix() - this.upgraded
if timeout > 3600 {
this.upgraded = 0
}
ctx.SetHeader(`X-Server-Upgraded`, fmt.Sprintf("%v", timeout))
}
if this.App.IsQuit() {
err := errAppQuit
this.waiting.Do(func() {
if !this.App.IsQuit() {
this.waiting = &sync.Once{}
return
}
for ; this.autoRestartTimes < this.AutoRestartMaxTimes; this.autoRestartTimes++ {
this.App.Stop(this.App.Port)
this.App.Clean()
var port string
port, err = getPort()
if err == nil {
err = this.App.Start(this.ctx, true, port)
}
if err == nil {
this.autoRestartTimes = 0
break
}
log.Error(err)
}
this.waiting = &sync.Once{}
})
if err != nil {
if this.App.buildErr != nil {
RenderBuildError(ctx, this.App, this.App.buildErr.Error())
return true
}
log.Warn(errAppQuit)
RenderError(ctx, this.App, "App quit unexpetedly.")
return true
}
}
return false
},
ResponseAfter: func(ctx reverseproxy.Context) bool {
if len(this.App.LastError) != 0 {
RenderAppError(ctx, this.App, this.App.LastError)
return true
}
if ctx.IsDead() {
RenderError(ctx, this.App, "App quit unexpetedly.")
}
return false
},
}
this.appOldPort = app.Port
err := this.ReserveProxy.Initialize(config)
if err != nil {
return err
}
log.Info("== Listening to " + router.dst)
log.Info(`== Server(`+engine+`) Address `, config.Listen)
err = this.ReserveProxy.Listen()
if err != nil {
return err
}
return this.ReserveProxy.Stop()
}