mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
refactor and remove unused code
This commit is contained in:
parent
eaf191e350
commit
0a7b28caf5
15 changed files with 24 additions and 52 deletions
|
@ -2,7 +2,7 @@
|
||||||
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
|
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
|
||||||
version: 0.1
|
version: 0.1
|
||||||
cli:
|
cli:
|
||||||
version: 1.22.9
|
version: 1.22.10
|
||||||
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
|
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
|
||||||
plugins:
|
plugins:
|
||||||
sources:
|
sources:
|
||||||
|
@ -23,7 +23,6 @@ lint:
|
||||||
enabled:
|
enabled:
|
||||||
- hadolint@2.12.1-beta
|
- hadolint@2.12.1-beta
|
||||||
- actionlint@1.7.7
|
- actionlint@1.7.7
|
||||||
- checkov@3.2.360
|
|
||||||
- git-diff-check
|
- git-diff-check
|
||||||
- gofmt@1.20.4
|
- gofmt@1.20.4
|
||||||
- golangci-lint@1.63.4
|
- golangci-lint@1.63.4
|
||||||
|
@ -32,7 +31,7 @@ lint:
|
||||||
- prettier@3.4.2
|
- prettier@3.4.2
|
||||||
- shellcheck@0.10.0
|
- shellcheck@0.10.0
|
||||||
- shfmt@3.6.0
|
- shfmt@3.6.0
|
||||||
- trufflehog@3.88.4
|
- trufflehog@3.88.5
|
||||||
actions:
|
actions:
|
||||||
disabled:
|
disabled:
|
||||||
- trunk-announce
|
- trunk-announce
|
||||||
|
|
|
@ -9,8 +9,7 @@ type agentCommandValidator struct{}
|
||||||
|
|
||||||
func (v agentCommandValidator) IsCommandValid(cmd string) bool {
|
func (v agentCommandValidator) IsCommandValid(cmd string) bool {
|
||||||
switch cmd {
|
switch cmd {
|
||||||
case CommandStart,
|
case CommandStart, CommandNewClient:
|
||||||
CommandNewClient:
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -72,8 +72,7 @@ func main() {
|
||||||
E.LogFatal("init CA error", err)
|
E.LogFatal("init CA error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch args.Command {
|
if args.Command == CommandNewClient {
|
||||||
case CommandNewClient:
|
|
||||||
printNewClientHelp(ca)
|
printNewClientHelp(ca)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,6 @@ type (
|
||||||
const (
|
const (
|
||||||
EndpointVersion = "/version"
|
EndpointVersion = "/version"
|
||||||
EndpointName = "/name"
|
EndpointName = "/name"
|
||||||
EndpointCACert = "/ca-cert"
|
|
||||||
EndpointProxyHTTP = "/proxy/http"
|
EndpointProxyHTTP = "/proxy/http"
|
||||||
EndpointHealth = "/health"
|
EndpointHealth = "/health"
|
||||||
EndpointLogs = "/logs"
|
EndpointLogs = "/logs"
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (cfg *AgentConfig) Fetch(ctx context.Context, endpoint string) ([]byte, int
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
data, err := io.ReadAll(resp.Body)
|
data, _ := io.ReadAll(resp.Body)
|
||||||
return data, resp.StatusCode, nil
|
return data, resp.StatusCode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,7 @@ func DockerSocketHandler() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For non-event streams, just copy the body
|
// For non-event streams, just copy the body
|
||||||
godoxyIO.NewPipe(r.Context(), resp.Body, NopWriteCloser{w}).Start()
|
_ = godoxyIO.NewPipe(r.Context(), resp.Body, NopWriteCloser{w}).Start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ func (NopWriteCloser) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandler(caCertPEM []byte) http.Handler {
|
func NewHandler() http.Handler {
|
||||||
mux := ServeMux{http.NewServeMux()}
|
mux := ServeMux{http.NewServeMux()}
|
||||||
|
|
||||||
mux.HandleFunc(agent.EndpointProxyHTTP+"/{path...}", ProxyHTTP)
|
mux.HandleFunc(agent.EndpointProxyHTTP+"/{path...}", ProxyHTTP)
|
||||||
|
@ -40,9 +40,6 @@ func NewHandler(caCertPEM []byte) http.Handler {
|
||||||
mux.HandleMethods("GET", agent.EndpointName, func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleMethods("GET", agent.EndpointName, func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, env.AgentName)
|
fmt.Fprint(w, env.AgentName)
|
||||||
})
|
})
|
||||||
mux.HandleMethods("GET", agent.EndpointCACert, func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Write(caCertPEM)
|
|
||||||
})
|
|
||||||
mux.HandleMethods("GET", agent.EndpointHealth, CheckHealth)
|
mux.HandleMethods("GET", agent.EndpointHealth, CheckHealth)
|
||||||
mux.HandleMethods("GET", agent.EndpointLogs, memlogger.LogsWS(nil))
|
mux.HandleMethods("GET", agent.EndpointLogs, memlogger.LogsWS(nil))
|
||||||
mux.ServeMux.HandleFunc("/", DockerSocketHandler())
|
mux.ServeMux.HandleFunc("/", DockerSocketHandler())
|
||||||
|
|
|
@ -17,14 +17,14 @@ import (
|
||||||
|
|
||||||
func ProxyHTTP(w http.ResponseWriter, r *http.Request) {
|
func ProxyHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
host := r.Header.Get(agentproxy.HeaderXProxyHost)
|
host := r.Header.Get(agentproxy.HeaderXProxyHost)
|
||||||
isHTTPs := strutils.ParseBool(r.Header.Get(agentproxy.HeaderXProxyHTTPS))
|
isHTTPS := strutils.ParseBool(r.Header.Get(agentproxy.HeaderXProxyHTTPS))
|
||||||
skipTLSVerify := strutils.ParseBool(r.Header.Get(agentproxy.HeaderXProxySkipTLSVerify))
|
skipTLSVerify := strutils.ParseBool(r.Header.Get(agentproxy.HeaderXProxySkipTLSVerify))
|
||||||
responseHeaderTimeout, err := strconv.Atoi(r.Header.Get(agentproxy.HeaderXProxyResponseHeaderTimeout))
|
responseHeaderTimeout, err := strconv.Atoi(r.Header.Get(agentproxy.HeaderXProxyResponseHeaderTimeout))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
responseHeaderTimeout = 0
|
responseHeaderTimeout = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
logging.Debug().Msgf("proxy http request: host=%s, isHTTPs=%t, skipTLSVerify=%t, responseHeaderTimeout=%d", host, isHTTPs, skipTLSVerify, responseHeaderTimeout)
|
logging.Debug().Msgf("proxy http request: host=%s, isHTTPs=%t, skipTLSVerify=%t, responseHeaderTimeout=%d", host, isHTTPS, skipTLSVerify, responseHeaderTimeout)
|
||||||
|
|
||||||
if host == "" {
|
if host == "" {
|
||||||
http.Error(w, "missing required headers", http.StatusBadRequest)
|
http.Error(w, "missing required headers", http.StatusBadRequest)
|
||||||
|
@ -32,7 +32,7 @@ func ProxyHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
scheme := "http"
|
scheme := "http"
|
||||||
if isHTTPs {
|
if isHTTPS {
|
||||||
scheme = "https"
|
scheme = "https"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,9 +43,11 @@ func StartAgentServer(parent task.Parent, opt Options) {
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Handler: handler.NewHandler(caCertPEM),
|
Handler: handler.NewHandler(),
|
||||||
TLSConfig: tlsConfig,
|
TLSConfig: tlsConfig,
|
||||||
ErrorLog: log.New(logging.GetLogger(), "", 0),
|
ErrorLog: log.New(logging.GetLogger(), "", 0),
|
||||||
}
|
}
|
||||||
server.Serve(tls.NewListener(l, tlsConfig))
|
if err := server.Serve(tls.NewListener(l, tlsConfig)); err != nil {
|
||||||
|
logging.Fatal().Err(err).Int("port", opt.Port).Msg("failed to serve")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,9 +36,8 @@ func (res *fetchResult) ContentType() string {
|
||||||
if res.contentType == "" {
|
if res.contentType == "" {
|
||||||
if bytes.HasPrefix(res.icon, []byte("<svg")) || bytes.HasPrefix(res.icon, []byte("<?xml")) {
|
if bytes.HasPrefix(res.icon, []byte("<svg")) || bytes.HasPrefix(res.icon, []byte("<?xml")) {
|
||||||
return "image/svg+xml"
|
return "image/svg+xml"
|
||||||
} else {
|
|
||||||
return "image/x-icon"
|
|
||||||
}
|
}
|
||||||
|
return "image/x-icon"
|
||||||
}
|
}
|
||||||
return res.contentType
|
return res.contentType
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
//nolint:misspell
|
||||||
package common
|
package common
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -15,7 +15,6 @@ var (
|
||||||
timeFmt string
|
timeFmt string
|
||||||
level zerolog.Level
|
level zerolog.Level
|
||||||
prefix string
|
prefix string
|
||||||
prefixHTML []byte
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -32,8 +31,6 @@ func init() {
|
||||||
}
|
}
|
||||||
prefixLength := len(timeFmt) + 5 // level takes 3 + 2 spaces
|
prefixLength := len(timeFmt) + 5 // level takes 3 + 2 spaces
|
||||||
prefix = strings.Repeat(" ", prefixLength)
|
prefix = strings.Repeat(" ", prefixLength)
|
||||||
// prefixHTML = []byte(strings.Repeat(" ", prefixLength))
|
|
||||||
prefixHTML = []byte(prefix)
|
|
||||||
|
|
||||||
if zerolog.TraceLevel != -1 && zerolog.NoLevel != 6 {
|
if zerolog.TraceLevel != -1 && zerolog.NoLevel != 6 {
|
||||||
panic("zerolog implementation changed")
|
panic("zerolog implementation changed")
|
||||||
|
|
|
@ -37,26 +37,6 @@ func (handler *EventHandler) Handle(parent task.Parent, events []watcher.Event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if common.IsDebug {
|
|
||||||
// eventsLog := E.NewBuilder("events")
|
|
||||||
// for _, event := range events {
|
|
||||||
// eventsLog.Addf("event %s, actor: name=%s, id=%s", event.Action, event.ActorName, event.ActorID)
|
|
||||||
// }
|
|
||||||
// E.LogDebug(eventsLog.About(), eventsLog.Error(), handler.provider.Logger())
|
|
||||||
|
|
||||||
// oldRoutesLog := E.NewBuilder("old routes")
|
|
||||||
// for k := range oldRoutes {
|
|
||||||
// oldRoutesLog.Adds(k)
|
|
||||||
// }
|
|
||||||
// E.LogDebug(oldRoutesLog.About(), oldRoutesLog.Error(), handler.provider.Logger())
|
|
||||||
|
|
||||||
// newRoutesLog := E.NewBuilder("new routes")
|
|
||||||
// for k := range newRoutes {
|
|
||||||
// newRoutesLog.Adds(k)
|
|
||||||
// }
|
|
||||||
// E.LogDebug(newRoutesLog.About(), newRoutesLog.Error(), handler.provider.Logger())
|
|
||||||
// }
|
|
||||||
|
|
||||||
for k, oldr := range oldRoutes {
|
for k, oldr := range oldRoutes {
|
||||||
newr, ok := newRoutes[k]
|
newr, ok := newRoutes[k]
|
||||||
switch {
|
switch {
|
||||||
|
|
|
@ -41,7 +41,6 @@ type (
|
||||||
|
|
||||||
// var globalMux = http.NewServeMux() // TODO: support regex subdomain matching.
|
// var globalMux = http.NewServeMux() // TODO: support regex subdomain matching.
|
||||||
|
|
||||||
// TODO: fix this for agent
|
|
||||||
func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, E.Error) {
|
func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, E.Error) {
|
||||||
httpConfig := base.HTTPConfig
|
httpConfig := base.HTTPConfig
|
||||||
proxyURL := base.ProxyURL
|
proxyURL := base.ProxyURL
|
||||||
|
|
|
@ -280,14 +280,15 @@ func (r *Route) Finalize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if pp == 0 {
|
if pp == 0 {
|
||||||
if isDocker {
|
switch {
|
||||||
|
case isDocker:
|
||||||
pp = lowestPort(cont.PrivatePortMapping)
|
pp = lowestPort(cont.PrivatePortMapping)
|
||||||
if pp == 0 {
|
if pp == 0 {
|
||||||
pp = lowestPort(cont.PublicPortMapping)
|
pp = lowestPort(cont.PublicPortMapping)
|
||||||
}
|
}
|
||||||
} else if r.Scheme == "https" {
|
case r.Scheme == "https":
|
||||||
pp = 443
|
pp = 443
|
||||||
} else {
|
default:
|
||||||
pp = 80
|
pp = 80
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue