mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
fixed container routes not being loaded, added X-Forwarded-{Scheme,Proto,Host}, fixed containers with no mapping being served
This commit is contained in:
parent
48dd1397e8
commit
478311fe9e
8 changed files with 38 additions and 16 deletions
4
.github/workflows/docker-image.yml
vendored
4
.github/workflows/docker-image.yml
vendored
|
@ -24,8 +24,8 @@ jobs:
|
||||||
matrix:
|
matrix:
|
||||||
platform:
|
platform:
|
||||||
- linux/amd64
|
- linux/amd64
|
||||||
- linux/arm/v6
|
# - linux/arm/v6
|
||||||
- linux/arm/v7
|
# - linux/arm/v7
|
||||||
- linux/arm64
|
- linux/arm64
|
||||||
steps:
|
steps:
|
||||||
- name: Prepare
|
- name: Prepare
|
||||||
|
|
|
@ -34,6 +34,7 @@ func (cfg *Config) RoutesByAlias() map[string]U.SerializedObject {
|
||||||
}
|
}
|
||||||
obj["provider"] = p.GetName()
|
obj["provider"] = p.GetName()
|
||||||
obj["type"] = string(r.Type())
|
obj["type"] = string(r.Type())
|
||||||
|
obj["started"] = r.Started()
|
||||||
routes[alias] = obj
|
routes[alias] = obj
|
||||||
})
|
})
|
||||||
return routes
|
return routes
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
. "github.com/yusing/go-proxy/internal/common"
|
. "github.com/yusing/go-proxy/internal/common"
|
||||||
D "github.com/yusing/go-proxy/internal/docker"
|
D "github.com/yusing/go-proxy/internal/docker"
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||||
|
@ -72,11 +73,11 @@ func (e *RawEntry) FillMissingFields() bool {
|
||||||
// try to fallback to first public port
|
// try to fallback to first public port
|
||||||
if p, ok := F.FirstValueOf(e.PublicPortMapping); ok {
|
if p, ok := F.FirstValueOf(e.PublicPortMapping); ok {
|
||||||
pp = fmt.Sprint(p.PublicPort)
|
pp = fmt.Sprint(p.PublicPort)
|
||||||
}
|
} else if e.Running {
|
||||||
// ignore only if it is NOT RUNNING
|
// ignore only if it is NOT RUNNING
|
||||||
// because stopped containers
|
// because stopped containers
|
||||||
// will have empty port mapping got from docker
|
// will have empty port mapping got from docker
|
||||||
if e.Running {
|
logrus.Debugf("ignored port %s for %s", pp, e.ContainerName)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -315,11 +315,6 @@ func (p *ReverseProxy) serveHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
outreq.Header.Set("Upgrade", reqUpType)
|
outreq.Header.Set("Upgrade", reqUpType)
|
||||||
}
|
}
|
||||||
|
|
||||||
outreq.Header.Del("Forwarded")
|
|
||||||
// outreq.Header.Del("X-Forwarded-For")
|
|
||||||
// outreq.Header.Del("X-Forwarded-Host")
|
|
||||||
// outreq.Header.Del("X-Forwarded-Proto")
|
|
||||||
|
|
||||||
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
||||||
// If we aren't the first proxy retain prior
|
// If we aren't the first proxy retain prior
|
||||||
// X-Forwarded-For information as a comma+space
|
// X-Forwarded-For information as a comma+space
|
||||||
|
@ -333,6 +328,14 @@ func (p *ReverseProxy) serveHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
outreq.Header.Set("X-Forwarded-For", clientIP)
|
outreq.Header.Set("X-Forwarded-For", clientIP)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if req.TLS == nil {
|
||||||
|
outreq.Header.Set("X-Forwarded-Proto", "http")
|
||||||
|
outreq.Header.Set("X-Forwarded-Scheme", "http")
|
||||||
|
} else {
|
||||||
|
outreq.Header.Set("X-Forwarded-Proto", "https")
|
||||||
|
outreq.Header.Set("X-Forwarded-Scheme", "https")
|
||||||
|
}
|
||||||
|
outreq.Header.Set("X-Forwarded-Host", req.Host)
|
||||||
|
|
||||||
if _, ok := outreq.Header["User-Agent"]; !ok {
|
if _, ok := outreq.Header["User-Agent"]; !ok {
|
||||||
// If the outbound request doesn't have a User-Agent header set,
|
// If the outbound request doesn't have a User-Agent header set,
|
||||||
|
|
|
@ -43,6 +43,10 @@ func (rp *ReverseProxyEntry) UseIdleWatcher() bool {
|
||||||
return rp.IdleTimeout > 0 && rp.DockerHost != ""
|
return rp.IdleTimeout > 0 && rp.DockerHost != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rp *ReverseProxyEntry) IsDocker() bool {
|
||||||
|
return rp.DockerHost != ""
|
||||||
|
}
|
||||||
|
|
||||||
func ValidateEntry(m *M.RawEntry) (any, E.NestedError) {
|
func ValidateEntry(m *M.RawEntry) (any, E.NestedError) {
|
||||||
if !m.FillMissingFields() {
|
if !m.FillMissingFields() {
|
||||||
return nil, E.Missing("fields")
|
return nil, E.Missing("fields")
|
||||||
|
|
|
@ -133,6 +133,10 @@ func (r *HTTPRoute) Start() E.NestedError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.entry.IsDocker() && !r.entry.ContainerRunning {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
r.mux = http.NewServeMux()
|
r.mux = http.NewServeMux()
|
||||||
for _, p := range r.PathPatterns {
|
for _, p := range r.PathPatterns {
|
||||||
r.mux.HandleFunc(string(p), r.handler.ServeHTTP)
|
r.mux.HandleFunc(string(p), r.handler.ServeHTTP)
|
||||||
|
@ -160,6 +164,10 @@ func (r *HTTPRoute) Stop() E.NestedError {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *HTTPRoute) Started() bool {
|
||||||
|
return r.mux != nil
|
||||||
|
}
|
||||||
|
|
||||||
func (u *URL) String() string {
|
func (u *URL) String() string {
|
||||||
return (*url.URL)(u).String()
|
return (*url.URL)(u).String()
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ type (
|
||||||
RouteImpl interface {
|
RouteImpl interface {
|
||||||
Start() E.NestedError
|
Start() E.NestedError
|
||||||
Stop() E.NestedError
|
Stop() E.NestedError
|
||||||
|
Started() bool
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
RouteType string
|
RouteType string
|
||||||
|
@ -41,14 +42,14 @@ const (
|
||||||
var NewRoutes = F.NewMapOf[string, Route]
|
var NewRoutes = F.NewMapOf[string, Route]
|
||||||
|
|
||||||
func NewRoute(en *M.RawEntry) (Route, E.NestedError) {
|
func NewRoute(en *M.RawEntry) (Route, E.NestedError) {
|
||||||
rt, err := P.ValidateEntry(en)
|
entry, err := P.ValidateEntry(en)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var t RouteType
|
var t RouteType
|
||||||
|
var rt RouteImpl
|
||||||
switch e := rt.(type) {
|
switch e := entry.(type) {
|
||||||
case *P.StreamEntry:
|
case *P.StreamEntry:
|
||||||
rt, err = NewStreamRoute(e)
|
rt, err = NewStreamRoute(e)
|
||||||
t = RouteTypeStream
|
t = RouteTypeStream
|
||||||
|
@ -61,7 +62,7 @@ func NewRoute(en *M.RawEntry) (Route, E.NestedError) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &route{RouteImpl: rt.(RouteImpl), entry: en, type_: t}, nil
|
return &route{RouteImpl: rt, entry: en, type_: t}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rt *route) Entry() *M.RawEntry {
|
func (rt *route) Entry() *M.RawEntry {
|
||||||
|
|
|
@ -99,6 +99,10 @@ func (r *StreamRoute) Stop() E.NestedError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *StreamRoute) Started() bool {
|
||||||
|
return r.started.Load()
|
||||||
|
}
|
||||||
|
|
||||||
func (r *StreamRoute) grAcceptConnections() {
|
func (r *StreamRoute) grAcceptConnections() {
|
||||||
defer r.wg.Done()
|
defer r.wg.Done()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue