blob: 6d2d7e00f5e4d009b907a2be4a9892c8afce777c [file] [log] [blame]
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04001package main
2
3import (
4 "fmt"
5 "golang.org/x/crypto/ssh"
6 "net"
7 "os"
8
9 "github.com/go-git/go-billy/v5/memfs"
10 "github.com/go-git/go-git/v5"
11 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
12 "github.com/go-git/go-git/v5/storage/memory"
13 "github.com/spf13/cobra"
14
15 "github.com/giolekva/pcloud/core/installer"
16 "github.com/giolekva/pcloud/core/installer/welcome"
17)
18
19var welcomeFlags struct {
20 repo string
21 sshKey string
22 port int
23}
24
25func welcomeCmd() *cobra.Command {
26 cmd := &cobra.Command{
27 Use: "welcome",
28 RunE: welcomeCmdRun,
29 }
30 cmd.Flags().StringVar(
31 &welcomeFlags.repo,
32 "repo-addr",
33 "",
34 "",
35 )
36 cmd.Flags().StringVar(
37 &welcomeFlags.sshKey,
38 "ssh-key",
39 "",
40 "",
41 )
42 cmd.Flags().IntVar(
43 &welcomeFlags.port,
44 "port",
45 8080,
46 "",
47 )
48 return cmd
49}
50
51func welcomeCmdRun(cmd *cobra.Command, args []string) error {
52 sshKey, err := os.ReadFile(welcomeFlags.sshKey)
53 if err != nil {
54 return err
55 }
56 auth := authSSH(sshKey)
57 repo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
58 URL: welcomeFlags.repo,
59 Auth: auth,
60 RemoteName: "origin",
61 ReferenceName: "refs/heads/master",
62 Depth: 1,
63 InsecureSkipTLS: true,
64 Progress: os.Stdout,
65 })
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040066 nsCreator, err := newNSCreator()
67 if err != nil {
68 return err
69 }
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040070 s := welcome.NewServer(
71 welcomeFlags.port,
72 installer.NewRepoIO(repo, auth.Signer),
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040073 nsCreator,
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040074 )
75 s.Start()
76 return nil
77}
78
79func authSSH(pemBytes []byte) *gitssh.PublicKeys {
80 a, err := gitssh.NewPublicKeys("git", pemBytes, "")
81 if err != nil {
82 panic(err)
83 }
84 a.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
85 // TODO(giolekva): verify server public key
86 fmt.Printf("--- %+v\n", ssh.MarshalAuthorizedKey(key))
87 return nil
88 }
89 return a
90}