💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › d842f15e522c18bbf3797e587d… captured on 2023-01-29 at 16:01:07. Gemini links have been rewritten to link to archived content

View Raw

More Information

➡️ Next capture (2023-03-20)

-=-=-=-=-=-=-

0 package main

1

2 import (

3 "fmt"

4 "log"

5 "os"

6 "strconv"

7 "strings"

8

9 "golang.org/x/crypto/ssh/terminal"

10

11 "gemigit/access"

12 "gemigit/auth"

13 "gemigit/config"

14 "gemigit/db"

15 "gemigit/httpgit"

16 "gemigit/repo"

17 "gemigit/gmi"

18

19 "github.com/pitr/gig"

20 )

21

22 const textRegistrationSuccess =

23 "# Your registration was completed successfully\n\n" +

24 "=> /login Login now"

25

26 func showFileContent(content string) string {

27 lines := strings.Split(content, "\n")

28 file := ""

29 for i, line := range lines {

30 number := strconv.Itoa(i)

31 space := 6 - len(number)

32 if space < 1 {

33 space = 1

34 }

35 file += number + strings.Repeat(" ", space)

36 file += line + "\n"

37 }

38 return strings.Replace(file, "%", "%%", -1)

39 }

40

41 func main() {

42

43 if err := config.LoadConfig(); err != nil {

44 log.Fatalln(err.Error())

45 }

46

47 if len(os.Args) > 1 {

48 switch (os.Args[1]) {

49 case "chpasswd":

50 if (config.Cfg.Ldap.Enabled) {

51 fmt.Println("Not valid when LDAP is enabled")

52 return

53 }

54 if len(os.Args) < 3 {

55 fmt.Println(os.Args[0] +

56 " chpasswd <username>")

57 return

58 }

59 fmt.Print("New Password : ")

60 password, err := terminal.ReadPassword(0)

61 fmt.Print("\n")

62 if err != nil {

63 log.Fatalln(err.Error())

64 }

65 err = db.Init(config.Cfg.Database.Type,

66 config.Cfg.Database.Url, false)

67 if err != nil {

68 log.Fatalln(err.Error())

69 }

70 defer db.Close()

71 if err := db.ChangePassword(os.Args[2],

72 string(password));

73 err != nil {

74 fmt.Println(err.Error())

75 return

76 }

77 fmt.Println(os.Args[2] + "'s password changed")

78 return

79 case "register":

80 if (config.Cfg.Ldap.Enabled) {

81 fmt.Println("Not valid when LDAP is enabled")

82 return

83 }

84 if len(os.Args) < 3 {

85 fmt.Println(os.Args[0] +

86 " register <username>")

87 return

88 }

89 fmt.Print("Password : ")

90 password, err := terminal.ReadPassword(0)

91 fmt.Print("\n")

92 if err != nil {

93 log.Fatalln(err.Error())

94 }

95 err = db.Init(config.Cfg.Database.Type,

96 config.Cfg.Database.Url, false)

97 if err != nil {

98 log.Fatalln(err.Error())

99 }

100 defer db.Close()

101 if err := db.Register(os.Args[2], string(password));

102 err != nil {

103 fmt.Println(err.Error())

104 return

105 }

106 fmt.Println("User " + os.Args[2] + " created")

107 return

108 case "rmuser":

109 if len(os.Args) < 3 {

110 fmt.Println(os.Args[0] + " rmuser <username>")

111 return

112 }

113 err := db.Init(config.Cfg.Database.Type,

114 config.Cfg.Database.Url, false)

115 if err != nil {

116 log.Fatalln(err.Error())

117 }

118 defer db.Close()

119 err = db.DeleteUser(os.Args[2])

120 if err != nil {

121 log.Fatalln(err)

122 }

123 fmt.Println("User " + os.Args[2] +

124 " deleted successfully")

125 return

126 case "init":

127 err := db.Init(config.Cfg.Database.Type,

128 config.Cfg.Database.Url, true)

129 if err != nil {

130 log.Fatalln(err.Error())

131 }

132 defer db.Close()

133 return

134 }

135 fmt.Println("usage: " + os.Args[0] + " [command]")

136 fmt.Println("commands :")

137 fmt.Println("\tchpasswd <username> - Change user password")

138 fmt.Println("\tregister <username> - Create user")

139 fmt.Println("\trmuser <username> - Remove user")

140 fmt.Println("\tinit - Initialize database")

141 return

142 }

143

144 log.SetFlags(log.LstdFlags | log.Lshortfile)

145

146 if err := access.Init(); err != nil {

147 log.Fatalln(err.Error())

148 }

149

150 if err := gmi.LoadTemplate(config.Cfg.Gemini.Templates); err != nil {

151 log.Fatalln(err.Error())

152 }

153

154 err := db.Init(config.Cfg.Database.Type,

155 config.Cfg.Database.Url, false)

156 if err != nil {

157 log.Fatalln(err.Error())

158 }

159 defer db.Close()

160 if err := repo.Init("repos"); err != nil {

161 log.Fatalln(err.Error())

162 }

163

164 if !config.Cfg.Git.Remote.Enabled {

165 go httpgit.Listen("repos/",

166 config.Cfg.Git.Address,

167 config.Cfg.Git.Port)

168 }

169 go auth.Decrease()

170

171 gig.DefaultLoggerConfig.Format = "${time_rfc3339} - ${remote_ip} | " +

172 "Path=${path}, Status=${status}, " +

173 "Latency=${latency}\n"

174 g := gig.Default()

175 g.Use(gig.Recover())

176 g.Static("/static", "./static")

177

178 secure := g.Group("/account", gig.PassAuth(

179 func(sig string, c gig.Context) (string, error) {

180 _, b := db.GetUser(sig)

181 if !b {

182 return "/login", nil

183 }

184 return "", nil

185 }))

186

187 secure.Handle("", gmi.ShowAccount)

188 // groups management

189 secure.Handle("/groups", gmi.ShowGroups)

190 secure.Handle("/groups/:group", gmi.ShowMembers)

191 secure.Handle("/groups/:group/desc", gmi.SetGroupDesc)

192 secure.Handle("/groups/:group/add", gmi.AddToGroup)

193 secure.Handle("/groups/:group/leave", gmi.LeaveGroup)

194 secure.Handle("/groups/:group/delete", gmi.DeleteGroup)

195 secure.Handle("/groups/:group/kick/:user", gmi.RmFromGroup)

196

197 // repository settings

198 secure.Handle("/repo/:repo/*", gmi.RepoFile)

199 secure.Handle("/repo/:repo/togglepublic", gmi.TogglePublic)

200 secure.Handle("/repo/:repo/chname", gmi.ChangeRepoName)

201 secure.Handle("/repo/:repo/chdesc", gmi.ChangeRepoDesc)

202 secure.Handle("/repo/:repo/delrepo", gmi.DeleteRepo)

203

204 // access management

205 secure.Handle("/repo/:repo/access", gmi.ShowAccess)

206 secure.Handle("/repo/:repo/access/add", gmi.AddUserAccess)

207 secure.Handle("/repo/:repo/access/addg", gmi.AddGroupAccess)

208 secure.Handle("/repo/:repo/access/:user/first",

209 gmi.UserAccessFirstOption)

210 secure.Handle("/repo/:repo/access/:user/second",

211 gmi.UserAccessSecondOption)

212 secure.Handle("/repo/:repo/access/:group/g/first",

213 gmi.GroupAccessFirstOption)

214 secure.Handle("/repo/:repo/access/:group/g/second",

215 gmi.GroupAccessSecondOption)

216 secure.Handle("/repo/:repo/access/:user/kick",

217 gmi.RemoveUserAccess)

218 secure.Handle("/repo/:repo/access/:group/g/kick",

219 gmi.RemoveGroupAccess)

220

221 // repository view

222 secure.Handle("/repo/:repo", gmi.RepoLog)

223 secure.Handle("/repo/:repo/license", gmi.RepoLicense)

224 secure.Handle("/repo/:repo/readme", gmi.RepoReadme)

225 secure.Handle("/repo/:repo/refs", gmi.RepoRefs)

226 secure.Handle("/repo/:repo/files", gmi.RepoFiles)

227 secure.Handle("/repo/:repo/files/:blob", gmi.RepoFileContent)

228

229 // user page

230 secure.Handle("/chdesc", gmi.ChangeDesc)

231 secure.Handle("/addrepo", gmi.AddRepo)

232 secure.Handle("/addgroup", gmi.AddGroup)

233

234 if !config.Cfg.Ldap.Enabled {

235 secure.Handle("/chpasswd", gmi.ChangePassword)

236 }

237

238 secure.Handle("/disconnect", gmi.Disconnect)

239

240 if config.Cfg.Git.Key != "" {

241 api := g.Group("/api")

242 api.Handle("/:key/init/:username/:repo", repo.ApiInit)

243 api.Handle("/:key/rm/:username/:repo", repo.ApiRemove)

244 api.Handle("/:key/mv/:username/:repo/:newname",

245 repo.ApiRename)

246 }

247

248 public := g.Group("/repo")

249 public.Handle("", gmi.PublicList)

250 public.Handle("/:user/:repo/*", gmi.PublicFile)

251 public.Handle("/:user", gmi.PublicAccount)

252 public.Handle("/:user/:repo", gmi.PublicLog)

253 public.Handle("/:user/:repo/refs", gmi.PublicRefs)

254 public.Handle("/:user/:repo/license", gmi.PublicLicense)

255 public.Handle("/:user/:repo/readme", gmi.PublicReadme)

256 public.Handle("/:user/:repo/files", gmi.PublicFiles)

257 public.Handle("/:user/:repo/files/:blob", gmi.PublicFileContent)

258

259 g.PassAuthLoginHandle("/login", gmi.Login)

260

261 if config.Cfg.Users.Registration {

262 g.Handle("/register", gmi.Register)

263 g.Handle("/register/:name", gmi.RegisterConfirm)

264 }

265

266 g.Handle("/", func(c gig.Context) error {

267 return gmi.ShowIndex(c)

268 })

269

270 err = g.Run(config.Cfg.Gemini.Address + ":" + config.Cfg.Gemini.Port,

271 config.Cfg.Gemini.Certificate, config.Cfg.Gemini.Key)

272

273 if err != nil {

274 log.Fatal(err.Error())

275 }

276 }

277