Skip to content

Commit

Permalink
refactored http import name, fixed and simplified idlewatcher/idlewak…
Browse files Browse the repository at this point in the history
…er implementation, dependencies update
  • Loading branch information
yusing committed Oct 7, 2024
1 parent 929b7f7 commit 921ce23
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 261 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
compose.yml
*.compose.yml

config*/
certs*/
Expand All @@ -20,3 +21,4 @@ todo.md
.*.swp
.aider*
mtrace.json
.env
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Stage 1: Builder
FROM golang:1.23.1-alpine AS builder
FROM golang:1.23.2-alpine AS builder
RUN apk add --no-cache tzdata make

WORKDIR /src
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ get:
debug:
make build && sudo GOPROXY_DEBUG=1 bin/go-proxy

mtrace:
bin/go-proxy debug-ls-mtrace > mtrace.json

run-test:
make build && sudo GOPROXY_TEST=1 bin/go-proxy

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ _Join our [Discord](https://discord.gg/umReR62nRd) for help and discussions_

4. Setup `docker-socket-proxy` other docker nodes _(if any)_ (see [Multi docker nodes setup](https://github.com/yusing/go-proxy/wiki/Configurations#multi-docker-nodes-setup)) and then them inside `config.yml`

5. Done. You may now do some extra configuration
5. Run go-proxy `docker compose up -d`
then list all routes to see if further configurations are needed:
`docker exec go-proxy /app/go-proxy ls-routes`

6. You may now do some extra configuration
- With text editor (e.g. Visual Studio Code)
- With Web UI via `http://localhost:3000` or `https://gp.y.z`
- For more info, [See Wiki]([wiki](https://github.com/yusing/go-proxy/wiki))

[🔼Back to top](#table-of-content)
|

### Use JSON Schema in VSCode

Expand Down
2 changes: 1 addition & 1 deletion compose.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
# Make sure the value is same as `GOPROXY_API_ADDR` below (if you have changed it)
#
# environment:
# NEXT_PUBLIC_GOPROXY_API_ADDR: 127.0.0.1:8888
# GOPROXY_API_ADDR: 127.0.0.1:8888
app:
image: ghcr.io/yusing/go-proxy:latest
container_name: go-proxy
Expand Down
2 changes: 1 addition & 1 deletion examples/microbin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
limits:
memory: 256M
env_file: .env
image: docker.i.sh/danielszabo99/microbin:latest
image: danielszabo99/microbin:latest
ports:
- 8080
restart: unless-stopped
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/yusing/go-proxy

go 1.23.1
go 1.23.2

require (
github.com/coder/websocket v1.8.12
github.com/docker/cli v27.3.1+incompatible
github.com/docker/docker v27.3.1+incompatible
github.com/fsnotify/fsnotify v1.7.0
github.com/go-acme/lego/v4 v4.19.0
github.com/go-acme/lego/v4 v4.19.2
github.com/puzpuzpuz/xsync/v3 v3.4.0
github.com/santhosh-tekuri/jsonschema v1.2.4
github.com/sirupsen/logrus v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-acme/lego/v4 v4.19.0 h1:c7YabBOwoa2URsPiCNGQsdzQnbd8Y23B4/66gxh4H7c=
github.com/go-acme/lego/v4 v4.19.0/go.mod h1:wtDe3dDkmV4/oI2nydpNXSJpvV10J9RCyZ6MbYxNtlQ=
github.com/go-acme/lego/v4 v4.19.2 h1:Y8hrmMvWETdqzzkRly7m98xtPJJivWFsgWi8fcvZo+Y=
github.com/go-acme/lego/v4 v4.19.2/go.mod h1:wtDe3dDkmV4/oI2nydpNXSJpvV10J9RCyZ6MbYxNtlQ=
github.com/go-jose/go-jose/v4 v4.0.4 h1:VsjPI33J0SB9vQM6PLmNjoHqMQNGPiZ0rHL7Ni7Q6/E=
github.com/go-jose/go-jose/v4 v4.0.4/go.mod h1:NKb5HO1EZccyMpiZNbdUw/14tiXNyUJh188dfnMCAfc=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down
25 changes: 13 additions & 12 deletions internal/docker/idlewatcher/html/loading_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,23 @@
<body>
<script>
window.onload = async function () {
let result = await fetch(window.location.href, {
let resp = await fetch(window.location.href, {
headers: {
{{ range $key, $value := .RequestHeaders }}
'{{ $key }}' : {{ $value }}
{{ end }}
"{{.CheckRedirectHeader}}": "1",
},
}).then((resp) => resp.text())
.catch((err) => {
document.getElementById("message").innerText = err;
});
if (result) {
document.documentElement.innerHTML = result
});
if (resp.ok) {
window.location.href = resp.url;
} else {
document.getElementById("message").innerText =
await resp.text();
document
.getElementById("spinner")
.classList.replace("spinner", "error");
}
};
</script>
<div class="{{.SpinnerClass}}"></div>
<div class="message">{{.Message}}</div>
<div id="spinner" class="spinner"></div>
<div id="message" class="message">{{.Message}}</div>
</body>
</html>
65 changes: 8 additions & 57 deletions internal/docker/idlewatcher/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,35 @@ import (
"bytes"
_ "embed"
"fmt"
"io"
"net/http"
"strings"
"text/template"
)

type templateData struct {
Title string
Message string
RequestHeaders http.Header
SpinnerClass string
CheckRedirectHeader string
Title string
Message string
}

//go:embed html/loading_page.html
var loadingPage []byte
var loadingPageTmpl = template.Must(template.New("loading_page").Parse(string(loadingPage)))

const (
htmlContentType = "text/html; charset=utf-8"
const headerCheckRedirect = "X-GoProxy-Check-Redirect"

errPrefix = "\u1000"

headerGoProxyTargetURL = "X-GoProxy-Target"
headerContentType = "Content-Type"

spinnerClassSpinner = "spinner"
spinnerClassErrorSign = "error"
)

func (w *watcher) makeSuccResp(redirectURL string, resp *http.Response) (*http.Response, error) {
h := make(http.Header)
h.Set("Location", redirectURL)
h.Set("Content-Length", "0")
h.Set(headerContentType, htmlContentType)
return &http.Response{
StatusCode: http.StatusTemporaryRedirect,
Header: h,
Body: http.NoBody,
TLS: resp.TLS,
}, nil
}

func (w *watcher) makeErrResp(errFmt string, args ...any) (*http.Response, error) {
return w.makeResp(errPrefix+errFmt, args...)
}

func (w *watcher) makeResp(format string, args ...any) (*http.Response, error) {
func (w *watcher) makeRespBody(format string, args ...any) []byte {
msg := fmt.Sprintf(format, args...)

data := new(templateData)
data.CheckRedirectHeader = headerCheckRedirect
data.Title = w.ContainerName
data.Message = strings.ReplaceAll(msg, "\n", "<br>")
data.Message = strings.ReplaceAll(data.Message, " ", "&ensp;")
data.RequestHeaders = make(http.Header)
data.RequestHeaders.Add(headerGoProxyTargetURL, "window.location.href")
if strings.HasPrefix(data.Message, errPrefix) {
data.Message = strings.TrimLeft(data.Message, errPrefix)
data.SpinnerClass = spinnerClassErrorSign
} else {
data.SpinnerClass = spinnerClassSpinner
}

buf := bytes.NewBuffer(make([]byte, 128)) // more than enough
err := loadingPageTmpl.Execute(buf, data)
if err != nil { // should never happen
if err != nil { // should never happen in production
panic(err)
}
return &http.Response{
StatusCode: http.StatusAccepted,
Header: http.Header{
headerContentType: {htmlContentType},
"Cache-Control": {
"no-cache",
"no-store",
"must-revalidate",
},
},
Body: io.NopCloser(buf),
ContentLength: int64(buf.Len()),
}, nil
return buf.Bytes()
}
82 changes: 0 additions & 82 deletions internal/docker/idlewatcher/round_trip.go

This file was deleted.

Loading

0 comments on commit 921ce23

Please sign in to comment.