navid/veinsforge / commit
221a1bffd4fe
Navid M <navidmomtahen@gmail.com> · 2026-11-27
parents: c446c7fc1c70
Browse files at this commit · Download source
--- a/internal/web/admin_handlers.go+++ b/internal/web/admin_handlers.go@@ -17,14 +17,15 @@ } stats, _ := s.data.SiteStats(r.Context()) audit, _ := s.data.ListAudit(r.Context(), 0, 20)- running, lastErr, pin, address := "", "", "", ""+ running, lastErr, pin := "", "", "" if s.native != nil {- ok, e, p, a := s.native.Status()+ ok, e, p, _ := s.native.Status() if ok { running = "running" }- lastErr, pin, address = e, p, a+ lastErr, pin = e, p }+ address := s.nativeAddress(r) page := s.page(r, "Admin") page.Data = map[string]any{ "Stats": stats, "Audit": audit, "NativeState": running,--- a/internal/web/helpers.go+++ b/internal/web/helpers.go@@ -5,6 +5,7 @@ "context" "encoding/base64" "fmt"+ "net" "net/http" "net/url" "path"@@ -84,6 +85,44 @@ return "/" } return "/" + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)+}++// nativeAddress returns the host:port clients should use for the native+// protocol. An explicitly configured public host wins; when that host is only+// a loopback/default, prefer the host the browser actually reached us on so+// the UI never advertises "your-host" or 127.0.0.1 to remote users.+func (s *Server) nativeAddress(r *http.Request) string {+ addr := s.cfg.NativeAddress()+ host, port, err := net.SplitHostPort(addr)+ if err != nil {+ return addr+ }+ host = strings.Trim(host, "[]")+ if r != nil && isLoopbackHost(host) {+ requested := r.Host+ if h, _, splitErr := net.SplitHostPort(requested); splitErr == nil {+ requested = h+ }+ requested = strings.Trim(requested, "[]")+ if requested != "" && !isLoopbackHost(requested) {+ host = requested+ }+ }+ return net.JoinHostPort(host, port)+}++func isLoopbackHost(host string) bool {+ if host == "" || strings.EqualFold(host, "localhost") {+ return true+ }+ if ip := net.ParseIP(host); ip != nil {+ return ip.IsLoopback() || ip.IsUnspecified()+ }+ return false+}++func (s *Server) nativeCloneURL(r *http.Request, owner, name string) string {+ return "veins://" + s.nativeAddress(r) + "/" + url.PathEscape(owner) + "/" + url.PathEscape(name) } func ownerURL(repo *data.Repo) string {--- a/internal/web/helpers_test.go+++ b/internal/web/helpers_test.go@@ -1,6 +1,48 @@ package web -import "testing"+import (+ "net/http"+ "testing"++ "veinsforge/internal/config"+)++func TestNativeAddressPrefersPublicHost(t *testing.T) {+ configured := &Server{cfg: config.Config{+ NativeHost: "forge.example.com",+ NativeListen: "0.0.0.0:7419",+ }}+ req, _ := http.NewRequest(http.MethodGet, "http://internal.example.net/", nil)+ if got := configured.nativeAddress(req); got != "forge.example.com:7419" {+ t.Fatalf("configured host: got %q", got)+ }+}++func TestNativeAddressUsesRequestHostOverLoopback(t *testing.T) {+ s := &Server{cfg: config.Config{+ NativeHost: "127.0.0.1",+ NativeListen: "127.0.0.1:7419",+ }}+ req, _ := http.NewRequest(http.MethodGet, "http://forge.example.com:8080/", nil)+ if got := s.nativeAddress(req); got != "forge.example.com:7419" {+ t.Fatalf("loopback fallback: got %q", got)+ }+ req, _ = http.NewRequest(http.MethodGet, "http://127.0.0.1:8080/", nil)+ if got := s.nativeAddress(req); got != "127.0.0.1:7419" {+ t.Fatalf("loopback request: got %q", got)+ }+}++func TestNativeCloneURL(t *testing.T) {+ s := &Server{cfg: config.Config{+ NativeHost: "127.0.0.1",+ NativeListen: "127.0.0.1:7419",+ }}+ req, _ := http.NewRequest(http.MethodGet, "http://forge.example.com/", nil)+ if got := s.nativeCloneURL(req, "alice", "demo"); got != "veins://forge.example.com:7419/alice/demo" {+ t.Fatalf("clone URL = %q", got)+ }+} func TestBuildTree(t *testing.T) { files := []string{"README.md", "src/main.go", "src/util/a.go", "src/util/b.go", "z.txt"}--- a/internal/web/repo_browse.go+++ b/internal/web/repo_browse.go@@ -80,7 +80,7 @@ dataMap["Starred"] = s.data.IsStarred(r.Context(), repo.ID, u.ID) } dataMap["StarCount"] = repo.Stars- dataMap["NativeURL"] = s.cfg.NativeURL(repo.OwnerName, repo.Name)+ dataMap["NativeURL"] = s.nativeCloneURL(r, repo.OwnerName, repo.Name) if s.native != nil { _, _, pin, _ := s.native.Status() dataMap["NativePin"] = pin--- a/internal/web/server.go+++ b/internal/web/server.go@@ -45,17 +45,18 @@ } type Page struct {- Title string- Path string- User *data.User- CSRF string- Notice string- Error string- SiteName string- SiteDesc string- Data any- NativePin string- NativeURL string+ Title string+ Path string+ User *data.User+ CSRF string+ Notice string+ Error string+ SiteName string+ SiteDesc string+ Data any+ NativePin string+ NativeURL string+ NativeAddress string } func New(cfg config.Config, store *data.Store, runner *vn.Runner, native *vn.NativeServer, logger *log.Logger) (*Server, error) {@@ -224,6 +225,9 @@ } if page.NativePin == "" && s.native != nil { _, _, page.NativePin, page.NativeURL = s.native.Status()+ }+ if page.NativeAddress == "" {+ page.NativeAddress = s.nativeAddress(r) } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(status)--- a/internal/web/templates/home.html+++ b/internal/web/templates/home.html@@ -4,7 +4,7 @@ <p class="muted">{{.Data.Stats.Users}} accounts · {{.Data.Stats.PublicRepos}} public repositories · {{.Data.Stats.Issues}} open issues</p> <h2>Clone</h2>-<pre><code>vn clone veins://your-host:7419/owner/repo -key:your-key</code></pre>+<pre><code>vn clone veins://{{.NativeAddress}}/owner/repo -key:your-key</code></pre> <p class="small">Register an Ed25519 public key in <a href="/settings">settings</a>, then select it once with <code>vn auth use your-key</code>. Public repositories can be cloned anonymously.</p> {{if .Data.UserRepos}}--- a/internal/web/templates/settings_user.html+++ b/internal/web/templates/settings_user.html@@ -34,7 +34,7 @@ <p class="small muted">Ed25519 keys used by <code>vn clone</code>, <code>vn fetch</code>, and <code>vn push</code>.</p> <p class="small">To use one key for every repository on this server, run once:</p> <pre><code>vn auth use work</code></pre>-<p class="small muted">For a single server instead: <code>vn auth use work forge.example.com:7419</code></p>+<p class="small muted">For a single server instead: <code>vn auth use work {{.NativeAddress}}</code></p> {{if .Data.Keys}} <table class="table"> <thead><tr><th>Name</th><th>Fingerprint</th><th>Public key</th><th>Added</th><th></th></tr></thead>