mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
fixed unchecked integer conversion, fixed 'invalid host' bug, corrected error message
This commit is contained in:
parent
6f3a5ebe6e
commit
4120fd8d1c
3 changed files with 23 additions and 19 deletions
2
.github/workflows/docker-image.yml
vendored
2
.github/workflows/docker-image.yml
vendored
|
@ -142,6 +142,6 @@ jobs:
|
|||
format: "sarif"
|
||||
output: "trivy-results.sarif"
|
||||
- name: Upload Trivy SARIF Report
|
||||
uses: github/codeql-action/upload-sarif@v2
|
||||
uses: github/codeql-action/upload-sarif@v3
|
||||
with:
|
||||
sarif_file: "trivy-results.sarif"
|
||||
|
|
|
@ -39,8 +39,8 @@ func FromJson(json types.ContainerJSON, dockerHost string) Container {
|
|||
ports := make([]types.Port, 0)
|
||||
for k, bindings := range json.NetworkSettings.Ports {
|
||||
for _, v := range bindings {
|
||||
pubPort, _ := strconv.Atoi(v.HostPort)
|
||||
privPort, _ := strconv.Atoi(k.Port())
|
||||
pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16)
|
||||
privPort, _ := strconv.ParseUint(k.Port(), 10, 16)
|
||||
ports = append(ports, types.Port{
|
||||
IP: v.HostIP,
|
||||
PublicPort: uint16(pubPort),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package route
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"net/http"
|
||||
|
@ -48,7 +49,7 @@ func SetFindMuxDomains(domains []string) {
|
|||
if len(domains) == 0 {
|
||||
findMuxFunc = findMuxAnyDomain
|
||||
} else {
|
||||
findMuxFunc = findMuxByDomain(domains)
|
||||
findMuxFunc = findMuxByDomains(domains)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,44 +170,47 @@ func (u *URL) MarshalText() (text []byte, err error) {
|
|||
func ProxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
mux, err := findMuxFunc(r.Host)
|
||||
if err != nil {
|
||||
err = E.Failure("request").
|
||||
Subjectf("%s %s%s", r.Method, r.Host, r.URL.Path).
|
||||
With(err)
|
||||
http.Error(w, err.String(), http.StatusNotFound)
|
||||
logrus.Error(err)
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
logrus.Error(E.Failure("request").
|
||||
Subjectf("%s %s", r.Method, r.URL.String()).
|
||||
With(err))
|
||||
return
|
||||
}
|
||||
mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func findMuxAnyDomain(host string) (*http.ServeMux, E.NestedError) {
|
||||
func findMuxAnyDomain(host string) (*http.ServeMux, error) {
|
||||
hostSplit := strings.Split(host, ".")
|
||||
n := len(hostSplit)
|
||||
if n <= 2 {
|
||||
return nil, E.Missing("subdomain")
|
||||
return nil, fmt.Errorf("missing subdomain in url")
|
||||
}
|
||||
sd := strings.Join(hostSplit[:n-2], ".")
|
||||
if r, ok := httpRoutes.Load(PT.Alias(sd)); ok {
|
||||
return r.mux, nil
|
||||
}
|
||||
return nil, E.NotExist("route", sd)
|
||||
return nil, fmt.Errorf("no such route: %s", sd)
|
||||
}
|
||||
|
||||
func findMuxByDomain(domains []string) func(host string) (*http.ServeMux, E.NestedError) {
|
||||
return func(host string) (*http.ServeMux, E.NestedError) {
|
||||
func findMuxByDomains(domains []string) func(host string) (*http.ServeMux, error) {
|
||||
return func(host string) (*http.ServeMux, error) {
|
||||
var subdomain string
|
||||
|
||||
for _, domain := range domains {
|
||||
subdomain = strings.TrimSuffix(subdomain, domain)
|
||||
if subdomain != domain {
|
||||
if !strings.HasPrefix(domain, ".") {
|
||||
domain = "." + domain
|
||||
}
|
||||
subdomain = strings.TrimSuffix(host, domain)
|
||||
if len(subdomain) < len(host) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if subdomain == "" { // not matched
|
||||
return nil, E.Invalid("host", host)
|
||||
if len(subdomain) == len(host) { // not matched
|
||||
return nil, fmt.Errorf("%s does not match any base domain", host)
|
||||
}
|
||||
if r, ok := httpRoutes.Load(PT.Alias(subdomain)); ok {
|
||||
return r.mux, nil
|
||||
}
|
||||
return nil, E.NotExist("route", subdomain)
|
||||
return nil, fmt.Errorf("no such route: %s", subdomain)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue