aboutsummaryrefslogtreecommitdiff
path: root/ssh_utils.go
blob: a08e20ce3187073860517bc7fac3279062eed40a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
	"fmt"
	"net"
	"os"
	"os/exec"

	glider_ssh "github.com/gliderlabs/ssh"
	"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 {
		return err
	}

	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) {
			client_public_key := session.PublicKey()
			var client_public_key_string string
			if client_public_key != nil {
				client_public_key_string = string(go_ssh.MarshalAuthorizedKey(client_public_key))
			}
			_ = client_public_key_string

			cmd := session.Command()

			if len(cmd) < 2 {
				fmt.Fprintln(session.Stderr(), "Insufficient arguments")
				return
			}

			if cmd[0] != "git-upload-pack" {
				fmt.Fprintln(session.Stderr(), "Unsupported command")
				return
			}

			fs_path, err := get_repo_path_from_ssh_path(session.Context(), cmd[1])
			if err != nil {
				fmt.Fprintln(session.Stderr(), "Error while getting repo path:", err)
				return
			}

			proc := exec.CommandContext(session.Context(), cmd[0], fs_path)
			proc.Stdin = session
			proc.Stdout = session
			proc.Stderr = session.Stderr()

			err = proc.Start()
			if err != nil {
				fmt.Fprintln(session.Stderr(), "Error while starting process:", err)
				return
			}
			err = proc.Wait()
			if exit_error, ok := err.(*exec.ExitError); ok {
				fmt.Fprintln(session.Stderr(), "Process exited with error", exit_error.ExitCode())
			} else if err != nil {
				fmt.Fprintln(session.Stderr(), "Error while waiting for process:", err)
			}
		},
		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 },
	}

	server.AddHostKey(host_key)

	listener, err := net.Listen(config.SSH.Net, config.SSH.Addr)
	if err != nil {
		return err
	}

	go func() {
		err = server.Serve(listener)
		if err != nil {
			clog.Fatal(1, "Serving SSH: "+err.Error())
		}
	}()

	return nil
}