From 1364d688e454454258c6c97b1dc844cc94a67a9e Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 13 Feb 2025 15:16:11 +0800 Subject: [PATCH] ssh_url_generation.go, etc.: Add config ssh.root and use it Detecting it based on HTTP host name is definitely unreliable. Just add a configuration option and it should work. --- config.go | 1 + forge.scfg | 1 + http_handle_repo_index.go | 3 +-- http_handle_repo_info.go | 3 +-- ssh_server.go | 3 +++ ssh_url_generation.go | 11 +++++++++++ diff --git a/config.go b/config.go index dbe06dee30a67ff6da58c46652e219a7836c00b8..525581a37f18b8d85ad4d9ffcf14eb34b8fb209b 100644 --- a/config.go +++ b/config.go @@ -24,6 +24,7 @@ SSH struct { Net string `scfg:"net"` Addr string `scfg:"addr"` Key string `scfg:"key"` + Root string `scfg:"root"` } `scfg:"ssh"` Git struct { Root string `scfg:"root"` diff --git a/forge.scfg b/forge.scfg index 27f6fdc3c3474df1b6a5b326b4bdf4e7a231922b..11facbab8dda830492ade2fdd47087d959716c63 100644 --- a/forge.scfg +++ b/forge.scfg @@ -8,6 +8,7 @@ ssh { net tcp addr :2222 key /etc/ssh/ssh_host_ed25519_key + root ssh://forge.example.org } db { diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index 8a6f7b88a27019581c4a62ff38b76879f451a7a7..25bc10787e14ccbad0ca44eb4b6b0fc2f34c6467 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -2,7 +2,6 @@ package main import ( "net/http" - "net/url" ) func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string]any) { @@ -40,7 +39,7 @@ params["readme_filename"], params["readme"] = render_readme_at_tree(tree) params["files"] = build_display_git_tree(tree) - params["clone_url"] = "ssh://" + r.Host + "/" + url.PathEscape(group_name) + "/:/repos/" + url.PathEscape(repo_name) + params["clone_url"] = generate_ssh_remote_url(group_name, repo_name) err = templates.ExecuteTemplate(w, "repo_index", params) if err != nil { diff --git a/http_handle_repo_info.go b/http_handle_repo_info.go index e220f18e375b0e895a393665090c64773e877a13..f5b9dfd5152cb7a2b20dd503c9a3bf26a6d3ff41 100644 --- a/http_handle_repo_info.go +++ b/http_handle_repo_info.go @@ -2,9 +2,8 @@ package main import ( "net/http" - "net/url" ) func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string]any) { - http.Error(w, "\x1b[1;93mHi! We do not support Git operations over HTTP yet.\x1b[0m\n\x1b[1;93mMeanwhile, please use ssh by simply replacing the scheme with \"ssh://\":\x1b[0m\n\x1b[1;93mssh://"+r.Host+"/"+url.PathEscape(params["group_name"].(string))+"/:/repos/"+url.PathEscape(params["repo_name"].(string))+"\x1b[0m", http.StatusNotImplemented) + http.Error(w, "\x1b[1;93mHi! We do not support Git operations over HTTP yet.\x1b[0m\n\x1b[1;93mMeanwhile, please use ssh by simply replacing the scheme with \"ssh://\":\x1b[0m\n\x1b[1;93m"+ generate_ssh_remote_url(params["group_name"].(string), params["repo_name"].(string)) + "\x1b[0m", http.StatusNotImplemented) } diff --git a/ssh_server.go b/ssh_server.go index 8cea1a985ecc3e85161311dcb273d9f95d8b3a92..b226b7c732172fe727bf450553588aa312c810b3 100644 --- a/ssh_server.go +++ b/ssh_server.go @@ -80,6 +80,9 @@ } }, PublicKeyHandler: func(ctx glider_ssh.Context, key glider_ssh.PublicKey) bool { return true }, KeyboardInteractiveHandler: func(ctx glider_ssh.Context, challenge go_ssh.KeyboardInteractiveChallenge) bool { return true }, + // It is intentional that we do not check any credentials and accept all connections. + // This allows all users to connect and clone repositories; when pushing is added later, + // we will check their public key in the session handler, not in the auth handlers. } server.AddHostKey(host_key) diff --git a/ssh_url_generation.go b/ssh_url_generation.go new file mode 100644 index 0000000000000000000000000000000000000000..0cf4c1e722a532ad13664fe7ced73288bece3716 --- /dev/null +++ b/ssh_url_generation.go @@ -0,0 +1,11 @@ +package main + +import ( + "net/url" + "strings" +) + +func generate_ssh_remote_url(group_name, repo_name string) string { + return strings.TrimSuffix(config.SSH.Root, "/")+"/"+url.PathEscape(group_name)+"/:/repos/"+url.PathEscape(repo_name) +} + -- 2.48.1