From 11a8f2889e874ecebdbc49a6887918c7b043c503 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 12 Feb 2025 22:37:51 +0800 Subject: [PATCH] {ssh,global}.go, index.html: Add global data containing ssh fp --- global.go | 6 ++++++ handle_group_index.go | 1 + handle_index.go | 1 + handle_repo_commit.go | 1 + handle_repo_index.go | 3 ++- handle_repo_info.go | 2 +- handle_repo_log.go | 1 + handle_repo_raw.go | 1 + handle_repo_tree.go | 1 + ssh.go | 8 ++++++++ templates/index.html.tmpl | 22 +++++++++++++++++++--- diff --git a/global.go b/global.go new file mode 100644 index 0000000000000000000000000000000000000000..3e06171d332eece5ae016773d57c5326971cf9fb --- /dev/null +++ b/global.go @@ -0,0 +1,6 @@ +package main + +var global_data = map[string]any { + "server_public_key_string": &server_public_key_string, + "server_public_key_fingerprint": &server_public_key_fingerprint, +} diff --git a/handle_group_index.go b/handle_group_index.go index 0bb4a5783e790c8a1b6f515fac0c6b18f20e01c4..2a8e1cad25f4a44c5b16bdbcd74ab9c7db25d582 100644 --- a/handle_group_index.go +++ b/handle_group_index.go @@ -6,6 +6,7 @@ ) func handle_group_repos(w http.ResponseWriter, r *http.Request, params map[string]string) { data := make(map[string]any) + data["global"] = global_data group_name := params["group_name"] data["group_name"] = group_name diff --git a/handle_index.go b/handle_index.go index c6315466f14d18f54ac0039d56cefc67f362630b..bf36f98865039d4647601b9e7b3e1145fbb036f4 100644 --- a/handle_index.go +++ b/handle_index.go @@ -6,6 +6,7 @@ ) func handle_index(w http.ResponseWriter, r *http.Request) { data := make(map[string]any) + data["global"] = global_data rows, err := database.Query(r.Context(), "SELECT name FROM groups") if err != nil { diff --git a/handle_repo_commit.go b/handle_repo_commit.go index b567baa78e2895f25ccedb2bad233062c7328cea..d79b088a9c945a8e17fa7ceafe2e9f36699d5156 100644 --- a/handle_repo_commit.go +++ b/handle_repo_commit.go @@ -18,6 +18,7 @@ } func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]string) { data := make(map[string]any) + data["global"] = global_data group_name, repo_name, commit_id_specified_string := params["group_name"], params["repo_name"], params["commit_id"] data["group_name"], data["repo_name"] = group_name, repo_name repo, err := open_git_repo(r.Context(), group_name, repo_name) diff --git a/handle_repo_index.go b/handle_repo_index.go index 1c03ece62091a8be2704757e0b44a8130f3b3c83..0c7b5702444d80473dafc7c6e39bb119241ef1ee 100644 --- a/handle_repo_index.go +++ b/handle_repo_index.go @@ -7,6 +7,7 @@ ) func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string]string) { data := make(map[string]any) + data["global"] = global_data group_name, repo_name := params["group_name"], params["repo_name"] data["group_name"], data["repo_name"] = group_name, repo_name repo, err := open_git_repo(r.Context(), group_name, repo_name) @@ -41,7 +42,7 @@ data["readme_filename"], data["readme"] = render_readme_at_tree(tree) data["files"] = build_display_git_tree(tree) - data["clone_url"] = "ssh://" + r.Host + "/" + url.PathEscape(params["group_name"]) + "/:/repos/" + url.PathEscape(params["repo_name"]) + data["clone_url"] = "ssh://" + r.Host + "/" + url.PathEscape(params["group_name"]) + "/:/repos/" + url.PathEscape(params["repo_name"]) err = templates.ExecuteTemplate(w, "repo_index", data) if err != nil { diff --git a/handle_repo_info.go b/handle_repo_info.go index 4b013713ee1eea83f23bb5b7e058e3bbc31ce18f..5dd92e97a93906c06ae6da3ed0548e754b51f51a 100644 --- a/handle_repo_info.go +++ b/handle_repo_info.go @@ -6,5 +6,5 @@ "net/url" ) func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string]string) { - 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"]) + "/:/repos/" + url.PathEscape(params["repo_name"]) + "\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;93mssh://"+r.Host+"/"+url.PathEscape(params["group_name"])+"/:/repos/"+url.PathEscape(params["repo_name"])+"\x1b[0m", http.StatusNotImplemented) } diff --git a/handle_repo_log.go b/handle_repo_log.go index 1c32862ec658589c3fc113331ff3ce3098b24a47..6a3f44600cc3d7b26c19ef2c729514890840762d 100644 --- a/handle_repo_log.go +++ b/handle_repo_log.go @@ -9,6 +9,7 @@ // TODO: I probably shouldn't include *all* commits here... func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]string) { data := make(map[string]any) + data["global"] = global_data group_name, repo_name, ref_name := params["group_name"], params["repo_name"], params["ref"] data["group_name"], data["repo_name"], data["ref"] = group_name, repo_name, ref_name repo, err := open_git_repo(r.Context(), group_name, repo_name) diff --git a/handle_repo_raw.go b/handle_repo_raw.go index 4cf7d1a1e1db0fb5eee39afdf20ecd71d132bf85..24b579414b23c97a0ad1de73e381815891a761e8 100644 --- a/handle_repo_raw.go +++ b/handle_repo_raw.go @@ -11,6 +11,7 @@ ) func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]string) { data := make(map[string]any) + data["global"] = global_data raw_path_spec := params["rest"] group_name, repo_name, path_spec := params["group_name"], params["repo_name"], strings.TrimSuffix(raw_path_spec, "/") diff --git a/handle_repo_tree.go b/handle_repo_tree.go index 8076ed6f75cdefb4a0a72cb1282fa3b26354e8d3..1dc06a83a628b6d3539969e3e73fbabf05c1b4d2 100644 --- a/handle_repo_tree.go +++ b/handle_repo_tree.go @@ -16,6 +16,7 @@ ) func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string]string) { data := make(map[string]any) + data["global"] = global_data raw_path_spec := params["rest"] group_name, repo_name, path_spec := params["group_name"], params["repo_name"], strings.TrimSuffix(raw_path_spec, "/") ref_type, ref_name, err := get_param_ref_and_type(r) diff --git a/ssh.go b/ssh.go index 26370c75606500fb9d3b411be5e11645373fb506..a08e20ce3187073860517bc7fac3279062eed40a 100644 --- a/ssh.go +++ b/ssh.go @@ -11,6 +11,10 @@ "go.lindenii.runxiyu.org/lindenii-common/clog" go_ssh "golang.org/x/crypto/ssh" ) +var server_public_key_string string +var server_public_key_fingerprint string +var server_public_key go_ssh.PublicKey + func serve_ssh() error { host_key_bytes, err := os.ReadFile(config.SSH.Key) if err != nil { @@ -21,6 +25,10 @@ host_key, err := go_ssh.ParsePrivateKey(host_key_bytes) if err != nil { return err } + + server_public_key = host_key.PublicKey() + server_public_key_string = string(go_ssh.MarshalAuthorizedKey(server_public_key)) + server_public_key_fingerprint = string(go_ssh.FingerprintSHA256(server_public_key)) server := &glider_ssh.Server{ Handler: func(session glider_ssh.Session) { diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index 505ea70b6b22a0de21b7e3c03e98c62dbf85a661..e88b568e6b417860d3d36e71755f3500bb294912 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -3,13 +3,14 @@ {{ template "head_common" . }} - Groups – Lindenii Forge + Index – Lindenii Forge
-

+

Lindenii Forge

+

Groups -

+ +

+ Info +

+ + + + + + + + + + + +
SSH Public Key{{ .global.server_public_key_string }}
SSH Fingerprint{{ .global.server_public_key_fingerprint }}