Commit 11ef7697 authored by ibuler's avatar ibuler

[Update] merge

parents e70545d3 3aa06abd
......@@ -14,6 +14,8 @@ FROM alpine
WORKDIR /opt/coco/
COPY --from=stage-build /go/src/cocogo/cmd/coco .
COPY --from=stage-build /go/src/cocogo/cmd/locale .
COPY --from=stage-build /go/src/cocogo/cmd/static .
COPY --from=stage-build /go/src/cocogo/cmd/templates .
RUN echo > config.yml
EXPOSE 2222
CMD ["./coco"]
......@@ -4,36 +4,35 @@ BUILD := $(shell git rev-parse --short HEAD)
VERSION = $(BRANCH)-$(BUILD)
NAME := coco
DIRNAME := cocogo
BASEPATH := $(shell pwd)
CGO_ENABLED = 0
GOCMD = go
GOBUILD = $(GOCMD) build
ASSETS = $(shell echo "locale static templates coco config_example.yml")
SOFTWARENAME = $(NAME)-$(VERSION)
COCOSRCFILE = coco.go
SOFTWARENAME=$(NAME)-$(VERSION)
COCOSRCFILE= coco.go
BUILDDIR:=$(BASEPATH)/../build
ASSETS=locale static templates config_example.yml
PLATFORMS := linux darwin
.PHONY: windows
windows:
@echo "编译windows"
mkdir -p $(BASEPATH)/../build
GOOS=windows GOARCH=amd64 go build -o $(BASEPATH)/$(NAME) $(COCOSRCFILE)
tar czvf $(BASEPATH)/../build/$(SOFTWARENAME)-windows-amd64.tar.gz $(SOFTWARENAME)-windows-amd64 locale/ config_example.yml
.PHONY: release
release: linux darwin
.PHONY: linux
linux:
@echo "编译linux"
mkdir -p $(BASEPATH)/../build
GOOS=linux GOARCH=amd64 go build -o $(BASEPATH)/$(NAME) $(COCOSRCFILE)
tar czvf $(BASEPATH)/../build/$(SOFTWARENAME)-linux-amd64.tar.gz $(ASSETS)
.PHONY: darwin
darwin:
@echo "编译darwin"
mkdir -p $(BASEPATH)/../build
GOOS=darwin GOARCH=amd64 go build -o $(BASEPATH)/$(NAME) $(COCOSRCFILE)
tar czvf $(BASEPATH)/../build/$(SOFTWARENAME)-darwin-amd64.tar.gz $(SOFTWARENAME)-darwin-amd64 locale/ config_example.yml
.PHONY:Asset
Asset:
@[ -d $(BUILDDIR) ] || mkdir -p $(BUILDDIR)
@[ -d $(DIRNAME) ] || mkdir -p $(DIRNAME)
cp -r $(ASSETS) $(DIRNAME)
.PHONY: $(PLATFORMS)
$(PLATFORMS): Asset
@echo "编译" $@
GOOS=$@ GOARCH=amd64 go build -o $(NAME) $(COCOSRCFILE)
cp -f $(NAME) $(DIRNAME)
tar czvf $(BUILDDIR)/$(SOFTWARENAME)-$@-amd64.tar.gz $(DIRNAME)
.PHONY: docker
docker:
......@@ -42,6 +41,8 @@ docker:
.PHONY: clean
clean:
-rm -rf $(BASEPATH)/../build
-rm -rf $(NAME)
-rm -rf $(DIRNAME)
-rm -rf $(BUILDDIR)
......@@ -18,6 +18,7 @@ var (
ContextKeySystemUser = &contextKey{"systemUser"}
ContextKeySSHSession = &contextKey{"sshSession"}
ContextKeyLocalAddr = &contextKey{"localAddr"}
ContextKeyRemoteAddr = &contextKey{"RemoteAddr"}
ContextKeySSHCtx = &contextKey{"sshCtx"}
ContextKeySeed = &contextKey{"seed"}
ContextKeyToken = &contextKey{"token"}
......
package httpd
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"github.com/LeeEirc/elfinder"
socketio "github.com/googollee/go-socket.io"
"github.com/gorilla/mux"
"cocogo/pkg/cctx"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"cocogo/pkg/service"
)
func AuthDecorator(handler http.HandlerFunc) http.HandlerFunc {
return func(responseWriter http.ResponseWriter, request *http.Request) {
cookies := strings.Split(request.Header.Get("Cookie"), ";")
var csrfToken string
var sessionid string
var remoteIP string
for _, line := range cookies {
if strings.Contains(line, "csrftoken") {
csrfToken = strings.Split(line, "=")[1]
}
if strings.Contains(line, "sessionid") {
sessionid = strings.Split(line, "=")[1]
}
}
user, err := service.CheckUserCookie(sessionid, csrfToken)
if err != nil {
loginUrl := fmt.Sprintf("/users/login/?next=%s", request.URL.Path)
http.Redirect(responseWriter, request, loginUrl, http.StatusFound)
return
}
xForwardFors := strings.Split(request.Header.Get("X-Forwarded-For"), ",")
if len(xForwardFors) >= 1 {
remoteIP = xForwardFors[0]
} else {
remoteIP = strings.Split(request.RemoteAddr, ":")[0]
}
ctx := context.WithValue(request.Context(), cctx.ContextKeyUser, user)
ctx = context.WithValue(ctx, cctx.ContextKeyRemoteAddr, remoteIP)
handler(responseWriter, request.WithContext(ctx))
}
}
func OnELFinderConnect(s socketio.Conn) error {
u := s.URL()
sid := u.Query().Get("sid")
......@@ -38,6 +78,9 @@ func sftpFinder(wr http.ResponseWriter, req *http.Request) {
}
func sftpHostConnectorView(wr http.ResponseWriter, req *http.Request) {
user := req.Context().Value(cctx.ContextKeyUser).(*model.User)
remoteIP := req.Context().Value(cctx.ContextKeyRemoteAddr).(string)
logger.Debugf("user: %s; remote ip: %s; create connector", user.Name, remoteIP)
con := elfinder.NewElFinderConnector([]elfinder.Volume{&elfinder.DefaultVolume})
con.ServeHTTP(wr, req)
}
......@@ -4,38 +4,17 @@ import (
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/gliderlabs/ssh"
"github.com/googollee/go-socket.io"
"github.com/satori/go.uuid"
socketio "github.com/googollee/go-socket.io"
uuid "github.com/satori/go.uuid"
"cocogo/pkg/logger"
"cocogo/pkg/proxy"
"cocogo/pkg/service"
)
func AuthDecorator(handler http.HandlerFunc) http.HandlerFunc {
return func(responseWriter http.ResponseWriter, request *http.Request) {
cookies := strings.Split(request.Header.Get("Cookie"), ";")
var csrfToken string
var sessionid string
for _, line := range cookies {
if strings.Contains(line, "csrftoken") {
csrfToken = strings.Split(line, "=")[1]
}
if strings.Contains(line, "sessionid") {
sessionid = strings.Split(line, "=")[1]
}
}
_, err := service.CheckUserCookie(sessionid, csrfToken)
if err != nil {
http.Redirect(responseWriter, request, "", http.StatusFound)
}
}
}
// OnConnectHandler 当websocket连接后触发
func OnConnectHandler(s socketio.Conn) error {
// 首次连接 1.获取当前用户的信息
......
......@@ -42,9 +42,10 @@ func StartHTTPServer() {
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
router.Handle("/socket.io/", server)
router.HandleFunc("/coco/elfinder/sftp/{host}/", sftpHostFinder)
router.HandleFunc("/coco/elfinder/sftp/", sftpFinder)
router.HandleFunc("/coco/elfinder/sftp/connector/{host}/", sftpHostConnectorView).Methods("GET", "POST")
router.HandleFunc("/coco/elfinder/sftp/{host}/", AuthDecorator(sftpHostFinder))
router.HandleFunc("/coco/elfinder/sftp/", AuthDecorator(sftpFinder))
router.HandleFunc("/coco/elfinder/sftp/connector/{host}/",
AuthDecorator(sftpHostConnectorView)).Methods("GET", "POST")
addr := net.JoinHostPort(conf.BindHost, conf.HTTPPort)
logger.Debug("Start HTTP server at ", addr)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment