| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package dockerimg |
| 2 | |
| 3 | import ( |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 4 | "crypto/subtle" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 5 | "fmt" |
| 6 | "log/slog" |
| 7 | "net/http" |
| 8 | "net/http/cgi" |
| 9 | "os/exec" |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 10 | "runtime" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | type gitHTTP struct { |
| 15 | gitRepoRoot string |
| Josh Bleecher Snyder | 9f6a998 | 2025-04-22 17:34:15 -0700 | [diff] [blame] | 16 | pass []byte |
| Josh Bleecher Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 17 | browserC chan bool // browser launch requests |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | func (g *gitHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 21 | defer func() { |
| 22 | if err := recover(); err != nil { |
| 23 | slog.ErrorContext(r.Context(), "gitHTTP.ServeHTTP panic", slog.Any("recovered_err", err)) |
| 24 | |
| 25 | // Return an error response to the client |
| 26 | http.Error(w, fmt.Sprintf("panic: %v\n", err), http.StatusInternalServerError) |
| 27 | } |
| 28 | }() |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 29 | |
| 30 | // Get the Authorization header |
| 31 | username, password, ok := r.BasicAuth() |
| 32 | |
| 33 | // Check if credentials were provided |
| 34 | if !ok { |
| 35 | // No credentials provided, return 401 Unauthorized |
| 36 | w.Header().Set("WWW-Authenticate", `Basic realm="Sketch Git Repository"`) |
| 37 | http.Error(w, "Unauthorized", http.StatusUnauthorized) |
| 38 | slog.InfoContext(r.Context(), "githttp: denied (basic auth)", "remote addr", r.RemoteAddr) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 39 | return |
| 40 | } |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 41 | |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 42 | // Check if credentials are valid |
| Josh Bleecher Snyder | 9f6a998 | 2025-04-22 17:34:15 -0700 | [diff] [blame] | 43 | if username != "sketch" || subtle.ConstantTimeCompare([]byte(password), g.pass) != 1 { |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 44 | w.Header().Set("WWW-Authenticate", `Basic realm="Git Repository"`) |
| 45 | http.Error(w, "Unauthorized", http.StatusUnauthorized) |
| 46 | slog.InfoContext(r.Context(), "githttp: denied (basic auth)", "remote addr", r.RemoteAddr) |
| 47 | return |
| 48 | } |
| 49 | |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 50 | // TODO: real mux? |
| 51 | if strings.HasPrefix(r.URL.Path, "/browser") { |
| 52 | if r.Method != http.MethodPost { |
| 53 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 54 | return |
| 55 | } |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 56 | defer r.Body.Close() |
| Josh Bleecher Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 57 | |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 58 | select { |
| Josh Bleecher Snyder | 9957046 | 2025-05-05 10:26:14 -0700 | [diff] [blame] | 59 | case g.browserC <- true: |
| 60 | slog.InfoContext(r.Context(), "open browser requested") |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 61 | w.WriteHeader(http.StatusOK) |
| 62 | default: |
| 63 | http.Error(w, "Too many browser launch requests", http.StatusTooManyRequests) |
| 64 | } |
| 65 | return |
| 66 | } |
| 67 | |
| Philip Zeyliger | 5e227dd | 2025-04-21 15:55:29 -0700 | [diff] [blame] | 68 | if runtime.GOOS == "darwin" { |
| 69 | // On the Mac, Docker connections show up from localhost. On Linux, the docker |
| 70 | // network is more arbitrary, so we don't do this additional check there. |
| 71 | if !strings.HasPrefix(r.RemoteAddr, "127.0.0.1:") { |
| 72 | slog.InfoContext(r.Context(), "githttp: denied", "remote addr", r.RemoteAddr) |
| 73 | http.Error(w, "no", http.StatusUnauthorized) |
| 74 | return |
| 75 | } |
| 76 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 77 | gitBin, err := exec.LookPath("git") |
| 78 | if err != nil { |
| 79 | http.Error(w, "no git: "+err.Error(), http.StatusInternalServerError) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | w.Header().Set("Cache-Control", "no-cache") |
| 84 | h := &cgi.Handler{ |
| 85 | Path: gitBin, |
| 86 | Args: []string{"http-backend"}, |
| 87 | Dir: g.gitRepoRoot, |
| 88 | Env: []string{ |
| 89 | "GIT_PROJECT_ROOT=" + g.gitRepoRoot, |
| 90 | "PATH_INFO=" + r.URL.Path, |
| 91 | "QUERY_STRING=" + r.URL.RawQuery, |
| 92 | "REQUEST_METHOD=" + r.Method, |
| 93 | "GIT_HTTP_EXPORT_ALL=true", |
| 94 | "GIT_HTTP_ALLOW_REPACK=true", |
| 95 | "GIT_HTTP_ALLOW_PUSH=true", |
| 96 | "GIT_HTTP_VERBOSE=1", |
| 97 | }, |
| 98 | } |
| 99 | h.ServeHTTP(w, r) |
| 100 | } |