diff --git a/Dockerfile b/Dockerfile index aa9549c597345adae3ccfcff7f0c19e9a775962a..8652c360df24ee30d7f9c5d7bab5874c7dba4333 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,12 @@ FROM golang:1.12-alpine as stage-build LABEL stage=stage-build WORKDIR /opt/coco +RUN apk update && apk add git ARG GOPROXY ENV GOPROXY=$GOPROXY ENV GO111MODULE=on -COPY go.mod go.sum ./ RUN apk update && apk add git +COPY go.mod go.sum ./ RUN go mod download COPY . . RUN cd cmd && go build koko.go @@ -20,6 +21,7 @@ COPY cmd/config_example.yml . COPY entrypoint.sh . RUN chmod 755 ./entrypoint.sh \ && apk add -U tzdata \ + && apk add curl \ && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone \ && apk del tzdata \ diff --git a/entrypoint.sh b/entrypoint.sh index 983d69a8bdb1758ef52d2eaa18604c73c98dc44b..dde79302beb1746d7890b09a2f9f3b5561ffb2b7 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,12 @@ #!/bin/sh # +while [ "$(curl -I -m 10 -o /dev/null -s -w %{http_code} $CORE_HOST)" != "302" ] +do + echo "wait for jms_core ready" + sleep 2 +done + if [ ! -f "/opt/coco/config.yml" ]; then cp /opt/coco/config_example.yml /opt/coco/config.yml sed -i '5d' /opt/coco/config.yml diff --git a/pkg/httpd/server.go b/pkg/httpd/server.go index d91aa7679c3a4dfe308e2fb3d4869f6deacb2361..2600124e1d4ad6da07df5a5c79a51d9cb2d55d69 100644 --- a/pkg/httpd/server.go +++ b/pkg/httpd/server.go @@ -7,11 +7,11 @@ import ( "time" "github.com/gorilla/mux" - "github.com/kataras/neffos" - "github.com/kataras/neffos/gorilla" - + gorillaws "github.com/gorilla/websocket" "github.com/jumpserver/koko/pkg/config" "github.com/jumpserver/koko/pkg/logger" + "github.com/kataras/neffos" + "github.com/kataras/neffos/gorilla" ) var ( @@ -19,6 +19,12 @@ var ( Timeout = time.Duration(60) ) +var upgrader = gorilla.Upgrader(gorillaws.Upgrader{ + CheckOrigin: func(r *http.Request) bool { + return true + }, +}) + var wsEvents = neffos.WithTimeout{ ReadTimeout: Timeout * time.Second, WriteTimeout: Timeout * time.Second, @@ -50,7 +56,7 @@ var wsEvents = neffos.WithTimeout{ func StartHTTPServer() { conf := config.GetConf() - sshWs := neffos.New(gorilla.DefaultUpgrader, wsEvents) + sshWs := neffos.New(upgrader, wsEvents) sshWs.IDGenerator = func(w http.ResponseWriter, r *http.Request) string { return neffos.DefaultIDGenerator(w, r) } diff --git a/pkg/httpd/upgrader.go b/pkg/httpd/upgrader.go deleted file mode 100644 index d5f8cd1022ca37e7abf83c8a538a00270ff2230a..0000000000000000000000000000000000000000 --- a/pkg/httpd/upgrader.go +++ /dev/null @@ -1,102 +0,0 @@ -package httpd - -import ( - "net" - "net/http" - "sync" - "time" - - "github.com/kataras/neffos" - - gorilla "github.com/gorilla/websocket" -) - -// DefaultUpgrader is a gorilla/websocket Upgrader with all fields set to the default values. -var DefaultUpgrader = Upgrader(gorilla.Upgrader{}) - -// Upgrader is a `neffos.Upgrader` type for the gorilla/websocket subprotocol implementation. -// Should be used on `New` to construct the neffos server. -func Upgrader(upgrader gorilla.Upgrader) neffos.Upgrader { - return func(w http.ResponseWriter, r *http.Request) (neffos.Socket, error) { - header := w.Header() - header.Set("Access-Control-Allow-Origin", "*") - underline, err := upgrader.Upgrade(w, r, header) - if err != nil { - return nil, err - } - - return newSocket(underline, r, false), nil - } -} - -// Socket completes the `neffos.Socket` interface, -// it describes the underline websocket connection. -type Socket struct { - UnderlyingConn *gorilla.Conn - request *http.Request - - client bool - - mu sync.Mutex -} - -func newSocket(underline *gorilla.Conn, request *http.Request, client bool) *Socket { - return &Socket{ - UnderlyingConn: underline, - request: request, - client: client, - } -} - -// NetConn returns the underline net connection. -func (s *Socket) NetConn() net.Conn { - return s.UnderlyingConn.UnderlyingConn() -} - -// Request returns the http request value. -func (s *Socket) Request() *http.Request { - return s.request -} - -// ReadData reads binary or text messages from the remote connection. -func (s *Socket) ReadData(timeout time.Duration) ([]byte, error) { - for { - if timeout > 0 { - s.UnderlyingConn.SetReadDeadline(time.Now().Add(timeout)) - } - - opCode, data, err := s.UnderlyingConn.ReadMessage() - if err != nil { - return nil, err - } - - if opCode != gorilla.BinaryMessage && opCode != gorilla.TextMessage { - // if gorilla.IsUnexpectedCloseError(err, gorilla.CloseGoingAway) ... - continue - } - - return data, err - } -} - -// WriteBinary sends a binary message to the remote connection. -func (s *Socket) WriteBinary(body []byte, timeout time.Duration) error { - return s.write(body, gorilla.BinaryMessage, timeout) -} - -// WriteText sends a text message to the remote connection. -func (s *Socket) WriteText(body []byte, timeout time.Duration) error { - return s.write(body, gorilla.TextMessage, timeout) -} - -func (s *Socket) write(body []byte, opCode int, timeout time.Duration) error { - if timeout > 0 { - s.UnderlyingConn.SetWriteDeadline(time.Now().Add(timeout)) - } - - s.mu.Lock() - err := s.UnderlyingConn.WriteMessage(opCode, body) - s.mu.Unlock() - - return err -} diff --git a/pkg/koko/koko.go b/pkg/koko/koko.go index 06fe8cdd67f13c708708d5ca4abef903a3f71255..6973230a177744073624e884ab563b143dfaa9aa 100644 --- a/pkg/koko/koko.go +++ b/pkg/koko/koko.go @@ -14,7 +14,7 @@ import ( "github.com/jumpserver/koko/pkg/sshd" ) -const version = "1.5.0" +const version = "1.5.2" type Coco struct { }