-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add more login methods
Showing
12 changed files
with
524 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: Build Docker | ||
|
||
on: | ||
push: | ||
pull_request: | ||
branches: [main] | ||
|
||
env: | ||
BEEPER_BRIDGE_TYPE: dummybridge | ||
CI_REGISTRY_IMAGE: "${{ secrets.CI_REGISTRY }}/bridge/dummybridgego" | ||
GHCR_REGISTRY: ghcr.io | ||
GHCR_REGISTRY_IMAGE: "ghcr.io/${{ github.repository }}/go" | ||
GHCR_REGISTRY_LOGINHELPER_IMAGE: "ghcr.io/${{ github.repository }}/loginhelper" | ||
|
||
jobs: | ||
build-docker-dummybridge: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
|
||
- name: Login to Beeper Docker registry | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ secrets.CI_REGISTRY }} | ||
username: ${{ secrets.CI_REGISTRY_USER }} | ||
password: ${{ secrets.CI_REGISTRY_PASSWORD }} | ||
|
||
- name: Login to ghcr | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ env.GHCR_REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Docker Build | ||
uses: docker/build-push-action@v6 | ||
with: | ||
# cache-from: ${{ env.CI_REGISTRY_IMAGE }}:latest | ||
pull: true | ||
file: ./cmd/dummybridge/Dockerfile | ||
tags: | | ||
${{ env.CI_REGISTRY_IMAGE }}:${{ github.sha }} | ||
${{ env.GHCR_REGISTRY_IMAGE }}:${{ github.sha }} | ||
push: true | ||
|
||
build-docker-loginhelper: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
|
||
- name: Login to ghcr | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ env.GHCR_REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Docker Build | ||
uses: docker/build-push-action@v6 | ||
with: | ||
cache-from: ${{ env.GHCR_REGISTRY_LOGINHELPER_IMAGE }}/loginhelper:latest | ||
pull: true | ||
file: ./cmd/dummybridge/Dockerfile | ||
tags: | | ||
${{ env.GHCR_REGISTRY_LOGINHELPER_IMAGE }}:${{ github.sha }} | ||
${{ env.GHCR_REGISTRY_LOGINHELPER_IMAGE }}:latest | ||
push: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.db* | ||
*.yaml | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM golang:1-alpine3.20 AS builder | ||
|
||
COPY . /build | ||
WORKDIR /build | ||
ENV CGO_ENABLED=0 | ||
RUN go build ./cmd/dummybridge -o /usr/bin/dummybridge | ||
|
||
FROM alpine:3.20 | ||
|
||
RUN apk add --no-cache ca-certificates | ||
COPY --from=builder /usr/bin/dummybridge /usr/bin/dummybridge | ||
CMD ["/usr/bin/dummybridge"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM golang:1-alpine3.20 AS builder | ||
|
||
COPY . /build | ||
WORKDIR /build | ||
ENV CGO_ENABLED=0 | ||
RUN go build ./cmd/loginhelper -o /usr/bin/loginhelper | ||
|
||
FROM alpine:3.20 | ||
|
||
RUN apk add --no-cache ca-certificates | ||
COPY --from=builder /usr/bin/loginhelper /usr/bin/loginhelper | ||
CMD ["/usr/bin/loginhelper"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
"encoding/json" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"go.mau.fi/util/exerrors" | ||
) | ||
|
||
//go:embed pages/*.html | ||
var pages embed.FS | ||
|
||
var waiters = make(map[string]chan map[string]string) | ||
var inFlightRequests = make(map[string]int) | ||
var lock sync.RWMutex | ||
|
||
func addWaiter(ip, id string, waiter chan map[string]string) bool { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
if inFlightRequests[ip] > 5 { | ||
return false | ||
} | ||
inFlightRequests[ip]++ | ||
waiters[id] = waiter | ||
return true | ||
} | ||
|
||
func callWaiter(id string, data map[string]string) bool { | ||
lock.RLock() | ||
defer lock.RUnlock() | ||
waiter, ok := waiters[id] | ||
if !ok { | ||
return false | ||
} | ||
waiter <- data | ||
return true | ||
} | ||
|
||
type errorResp struct { | ||
Error string `json:"error"` | ||
} | ||
|
||
func response(w http.ResponseWriter, status int, resp any) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(status) | ||
_ = json.NewEncoder(w).Encode(resp) | ||
} | ||
|
||
func postWait(w http.ResponseWriter, r *http.Request) { | ||
ip := r.Header.Get("X-Forwarded-For") | ||
waiter := make(chan map[string]string) | ||
reqID := r.PathValue("id") | ||
if !addWaiter(ip, reqID, waiter) { | ||
response(w, http.StatusTooManyRequests, errorResp{"Too many in-flight requests"}) | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second) | ||
defer cancel() | ||
defer func() { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
inFlightRequests[ip]-- | ||
delete(waiters, reqID) | ||
}() | ||
select { | ||
case <-ctx.Done(): | ||
response(w, http.StatusTooManyRequests, errorResp{"Wait timeout"}) | ||
case resp := <-waiter: | ||
response(w, http.StatusOK, resp) | ||
} | ||
} | ||
|
||
func postSubmit(w http.ResponseWriter, r *http.Request) { | ||
reqID := r.PathValue("id") | ||
var input map[string]string | ||
err := json.NewDecoder(r.Body).Decode(&input) | ||
if err != nil { | ||
response(w, http.StatusBadRequest, errorResp{"Invalid JSON"}) | ||
return | ||
} | ||
if callWaiter(reqID, input) { | ||
response(w, http.StatusOK, struct{}{}) | ||
} else { | ||
response(w, http.StatusNotFound, errorResp{"Request not found"}) | ||
} | ||
} | ||
|
||
func postSetCookies(w http.ResponseWriter, r *http.Request) { | ||
var input map[string]string | ||
err := json.NewDecoder(r.Body).Decode(&input) | ||
if err != nil { | ||
response(w, http.StatusBadRequest, errorResp{"Invalid JSON"}) | ||
return | ||
} | ||
for k, v := range input { | ||
http.SetCookie(w, &http.Cookie{ | ||
Name: k, | ||
Value: v, | ||
Expires: time.Now().Add(1 * time.Hour), | ||
HttpOnly: true, | ||
Secure: true, | ||
}) | ||
} | ||
response(w, http.StatusOK, struct{}{}) | ||
} | ||
|
||
func main() { | ||
http.Handle("/pages/", http.FileServerFS(pages)) | ||
http.HandleFunc("POST /api/daw_wait/{id}", postWait) | ||
http.HandleFunc("POST /api/daw_submit/{id}", postSubmit) | ||
http.HandleFunc("POST /api/set_cookies", postSetCookies) | ||
exerrors.PanicIfNotNil(http.ListenAndServe(":8080", nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Dummybridge cookie login</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<script> | ||
function saveToCookies() { | ||
fetch("../api/set_cookies", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
username: document.getElementById("username").value, | ||
password: document.getElementById("password").value | ||
}), | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
}).catch(err => alert(err)) | ||
} | ||
</script> | ||
<style> | ||
div { | ||
margin-bottom: 8px; | ||
} | ||
input { | ||
padding: 8px; | ||
} | ||
button { | ||
padding: 8px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div> | ||
<label for="username">Username:</label> | ||
<input type="text" id="username" placeholder="Username"/> | ||
</div> | ||
|
||
<div> | ||
<label for="password">Password:</label> | ||
<input type="password" id="password" placeholder="Password"/> | ||
</div> | ||
|
||
<button id="login" onClick="saveToCookies()">Save to cookies</button> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Dummybridge display & wait login</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<script> | ||
function normalizeCrockfordBase32(input) { | ||
return input.toUpperCase() | ||
.replace("O", "0") | ||
.replace("I", "1") | ||
.replace("L", "1") | ||
.replace("_", "-") | ||
.replace(/[^0123456789ABCDEFGHJKMNPQRSTVWXYZ-]/, "") | ||
} | ||
function submitToWaiter() { | ||
const reqID = document.getElementById("reqid").value | ||
fetch(`../api/daw_submit/${reqID}`, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
username: document.getElementById("username").value, | ||
password: document.getElementById("password").value | ||
}), | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
}).catch(err => alert(err)) | ||
} | ||
</script> | ||
<style> | ||
div { | ||
margin-bottom: 10px; | ||
} | ||
input { | ||
padding: 8px; | ||
} | ||
button { | ||
padding: 8px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div> | ||
<label for="reqid">Request ID:</label> | ||
<input type="text" id="reqid" oninput="this.value = normalizeCrockfordBase32(this.value)" placeholder="ABC-123-DEF"/> | ||
</div> | ||
|
||
<div> | ||
<label for="username">Username:</label> | ||
<input type="text" id="username" placeholder="Username"/> | ||
</div> | ||
|
||
<div> | ||
<label for="password">Password:</label> | ||
<input type="password" id="password" placeholder="Password"/> | ||
</div> | ||
|
||
<button id="login" onClick="submitToWaiter()">Submit to waiter</button> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Dummybridge localstorage login</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<script> | ||
function saveToLocalstorage() { | ||
localStorage.setItem("username", document.getElementById("username").value) | ||
localStorage.setItem("password", document.getElementById("password").value) | ||
} | ||
</script> | ||
<style> | ||
div { | ||
margin-bottom: 10px; | ||
} | ||
input { | ||
padding: 8px; | ||
} | ||
button { | ||
padding: 8px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div> | ||
<label for="username">Username:</label> | ||
<input type="text" id="username" placeholder="Username"/> | ||
</div> | ||
|
||
<div> | ||
<label for="password">Password:</label> | ||
<input type="password" id="password" placeholder="Password"/> | ||
</div> | ||
|
||
<button id="login" onClick="saveToLocalstorage()">Save to cookies</button> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters