Sign in to T5DE's hub and report presence and community outfits
CI / build (push) Successful in 1m6s
CI / build (push) Successful in 1m6s
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
50e4db8521
commit
7887eada61
9 files changed
+476
-2
No files matched your search
+66
-1
@@ -5,12 +5,19 @@
|
||||
//
|
||||
// It listens on a free port of 127.0.0.1 and, once it's listening, writes {"address", "token"} to the ready file
|
||||
// (written whole, by renaming), which the client reads.
|
||||
//
|
||||
// t5de-proxy signin [-hub URL]
|
||||
//
|
||||
// signs the proxy in to T5DE's hub: it shows a code to enter on the hub's website and saves the sign-in (encrypted
|
||||
// for the Windows user). From then on the proxy connects to the hub as it runs.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -18,17 +25,28 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.t5de.dev/t5de/t5de-proxy/internal/buildinfo"
|
||||
"git.t5de.dev/t5de/t5de-proxy/internal/hub"
|
||||
"git.t5de.dev/t5de/t5de-proxy/internal/parent"
|
||||
"git.t5de.dev/t5de/t5de-proxy/internal/products"
|
||||
"git.t5de.dev/t5de/t5de-proxy/internal/proxy"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "signin" {
|
||||
if err := signin(os.Args[2:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "t5de-proxy signin:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
listen := flag.String("listen", "127.0.0.1:0", "address to listen on")
|
||||
ready := flag.String("ready", "", "file to write the address and token to once listening")
|
||||
parentPID := flag.Int("parent", 0, "exit when this process exits")
|
||||
@@ -107,12 +125,22 @@ func run(logger *log.Logger, listen, ready string, parentPID int, trace bool, pa
|
||||
}()
|
||||
}
|
||||
handler := &proxy.Server{Token: token, Log: logger, Trace: trace}
|
||||
if prods, err := products.New(packs, data, logger.Printf); err != nil {
|
||||
prods, err := products.New(packs, data, logger.Printf)
|
||||
if err != nil {
|
||||
logger.Printf("community products are off: %v", err)
|
||||
} else {
|
||||
handler.Interceptor, handler.IMQFilter = prods, prods
|
||||
logger.Printf("community products from %s", strings.Join(packs, ", "))
|
||||
}
|
||||
if saved, err := hub.Load(filepath.Join(data, "hub.json")); err == nil {
|
||||
client := hub.New(filepath.Join(data, "hub.json"), saved, logger)
|
||||
if prods != nil {
|
||||
prods.Observer = client
|
||||
}
|
||||
go client.Run(context.Background())
|
||||
} else if !errors.Is(err, hub.ErrSignedOut) {
|
||||
logger.Printf("hub: %v", err)
|
||||
}
|
||||
server := &http.Server{
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: 30 * time.Second,
|
||||
@@ -133,3 +161,40 @@ func writeReady(path, address, token string) error {
|
||||
}
|
||||
return os.Rename(tmp, path)
|
||||
}
|
||||
|
||||
// defaultHub is T5DE's hub.
|
||||
const defaultHub = "https://hub.t5de.dev"
|
||||
|
||||
func signin(args []string) error {
|
||||
fs := flag.NewFlagSet("signin", flag.ExitOnError)
|
||||
hubURL := fs.String("hub", envOr("T5DE_HUB", defaultHub), "the hub's address (or $T5DE_HUB)")
|
||||
data := fs.String("data", "", "where the proxy keeps its state (default: %APPDATA%/T5DE/proxy)")
|
||||
fs.Parse(args)
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer stop()
|
||||
saved, err := hub.SignIn(ctx, *hubURL, func(code, page string) {
|
||||
fmt.Printf("To sign T5DE in, enter this code at %s:\n\n %s\n\nOpening it in your browser...\n", page, code)
|
||||
openBrowser(page)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := saved.Save(filepath.Join(dataDir(*data), "hub.json")); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Signed in. T5DE connects to the hub the next time the client starts.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func envOr(name, fallback string) string {
|
||||
if v := os.Getenv(name); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func openBrowser(page string) {
|
||||
if runtime.GOOS == "windows" {
|
||||
exec.Command("rundll32", "url.dll,FileProtocolHandler", page).Start()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user