Commit 11ef7697 authored by ibuler's avatar ibuler

[Update] merge

parents e70545d3 3aa06abd
...@@ -14,6 +14,8 @@ FROM alpine ...@@ -14,6 +14,8 @@ FROM alpine
WORKDIR /opt/coco/ WORKDIR /opt/coco/
COPY --from=stage-build /go/src/cocogo/cmd/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/locale .
COPY --from=stage-build /go/src/cocogo/cmd/static .
COPY --from=stage-build /go/src/cocogo/cmd/templates .
RUN echo > config.yml RUN echo > config.yml
EXPOSE 2222 EXPOSE 2222
CMD ["./coco"] CMD ["./coco"]
...@@ -4,36 +4,35 @@ BUILD := $(shell git rev-parse --short HEAD) ...@@ -4,36 +4,35 @@ BUILD := $(shell git rev-parse --short HEAD)
VERSION = $(BRANCH)-$(BUILD) VERSION = $(BRANCH)-$(BUILD)
NAME := coco NAME := coco
DIRNAME := cocogo
BASEPATH := $(shell pwd) BASEPATH := $(shell pwd)
CGO_ENABLED = 0 CGO_ENABLED = 0
GOCMD = go GOCMD = go
GOBUILD = $(GOCMD) build GOBUILD = $(GOCMD) build
ASSETS = $(shell echo "locale static templates coco config_example.yml") SOFTWARENAME=$(NAME)-$(VERSION)
SOFTWARENAME = $(NAME)-$(VERSION) COCOSRCFILE= coco.go
COCOSRCFILE = coco.go BUILDDIR:=$(BASEPATH)/../build
ASSETS=locale static templates config_example.yml
PLATFORMS := linux darwin
.PHONY: windows .PHONY: release
windows: release: linux darwin
@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: 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 .PHONY:Asset
darwin: Asset:
@echo "编译darwin" @[ -d $(BUILDDIR) ] || mkdir -p $(BUILDDIR)
mkdir -p $(BASEPATH)/../build @[ -d $(DIRNAME) ] || mkdir -p $(DIRNAME)
GOOS=darwin GOARCH=amd64 go build -o $(BASEPATH)/$(NAME) $(COCOSRCFILE) cp -r $(ASSETS) $(DIRNAME)
tar czvf $(BASEPATH)/../build/$(SOFTWARENAME)-darwin-amd64.tar.gz $(SOFTWARENAME)-darwin-amd64 locale/ config_example.yml
.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 .PHONY: docker
docker: docker:
...@@ -42,6 +41,8 @@ docker: ...@@ -42,6 +41,8 @@ docker:
.PHONY: clean .PHONY: clean
clean: clean:
-rm -rf $(BASEPATH)/../build -rm -rf $(NAME)
-rm -rf $(DIRNAME)
-rm -rf $(BUILDDIR)
...@@ -18,6 +18,7 @@ var ( ...@@ -18,6 +18,7 @@ var (
ContextKeySystemUser = &contextKey{"systemUser"} ContextKeySystemUser = &contextKey{"systemUser"}
ContextKeySSHSession = &contextKey{"sshSession"} ContextKeySSHSession = &contextKey{"sshSession"}
ContextKeyLocalAddr = &contextKey{"localAddr"} ContextKeyLocalAddr = &contextKey{"localAddr"}
ContextKeyRemoteAddr = &contextKey{"RemoteAddr"}
ContextKeySSHCtx = &contextKey{"sshCtx"} ContextKeySSHCtx = &contextKey{"sshCtx"}
ContextKeySeed = &contextKey{"seed"} ContextKeySeed = &contextKey{"seed"}
ContextKeyToken = &contextKey{"token"} ContextKeyToken = &contextKey{"token"}
......
package httpd package httpd
import ( import (
"context"
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"strings"
"github.com/LeeEirc/elfinder" "github.com/LeeEirc/elfinder"
socketio "github.com/googollee/go-socket.io" socketio "github.com/googollee/go-socket.io"
"github.com/gorilla/mux" "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 { func OnELFinderConnect(s socketio.Conn) error {
u := s.URL() u := s.URL()
sid := u.Query().Get("sid") sid := u.Query().Get("sid")
...@@ -38,6 +78,9 @@ func sftpFinder(wr http.ResponseWriter, req *http.Request) { ...@@ -38,6 +78,9 @@ func sftpFinder(wr http.ResponseWriter, req *http.Request) {
} }
func sftpHostConnectorView(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 := elfinder.NewElFinderConnector([]elfinder.Volume{&elfinder.DefaultVolume})
con.ServeHTTP(wr, req) con.ServeHTTP(wr, req)
} }
...@@ -4,38 +4,17 @@ import ( ...@@ -4,38 +4,17 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/http"
"strings" "strings"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"
"github.com/googollee/go-socket.io" socketio "github.com/googollee/go-socket.io"
"github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"cocogo/pkg/logger" "cocogo/pkg/logger"
"cocogo/pkg/proxy" "cocogo/pkg/proxy"
"cocogo/pkg/service" "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连接后触发 // OnConnectHandler 当websocket连接后触发
func OnConnectHandler(s socketio.Conn) error { func OnConnectHandler(s socketio.Conn) error {
// 首次连接 1.获取当前用户的信息 // 首次连接 1.获取当前用户的信息
......
...@@ -42,9 +42,10 @@ func StartHTTPServer() { ...@@ -42,9 +42,10 @@ func StartHTTPServer() {
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs)) router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
router.Handle("/socket.io/", server) router.Handle("/socket.io/", server)
router.HandleFunc("/coco/elfinder/sftp/{host}/", sftpHostFinder) router.HandleFunc("/coco/elfinder/sftp/{host}/", AuthDecorator(sftpHostFinder))
router.HandleFunc("/coco/elfinder/sftp/", sftpFinder) router.HandleFunc("/coco/elfinder/sftp/", AuthDecorator(sftpFinder))
router.HandleFunc("/coco/elfinder/sftp/connector/{host}/", sftpHostConnectorView).Methods("GET", "POST") router.HandleFunc("/coco/elfinder/sftp/connector/{host}/",
AuthDecorator(sftpHostConnectorView)).Methods("GET", "POST")
addr := net.JoinHostPort(conf.BindHost, conf.HTTPPort) addr := net.JoinHostPort(conf.BindHost, conf.HTTPPort)
logger.Debug("Start HTTP server at ", addr) 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