mirror of
https://shylinux.com/x/icebergs
synced 2025-04-26 01:24:05 +08:00
opt git.server
This commit is contained in:
parent
8ebaf882a5
commit
50b58d97cf
@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
func _autogen_script(m *ice.Message, dir string) {
|
func _autogen_script(m *ice.Message, dir string) {
|
||||||
if b, e := kit.Render(`chapter "{{.Option "name"}}"
|
if b, e := kit.Render(`chapter "{{.Option "name"}}"
|
||||||
field "{{.Option "name"}}" web.code.{{.Option "name"}}.{{.Option "name"}}
|
field "{{.Option "name"}}" web.code.{{.Option "name"}}
|
||||||
`, m); m.Assert(e) {
|
`, m); m.Assert(e) {
|
||||||
m.Cmd(nfs.DEFS, dir, string(b))
|
m.Cmd(nfs.DEFS, dir, string(b))
|
||||||
}
|
}
|
||||||
|
1
go.mod
1
go.mod
@ -3,7 +3,6 @@ module shylinux.com/x/icebergs
|
|||||||
go 1.11
|
go 1.11
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/AaronO/go-git-http v0.0.0-20161214145340-1d9485b3a98f
|
|
||||||
github.com/gorilla/websocket v1.4.2
|
github.com/gorilla/websocket v1.4.2
|
||||||
github.com/kr/pty v1.1.8
|
github.com/kr/pty v1.1.8
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||||
|
2
go.sum
2
go.sum
@ -1,5 +1,3 @@
|
|||||||
github.com/AaronO/go-git-http v0.0.0-20161214145340-1d9485b3a98f h1:x/RDwGRneK2/891S2o7KhZt3MhHMSCssoeDOfvolTMk=
|
|
||||||
github.com/AaronO/go-git-http v0.0.0-20161214145340-1d9485b3a98f/go.mod h1:+6Yuq73F9068Na+mSBNXCvyuxvgw4f/g5ii40e3U8Sc=
|
|
||||||
github.com/creack/pty v1.1.7 h1:6pwm8kMQKCmgUg0ZHTm5+/YvRK0s3THD/28+T6/kk4A=
|
github.com/creack/pty v1.1.7 h1:6pwm8kMQKCmgUg0ZHTm5+/YvRK0s3THD/28+T6/kk4A=
|
||||||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
|
@ -1,15 +1,19 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/flate"
|
||||||
|
"compress/gzip"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
githttp "github.com/AaronO/go-git-http"
|
|
||||||
ice "shylinux.com/x/icebergs"
|
ice "shylinux.com/x/icebergs"
|
||||||
"shylinux.com/x/icebergs/base/aaa"
|
"shylinux.com/x/icebergs/base/aaa"
|
||||||
"shylinux.com/x/icebergs/base/cli"
|
"shylinux.com/x/icebergs/base/cli"
|
||||||
@ -19,8 +23,38 @@ import (
|
|||||||
kit "shylinux.com/x/toolkits"
|
kit "shylinux.com/x/toolkits"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func requestReader(req *http.Request) (io.ReadCloser, error) {
|
||||||
|
switch req.Header.Get("content-encoding") {
|
||||||
|
case "gzip":
|
||||||
|
return gzip.NewReader(req.Body)
|
||||||
|
case "deflate":
|
||||||
|
return flate.NewReader(req.Body), nil
|
||||||
|
}
|
||||||
|
return req.Body, nil
|
||||||
|
}
|
||||||
|
func packetWrite(str string) []byte {
|
||||||
|
s := strconv.FormatInt(int64(len(str)+4), 16)
|
||||||
|
if len(s)%4 != 0 {
|
||||||
|
s = strings.Repeat("0", 4-len(s)%4) + s
|
||||||
|
}
|
||||||
|
return []byte(s + str)
|
||||||
|
}
|
||||||
|
func packetFlush() []byte {
|
||||||
|
return []byte("0000")
|
||||||
|
}
|
||||||
|
|
||||||
var basicAuthRegex = regexp.MustCompile("^([^:]*):(.*)$")
|
var basicAuthRegex = regexp.MustCompile("^([^:]*):(.*)$")
|
||||||
|
|
||||||
|
func _server_param(m *ice.Message, arg ...string) (string, string) {
|
||||||
|
repos, service := path.Join(arg...), kit.Select(arg[len(arg)-1], m.Option("service"))
|
||||||
|
switch {
|
||||||
|
case strings.HasSuffix(repos, "info/refs"):
|
||||||
|
repos = strings.TrimSuffix(repos, "info/refs")
|
||||||
|
default:
|
||||||
|
repos = strings.TrimSuffix(repos, service)
|
||||||
|
}
|
||||||
|
return kit.Path(m.Conf(SERVER, kit.META_PATH), "repos", repos), strings.TrimPrefix(service, "git-")
|
||||||
|
}
|
||||||
func _server_login(m *ice.Message) error {
|
func _server_login(m *ice.Message) error {
|
||||||
parts := strings.SplitN(m.R.Header.Get("Authorization"), " ", 2)
|
parts := strings.SplitN(m.R.Header.Get("Authorization"), " ", 2)
|
||||||
if len(parts) < 2 {
|
if len(parts) < 2 {
|
||||||
@ -58,52 +92,110 @@ func _server_repos(m *ice.Message, arg ...string) {
|
|||||||
m.Cmd(cli.SYSTEM, GIT, INIT, "--bare", p) // 创建仓库
|
m.Cmd(cli.SYSTEM, GIT, INIT, "--bare", p) // 创建仓库
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func _server_cmd(m *ice.Message, arg ...string) error {
|
||||||
|
repos, service := _server_param(m, arg...)
|
||||||
|
|
||||||
|
if strings.HasSuffix(path.Join(arg...), "info/refs") {
|
||||||
|
command := exec.Command("/usr/bin/git", service, "--stateless-rpc", "--advertise-refs", ".")
|
||||||
|
command.Dir = repos
|
||||||
|
out, err := command.Output()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.W.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
|
||||||
|
m.W.Header().Set("Pragma", "no-cache")
|
||||||
|
m.W.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
|
||||||
|
|
||||||
|
m.W.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-advertisement", service))
|
||||||
|
m.W.WriteHeader(http.StatusOK)
|
||||||
|
m.W.Write(packetWrite("# service=git-" + service + "\n"))
|
||||||
|
m.W.Write(packetFlush())
|
||||||
|
m.W.Write(out)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
command := exec.Command("/usr/bin/git", service, "--stateless-rpc", ".")
|
||||||
|
command.Dir = repos
|
||||||
|
|
||||||
|
stdin, err := command.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
stdout, err := command.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer stdout.Close()
|
||||||
|
|
||||||
|
if err = command.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, err := requestReader(m.R)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer reader.Close()
|
||||||
|
|
||||||
|
io.Copy(stdin, reader)
|
||||||
|
stdin.Close()
|
||||||
|
|
||||||
|
m.W.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-result", service))
|
||||||
|
io.Copy(m.W, stdout)
|
||||||
|
|
||||||
|
if err = command.Wait(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
const SERVER = "server"
|
const SERVER = "server"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Index.Merge(&ice.Context{
|
Index.Merge(&ice.Context{Configs: map[string]*ice.Config{
|
||||||
Configs: map[string]*ice.Config{
|
SERVER: {Name: SERVER, Help: "服务器", Value: kit.Data(kit.MDB_PATH, ice.USR_LOCAL)},
|
||||||
SERVER: {Name: SERVER, Help: "服务器", Value: kit.Data(kit.MDB_PATH, ice.USR_LOCAL)},
|
}, Commands: map[string]*ice.Command{
|
||||||
},
|
web.WEB_LOGIN: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
Commands: map[string]*ice.Command{
|
m.Render(ice.RENDER_VOID)
|
||||||
web.WEB_LOGIN: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
}},
|
||||||
m.Render(ice.RENDER_VOID)
|
"/repos/": {Name: "/repos/", Help: "代码库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
}},
|
if m.Option("go-get") == "1" { // 下载地址
|
||||||
"/repos/": {Name: "/repos/", Help: "代码库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
p := m.Conf(web.SHARE, kit.Keym(kit.MDB_DOMAIN)) + "/x/" + path.Join(arg...)
|
||||||
if m.Option("go-get") == "1" {
|
m.RenderResult(`<meta name="go-import" content="%s git %s">`, strings.TrimPrefix(p, "https://"), p)
|
||||||
p := m.Conf(web.SHARE, kit.Keym(kit.MDB_DOMAIN)) + "/x/" + path.Join(arg...)
|
return
|
||||||
m.RenderResult(`<meta name="go-import" content="%s git %s">`, strings.TrimPrefix(p, "https://"), p)
|
}
|
||||||
return
|
|
||||||
|
switch _, service := _server_param(m, arg...); service {
|
||||||
|
case "receive-pack": // 上传代码
|
||||||
|
if err := _server_login(m); err != nil {
|
||||||
|
m.W.Header().Set("WWW-Authenticate", `Basic realm="git server"`)
|
||||||
|
http.Error(m.W, err.Error(), 401)
|
||||||
|
return // 认证失败
|
||||||
}
|
}
|
||||||
switch m.Option("service") {
|
_server_repos(m, arg...)
|
||||||
case "git-receive-pack": // 上传代码
|
|
||||||
if err := _server_login(m); err != nil {
|
|
||||||
m.W.Header().Set("WWW-Authenticate", `Basic realm="git server"`)
|
|
||||||
http.Error(m.W, err.Error(), 401) // 认证失败
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_server_repos(m, arg...)
|
|
||||||
|
|
||||||
case "git-upload-pack": // 下载代码
|
case "upload-pack": // 下载代码
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
githttp.New(kit.Path(m.Conf(SERVER, kit.META_PATH))).ServeHTTP(m.W, m.R)
|
if err := _server_cmd(m, arg...); err != nil {
|
||||||
|
http.Error(m.W, err.Error(), 500)
|
||||||
|
return // 未知错误
|
||||||
|
}
|
||||||
|
// githttp.New(kit.Path(m.Conf(SERVER, kit.META_PATH))).ServeHTTP(m.W, m.R)
|
||||||
|
}},
|
||||||
|
SERVER: {Name: "server path auto create", Help: "服务器", Action: map[string]*ice.Action{
|
||||||
|
mdb.CREATE: {Name: "create name", Help: "添加", Hand: func(m *ice.Message, arg ...string) {
|
||||||
|
m.Option(cli.CMD_DIR, path.Join(m.Conf(SERVER, kit.META_PATH), REPOS))
|
||||||
|
m.Cmd(cli.SYSTEM, GIT, INIT, "--bare", m.Option(kit.MDB_NAME))
|
||||||
}},
|
}},
|
||||||
SERVER: {Name: "server path auto create", Help: "服务器", Action: map[string]*ice.Action{
|
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
mdb.CREATE: {Name: "create name", Help: "添加", Hand: func(m *ice.Message, arg ...string) {
|
if m.Option(nfs.DIR_ROOT, path.Join(m.Conf(SERVER, kit.META_PATH), REPOS)); len(arg) == 0 {
|
||||||
m.Option(cli.CMD_DIR, path.Join(m.Conf(SERVER, kit.META_PATH), REPOS))
|
m.Cmdy(nfs.DIR, "./")
|
||||||
m.Cmd(cli.SYSTEM, GIT, INIT, "--bare", m.Option(kit.MDB_NAME))
|
return
|
||||||
}},
|
}
|
||||||
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
m.Cmdy("_sum", path.Join(m.Option(nfs.DIR_ROOT), arg[0]))
|
||||||
if m.Option(nfs.DIR_ROOT, path.Join(m.Conf(SERVER, kit.META_PATH), REPOS)); len(arg) == 0 {
|
}},
|
||||||
m.Cmdy(nfs.DIR, "./")
|
}})
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Cmdy("_sum", path.Join(m.Option(nfs.DIR_ROOT), arg[0]))
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user