add kubectl/kubeconfig/quota/terminal api, but quota api is tempoary, will be changed as soon
This commit is contained in:
@@ -22,14 +22,16 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/components"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/containers"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/iam"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/kubeconfig"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/kubectl"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/nodes"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/pods"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/quota"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/registries"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/routes"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/storage"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/terminal"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/users"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/volumes"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/workloadstatus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -37,8 +39,6 @@ func init() {
|
||||
ws := new(restful.WebService)
|
||||
ws.Path("/api/v1alpha1")
|
||||
|
||||
kubeconfig.Register(ws, "/namespaces/{namespace}/kubeconfig")
|
||||
kubectl.Register(ws, "/namespaces/{namespace}/kubectl")
|
||||
registries.Register(ws, "/registries")
|
||||
storage.Register(ws, "/storage")
|
||||
volumes.Register(ws, "/volumes")
|
||||
@@ -47,10 +47,15 @@ func init() {
|
||||
containers.Register(ws)
|
||||
iam.Register(ws)
|
||||
components.Register(ws, "/components")
|
||||
|
||||
routes.Register(ws)
|
||||
user.Register(ws, "/users/{user}")
|
||||
terminal.Register(ws, "/namespaces/{namespace}/pod/{pod}/shell/{container}")
|
||||
workloadstatus.Register(ws, "/status")
|
||||
quota.Register(ws, "/quota")
|
||||
|
||||
// add webservice to default container
|
||||
restful.Add(ws)
|
||||
|
||||
// add websocket handler to default container
|
||||
terminal.RegisterWebSocketHandler(restful.DefaultContainer, "/api/v1alpha1/sockjs/")
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kubectl
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
)
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
|
||||
ws.Route(ws.GET(subPath).Consumes("*/*").Produces(restful.MIME_JSON).To(handleKubectl).Doc("use to "+
|
||||
"get a kubectl pod in specified namespaces").Param(ws.PathParameter("namespace",
|
||||
"namespace").DataType("string")).Do(returns200, returns500))
|
||||
|
||||
}
|
||||
|
||||
func handleKubectl(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
ns := req.PathParameter("namespace")
|
||||
|
||||
kubectlPod, err := models.GetKubectlPod(ns)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteError(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
resp.WriteEntity(kubectlPod)
|
||||
}
|
||||
|
||||
func returns200(b *restful.RouteBuilder) {
|
||||
b.Returns(http.StatusOK, "OK", nil)
|
||||
}
|
||||
|
||||
func returns500(b *restful.RouteBuilder) {
|
||||
b.Returns(http.StatusInternalServerError, "fail", nil)
|
||||
}
|
||||
54
pkg/apis/v1alpha/quota/quota.go
Normal file
54
pkg/apis/v1alpha/quota/quota.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package quota
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
)
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
|
||||
ws.Route(ws.GET(subPath).To(getClusterQuota).Produces(restful.MIME_JSON))
|
||||
ws.Route(ws.GET(subPath + "/namespaces/{namespace}").To(getNamespaceQuota).Produces(restful.MIME_JSON))
|
||||
|
||||
}
|
||||
|
||||
func getNamespaceQuota(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
quota, err := models.GetNamespaceQuota(namespace)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
}
|
||||
|
||||
resp.WriteEntity(quota)
|
||||
}
|
||||
|
||||
func getClusterQuota(req *restful.Request, resp *restful.Response) {
|
||||
quota, err := models.GetClusterQuota()
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
}
|
||||
|
||||
resp.WriteEntity(quota)
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package kubeconfig
|
||||
package terminal
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -22,30 +22,23 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
)
|
||||
|
||||
const DefaultServiceAccount = "default"
|
||||
|
||||
type Config struct {
|
||||
Certificate string
|
||||
Server string
|
||||
User string
|
||||
Token string
|
||||
}
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
|
||||
ws.Route(ws.GET(subPath).To(handleKubeconfig))
|
||||
ws.Route(ws.GET(subPath).To(handleExecShell))
|
||||
|
||||
}
|
||||
|
||||
func handleKubeconfig(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
ns := req.PathParameter("namespace")
|
||||
|
||||
kubectlConfig, err := models.GetKubeConfig(ns)
|
||||
|
||||
func handleExecShell(req *restful.Request, resp *restful.Response) {
|
||||
res, err := models.HandleExecShell(req)
|
||||
if err != nil {
|
||||
resp.WriteError(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
resp.WriteEntity(kubectlConfig)
|
||||
resp.WriteEntity(res)
|
||||
|
||||
}
|
||||
|
||||
func RegisterWebSocketHandler(container *restful.Container, path string) {
|
||||
handler := models.CreateTerminalHandler(path[0 : len(path)-1])
|
||||
container.Handle(path, handler)
|
||||
}
|
||||
103
pkg/apis/v1alpha/users/user.go
Normal file
103
pkg/apis/v1alpha/users/user.go
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
)
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
|
||||
ws.Route(ws.POST(subPath).To(createUser).Consumes("*/*").Produces(restful.MIME_JSON))
|
||||
ws.Route(ws.DELETE(subPath).To(delUser).Produces(restful.MIME_JSON))
|
||||
ws.Route(ws.GET(subPath + "/kubectl").To(getKubectl).Produces(restful.MIME_JSON))
|
||||
ws.Route(ws.GET(subPath + "/kubeconfig").To(getKubeconfig).Produces(restful.MIME_JSON))
|
||||
|
||||
}
|
||||
|
||||
func createUser(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
user := req.PathParameter("user")
|
||||
|
||||
err := models.CreateKubeConfig(user)
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
err = models.CreateKubectlPod(user)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(constants.MessageResponse{Message: "successfully created"})
|
||||
}
|
||||
|
||||
func delUser(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
user := req.PathParameter("user")
|
||||
|
||||
err := models.DelKubectlPod(user)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
err = models.DelKubeConfig(user)
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(constants.MessageResponse{Message: "successfully deleted"})
|
||||
}
|
||||
|
||||
func getKubectl(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
user := req.PathParameter("user")
|
||||
|
||||
kubectlPod, err := models.GetKubectlPod(user)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(kubectlPod)
|
||||
}
|
||||
|
||||
func getKubeconfig(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
user := req.PathParameter("user")
|
||||
|
||||
kubectlConfig, err := models.GetKubeConfig(user)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(kubectlConfig)
|
||||
}
|
||||
49
pkg/apis/v1alpha/workloadstatus/workloadstatus.go
Normal file
49
pkg/apis/v1alpha/workloadstatus/workloadstatus.go
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package workloadstatus
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
)
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
|
||||
ws.Route(ws.GET(subPath).To(getClusterStatus).Produces(restful.MIME_JSON))
|
||||
ws.Route(ws.GET(subPath + "/namespaces/{namespace}").To(getNamespaceStatus).Produces(restful.MIME_JSON))
|
||||
|
||||
}
|
||||
|
||||
func getClusterStatus(req *restful.Request, resp *restful.Response) {
|
||||
res, err := models.GetClusterResourceStatus()
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
}
|
||||
resp.WriteEntity(res)
|
||||
}
|
||||
|
||||
func getNamespaceStatus(req *restful.Request, resp *restful.Response) {
|
||||
res, err := models.GetNamespacesResourceStatus(req.PathParameter("namespace"))
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
}
|
||||
resp.WriteEntity(res)
|
||||
}
|
||||
@@ -19,13 +19,20 @@ package app
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
_ "kubesphere.io/kubesphere/pkg/apis/v1alpha"
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models/jobs/cronjobs"
|
||||
"kubesphere.io/kubesphere/pkg/models/jobs/resources"
|
||||
"kubesphere.io/kubesphere/pkg/options"
|
||||
)
|
||||
|
||||
@@ -53,7 +60,29 @@ func newKubeSphereServer(options *options.ServerRunOptions) *kubeSphereServer {
|
||||
return &s
|
||||
}
|
||||
|
||||
func preCheck() error {
|
||||
k8sClient := client.NewK8sClient()
|
||||
nsList, err := k8sClient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, ns := range nsList.Items {
|
||||
if ns.Name == constants.NameSpace {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
namespace := v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: constants.NameSpace}}
|
||||
_, err = k8sClient.CoreV1().Namespaces().Create(&namespace)
|
||||
return err
|
||||
}
|
||||
func (server *kubeSphereServer) run() {
|
||||
err := preCheck()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return
|
||||
}
|
||||
go resources.Run()
|
||||
go cronjobs.Run()
|
||||
|
||||
if len(server.certFile) > 0 && len(server.keyFile) > 0 {
|
||||
servingCert, err := tls.LoadX509KeyPair(server.certFile, server.keyFile)
|
||||
|
||||
130
pkg/client/etcdclient.go
Normal file
130
pkg/client/etcdclient.go
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/coreos/etcd/clientv3"
|
||||
"github.com/coreos/etcd/pkg/transport"
|
||||
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
|
||||
"github.com/golang/glog"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/options"
|
||||
)
|
||||
|
||||
type EtcdClient struct {
|
||||
cli *clientv3.Client
|
||||
}
|
||||
|
||||
func (cli EtcdClient) Put(key, value string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
|
||||
_, err := cli.cli.Put(ctx, key, value)
|
||||
cancel()
|
||||
if err != nil {
|
||||
switch err {
|
||||
case context.Canceled:
|
||||
glog.Errorf("ctx is canceled by another routine: %v\n", err)
|
||||
case context.DeadlineExceeded:
|
||||
glog.Errorf("ctx is attached with a deadline is exceeded: %v\n", err)
|
||||
case rpctypes.ErrEmptyKey:
|
||||
glog.Errorf("client-side error: %v\n", err)
|
||||
default:
|
||||
glog.Errorf("bad cluster endpoints, which are not etcd servers: %v\n", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cli EtcdClient) Get(key string) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
|
||||
resp, err := cli.cli.Get(ctx, key)
|
||||
cancel()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(resp.Kvs) == 0 {
|
||||
return nil, fmt.Errorf("empty value of key: %s", key)
|
||||
}
|
||||
return resp.Kvs[0].Value, nil
|
||||
}
|
||||
|
||||
func (cli EtcdClient) Close() {
|
||||
|
||||
cli.cli.Close()
|
||||
}
|
||||
|
||||
func newEtcdClientWithHttps(certFile, keyFile, caFile string, endpoints []string, dialTimeout int) (*clientv3.Client, error) {
|
||||
tlsInfo := transport.TLSInfo{
|
||||
CertFile: certFile,
|
||||
KeyFile: keyFile,
|
||||
TrustedCAFile: caFile,
|
||||
}
|
||||
tlsConfig, err := tlsInfo.ClientConfig()
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cli, err := clientv3.New(clientv3.Config{
|
||||
Endpoints: endpoints,
|
||||
DialTimeout: time.Duration(dialTimeout) * time.Second,
|
||||
TLS: tlsConfig,
|
||||
})
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
return cli, nil // make sure to close the client
|
||||
}
|
||||
|
||||
func newEtcdClient(endpoints []string, dialTimeout int) (*clientv3.Client, error) {
|
||||
cli, err := clientv3.New(clientv3.Config{
|
||||
Endpoints: endpoints,
|
||||
DialTimeout: time.Duration(dialTimeout) * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
return cli, nil // make sure to close the client
|
||||
}
|
||||
|
||||
func NewEtcdClient() (*EtcdClient, error) {
|
||||
var cli *clientv3.Client
|
||||
var err error
|
||||
cert := options.ServerOptions.GetEtcdCertFile()
|
||||
key := options.ServerOptions.GetEtcdKeyFile()
|
||||
ca := options.ServerOptions.GetEtcdCaFile()
|
||||
endpoints := options.ServerOptions.GetEtcdEndPoints()
|
||||
if len(cert) > 0 && len(key) > 0 && len(ca) > 0 {
|
||||
cli, err = newEtcdClientWithHttps(cert, key, ca, endpoints, 20)
|
||||
} else {
|
||||
cli, err = newEtcdClient(endpoints, 20)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &EtcdClient{cli}, nil
|
||||
}
|
||||
@@ -25,8 +25,14 @@ type PageableResponse struct {
|
||||
TotalCount int `json:"total_count"`
|
||||
}
|
||||
|
||||
const APIVERSION = "v1alpha1"
|
||||
const KIND = "kubesphere"
|
||||
|
||||
const DATA_HOME = "/etc/kubesphere"
|
||||
const INGRESS_CONTROLLER_FOLDER = DATA_HOME + "/ingress-controller"
|
||||
const (
|
||||
APIVERSION = "v1alpha1"
|
||||
KIND = "kubesphere"
|
||||
Root = "/kubesphere"
|
||||
UpdateCircle = 60
|
||||
QuotaKey = "resource-quota"
|
||||
WorkloadStatusKey = "workload-status"
|
||||
NameSpace = "kubesphere"
|
||||
DATA_HOME = "/etc/kubesphere"
|
||||
INGRESS_CONTROLLER_FOLDER = DATA_HOME + "/ingress-controller"
|
||||
)
|
||||
|
||||
231
pkg/models/jobs/cronjobs/resource-quota.go
Normal file
231
pkg/models/jobs/cronjobs/resource-quota.go
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cronjobs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models/jobs/resources"
|
||||
)
|
||||
|
||||
const (
|
||||
pods = "count/pods"
|
||||
daemonsets = "count/daemonsets.apps"
|
||||
deployments = "count/deployments.apps"
|
||||
ingress = "count/ingresses.extensions"
|
||||
roles = "count/roles.rbac.authorization.k8s.io"
|
||||
services = "count/services"
|
||||
statefulsets = "count/statefulsets.apps"
|
||||
persistentvolumeclaims = "persistentvolumeclaims"
|
||||
)
|
||||
|
||||
type resourceUsage struct {
|
||||
NameSpace string
|
||||
Data v1.ResourceQuotaStatus
|
||||
UpdateTimeStamp int64
|
||||
}
|
||||
|
||||
type resourceQuotaWorker struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
resChan chan dataType
|
||||
stopChan chan struct{}
|
||||
}
|
||||
|
||||
func (ru resourceUsage) namespace() string {
|
||||
return ru.NameSpace
|
||||
}
|
||||
|
||||
type workloadList map[string][]resources.WorkLoadObject
|
||||
|
||||
type otherResourceList map[string][]resources.OtherResourceObject
|
||||
|
||||
type workload struct {
|
||||
ResourceType string `json:"type"`
|
||||
ResourceList workloadList `json:"lists"`
|
||||
UpdateTimeStamp int64 `json:"updateTimestamp"`
|
||||
}
|
||||
|
||||
type otherResource struct {
|
||||
ResourceType string `json:"type"`
|
||||
ResourceList otherResourceList `json:"lists"`
|
||||
UpdateTimeStamp int64 `json:"updateTimestamp"`
|
||||
}
|
||||
|
||||
var workLoads = []string{"deployments", "daemonsets", "statefulsets"}
|
||||
|
||||
var resourceMap = map[string]string{daemonsets: "daemonsets", deployments: "deployments", ingress: "ingresses",
|
||||
roles: "roles", services: "services", statefulsets: "statefulsets", persistentvolumeclaims: "persistent-volume-claim", pods: "pods"}
|
||||
|
||||
func contain(items []string, item string) bool {
|
||||
for _, v := range items {
|
||||
if v == item {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (rw *resourceQuotaWorker) getResourceusage(namespace, resourceName string) (int, error) {
|
||||
|
||||
etcdcli, err := client.NewEtcdClient()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer etcdcli.Close()
|
||||
key := constants.Root + "/" + resourceName
|
||||
value, err := etcdcli.Get(key)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
|
||||
if contain(workLoads, resourceName) {
|
||||
resourceStatus := workload{ResourceList: make(workloadList)}
|
||||
|
||||
err := json.Unmarshal(value, &resourceStatus)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return len(resourceStatus.ResourceList[namespace]), nil
|
||||
} else {
|
||||
resourceStatus := otherResource{ResourceList: make(otherResourceList)}
|
||||
|
||||
err := json.Unmarshal(value, &resourceStatus)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return len(resourceStatus.ResourceList[namespace]), nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (rw *resourceQuotaWorker) updateNamespaceQuota(tmpResourceList, resourceList v1.ResourceList) {
|
||||
if tmpResourceList == nil {
|
||||
tmpResourceList = resourceList
|
||||
}
|
||||
for resource, usage := range resourceList {
|
||||
tmpUsage, exist := tmpResourceList[resource]
|
||||
if !exist {
|
||||
tmpResourceList[resource] = usage
|
||||
}
|
||||
if tmpUsage.Cmp(usage) == 1 {
|
||||
tmpResourceList[resource] = usage
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (rw *resourceQuotaWorker) getNamespaceResourceUsageByQuota(namespace string) (*v1.ResourceQuotaStatus, error) {
|
||||
quotaList, err := rw.k8sClient.CoreV1().ResourceQuotas(namespace).List(meta_v1.ListOptions{})
|
||||
if err != nil || len(quotaList.Items) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
quotaStatus := v1.ResourceQuotaStatus{Hard: make(v1.ResourceList), Used: make(v1.ResourceList)}
|
||||
|
||||
for _, quota := range quotaList.Items {
|
||||
rw.updateNamespaceQuota(quotaStatus.Hard, quota.Status.Hard)
|
||||
rw.updateNamespaceQuota(quotaStatus.Used, quota.Status.Used)
|
||||
}
|
||||
|
||||
return "aStatus, nil
|
||||
}
|
||||
|
||||
func (rw *resourceQuotaWorker) getNamespaceQuota(namespace string) (v1.ResourceQuotaStatus, error) {
|
||||
quota, err := rw.getNamespaceResourceUsageByQuota(namespace)
|
||||
if err != nil {
|
||||
return v1.ResourceQuotaStatus{}, err
|
||||
}
|
||||
|
||||
if quota == nil {
|
||||
quota = new(v1.ResourceQuotaStatus)
|
||||
quota.Used = make(v1.ResourceList)
|
||||
}
|
||||
|
||||
for k, v := range resourceMap {
|
||||
if _, exist := quota.Used[v1.ResourceName(k)]; !exist {
|
||||
used, err := rw.getResourceusage(namespace, v)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var quantity resource.Quantity
|
||||
quantity.Set(int64(used))
|
||||
quota.Used[v1.ResourceName(k)] = quantity
|
||||
}
|
||||
}
|
||||
|
||||
return *quota, nil
|
||||
}
|
||||
|
||||
func (rw *resourceQuotaWorker) workOnce() {
|
||||
clusterQuota := new(v1.ResourceQuotaStatus)
|
||||
clusterQuota.Used = make(v1.ResourceList)
|
||||
namespaces, err := rw.k8sClient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, ns := range namespaces.Items {
|
||||
namespace := ns.Name
|
||||
nsquota, err := rw.getNamespaceQuota(namespace)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return
|
||||
}
|
||||
res := resourceUsage{NameSpace: namespace, Data: nsquota, UpdateTimeStamp: time.Now().Unix()}
|
||||
rw.resChan <- res
|
||||
|
||||
for k, v := range nsquota.Used {
|
||||
tmp := clusterQuota.Used[k]
|
||||
tmp.Add(v)
|
||||
clusterQuota.Used[k] = tmp
|
||||
}
|
||||
}
|
||||
|
||||
var quantity resource.Quantity
|
||||
quantity.Set(int64(len(namespaces.Items)))
|
||||
|
||||
clusterQuota.Used["count/namespaces"] = quantity
|
||||
res := resourceUsage{NameSpace: "\"\"", Data: *clusterQuota, UpdateTimeStamp: time.Now().Unix()}
|
||||
rw.resChan <- res
|
||||
|
||||
}
|
||||
|
||||
func (rw *resourceQuotaWorker) chanStop() chan struct{} {
|
||||
return rw.stopChan
|
||||
}
|
||||
|
||||
func (rw *resourceQuotaWorker) chanRes() chan dataType {
|
||||
return rw.resChan
|
||||
}
|
||||
136
pkg/models/jobs/cronjobs/run.go
Normal file
136
pkg/models/jobs/cronjobs/run.go
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cronjobs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
var etcdClient *client.EtcdClient
|
||||
|
||||
var stopChan = make(chan struct{})
|
||||
|
||||
type dataType interface {
|
||||
namespace() string
|
||||
}
|
||||
|
||||
type Worker interface {
|
||||
workOnce()
|
||||
chanRes() chan dataType
|
||||
chanStop() chan struct{}
|
||||
}
|
||||
|
||||
func registerWorker(workers map[string]Worker, name string) {
|
||||
|
||||
glog.Infof("Register cronjob: %s", name)
|
||||
k8sClient := client.NewK8sClient()
|
||||
switch name {
|
||||
case constants.WorkloadStatusKey:
|
||||
worker := workloadWorker{k8sClient: k8sClient, stopChan: stopChan, resChan: make(chan dataType, 10)}
|
||||
workers[constants.WorkloadStatusKey] = &worker
|
||||
case constants.QuotaKey:
|
||||
worker := resourceQuotaWorker{k8sClient: k8sClient, stopChan: stopChan, resChan: make(chan dataType, 10)}
|
||||
workers[constants.QuotaKey] = &worker
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func run(worker Worker) {
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
glog.Error(err)
|
||||
close(worker.chanRes())
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-worker.chanStop():
|
||||
return
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
worker.workOnce()
|
||||
time.Sleep(time.Duration(constants.UpdateCircle) * time.Second)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func startWorks(workers map[string]Worker) {
|
||||
for wokername, woker := range workers {
|
||||
glog.Infof("cronjob %s start to work", wokername)
|
||||
go run(woker)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func receiveResourceStatus(workers map[string]Worker) {
|
||||
defer func() {
|
||||
close(stopChan)
|
||||
}()
|
||||
|
||||
for {
|
||||
for name, worker := range workers {
|
||||
select {
|
||||
case res, ok := <-worker.chanRes():
|
||||
if !ok {
|
||||
glog.Errorf("cronjob:%s have stopped", name)
|
||||
registerWorker(workers, name)
|
||||
run(workers[name])
|
||||
} else {
|
||||
value, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
continue
|
||||
}
|
||||
key := constants.Root + "/" + name + "/" + res.namespace()
|
||||
err = etcdClient.Put(key, string(value))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Run() {
|
||||
glog.Info("Begin to run cronjob")
|
||||
var err error
|
||||
etcdClient, err = client.NewEtcdClient()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
defer etcdClient.Close()
|
||||
workers := make(map[string]Worker)
|
||||
workerList := []string{constants.QuotaKey, constants.WorkloadStatusKey}
|
||||
for _, name := range workerList {
|
||||
registerWorker(workers, name)
|
||||
}
|
||||
startWorks(workers)
|
||||
receiveResourceStatus(workers)
|
||||
}
|
||||
104
pkg/models/jobs/cronjobs/workload-status.go
Normal file
104
pkg/models/jobs/cronjobs/workload-status.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package cronjobs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
var workLoadList = []string{"deployments", "daemonsets", "statefulsets"}
|
||||
|
||||
type workLoadStatus struct {
|
||||
NameSpace string
|
||||
Data map[string]int
|
||||
UpdateTimeStamp int64
|
||||
}
|
||||
|
||||
func (ws workLoadStatus) namespace() string {
|
||||
return ws.NameSpace
|
||||
}
|
||||
|
||||
type workloadWorker struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
resChan chan dataType
|
||||
stopChan chan struct{}
|
||||
}
|
||||
|
||||
func (ww *workloadWorker) GetNamespacesResourceStatus(namespace string) (map[string]int, error) {
|
||||
|
||||
cli, err := client.NewEtcdClient()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
res := make(map[string]int)
|
||||
|
||||
for _, resourceName := range workLoadList {
|
||||
key := constants.Root + "/" + resourceName
|
||||
value, err := cli.Get(key)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
resourceStatus := workload{ResourceList: make(workloadList)}
|
||||
|
||||
err = json.Unmarshal(value, &resourceStatus)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
notReady := 0
|
||||
for _, v := range resourceStatus.ResourceList[namespace] {
|
||||
if !v.Ready {
|
||||
notReady++
|
||||
}
|
||||
}
|
||||
res[resourceName] = notReady
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (ww workloadWorker) workOnce() {
|
||||
namespaces, err := ww.k8sClient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
|
||||
resourceStatus := make(map[string]int)
|
||||
for _, item := range namespaces.Items {
|
||||
namespace := item.Name
|
||||
namespacesResourceStatus, err := ww.GetNamespacesResourceStatus(namespace)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
|
||||
var ws = workLoadStatus{UpdateTimeStamp: time.Now().Unix(), Data: namespacesResourceStatus, NameSpace: namespace}
|
||||
ww.resChan <- ws
|
||||
|
||||
for k, v := range namespacesResourceStatus {
|
||||
resourceStatus[k] = v + resourceStatus[k]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var ws = workLoadStatus{UpdateTimeStamp: time.Now().Unix(), Data: resourceStatus, NameSpace: "\"\""}
|
||||
ww.resChan <- ws
|
||||
}
|
||||
|
||||
func (ww workloadWorker) chanRes() chan dataType {
|
||||
return ww.resChan
|
||||
}
|
||||
|
||||
func (ww workloadWorker) chanStop() chan struct{} {
|
||||
return ww.stopChan
|
||||
}
|
||||
108
pkg/models/jobs/resources/daemonsets.go
Normal file
108
pkg/models/jobs/resources/daemonsets.go
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/apps/v1beta2"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type daemonset struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (ds *daemonset) list() (interface{}, error) {
|
||||
daemonsetList, err := ds.k8sClient.AppsV1beta2().DaemonSets("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return daemonsetList.Items, nil
|
||||
}
|
||||
|
||||
func (ds *daemonset) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := ds.k8sClient.AppsV1beta2().DaemonSets("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (ds *daemonset) generateObject(item v1beta2.DaemonSet) WorkLoadObject {
|
||||
var app string
|
||||
var ready bool
|
||||
name := item.Name
|
||||
namespace := item.Namespace
|
||||
availablePodNum := item.Status.CurrentNumberScheduled
|
||||
desirePodNum := item.Status.DesiredNumberScheduled
|
||||
createTime := item.CreationTimestamp
|
||||
release := item.ObjectMeta.Labels["release"]
|
||||
nodeSelector := item.Spec.Template.Spec.NodeSelector
|
||||
chart := item.ObjectMeta.Labels["chart"]
|
||||
|
||||
if len(release) > 0 && len(chart) > 0 {
|
||||
app = release + "/" + chart
|
||||
} else {
|
||||
app = "-"
|
||||
}
|
||||
|
||||
if availablePodNum >= desirePodNum {
|
||||
ready = true
|
||||
} else {
|
||||
ready = false
|
||||
}
|
||||
|
||||
workloadObject := WorkLoadObject{Namespace: namespace, Name: name, Available: availablePodNum, Desire: desirePodNum,
|
||||
App: app, CreateTime: createTime, Ready: ready, NodeSelector: nodeSelector}
|
||||
|
||||
return workloadObject
|
||||
|
||||
}
|
||||
|
||||
func (ds *daemonset) updateWithObject(status *ResourceStatus, item v1beta2.DaemonSet) {
|
||||
namespace := item.Namespace
|
||||
dsObject := ds.generateObject(item)
|
||||
|
||||
status.ResourceList.update(namespace, dsObject)
|
||||
}
|
||||
|
||||
func (ds *daemonset) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1beta2.DaemonSet)
|
||||
|
||||
for _, item := range items {
|
||||
ds.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ds *daemonset) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1beta2.DaemonSet)
|
||||
namespace := object.Namespace
|
||||
daemonsetObject := ds.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, daemonsetObject)
|
||||
return
|
||||
}
|
||||
|
||||
ds.updateWithObject(status, *object)
|
||||
}
|
||||
113
pkg/models/jobs/resources/deployments.go
Normal file
113
pkg/models/jobs/resources/deployments.go
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/apps/v1beta2"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type deployment struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (deploy *deployment) list() (interface{}, error) {
|
||||
deoloyList, err := deploy.k8sClient.AppsV1beta2().Deployments("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deoloyList.Items, nil
|
||||
}
|
||||
|
||||
func (deploy *deployment) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := deploy.k8sClient.AppsV1beta2().Deployments("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (deploy *deployment) generateObject(item v1beta2.Deployment) WorkLoadObject {
|
||||
var app string
|
||||
var ready bool
|
||||
var updateTime meta_v1.Time
|
||||
name := item.Name
|
||||
namespace := item.Namespace
|
||||
availablePodNum := item.Status.AvailableReplicas
|
||||
desirePodNum := *item.Spec.Replicas
|
||||
release := item.ObjectMeta.Labels["release"]
|
||||
chart := item.ObjectMeta.Labels["chart"]
|
||||
|
||||
if len(release) > 0 && len(chart) > 0 {
|
||||
app = release + "/" + chart
|
||||
} else {
|
||||
app = "-"
|
||||
}
|
||||
|
||||
for _, conditon := range item.Status.Conditions {
|
||||
if conditon.Type == "Progressing" {
|
||||
updateTime = conditon.LastUpdateTime
|
||||
}
|
||||
}
|
||||
|
||||
if availablePodNum >= desirePodNum {
|
||||
ready = true
|
||||
} else {
|
||||
ready = false
|
||||
}
|
||||
|
||||
deployObject := WorkLoadObject{Namespace: namespace, Name: name, Available: availablePodNum, Desire: desirePodNum,
|
||||
App: app, UpdateTime: updateTime, Ready: ready}
|
||||
|
||||
return deployObject
|
||||
|
||||
}
|
||||
|
||||
func (deploy *deployment) updateWithObject(status *ResourceStatus, item v1beta2.Deployment) {
|
||||
namespace := item.Namespace
|
||||
|
||||
deployObject := deploy.generateObject(item)
|
||||
status.ResourceList.update(namespace, deployObject)
|
||||
}
|
||||
|
||||
func (deploy *deployment) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1beta2.Deployment)
|
||||
|
||||
for _, item := range items {
|
||||
deploy.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (deploy *deployment) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1beta2.Deployment)
|
||||
namespace := object.Namespace
|
||||
deployObject := deploy.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, deployObject)
|
||||
return
|
||||
}
|
||||
|
||||
deploy.updateWithObject(status, *object)
|
||||
}
|
||||
87
pkg/models/jobs/resources/ingresses.go
Normal file
87
pkg/models/jobs/resources/ingresses.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/extensions/v1beta1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type ingress struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (ing *ingress) list() (interface{}, error) {
|
||||
list, err := ing.k8sClient.ExtensionsV1beta1().Ingresses("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list.Items, nil
|
||||
}
|
||||
|
||||
func (ing *ingress) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := ing.k8sClient.ExtensionsV1beta1().Ingresses("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (ing *ingress) generateObject(item v1beta1.Ingress) OtherResourceObject {
|
||||
name := item.Name
|
||||
ns := item.Namespace
|
||||
|
||||
object := OtherResourceObject{Namespace: ns, Name: name}
|
||||
|
||||
return object
|
||||
|
||||
}
|
||||
|
||||
func (ing *ingress) updateWithObject(status *ResourceStatus, item v1beta1.Ingress) {
|
||||
namespace := item.Namespace
|
||||
|
||||
object := ing.generateObject(item)
|
||||
status.ResourceList.update(namespace, object)
|
||||
}
|
||||
|
||||
func (ing *ingress) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1beta1.Ingress)
|
||||
|
||||
for _, item := range items {
|
||||
ing.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ing *ingress) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1beta1.Ingress)
|
||||
namespace := object.Namespace
|
||||
tmpObject := ing.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, tmpObject)
|
||||
return
|
||||
}
|
||||
|
||||
ing.updateWithObject(status, *object)
|
||||
}
|
||||
87
pkg/models/jobs/resources/namespaces.go
Normal file
87
pkg/models/jobs/resources/namespaces.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type namespace struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (ns *namespace) list() (interface{}, error) {
|
||||
nsList, err := ns.k8sClient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nsList.Items, nil
|
||||
}
|
||||
|
||||
func (ns *namespace) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := ns.k8sClient.CoreV1().Namespaces().Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (ns *namespace) generateObject(item v1.Namespace) OtherResourceObject {
|
||||
name := item.Name
|
||||
nsp := item.Namespace
|
||||
|
||||
object := OtherResourceObject{Namespace: nsp, Name: name}
|
||||
|
||||
return object
|
||||
|
||||
}
|
||||
|
||||
func (ns *namespace) updateWithObject(status *ResourceStatus, item v1.Namespace) {
|
||||
namespace := item.Namespace
|
||||
|
||||
object := ns.generateObject(item)
|
||||
status.ResourceList.update(namespace, object)
|
||||
}
|
||||
|
||||
func (ns *namespace) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1.Namespace)
|
||||
|
||||
for _, item := range items {
|
||||
ns.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ns *namespace) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1.Namespace)
|
||||
namespace := object.Namespace
|
||||
tmpObject := ns.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, tmpObject)
|
||||
return
|
||||
}
|
||||
|
||||
ns.updateWithObject(status, *object)
|
||||
}
|
||||
87
pkg/models/jobs/resources/pods.go
Normal file
87
pkg/models/jobs/resources/pods.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type pod struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (po *pod) list() (interface{}, error) {
|
||||
list, err := po.k8sClient.CoreV1().Pods("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list.Items, nil
|
||||
}
|
||||
|
||||
func (po *pod) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := po.k8sClient.CoreV1().Pods("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (po *pod) generateObject(item v1.Pod) OtherResourceObject {
|
||||
name := item.Name
|
||||
ns := item.Namespace
|
||||
|
||||
Object := OtherResourceObject{Namespace: ns, Name: name}
|
||||
|
||||
return Object
|
||||
|
||||
}
|
||||
|
||||
func (po *pod) updateWithObject(status *ResourceStatus, item v1.Pod) {
|
||||
namespace := item.Namespace
|
||||
|
||||
object := po.generateObject(item)
|
||||
status.ResourceList.update(namespace, object)
|
||||
}
|
||||
|
||||
func (po *pod) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1.Pod)
|
||||
|
||||
for _, item := range items {
|
||||
po.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (po *pod) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1.Pod)
|
||||
namespace := object.Namespace
|
||||
tmpObject := po.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, tmpObject)
|
||||
return
|
||||
}
|
||||
|
||||
po.updateWithObject(status, *object)
|
||||
}
|
||||
87
pkg/models/jobs/resources/pvcs.go
Normal file
87
pkg/models/jobs/resources/pvcs.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type persistmentVolume struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (pvc *persistmentVolume) list() (interface{}, error) {
|
||||
list, err := pvc.k8sClient.CoreV1().PersistentVolumeClaims("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list.Items, nil
|
||||
}
|
||||
|
||||
func (pvc *persistmentVolume) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := pvc.k8sClient.CoreV1().PersistentVolumeClaims("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (pvc *persistmentVolume) generateObject(item v1.PersistentVolumeClaim) OtherResourceObject {
|
||||
name := item.Name
|
||||
ns := item.Namespace
|
||||
|
||||
object := OtherResourceObject{Namespace: ns, Name: name}
|
||||
|
||||
return object
|
||||
|
||||
}
|
||||
|
||||
func (pvc *persistmentVolume) updateWithObject(status *ResourceStatus, item v1.PersistentVolumeClaim) {
|
||||
namespace := item.Namespace
|
||||
|
||||
object := pvc.generateObject(item)
|
||||
status.ResourceList.update(namespace, object)
|
||||
}
|
||||
|
||||
func (pvc *persistmentVolume) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1.PersistentVolumeClaim)
|
||||
|
||||
for _, item := range items {
|
||||
pvc.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (pvc *persistmentVolume) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1.PersistentVolumeClaim)
|
||||
namespace := object.Namespace
|
||||
tmpObject := pvc.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, tmpObject)
|
||||
return
|
||||
}
|
||||
|
||||
pvc.updateWithObject(status, *object)
|
||||
}
|
||||
87
pkg/models/jobs/resources/roles.go
Normal file
87
pkg/models/jobs/resources/roles.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/rbac/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type role struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (r *role) list() (interface{}, error) {
|
||||
list, err := r.k8sClient.RbacV1().Roles("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list.Items, nil
|
||||
}
|
||||
|
||||
func (r *role) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := r.k8sClient.RbacV1().Roles("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (r *role) generateObject(item v1.Role) OtherResourceObject {
|
||||
name := item.Name
|
||||
ns := item.Namespace
|
||||
|
||||
object := OtherResourceObject{Namespace: ns, Name: name}
|
||||
|
||||
return object
|
||||
|
||||
}
|
||||
|
||||
func (r *role) updateWithObject(status *ResourceStatus, item v1.Role) {
|
||||
namespace := item.Namespace
|
||||
|
||||
object := r.generateObject(item)
|
||||
status.ResourceList.update(namespace, object)
|
||||
}
|
||||
|
||||
func (r *role) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1.Role)
|
||||
|
||||
for _, item := range items {
|
||||
r.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (r *role) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1.Role)
|
||||
namespace := object.Namespace
|
||||
tmpObject := r.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, tmpObject)
|
||||
return
|
||||
}
|
||||
|
||||
r.updateWithObject(status, *object)
|
||||
}
|
||||
178
pkg/models/jobs/resources/run.go
Normal file
178
pkg/models/jobs/resources/run.go
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
var etcdClient *client.EtcdClient
|
||||
|
||||
var stopChan = make(chan struct{})
|
||||
|
||||
const (
|
||||
pods = "pods"
|
||||
deployments = "deployments"
|
||||
daemonsets = "daemonsets"
|
||||
statefulsets = "statefulsets"
|
||||
namespaces = "namespaces"
|
||||
ingresses = "ingresses"
|
||||
persistentVolumeClaim = "persistent-volume-claim"
|
||||
roles = "roles"
|
||||
services = "services"
|
||||
)
|
||||
|
||||
func registerResource(resourceChans map[string]ResourceChan, resourceType string) {
|
||||
resourceChan := ResourceChan{Type: resourceType, StatusChan: make(chan *ResourceStatus), StopChan: stopChan}
|
||||
resourceChans[resourceType] = resourceChan
|
||||
}
|
||||
|
||||
func updateStatus(resource Resource, resourceChan ResourceChan) {
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
glog.Error(err)
|
||||
close(resourceChan.StatusChan)
|
||||
}
|
||||
}()
|
||||
|
||||
var clusterStatus ResourceStatus
|
||||
clusterStatus.UpdateTimeStamp = time.Now().Unix()
|
||||
clusterStatus.ResourceType = resourceChan.Type
|
||||
|
||||
items, err := resource.list()
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return
|
||||
}
|
||||
resource.updateWithObjects(&clusterStatus, items)
|
||||
|
||||
watcher, err := resource.getWatcher()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-resourceChan.StopChan:
|
||||
return
|
||||
case event := <-watcher.ResultChan():
|
||||
resource.updateWithEvent(&clusterStatus, event)
|
||||
break
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if time.Now().Unix()-clusterStatus.UpdateTimeStamp > constants.UpdateCircle {
|
||||
clusterStatus.UpdateTimeStamp = time.Now().Unix()
|
||||
resourceChan.StatusChan <- &clusterStatus
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func updateResourceStatus(resourceChan ResourceChan) {
|
||||
glog.Infof("updateResourceStatus:%s", resourceChan.Type)
|
||||
client := client.NewK8sClient()
|
||||
switch resourceChan.Type {
|
||||
case deployments:
|
||||
deploy := deployment{k8sClient: client}
|
||||
go updateStatus(&deploy, resourceChan)
|
||||
case daemonsets:
|
||||
ds := daemonset{k8sClient: client}
|
||||
go updateStatus(&ds, resourceChan)
|
||||
case statefulsets:
|
||||
ss := statefulset{k8sClient: client}
|
||||
go updateStatus(&ss, resourceChan)
|
||||
case namespaces:
|
||||
ns := namespace{k8sClient: client}
|
||||
go updateStatus(&ns, resourceChan)
|
||||
case ingresses:
|
||||
ing := ingress{k8sClient: client}
|
||||
go updateStatus(&ing, resourceChan)
|
||||
case persistentVolumeClaim:
|
||||
pvc := persistmentVolume{k8sClient: client}
|
||||
go updateStatus(&pvc, resourceChan)
|
||||
case roles:
|
||||
r := role{k8sClient: client}
|
||||
go updateStatus(&r, resourceChan)
|
||||
case services:
|
||||
svc := service{k8sClient: client}
|
||||
go updateStatus(&svc, resourceChan)
|
||||
case pods:
|
||||
po := pod{k8sClient: client}
|
||||
go updateStatus(&po, resourceChan)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func updateAllResourceStatus(resourceChans map[string]ResourceChan) {
|
||||
for _, resourceChan := range resourceChans {
|
||||
updateResourceStatus(resourceChan)
|
||||
}
|
||||
}
|
||||
|
||||
func receiveResourceStatus(resourceChans map[string]ResourceChan) {
|
||||
defer func() {
|
||||
close(stopChan)
|
||||
}()
|
||||
|
||||
for {
|
||||
for _, resourceChan := range resourceChans {
|
||||
select {
|
||||
case res, ok := <-resourceChan.StatusChan:
|
||||
if !ok {
|
||||
glog.Errorf("job:calculate %s' status have stopped", resourceChan.Type)
|
||||
registerResource(resourceChans, resourceChan.Type)
|
||||
updateResourceStatus(resourceChans[resourceChan.Type])
|
||||
} else {
|
||||
value, _ := json.Marshal(res)
|
||||
key := constants.Root + "/" + res.ResourceType
|
||||
etcdClient.Put(key, string(value))
|
||||
}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Run() {
|
||||
glog.Info("Begin to submit resource status")
|
||||
var err error
|
||||
etcdClient, err = client.NewEtcdClient()
|
||||
defer etcdClient.Close()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
resourceChans := make(map[string]ResourceChan)
|
||||
resourceList := []string{statefulsets, deployments, daemonsets, namespaces, ingresses, services, roles, persistentVolumeClaim, pods}
|
||||
for _, resource := range resourceList {
|
||||
registerResource(resourceChans, resource)
|
||||
}
|
||||
updateAllResourceStatus(resourceChans)
|
||||
receiveResourceStatus(resourceChans)
|
||||
}
|
||||
87
pkg/models/jobs/resources/services.go
Normal file
87
pkg/models/jobs/resources/services.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type service struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (svc *service) list() (interface{}, error) {
|
||||
list, err := svc.k8sClient.CoreV1().Services("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list.Items, nil
|
||||
}
|
||||
|
||||
func (svc *service) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := svc.k8sClient.CoreV1().Services("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (svc *service) generateObject(item v1.Service) OtherResourceObject {
|
||||
name := item.Name
|
||||
ns := item.Namespace
|
||||
|
||||
object := OtherResourceObject{Namespace: ns, Name: name}
|
||||
|
||||
return object
|
||||
|
||||
}
|
||||
|
||||
func (svc *service) updateWithObject(status *ResourceStatus, item v1.Service) {
|
||||
namespace := item.Namespace
|
||||
|
||||
object := svc.generateObject(item)
|
||||
status.ResourceList.update(namespace, object)
|
||||
}
|
||||
|
||||
func (svc *service) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1.Service)
|
||||
|
||||
for _, item := range items {
|
||||
svc.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (svc *service) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1.Service)
|
||||
namespace := object.Namespace
|
||||
tmpObject := svc.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, tmpObject)
|
||||
return
|
||||
}
|
||||
|
||||
svc.updateWithObject(status, *object)
|
||||
}
|
||||
107
pkg/models/jobs/resources/statefulsets.go
Normal file
107
pkg/models/jobs/resources/statefulsets.go
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
"k8s.io/api/apps/v1beta2"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type statefulset struct {
|
||||
k8sClient *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func (ss *statefulset) list() (interface{}, error) {
|
||||
daemonsetList, err := ss.k8sClient.AppsV1beta2().StatefulSets("").List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return daemonsetList.Items, nil
|
||||
}
|
||||
|
||||
func (ss *statefulset) getWatcher() (watch.Interface, error) {
|
||||
watcher, err := ss.k8sClient.AppsV1beta2().StatefulSets("").Watch(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return watcher, nil
|
||||
}
|
||||
|
||||
func (ss *statefulset) generateObject(item v1beta2.StatefulSet) WorkLoadObject {
|
||||
var app string
|
||||
var ready bool
|
||||
name := item.Name
|
||||
namespace := item.Namespace
|
||||
availablePodNum := item.Status.ReadyReplicas
|
||||
desirePodNum := *item.Spec.Replicas
|
||||
createTime := item.CreationTimestamp
|
||||
release := item.ObjectMeta.Labels["release"]
|
||||
chart := item.ObjectMeta.Labels["chart"]
|
||||
|
||||
if len(release) > 0 && len(chart) > 0 {
|
||||
app = release + "/" + chart
|
||||
} else {
|
||||
app = "-"
|
||||
}
|
||||
if availablePodNum >= desirePodNum {
|
||||
ready = true
|
||||
} else {
|
||||
ready = false
|
||||
}
|
||||
|
||||
statefulSetObject := WorkLoadObject{Namespace: namespace, Name: name, Available: availablePodNum, Desire: desirePodNum,
|
||||
App: app, CreateTime: createTime, Ready: ready}
|
||||
|
||||
return statefulSetObject
|
||||
|
||||
}
|
||||
|
||||
func (ss *statefulset) updateWithObject(status *ResourceStatus, item v1beta2.StatefulSet) {
|
||||
namespace := item.Namespace
|
||||
ssObject := ss.generateObject(item)
|
||||
|
||||
status.ResourceList.update(namespace, ssObject)
|
||||
|
||||
}
|
||||
|
||||
func (ss *statefulset) updateWithObjects(status *ResourceStatus, objects interface{}) {
|
||||
if status.ResourceList == nil {
|
||||
status.ResourceList = make(Resources)
|
||||
}
|
||||
|
||||
items := objects.([]v1beta2.StatefulSet)
|
||||
|
||||
for _, item := range items {
|
||||
ss.updateWithObject(status, item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ss *statefulset) updateWithEvent(status *ResourceStatus, event watch.Event) {
|
||||
object := event.Object.(*v1beta2.StatefulSet)
|
||||
namespace := object.Namespace
|
||||
ssObject := ss.generateObject(*object)
|
||||
|
||||
if event.Type == watch.Deleted {
|
||||
status.ResourceList.del(namespace, ssObject)
|
||||
return
|
||||
}
|
||||
|
||||
ss.updateWithObject(status, *object)
|
||||
}
|
||||
105
pkg/models/jobs/resources/types.go
Normal file
105
pkg/models/jobs/resources/types.go
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package resources
|
||||
|
||||
import (
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
)
|
||||
|
||||
type resource interface {
|
||||
equal(object resource) bool
|
||||
}
|
||||
|
||||
type resourcetList interface {
|
||||
update(key string, value resource)
|
||||
del(key string, value resource)
|
||||
}
|
||||
|
||||
type ResourceStatus struct {
|
||||
ResourceType string `json:"type"`
|
||||
ResourceList resourcetList `json:"lists"`
|
||||
UpdateTimeStamp int64 `json:"updateTimestamp"`
|
||||
}
|
||||
|
||||
type ResourceChan struct {
|
||||
Type string
|
||||
StatusChan chan *ResourceStatus
|
||||
StopChan chan struct{}
|
||||
}
|
||||
|
||||
type Resource interface {
|
||||
list() (interface{}, error)
|
||||
getWatcher() (watch.Interface, error)
|
||||
updateWithObjects(workload *ResourceStatus, objects interface{})
|
||||
updateWithEvent(workload *ResourceStatus, event watch.Event)
|
||||
}
|
||||
|
||||
type WorkLoadObject struct {
|
||||
Name string `json:"name"`
|
||||
Namespace string `json:"namespace"`
|
||||
App string `json:"app"`
|
||||
Available int32 `json:"available"`
|
||||
Desire int32 `json:"desire"`
|
||||
Ready bool `json:"ready"`
|
||||
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
|
||||
UpdateTime meta_v1.Time `json:"updateTime,omitempty"`
|
||||
CreateTime meta_v1.Time `json:"createTime,omitempty"`
|
||||
}
|
||||
|
||||
type OtherResourceObject struct {
|
||||
Name string `json:"name"`
|
||||
Namespace string `json:"namespace"`
|
||||
}
|
||||
|
||||
type Resources map[string][]resource
|
||||
|
||||
func (resources Resources) update(namespace string, object resource) {
|
||||
for index, tmpObject := range resources[namespace] {
|
||||
if tmpObject.equal(object) {
|
||||
resources[namespace][index] = object
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
resources[namespace] = append(resources[namespace], object)
|
||||
}
|
||||
|
||||
func (resources Resources) del(namespace string, object resource) {
|
||||
for index, tmpObject := range resources[namespace] {
|
||||
if tmpObject.equal(object) {
|
||||
resources[namespace] = append(resources[namespace][:index], resources[namespace][index+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (workLoadObject WorkLoadObject) equal(object resource) bool {
|
||||
tmp := object.(WorkLoadObject)
|
||||
if workLoadObject.Name == tmp.Name && workLoadObject.Namespace == tmp.Namespace {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (otherResourceObject OtherResourceObject) equal(object resource) bool {
|
||||
tmp := object.(OtherResourceObject)
|
||||
if otherResourceObject.Name == tmp.Name && otherResourceObject.Namespace == tmp.Namespace {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -18,84 +18,259 @@ package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/base64"
|
||||
"text/template"
|
||||
"encoding/pem"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
rd "math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"gopkg.in/yaml.v2"
|
||||
"k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/options"
|
||||
)
|
||||
|
||||
var kubeconfigTemp = "apiVersion: v1\n" +
|
||||
"clusters:\n" +
|
||||
"- cluster:\n" +
|
||||
" certificate-authority-data: {{.Certificate}}\n" +
|
||||
" server: {{.Server}}\n" +
|
||||
" name: kubernetes\n" +
|
||||
"contexts:\n" +
|
||||
"- context:\n" +
|
||||
" cluster: kubernetes\n" +
|
||||
" user: {{.User}}\n" +
|
||||
" namespace: {{.User}}\n" +
|
||||
" name: default\n" +
|
||||
"current-context: default\n" +
|
||||
"kind: Config\n" +
|
||||
"preferences: {}\n" +
|
||||
"users:\n" +
|
||||
"- name: {{.User}}\n" +
|
||||
" user:\n" +
|
||||
" token: {{.Token}}\n"
|
||||
const (
|
||||
caPath = "/etc/kubernetes/pki/ca.crt"
|
||||
keyPath = "/etc/kubernetes/pki/ca.key"
|
||||
clusterName = "kubernetes"
|
||||
kubectlNamespace = "kubesphere"
|
||||
kubectlConfigKey = "config"
|
||||
)
|
||||
|
||||
const DefaultServiceAccount = "default"
|
||||
|
||||
type Config struct {
|
||||
Certificate string
|
||||
Server string
|
||||
User string
|
||||
Token string
|
||||
type clusterInfo struct {
|
||||
CertificateAuthorityData string `yaml:"certificate-authority-data"`
|
||||
Server string `yaml:"server"`
|
||||
}
|
||||
|
||||
func GetKubeConfig(namespace string) (string, error) {
|
||||
tmpl, err := template.New("").Parse(kubeconfigTemp)
|
||||
type cluster struct {
|
||||
Cluster clusterInfo `yaml:"cluster"`
|
||||
Name string `yaml:"name"`
|
||||
}
|
||||
|
||||
type contextInfo struct {
|
||||
Cluster string `yaml:"cluster"`
|
||||
User string `yaml:"user"`
|
||||
}
|
||||
|
||||
type contextObject struct {
|
||||
Context contextInfo `yaml:"context"`
|
||||
Name string `yaml:"name"`
|
||||
}
|
||||
|
||||
type userInfo struct {
|
||||
CaData string `yaml:"client-certificate-data"`
|
||||
KeyData string `yaml:"client-key-data"`
|
||||
}
|
||||
|
||||
type user struct {
|
||||
Name string `yaml:"name"`
|
||||
User userInfo `yaml:"user"`
|
||||
}
|
||||
|
||||
type kubeConfig struct {
|
||||
ApiVersion string `yaml:"apiVersion"`
|
||||
Clusters []cluster `yaml:"clusters"`
|
||||
Contexts []contextObject `yaml:"contexts"`
|
||||
CurrentContext string `yaml:"current-context"`
|
||||
Kind string `yaml:"kind"`
|
||||
Preferences map[string]string `yaml:"preferences"`
|
||||
Users []user `yaml:"users"`
|
||||
}
|
||||
|
||||
type CertInformation struct {
|
||||
Country []string
|
||||
Organization []string
|
||||
OrganizationalUnit []string
|
||||
EmailAddress []string
|
||||
Province []string
|
||||
Locality []string
|
||||
CommonName string
|
||||
CrtName, KeyName string
|
||||
IsCA bool
|
||||
Names []pkix.AttributeTypeAndValue
|
||||
}
|
||||
|
||||
func createCRT(RootCa *x509.Certificate, RootKey *rsa.PrivateKey, info CertInformation) ([]byte, []byte, error) {
|
||||
var cert, key bytes.Buffer
|
||||
Crt := newCertificate(info)
|
||||
Key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var buf []byte
|
||||
|
||||
buf, err = x509.CreateCertificate(rand.Reader, Crt, RootCa, &Key.PublicKey, RootKey)
|
||||
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
pem.Encode(&cert, &pem.Block{Type: "CERTIFICATE", Bytes: buf})
|
||||
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
buf = x509.MarshalPKCS1PrivateKey(Key)
|
||||
pem.Encode(&key, &pem.Block{Type: "PRIVATE KEY", Bytes: buf})
|
||||
|
||||
return cert.Bytes(), key.Bytes(), nil
|
||||
}
|
||||
|
||||
func Parse(crtPath, keyPath string) (rootcertificate *x509.Certificate, rootPrivateKey *rsa.PrivateKey, err error) {
|
||||
rootcertificate, err = parseCrt(crtPath)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
rootPrivateKey, err = parseKey(keyPath)
|
||||
return rootcertificate, rootPrivateKey, nil
|
||||
}
|
||||
|
||||
func parseCrt(path string) (*x509.Certificate, error) {
|
||||
buf, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
p := &pem.Block{}
|
||||
p, buf = pem.Decode(buf)
|
||||
return x509.ParseCertificate(p.Bytes)
|
||||
}
|
||||
|
||||
func parseKey(path string) (*rsa.PrivateKey, error) {
|
||||
buf, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
p, buf := pem.Decode(buf)
|
||||
return x509.ParsePKCS1PrivateKey(p.Bytes)
|
||||
}
|
||||
|
||||
func newCertificate(info CertInformation) *x509.Certificate {
|
||||
rd.Seed(time.Now().UnixNano())
|
||||
return &x509.Certificate{
|
||||
SerialNumber: big.NewInt(rd.Int63()),
|
||||
Subject: pkix.Name{
|
||||
Country: info.Country,
|
||||
Organization: info.Organization,
|
||||
OrganizationalUnit: info.OrganizationalUnit,
|
||||
Province: info.Province,
|
||||
CommonName: info.CommonName,
|
||||
Locality: info.Locality,
|
||||
ExtraNames: info.Names,
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(20, 0, 0),
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: info.IsCA,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
|
||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
||||
EmailAddresses: info.EmailAddress,
|
||||
}
|
||||
}
|
||||
|
||||
func generateCaAndKey(user, caPath, keyPath string) (string, string, error) {
|
||||
crtinfo := CertInformation{CommonName: user, IsCA: false}
|
||||
|
||||
crt, pri, err := Parse(caPath, keyPath)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return "", "", err
|
||||
}
|
||||
cert, key, err := createCRT(crt, pri, crtinfo)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
base64Cert := base64.StdEncoding.EncodeToString(cert)
|
||||
base64Key := base64.StdEncoding.EncodeToString(key)
|
||||
return base64Cert, base64Key, nil
|
||||
}
|
||||
|
||||
func createKubeConfig(userName string) (string, error) {
|
||||
tmpKubeConfig := kubeConfig{ApiVersion: "v1", Kind: "Config"}
|
||||
serverCa, err := ioutil.ReadFile(caPath)
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return "", err
|
||||
}
|
||||
base64ServerCa := base64.StdEncoding.EncodeToString(serverCa)
|
||||
tmpClusterInfo := clusterInfo{CertificateAuthorityData: base64ServerCa, Server: options.ServerOptions.GetApiServerHost()}
|
||||
tmpCluster := cluster{Cluster: tmpClusterInfo, Name: clusterName}
|
||||
tmpKubeConfig.Clusters = append(tmpKubeConfig.Clusters, tmpCluster)
|
||||
|
||||
kubeConfig, err := getKubeConfig(namespace, options.ServerOptions.GetApiServerHost())
|
||||
contextName := userName + "@" + clusterName
|
||||
tmpContext := contextObject{Context: contextInfo{User: userName, Cluster: clusterName}, Name: contextName}
|
||||
tmpKubeConfig.Contexts = append(tmpKubeConfig.Contexts, tmpContext)
|
||||
|
||||
cert, key, err := generateCaAndKey(userName, caPath, keyPath)
|
||||
|
||||
buf := bytes.NewBufferString("")
|
||||
err = tmpl.Execute(buf, kubeConfig)
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
|
||||
tmpUser := user{User: userInfo{CaData: cert, KeyData: key}, Name: userName}
|
||||
tmpKubeConfig.Users = append(tmpKubeConfig.Users, tmpUser)
|
||||
tmpKubeConfig.CurrentContext = contextName
|
||||
|
||||
config, err := yaml.Marshal(tmpKubeConfig)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(config), nil
|
||||
}
|
||||
|
||||
func getKubeConfig(namespace, apiserverHost string) (*Config, error) {
|
||||
func CreateKubeConfig(user string) error {
|
||||
k8sClient := client.NewK8sClient()
|
||||
saInfo, err := k8sClient.CoreV1().ServiceAccounts(namespace).Get(DefaultServiceAccount, meta_v1.GetOptions{})
|
||||
config, err := createKubeConfig(user)
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
secretName := saInfo.Secrets[0].Name
|
||||
|
||||
secretInfo, err := k8sClient.CoreV1().Secrets(namespace).Get(secretName, meta_v1.GetOptions{})
|
||||
data := map[string]string{"config": string(config)}
|
||||
var configmap = v1.ConfigMap{metav1.TypeMeta{Kind: "Configmap", APIVersion: "v1"}, metav1.ObjectMeta{Name: user}, data}
|
||||
_, err = k8sClient.CoreV1().ConfigMaps(kubectlNamespace).Create(&configmap)
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
secretData := secretInfo.Data
|
||||
certificate := string(secretData["ca.crt"])
|
||||
certificate = base64.StdEncoding.EncodeToString([]byte(certificate))
|
||||
server := apiserverHost
|
||||
token := string(secretData["token"])
|
||||
user := string(secretData["namespace"])
|
||||
|
||||
return &Config{Certificate: certificate, Server: server, Token: token, User: user}, nil
|
||||
}
|
||||
|
||||
func GetKubeConfig(user string) (string, error) {
|
||||
k8sClient := client.NewK8sClient()
|
||||
configmap, err := k8sClient.CoreV1().ConfigMaps(kubectlNamespace).Get(user, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return "", err
|
||||
}
|
||||
return configmap.Data[kubectlConfigKey], nil
|
||||
}
|
||||
|
||||
func DelKubeConfig(user string) error {
|
||||
k8sClient := client.NewK8sClient()
|
||||
err := k8sClient.CoreV1().ConfigMaps(kubectlNamespace).Delete(user, &metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -21,26 +21,27 @@ import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/api/apps/v1beta2"
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/options"
|
||||
)
|
||||
|
||||
const (
|
||||
deploymentName = "kubectl"
|
||||
)
|
||||
const namespace = constants.NameSpace
|
||||
|
||||
type kubectlPodInfo struct {
|
||||
Namespace string `json: "namespace"`
|
||||
Pod string `json: "podname"`
|
||||
Container string `json: "container"`
|
||||
Namespace string `json:"namespace"`
|
||||
Pod string `json:"pod"`
|
||||
Container string `json:"container"`
|
||||
}
|
||||
|
||||
func GetKubectlPod(namespace string) (kubectlPodInfo, error) {
|
||||
func GetKubectlPod(user string) (kubectlPodInfo, error) {
|
||||
k8sClient := client.NewK8sClient()
|
||||
deploy, err := k8sClient.AppsV1beta2().Deployments(namespace).Get(deploymentName, meta_v1.GetOptions{})
|
||||
deploy, err := k8sClient.AppsV1beta2().Deployments(namespace).Get(user, meta_v1.GetOptions{})
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return kubectlPodInfo{}, err
|
||||
@@ -84,3 +85,53 @@ func selectCorrectPod(namespace string, pods []v1.Pod) (kubectlPod v1.Pod, err e
|
||||
random := rand.Intn(len(kubectPodList))
|
||||
return kubectPodList[random], nil
|
||||
}
|
||||
|
||||
func CreateKubectlPod(user string) error {
|
||||
|
||||
replica := int32(1)
|
||||
selector := meta_v1.LabelSelector{MatchLabels: map[string]string{"user": user}}
|
||||
config := v1.ConfigMapVolumeSource{Items: []v1.KeyToPath{{Key: "config", Path: "config"}}, LocalObjectReference: v1.LocalObjectReference{Name: user}}
|
||||
deployment := v1beta2.Deployment{
|
||||
ObjectMeta: meta_v1.ObjectMeta{
|
||||
Name: user,
|
||||
},
|
||||
Spec: v1beta2.DeploymentSpec{
|
||||
Replicas: &replica,
|
||||
Selector: &selector,
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: meta_v1.ObjectMeta{
|
||||
Labels: map[string]string{
|
||||
"user": user,
|
||||
},
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
Containers: []v1.Container{
|
||||
{Name: "kubectl",
|
||||
Image: options.ServerOptions.GetKubectlImage(),
|
||||
VolumeMounts: []v1.VolumeMount{{Name: "kubeconfig", MountPath: "/root/.kube"}},
|
||||
},
|
||||
},
|
||||
Volumes: []v1.Volume{{Name: "kubeconfig", VolumeSource: v1.VolumeSource{ConfigMap: &config}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
k8sClient := client.NewK8sClient()
|
||||
_, err := k8sClient.AppsV1beta2().Deployments(namespace).Create(&deployment)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func DelKubectlPod(user string) error {
|
||||
k8sClient := client.NewK8sClient()
|
||||
deploy, err := k8sClient.AppsV1beta2().Deployments(namespace).Get(user, meta_v1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
replicas := int32(0)
|
||||
deploy.Spec.Replicas = &replicas
|
||||
k8sClient.AppsV1beta2().Deployments(namespace).Update(deploy)
|
||||
err = k8sClient.AppsV1beta2().Deployments(namespace).Delete(user, &meta_v1.DeleteOptions{})
|
||||
return err
|
||||
}
|
||||
|
||||
66
pkg/models/quota.go
Normal file
66
pkg/models/quota.go
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"k8s.io/api/core/v1"
|
||||
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
type resourceQuota struct {
|
||||
NameSpace string `json:"namespace"`
|
||||
Data v1.ResourceQuotaStatus `json:"data"`
|
||||
UpdateTimeStamp int64 `json:"updateTimeStamp"`
|
||||
}
|
||||
|
||||
func GetNamespaceQuota(namespace string) (*resourceQuota, error) {
|
||||
|
||||
cli, err := client.NewEtcdClient()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
key := constants.Root + "/" + constants.QuotaKey + "/" + namespace
|
||||
value, err := cli.Get(key)
|
||||
var data = v1.ResourceQuotaStatus{make(v1.ResourceList), make(v1.ResourceList)}
|
||||
var res = resourceQuota{Data: data}
|
||||
|
||||
err = json.Unmarshal(value, &res)
|
||||
if time.Now().Unix()-res.UpdateTimeStamp > 5*constants.UpdateCircle {
|
||||
err = errors.New("internal server error")
|
||||
return nil, err
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func GetClusterQuota() (*resourceQuota, error) {
|
||||
|
||||
return GetNamespaceQuota("\"\"")
|
||||
}
|
||||
315
pkg/models/terminal.go
Normal file
315
pkg/models/terminal.go
Normal file
@@ -0,0 +1,315 @@
|
||||
// Copyright 2018 The Kubesphere Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
restful "github.com/emicklei/go-restful"
|
||||
"github.com/golang/glog"
|
||||
"gopkg.in/igm/sockjs-go.v2/sockjs"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/remotecommand"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/options"
|
||||
)
|
||||
|
||||
// TerminalResponse is sent by handleExecShell. The Id is a random session id that binds the original REST request and the SockJS connection.
|
||||
// Any clientapi in possession of this Id can hijack the terminal session.
|
||||
type TerminalResponse struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
// PtyHandler is what remotecommand expects from a pty
|
||||
type PtyHandler interface {
|
||||
io.Reader
|
||||
io.Writer
|
||||
remotecommand.TerminalSizeQueue
|
||||
}
|
||||
|
||||
// TerminalSession implements PtyHandler (using a SockJS connection)
|
||||
type TerminalSession struct {
|
||||
id string
|
||||
bound chan error
|
||||
sockJSSession sockjs.Session
|
||||
sizeChan chan remotecommand.TerminalSize
|
||||
}
|
||||
|
||||
// TerminalMessage is the messaging protocol between ShellController and TerminalSession.
|
||||
//
|
||||
// OP DIRECTION FIELD(S) USED DESCRIPTION
|
||||
// ---------------------------------------------------------------------
|
||||
// bind fe->be SessionID Id sent back from TerminalResponse
|
||||
// stdin fe->be Data Keystrokes/paste buffer
|
||||
// resize fe->be Rows, Cols New terminal size
|
||||
// stdout be->fe Data Output from the process
|
||||
// toast be->fe Data OOB message to be shown to the user
|
||||
type TerminalMessage struct {
|
||||
Op, Data, SessionID string
|
||||
Rows, Cols uint16
|
||||
}
|
||||
|
||||
// TerminalSize handles pty->process resize events
|
||||
// Called in a loop from remotecommand as long as the process is running
|
||||
func (t TerminalSession) Next() *remotecommand.TerminalSize {
|
||||
select {
|
||||
case size := <-t.sizeChan:
|
||||
return &size
|
||||
}
|
||||
}
|
||||
|
||||
// Read handles pty->process messages (stdin, resize)
|
||||
// Called in a loop from remotecommand as long as the process is running
|
||||
func (t TerminalSession) Read(p []byte) (int, error) {
|
||||
m, err := t.sockJSSession.Recv()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var msg TerminalMessage
|
||||
if err := json.Unmarshal([]byte(m), &msg); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
switch msg.Op {
|
||||
case "stdin":
|
||||
return copy(p, msg.Data), nil
|
||||
case "resize":
|
||||
t.sizeChan <- remotecommand.TerminalSize{msg.Cols, msg.Rows}
|
||||
return 0, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("unknown message type '%s'", msg.Op)
|
||||
}
|
||||
}
|
||||
|
||||
// Write handles process->pty stdout
|
||||
// Called from remotecommand whenever there is any output
|
||||
func (t TerminalSession) Write(p []byte) (int, error) {
|
||||
msg, err := json.Marshal(TerminalMessage{
|
||||
Op: "stdout",
|
||||
Data: string(p),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if err = t.sockJSSession.Send(string(msg)); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// Toast can be used to send the user any OOB messages
|
||||
// hterm puts these in the center of the terminal
|
||||
func (t TerminalSession) Toast(p string) error {
|
||||
msg, err := json.Marshal(TerminalMessage{
|
||||
Op: "toast",
|
||||
Data: p,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = t.sockJSSession.Send(string(msg)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close shuts down the SockJS connection and sends the status code and reason to the client
|
||||
// Can happen if the process exits or if there is an error starting up the process
|
||||
// For now the status code is unused and reason is shown to the user (unless "")
|
||||
func (t TerminalSession) Close(status uint32, reason string) {
|
||||
t.sockJSSession.Close(status, reason)
|
||||
}
|
||||
|
||||
// terminalSessions stores a map of all TerminalSession objects
|
||||
// FIXME: this structure needs locking
|
||||
var terminalSessions = make(map[string]TerminalSession)
|
||||
|
||||
// handleTerminalSession is Called by net/http for any new /api/sockjs connections
|
||||
func handleTerminalSession(session sockjs.Session) {
|
||||
glog.Infof("handleTerminalSession, ID:%s", session.ID())
|
||||
var (
|
||||
buf string
|
||||
err error
|
||||
msg TerminalMessage
|
||||
terminalSession TerminalSession
|
||||
ok bool
|
||||
)
|
||||
|
||||
if buf, err = session.Recv(); err != nil {
|
||||
glog.Errorf("handleTerminalSession: can't Recv: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = json.Unmarshal([]byte(buf), &msg); err != nil {
|
||||
glog.Errorf("handleTerminalSession: can't UnMarshal (%v): %s", err, buf)
|
||||
return
|
||||
}
|
||||
|
||||
if msg.Op != "bind" {
|
||||
glog.Errorf("handleTerminalSession: expected 'bind' message, got: %s", buf)
|
||||
return
|
||||
}
|
||||
|
||||
if terminalSession, ok = terminalSessions[msg.SessionID]; !ok {
|
||||
glog.Errorf("handleTerminalSession: can't find session '%s'", msg.SessionID)
|
||||
return
|
||||
}
|
||||
|
||||
terminalSession.sockJSSession = session
|
||||
terminalSessions[msg.SessionID] = terminalSession
|
||||
terminalSession.bound <- nil
|
||||
}
|
||||
|
||||
// CreateAttachHandler is called from main for /api/sockjs
|
||||
func CreateTerminalHandler(path string) http.Handler {
|
||||
return sockjs.NewHandler(path, sockjs.DefaultOptions, handleTerminalSession)
|
||||
}
|
||||
|
||||
// startProcess is called by handleAttach
|
||||
// Executed cmd in the container specified in request and connects it up with the ptyHandler (a session)
|
||||
func startProcess(k8sClient kubernetes.Interface, cfg *rest.Config, request *restful.Request, cmd []string, ptyHandler PtyHandler) error {
|
||||
namespace := request.PathParameter("namespace")
|
||||
podName := request.PathParameter("pod")
|
||||
containerName := request.PathParameter("container")
|
||||
|
||||
req := k8sClient.CoreV1().RESTClient().Post().
|
||||
Resource("pods").
|
||||
Name(podName).
|
||||
Namespace(namespace).
|
||||
SubResource("exec")
|
||||
|
||||
req.VersionedParams(&v1.PodExecOptions{
|
||||
Container: containerName,
|
||||
Command: cmd,
|
||||
Stdin: true,
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
TTY: true,
|
||||
}, scheme.ParameterCodec)
|
||||
|
||||
exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = exec.Stream(remotecommand.StreamOptions{
|
||||
Stdin: ptyHandler,
|
||||
Stdout: ptyHandler,
|
||||
Stderr: ptyHandler,
|
||||
TerminalSizeQueue: ptyHandler,
|
||||
Tty: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// genTerminalSessionId generates a random session ID string. The format is not really interesting.
|
||||
// This ID is used to identify the session when the client opens the SockJS connection.
|
||||
// Not the same as the SockJS session id! We can't use that as that is generated
|
||||
// on the client side and we don't have it yet at this point.
|
||||
func genTerminalSessionId() (string, error) {
|
||||
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
id := make([]byte, hex.EncodedLen(len(bytes)))
|
||||
hex.Encode(id, bytes)
|
||||
glog.Infof("genTerminalSessionId, id:" + string(id))
|
||||
return string(id), nil
|
||||
}
|
||||
|
||||
// isValidShell checks if the shell is an allowed one
|
||||
func isValidShell(validShells []string, shell string) bool {
|
||||
for _, validShell := range validShells {
|
||||
if validShell == shell {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// WaitForTerminal is called from apihandler.handleAttach as a goroutine
|
||||
// Waits for the SockJS connection to be opened by the client the session to be bound in handleTerminalSession
|
||||
func WaitForTerminal(k8sClient kubernetes.Interface, cfg *rest.Config, request *restful.Request, sessionId string) {
|
||||
glog.Infof("WaitForTerminal, ID:%s", sessionId)
|
||||
shell := request.QueryParameter("shell")
|
||||
|
||||
select {
|
||||
case <-terminalSessions[sessionId].bound:
|
||||
close(terminalSessions[sessionId].bound)
|
||||
|
||||
var err error
|
||||
validShells := []string{"bash", "sh"}
|
||||
|
||||
if isValidShell(validShells, shell) {
|
||||
cmd := []string{shell}
|
||||
err = startProcess(k8sClient, cfg, request, cmd, terminalSessions[sessionId])
|
||||
} else {
|
||||
// No shell given or it was not valid: try some shells until one succeeds or all fail
|
||||
// FIXME: if the first shell fails then the first keyboard event is lost
|
||||
for _, testShell := range validShells {
|
||||
cmd := []string{testShell}
|
||||
if err = startProcess(k8sClient, cfg, request, cmd, terminalSessions[sessionId]); err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
terminalSessions[sessionId].Close(2, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
terminalSessions[sessionId].Close(1, "Process exited")
|
||||
}
|
||||
}
|
||||
|
||||
// Handles execute shell API call
|
||||
func HandleExecShell(request *restful.Request) (*TerminalResponse, error) {
|
||||
sessionId, err := genTerminalSessionId()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
terminalSessions[sessionId] = TerminalSession{
|
||||
id: sessionId,
|
||||
bound: make(chan error),
|
||||
sizeChan: make(chan remotecommand.TerminalSize),
|
||||
}
|
||||
|
||||
kubeconfig, err := options.ServerOptions.GetKubeConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
go WaitForTerminal(client.NewK8sClient(), kubeconfig, request, sessionId)
|
||||
return &TerminalResponse{Id: sessionId}, nil
|
||||
}
|
||||
70
pkg/models/workload-status.go
Normal file
70
pkg/models/workload-status.go
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
type workLoadStatus struct {
|
||||
NameSpace string `json:"namespace"`
|
||||
Data map[string]int `json:"data"`
|
||||
UpdateTimeStamp int64 `json:"updateTimeStamp"`
|
||||
}
|
||||
|
||||
var resourceList = []string{"deployments", "daemonsets", "statefulsets"}
|
||||
|
||||
func GetNamespacesResourceStatus(namespace string) (*workLoadStatus, error) {
|
||||
|
||||
cli, err := client.NewEtcdClient()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
res := workLoadStatus{Data: make(map[string]int)}
|
||||
|
||||
key := constants.Root + "/" + constants.WorkloadStatusKey + "/" + namespace
|
||||
value, err := cli.Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(value, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if time.Now().Unix()-res.UpdateTimeStamp > 5*constants.UpdateCircle {
|
||||
err = errors.New("data in etcd is too old")
|
||||
return nil, err
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func GetClusterResourceStatus() (*workLoadStatus, error) {
|
||||
|
||||
return GetNamespacesResourceStatus("\"\"")
|
||||
}
|
||||
@@ -20,8 +20,20 @@ package options
|
||||
import (
|
||||
goflag "flag"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
const (
|
||||
// High enough QPS to fit all expected use cases. QPS=0 is not set here, because
|
||||
// client code is overriding it.
|
||||
DefaultQPS = 1e6
|
||||
// High enough Burst to fit all expected use cases. Burst=0 is not set here, because
|
||||
// client code is overriding it.
|
||||
DefaultBurst = 1e6
|
||||
)
|
||||
|
||||
// ServerRunOptions runs a kubernetes api server.
|
||||
@@ -34,6 +46,11 @@ type ServerRunOptions struct {
|
||||
certFile string
|
||||
keyFile string
|
||||
kubeConfigFile string
|
||||
etcdEndpoints string
|
||||
etcdCertFile string
|
||||
etcdKeyFile string
|
||||
etcdCaFile string
|
||||
kubectlImage string
|
||||
}
|
||||
|
||||
// NewServerRunOptions creates a new ServerRunOptions object with default parameters
|
||||
@@ -66,6 +83,20 @@ func (s *ServerRunOptions) addFlags(fs *pflag.FlagSet) {
|
||||
|
||||
fs.StringVar(&s.kubeConfigFile, "kubeconfig", "",
|
||||
"Path to kubeconfig file with authorization and master location information.")
|
||||
|
||||
fs.StringVar(&s.etcdEndpoints, "etcd-endpoints", "",
|
||||
"Server addresses of etcd")
|
||||
fs.StringVar(&s.etcdCertFile, "etcd-tls-cert-file", "",
|
||||
"Cert File use to connect etcd in https mode.")
|
||||
|
||||
fs.StringVar(&s.etcdKeyFile, "etcd-tls-key-file", "",
|
||||
"Privatekey File use to connect etcd in https mode.")
|
||||
|
||||
fs.StringVar(&s.etcdCaFile, "etcd-tls-ca-file", "",
|
||||
"CA Fileuse to connect etcd in https mode.")
|
||||
|
||||
fs.StringVar(&s.kubectlImage, "kubectl-image", "kubectl:1.0",
|
||||
"kubectl pod's image")
|
||||
}
|
||||
|
||||
func (s *ServerRunOptions) GetApiServerHost() string {
|
||||
@@ -100,6 +131,56 @@ func (s *ServerRunOptions) GetKubeConfigFile() string {
|
||||
return s.kubeConfigFile
|
||||
}
|
||||
|
||||
func (s *ServerRunOptions) GetKubeConfig() (kubeConfig *rest.Config, err error) {
|
||||
|
||||
kubeConfigFile := s.kubeConfigFile
|
||||
|
||||
if len(kubeConfigFile) > 0 {
|
||||
|
||||
kubeConfig, err = clientcmd.BuildConfigFromFlags("", kubeConfigFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
kubeConfig, err = rest.InClusterConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
kubeConfig.QPS = DefaultQPS
|
||||
kubeConfig.Burst = DefaultBurst
|
||||
|
||||
return kubeConfig, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *ServerRunOptions) GetEtcdEndPoints() []string {
|
||||
endpoints := strings.Split(s.etcdEndpoints, ",")
|
||||
for k, v := range endpoints {
|
||||
endpoints[k] = strings.TrimSpace(v)
|
||||
}
|
||||
return endpoints
|
||||
}
|
||||
|
||||
func (s *ServerRunOptions) GetEtcdCertFile() string {
|
||||
return s.etcdCertFile
|
||||
}
|
||||
|
||||
func (s *ServerRunOptions) GetEtcdKeyFile() string {
|
||||
return s.etcdKeyFile
|
||||
}
|
||||
|
||||
func (s *ServerRunOptions) GetEtcdCaFile() string {
|
||||
return s.etcdCaFile
|
||||
}
|
||||
|
||||
func (s *ServerRunOptions) GetKubectlImage() string {
|
||||
return s.kubectlImage
|
||||
}
|
||||
|
||||
var ServerOptions = NewServerRunOptions()
|
||||
|
||||
func AddFlags(fs *pflag.FlagSet) {
|
||||
|
||||
1
vendor/github.com/coreos/etcd
generated
vendored
Submodule
1
vendor/github.com/coreos/etcd
generated
vendored
Submodule
Submodule vendor/github.com/coreos/etcd added at ce918cef63
191
vendor/github.com/docker/spdystream/LICENSE
generated
vendored
Normal file
191
vendor/github.com/docker/spdystream/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,191 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
Copyright 2014-2015 Docker, Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
958
vendor/github.com/docker/spdystream/connection.go
generated
vendored
Normal file
958
vendor/github.com/docker/spdystream/connection.go
generated
vendored
Normal file
@@ -0,0 +1,958 @@
|
||||
package spdystream
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/docker/spdystream/spdy"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidStreamId = errors.New("Invalid stream id")
|
||||
ErrTimeout = errors.New("Timeout occured")
|
||||
ErrReset = errors.New("Stream reset")
|
||||
ErrWriteClosedStream = errors.New("Write on closed stream")
|
||||
)
|
||||
|
||||
const (
|
||||
FRAME_WORKERS = 5
|
||||
QUEUE_SIZE = 50
|
||||
)
|
||||
|
||||
type StreamHandler func(stream *Stream)
|
||||
|
||||
type AuthHandler func(header http.Header, slot uint8, parent uint32) bool
|
||||
|
||||
type idleAwareFramer struct {
|
||||
f *spdy.Framer
|
||||
conn *Connection
|
||||
writeLock sync.Mutex
|
||||
resetChan chan struct{}
|
||||
setTimeoutLock sync.Mutex
|
||||
setTimeoutChan chan time.Duration
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func newIdleAwareFramer(framer *spdy.Framer) *idleAwareFramer {
|
||||
iaf := &idleAwareFramer{
|
||||
f: framer,
|
||||
resetChan: make(chan struct{}, 2),
|
||||
// setTimeoutChan needs to be buffered to avoid deadlocks when calling setIdleTimeout at about
|
||||
// the same time the connection is being closed
|
||||
setTimeoutChan: make(chan time.Duration, 1),
|
||||
}
|
||||
return iaf
|
||||
}
|
||||
|
||||
func (i *idleAwareFramer) monitor() {
|
||||
var (
|
||||
timer *time.Timer
|
||||
expired <-chan time.Time
|
||||
resetChan = i.resetChan
|
||||
setTimeoutChan = i.setTimeoutChan
|
||||
)
|
||||
Loop:
|
||||
for {
|
||||
select {
|
||||
case timeout := <-i.setTimeoutChan:
|
||||
i.timeout = timeout
|
||||
if timeout == 0 {
|
||||
if timer != nil {
|
||||
timer.Stop()
|
||||
}
|
||||
} else {
|
||||
if timer == nil {
|
||||
timer = time.NewTimer(timeout)
|
||||
expired = timer.C
|
||||
} else {
|
||||
timer.Reset(timeout)
|
||||
}
|
||||
}
|
||||
case <-resetChan:
|
||||
if timer != nil && i.timeout > 0 {
|
||||
timer.Reset(i.timeout)
|
||||
}
|
||||
case <-expired:
|
||||
i.conn.streamCond.L.Lock()
|
||||
streams := i.conn.streams
|
||||
i.conn.streams = make(map[spdy.StreamId]*Stream)
|
||||
i.conn.streamCond.Broadcast()
|
||||
i.conn.streamCond.L.Unlock()
|
||||
go func() {
|
||||
for _, stream := range streams {
|
||||
stream.resetStream()
|
||||
}
|
||||
i.conn.Close()
|
||||
}()
|
||||
case <-i.conn.closeChan:
|
||||
if timer != nil {
|
||||
timer.Stop()
|
||||
}
|
||||
|
||||
// Start a goroutine to drain resetChan. This is needed because we've seen
|
||||
// some unit tests with large numbers of goroutines get into a situation
|
||||
// where resetChan fills up, at least 1 call to Write() is still trying to
|
||||
// send to resetChan, the connection gets closed, and this case statement
|
||||
// attempts to grab the write lock that Write() already has, causing a
|
||||
// deadlock.
|
||||
//
|
||||
// See https://github.com/docker/spdystream/issues/49 for more details.
|
||||
go func() {
|
||||
for _ = range resetChan {
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for _ = range setTimeoutChan {
|
||||
}
|
||||
}()
|
||||
|
||||
i.writeLock.Lock()
|
||||
close(resetChan)
|
||||
i.resetChan = nil
|
||||
i.writeLock.Unlock()
|
||||
|
||||
i.setTimeoutLock.Lock()
|
||||
close(i.setTimeoutChan)
|
||||
i.setTimeoutChan = nil
|
||||
i.setTimeoutLock.Unlock()
|
||||
|
||||
break Loop
|
||||
}
|
||||
}
|
||||
|
||||
// Drain resetChan
|
||||
for _ = range resetChan {
|
||||
}
|
||||
}
|
||||
|
||||
func (i *idleAwareFramer) WriteFrame(frame spdy.Frame) error {
|
||||
i.writeLock.Lock()
|
||||
defer i.writeLock.Unlock()
|
||||
if i.resetChan == nil {
|
||||
return io.EOF
|
||||
}
|
||||
err := i.f.WriteFrame(frame)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
i.resetChan <- struct{}{}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *idleAwareFramer) ReadFrame() (spdy.Frame, error) {
|
||||
frame, err := i.f.ReadFrame()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// resetChan should never be closed since it is only closed
|
||||
// when the connection has closed its closeChan. This closure
|
||||
// only occurs after all Reads have finished
|
||||
// TODO (dmcgowan): refactor relationship into connection
|
||||
i.resetChan <- struct{}{}
|
||||
|
||||
return frame, nil
|
||||
}
|
||||
|
||||
func (i *idleAwareFramer) setIdleTimeout(timeout time.Duration) {
|
||||
i.setTimeoutLock.Lock()
|
||||
defer i.setTimeoutLock.Unlock()
|
||||
|
||||
if i.setTimeoutChan == nil {
|
||||
return
|
||||
}
|
||||
|
||||
i.setTimeoutChan <- timeout
|
||||
}
|
||||
|
||||
type Connection struct {
|
||||
conn net.Conn
|
||||
framer *idleAwareFramer
|
||||
|
||||
closeChan chan bool
|
||||
goneAway bool
|
||||
lastStreamChan chan<- *Stream
|
||||
goAwayTimeout time.Duration
|
||||
closeTimeout time.Duration
|
||||
|
||||
streamLock *sync.RWMutex
|
||||
streamCond *sync.Cond
|
||||
streams map[spdy.StreamId]*Stream
|
||||
|
||||
nextIdLock sync.Mutex
|
||||
receiveIdLock sync.Mutex
|
||||
nextStreamId spdy.StreamId
|
||||
receivedStreamId spdy.StreamId
|
||||
|
||||
pingIdLock sync.Mutex
|
||||
pingId uint32
|
||||
pingChans map[uint32]chan error
|
||||
|
||||
shutdownLock sync.Mutex
|
||||
shutdownChan chan error
|
||||
hasShutdown bool
|
||||
|
||||
// for testing https://github.com/docker/spdystream/pull/56
|
||||
dataFrameHandler func(*spdy.DataFrame) error
|
||||
}
|
||||
|
||||
// NewConnection creates a new spdy connection from an existing
|
||||
// network connection.
|
||||
func NewConnection(conn net.Conn, server bool) (*Connection, error) {
|
||||
framer, framerErr := spdy.NewFramer(conn, conn)
|
||||
if framerErr != nil {
|
||||
return nil, framerErr
|
||||
}
|
||||
idleAwareFramer := newIdleAwareFramer(framer)
|
||||
var sid spdy.StreamId
|
||||
var rid spdy.StreamId
|
||||
var pid uint32
|
||||
if server {
|
||||
sid = 2
|
||||
rid = 1
|
||||
pid = 2
|
||||
} else {
|
||||
sid = 1
|
||||
rid = 2
|
||||
pid = 1
|
||||
}
|
||||
|
||||
streamLock := new(sync.RWMutex)
|
||||
streamCond := sync.NewCond(streamLock)
|
||||
|
||||
session := &Connection{
|
||||
conn: conn,
|
||||
framer: idleAwareFramer,
|
||||
|
||||
closeChan: make(chan bool),
|
||||
goAwayTimeout: time.Duration(0),
|
||||
closeTimeout: time.Duration(0),
|
||||
|
||||
streamLock: streamLock,
|
||||
streamCond: streamCond,
|
||||
streams: make(map[spdy.StreamId]*Stream),
|
||||
nextStreamId: sid,
|
||||
receivedStreamId: rid,
|
||||
|
||||
pingId: pid,
|
||||
pingChans: make(map[uint32]chan error),
|
||||
|
||||
shutdownChan: make(chan error),
|
||||
}
|
||||
session.dataFrameHandler = session.handleDataFrame
|
||||
idleAwareFramer.conn = session
|
||||
go idleAwareFramer.monitor()
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// Ping sends a ping frame across the connection and
|
||||
// returns the response time
|
||||
func (s *Connection) Ping() (time.Duration, error) {
|
||||
pid := s.pingId
|
||||
s.pingIdLock.Lock()
|
||||
if s.pingId > 0x7ffffffe {
|
||||
s.pingId = s.pingId - 0x7ffffffe
|
||||
} else {
|
||||
s.pingId = s.pingId + 2
|
||||
}
|
||||
s.pingIdLock.Unlock()
|
||||
pingChan := make(chan error)
|
||||
s.pingChans[pid] = pingChan
|
||||
defer delete(s.pingChans, pid)
|
||||
|
||||
frame := &spdy.PingFrame{Id: pid}
|
||||
startTime := time.Now()
|
||||
writeErr := s.framer.WriteFrame(frame)
|
||||
if writeErr != nil {
|
||||
return time.Duration(0), writeErr
|
||||
}
|
||||
select {
|
||||
case <-s.closeChan:
|
||||
return time.Duration(0), errors.New("connection closed")
|
||||
case err, ok := <-pingChan:
|
||||
if ok && err != nil {
|
||||
return time.Duration(0), err
|
||||
}
|
||||
break
|
||||
}
|
||||
return time.Now().Sub(startTime), nil
|
||||
}
|
||||
|
||||
// Serve handles frames sent from the server, including reply frames
|
||||
// which are needed to fully initiate connections. Both clients and servers
|
||||
// should call Serve in a separate goroutine before creating streams.
|
||||
func (s *Connection) Serve(newHandler StreamHandler) {
|
||||
// use a WaitGroup to wait for all frames to be drained after receiving
|
||||
// go-away.
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Parition queues to ensure stream frames are handled
|
||||
// by the same worker, ensuring order is maintained
|
||||
frameQueues := make([]*PriorityFrameQueue, FRAME_WORKERS)
|
||||
for i := 0; i < FRAME_WORKERS; i++ {
|
||||
frameQueues[i] = NewPriorityFrameQueue(QUEUE_SIZE)
|
||||
|
||||
// Ensure frame queue is drained when connection is closed
|
||||
go func(frameQueue *PriorityFrameQueue) {
|
||||
<-s.closeChan
|
||||
frameQueue.Drain()
|
||||
}(frameQueues[i])
|
||||
|
||||
wg.Add(1)
|
||||
go func(frameQueue *PriorityFrameQueue) {
|
||||
// let the WaitGroup know this worker is done
|
||||
defer wg.Done()
|
||||
|
||||
s.frameHandler(frameQueue, newHandler)
|
||||
}(frameQueues[i])
|
||||
}
|
||||
|
||||
var (
|
||||
partitionRoundRobin int
|
||||
goAwayFrame *spdy.GoAwayFrame
|
||||
)
|
||||
Loop:
|
||||
for {
|
||||
readFrame, err := s.framer.ReadFrame()
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
fmt.Errorf("frame read error: %s", err)
|
||||
} else {
|
||||
debugMessage("(%p) EOF received", s)
|
||||
}
|
||||
break
|
||||
}
|
||||
var priority uint8
|
||||
var partition int
|
||||
switch frame := readFrame.(type) {
|
||||
case *spdy.SynStreamFrame:
|
||||
if s.checkStreamFrame(frame) {
|
||||
priority = frame.Priority
|
||||
partition = int(frame.StreamId % FRAME_WORKERS)
|
||||
debugMessage("(%p) Add stream frame: %d ", s, frame.StreamId)
|
||||
s.addStreamFrame(frame)
|
||||
} else {
|
||||
debugMessage("(%p) Rejected stream frame: %d ", s, frame.StreamId)
|
||||
continue
|
||||
}
|
||||
case *spdy.SynReplyFrame:
|
||||
priority = s.getStreamPriority(frame.StreamId)
|
||||
partition = int(frame.StreamId % FRAME_WORKERS)
|
||||
case *spdy.DataFrame:
|
||||
priority = s.getStreamPriority(frame.StreamId)
|
||||
partition = int(frame.StreamId % FRAME_WORKERS)
|
||||
case *spdy.RstStreamFrame:
|
||||
priority = s.getStreamPriority(frame.StreamId)
|
||||
partition = int(frame.StreamId % FRAME_WORKERS)
|
||||
case *spdy.HeadersFrame:
|
||||
priority = s.getStreamPriority(frame.StreamId)
|
||||
partition = int(frame.StreamId % FRAME_WORKERS)
|
||||
case *spdy.PingFrame:
|
||||
priority = 0
|
||||
partition = partitionRoundRobin
|
||||
partitionRoundRobin = (partitionRoundRobin + 1) % FRAME_WORKERS
|
||||
case *spdy.GoAwayFrame:
|
||||
// hold on to the go away frame and exit the loop
|
||||
goAwayFrame = frame
|
||||
break Loop
|
||||
default:
|
||||
priority = 7
|
||||
partition = partitionRoundRobin
|
||||
partitionRoundRobin = (partitionRoundRobin + 1) % FRAME_WORKERS
|
||||
}
|
||||
frameQueues[partition].Push(readFrame, priority)
|
||||
}
|
||||
close(s.closeChan)
|
||||
|
||||
// wait for all frame handler workers to indicate they've drained their queues
|
||||
// before handling the go away frame
|
||||
wg.Wait()
|
||||
|
||||
if goAwayFrame != nil {
|
||||
s.handleGoAwayFrame(goAwayFrame)
|
||||
}
|
||||
|
||||
// now it's safe to close remote channels and empty s.streams
|
||||
s.streamCond.L.Lock()
|
||||
// notify streams that they're now closed, which will
|
||||
// unblock any stream Read() calls
|
||||
for _, stream := range s.streams {
|
||||
stream.closeRemoteChannels()
|
||||
}
|
||||
s.streams = make(map[spdy.StreamId]*Stream)
|
||||
s.streamCond.Broadcast()
|
||||
s.streamCond.L.Unlock()
|
||||
}
|
||||
|
||||
func (s *Connection) frameHandler(frameQueue *PriorityFrameQueue, newHandler StreamHandler) {
|
||||
for {
|
||||
popFrame := frameQueue.Pop()
|
||||
if popFrame == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var frameErr error
|
||||
switch frame := popFrame.(type) {
|
||||
case *spdy.SynStreamFrame:
|
||||
frameErr = s.handleStreamFrame(frame, newHandler)
|
||||
case *spdy.SynReplyFrame:
|
||||
frameErr = s.handleReplyFrame(frame)
|
||||
case *spdy.DataFrame:
|
||||
frameErr = s.dataFrameHandler(frame)
|
||||
case *spdy.RstStreamFrame:
|
||||
frameErr = s.handleResetFrame(frame)
|
||||
case *spdy.HeadersFrame:
|
||||
frameErr = s.handleHeaderFrame(frame)
|
||||
case *spdy.PingFrame:
|
||||
frameErr = s.handlePingFrame(frame)
|
||||
case *spdy.GoAwayFrame:
|
||||
frameErr = s.handleGoAwayFrame(frame)
|
||||
default:
|
||||
frameErr = fmt.Errorf("unhandled frame type: %T", frame)
|
||||
}
|
||||
|
||||
if frameErr != nil {
|
||||
fmt.Errorf("frame handling error: %s", frameErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Connection) getStreamPriority(streamId spdy.StreamId) uint8 {
|
||||
stream, streamOk := s.getStream(streamId)
|
||||
if !streamOk {
|
||||
return 7
|
||||
}
|
||||
return stream.priority
|
||||
}
|
||||
|
||||
func (s *Connection) addStreamFrame(frame *spdy.SynStreamFrame) {
|
||||
var parent *Stream
|
||||
if frame.AssociatedToStreamId != spdy.StreamId(0) {
|
||||
parent, _ = s.getStream(frame.AssociatedToStreamId)
|
||||
}
|
||||
|
||||
stream := &Stream{
|
||||
streamId: frame.StreamId,
|
||||
parent: parent,
|
||||
conn: s,
|
||||
startChan: make(chan error),
|
||||
headers: frame.Headers,
|
||||
finished: (frame.CFHeader.Flags & spdy.ControlFlagUnidirectional) != 0x00,
|
||||
replyCond: sync.NewCond(new(sync.Mutex)),
|
||||
dataChan: make(chan []byte),
|
||||
headerChan: make(chan http.Header),
|
||||
closeChan: make(chan bool),
|
||||
}
|
||||
if frame.CFHeader.Flags&spdy.ControlFlagFin != 0x00 {
|
||||
stream.closeRemoteChannels()
|
||||
}
|
||||
|
||||
s.addStream(stream)
|
||||
}
|
||||
|
||||
// checkStreamFrame checks to see if a stream frame is allowed.
|
||||
// If the stream is invalid, then a reset frame with protocol error
|
||||
// will be returned.
|
||||
func (s *Connection) checkStreamFrame(frame *spdy.SynStreamFrame) bool {
|
||||
s.receiveIdLock.Lock()
|
||||
defer s.receiveIdLock.Unlock()
|
||||
if s.goneAway {
|
||||
return false
|
||||
}
|
||||
validationErr := s.validateStreamId(frame.StreamId)
|
||||
if validationErr != nil {
|
||||
go func() {
|
||||
resetErr := s.sendResetFrame(spdy.ProtocolError, frame.StreamId)
|
||||
if resetErr != nil {
|
||||
fmt.Errorf("reset error: %s", resetErr)
|
||||
}
|
||||
}()
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Connection) handleStreamFrame(frame *spdy.SynStreamFrame, newHandler StreamHandler) error {
|
||||
stream, ok := s.getStream(frame.StreamId)
|
||||
if !ok {
|
||||
return fmt.Errorf("Missing stream: %d", frame.StreamId)
|
||||
}
|
||||
|
||||
newHandler(stream)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) handleReplyFrame(frame *spdy.SynReplyFrame) error {
|
||||
debugMessage("(%p) Reply frame received for %d", s, frame.StreamId)
|
||||
stream, streamOk := s.getStream(frame.StreamId)
|
||||
if !streamOk {
|
||||
debugMessage("Reply frame gone away for %d", frame.StreamId)
|
||||
// Stream has already gone away
|
||||
return nil
|
||||
}
|
||||
if stream.replied {
|
||||
// Stream has already received reply
|
||||
return nil
|
||||
}
|
||||
stream.replied = true
|
||||
|
||||
// TODO Check for error
|
||||
if (frame.CFHeader.Flags & spdy.ControlFlagFin) != 0x00 {
|
||||
s.remoteStreamFinish(stream)
|
||||
}
|
||||
|
||||
close(stream.startChan)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) handleResetFrame(frame *spdy.RstStreamFrame) error {
|
||||
stream, streamOk := s.getStream(frame.StreamId)
|
||||
if !streamOk {
|
||||
// Stream has already been removed
|
||||
return nil
|
||||
}
|
||||
s.removeStream(stream)
|
||||
stream.closeRemoteChannels()
|
||||
|
||||
if !stream.replied {
|
||||
stream.replied = true
|
||||
stream.startChan <- ErrReset
|
||||
close(stream.startChan)
|
||||
}
|
||||
|
||||
stream.finishLock.Lock()
|
||||
stream.finished = true
|
||||
stream.finishLock.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) handleHeaderFrame(frame *spdy.HeadersFrame) error {
|
||||
stream, streamOk := s.getStream(frame.StreamId)
|
||||
if !streamOk {
|
||||
// Stream has already gone away
|
||||
return nil
|
||||
}
|
||||
if !stream.replied {
|
||||
// No reply received...Protocol error?
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO limit headers while not blocking (use buffered chan or goroutine?)
|
||||
select {
|
||||
case <-stream.closeChan:
|
||||
return nil
|
||||
case stream.headerChan <- frame.Headers:
|
||||
}
|
||||
|
||||
if (frame.CFHeader.Flags & spdy.ControlFlagFin) != 0x00 {
|
||||
s.remoteStreamFinish(stream)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) handleDataFrame(frame *spdy.DataFrame) error {
|
||||
debugMessage("(%p) Data frame received for %d", s, frame.StreamId)
|
||||
stream, streamOk := s.getStream(frame.StreamId)
|
||||
if !streamOk {
|
||||
debugMessage("(%p) Data frame gone away for %d", s, frame.StreamId)
|
||||
// Stream has already gone away
|
||||
return nil
|
||||
}
|
||||
if !stream.replied {
|
||||
debugMessage("(%p) Data frame not replied %d", s, frame.StreamId)
|
||||
// No reply received...Protocol error?
|
||||
return nil
|
||||
}
|
||||
|
||||
debugMessage("(%p) (%d) Data frame handling", stream, stream.streamId)
|
||||
if len(frame.Data) > 0 {
|
||||
stream.dataLock.RLock()
|
||||
select {
|
||||
case <-stream.closeChan:
|
||||
debugMessage("(%p) (%d) Data frame not sent (stream shut down)", stream, stream.streamId)
|
||||
case stream.dataChan <- frame.Data:
|
||||
debugMessage("(%p) (%d) Data frame sent", stream, stream.streamId)
|
||||
}
|
||||
stream.dataLock.RUnlock()
|
||||
}
|
||||
if (frame.Flags & spdy.DataFlagFin) != 0x00 {
|
||||
s.remoteStreamFinish(stream)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) handlePingFrame(frame *spdy.PingFrame) error {
|
||||
if s.pingId&0x01 != frame.Id&0x01 {
|
||||
return s.framer.WriteFrame(frame)
|
||||
}
|
||||
pingChan, pingOk := s.pingChans[frame.Id]
|
||||
if pingOk {
|
||||
close(pingChan)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) handleGoAwayFrame(frame *spdy.GoAwayFrame) error {
|
||||
debugMessage("(%p) Go away received", s)
|
||||
s.receiveIdLock.Lock()
|
||||
if s.goneAway {
|
||||
s.receiveIdLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
s.goneAway = true
|
||||
s.receiveIdLock.Unlock()
|
||||
|
||||
if s.lastStreamChan != nil {
|
||||
stream, _ := s.getStream(frame.LastGoodStreamId)
|
||||
go func() {
|
||||
s.lastStreamChan <- stream
|
||||
}()
|
||||
}
|
||||
|
||||
// Do not block frame handler waiting for closure
|
||||
go s.shutdown(s.goAwayTimeout)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) remoteStreamFinish(stream *Stream) {
|
||||
stream.closeRemoteChannels()
|
||||
|
||||
stream.finishLock.Lock()
|
||||
if stream.finished {
|
||||
// Stream is fully closed, cleanup
|
||||
s.removeStream(stream)
|
||||
}
|
||||
stream.finishLock.Unlock()
|
||||
}
|
||||
|
||||
// CreateStream creates a new spdy stream using the parameters for
|
||||
// creating the stream frame. The stream frame will be sent upon
|
||||
// calling this function, however this function does not wait for
|
||||
// the reply frame. If waiting for the reply is desired, use
|
||||
// the stream Wait or WaitTimeout function on the stream returned
|
||||
// by this function.
|
||||
func (s *Connection) CreateStream(headers http.Header, parent *Stream, fin bool) (*Stream, error) {
|
||||
// MUST synchronize stream creation (all the way to writing the frame)
|
||||
// as stream IDs **MUST** increase monotonically.
|
||||
s.nextIdLock.Lock()
|
||||
defer s.nextIdLock.Unlock()
|
||||
|
||||
streamId := s.getNextStreamId()
|
||||
if streamId == 0 {
|
||||
return nil, fmt.Errorf("Unable to get new stream id")
|
||||
}
|
||||
|
||||
stream := &Stream{
|
||||
streamId: streamId,
|
||||
parent: parent,
|
||||
conn: s,
|
||||
startChan: make(chan error),
|
||||
headers: headers,
|
||||
dataChan: make(chan []byte),
|
||||
headerChan: make(chan http.Header),
|
||||
closeChan: make(chan bool),
|
||||
}
|
||||
|
||||
debugMessage("(%p) (%p) Create stream", s, stream)
|
||||
|
||||
s.addStream(stream)
|
||||
|
||||
return stream, s.sendStream(stream, fin)
|
||||
}
|
||||
|
||||
func (s *Connection) shutdown(closeTimeout time.Duration) {
|
||||
// TODO Ensure this isn't called multiple times
|
||||
s.shutdownLock.Lock()
|
||||
if s.hasShutdown {
|
||||
s.shutdownLock.Unlock()
|
||||
return
|
||||
}
|
||||
s.hasShutdown = true
|
||||
s.shutdownLock.Unlock()
|
||||
|
||||
var timeout <-chan time.Time
|
||||
if closeTimeout > time.Duration(0) {
|
||||
timeout = time.After(closeTimeout)
|
||||
}
|
||||
streamsClosed := make(chan bool)
|
||||
|
||||
go func() {
|
||||
s.streamCond.L.Lock()
|
||||
for len(s.streams) > 0 {
|
||||
debugMessage("Streams opened: %d, %#v", len(s.streams), s.streams)
|
||||
s.streamCond.Wait()
|
||||
}
|
||||
s.streamCond.L.Unlock()
|
||||
close(streamsClosed)
|
||||
}()
|
||||
|
||||
var err error
|
||||
select {
|
||||
case <-streamsClosed:
|
||||
// No active streams, close should be safe
|
||||
err = s.conn.Close()
|
||||
case <-timeout:
|
||||
// Force ungraceful close
|
||||
err = s.conn.Close()
|
||||
// Wait for cleanup to clear active streams
|
||||
<-streamsClosed
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
duration := 10 * time.Minute
|
||||
time.AfterFunc(duration, func() {
|
||||
select {
|
||||
case err, ok := <-s.shutdownChan:
|
||||
if ok {
|
||||
fmt.Errorf("Unhandled close error after %s: %s", duration, err)
|
||||
}
|
||||
default:
|
||||
}
|
||||
})
|
||||
s.shutdownChan <- err
|
||||
}
|
||||
close(s.shutdownChan)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Closes spdy connection by sending GoAway frame and initiating shutdown
|
||||
func (s *Connection) Close() error {
|
||||
s.receiveIdLock.Lock()
|
||||
if s.goneAway {
|
||||
s.receiveIdLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
s.goneAway = true
|
||||
s.receiveIdLock.Unlock()
|
||||
|
||||
var lastStreamId spdy.StreamId
|
||||
if s.receivedStreamId > 2 {
|
||||
lastStreamId = s.receivedStreamId - 2
|
||||
}
|
||||
|
||||
goAwayFrame := &spdy.GoAwayFrame{
|
||||
LastGoodStreamId: lastStreamId,
|
||||
Status: spdy.GoAwayOK,
|
||||
}
|
||||
|
||||
err := s.framer.WriteFrame(goAwayFrame)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go s.shutdown(s.closeTimeout)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseWait closes the connection and waits for shutdown
|
||||
// to finish. Note the underlying network Connection
|
||||
// is not closed until the end of shutdown.
|
||||
func (s *Connection) CloseWait() error {
|
||||
closeErr := s.Close()
|
||||
if closeErr != nil {
|
||||
return closeErr
|
||||
}
|
||||
shutdownErr, ok := <-s.shutdownChan
|
||||
if ok {
|
||||
return shutdownErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Wait waits for the connection to finish shutdown or for
|
||||
// the wait timeout duration to expire. This needs to be
|
||||
// called either after Close has been called or the GOAWAYFRAME
|
||||
// has been received. If the wait timeout is 0, this function
|
||||
// will block until shutdown finishes. If wait is never called
|
||||
// and a shutdown error occurs, that error will be logged as an
|
||||
// unhandled error.
|
||||
func (s *Connection) Wait(waitTimeout time.Duration) error {
|
||||
var timeout <-chan time.Time
|
||||
if waitTimeout > time.Duration(0) {
|
||||
timeout = time.After(waitTimeout)
|
||||
}
|
||||
|
||||
select {
|
||||
case err, ok := <-s.shutdownChan:
|
||||
if ok {
|
||||
return err
|
||||
}
|
||||
case <-timeout:
|
||||
return ErrTimeout
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NotifyClose registers a channel to be called when the remote
|
||||
// peer inidicates connection closure. The last stream to be
|
||||
// received by the remote will be sent on the channel. The notify
|
||||
// timeout will determine the duration between go away received
|
||||
// and the connection being closed.
|
||||
func (s *Connection) NotifyClose(c chan<- *Stream, timeout time.Duration) {
|
||||
s.goAwayTimeout = timeout
|
||||
s.lastStreamChan = c
|
||||
}
|
||||
|
||||
// SetCloseTimeout sets the amount of time close will wait for
|
||||
// streams to finish before terminating the underlying network
|
||||
// connection. Setting the timeout to 0 will cause close to
|
||||
// wait forever, which is the default.
|
||||
func (s *Connection) SetCloseTimeout(timeout time.Duration) {
|
||||
s.closeTimeout = timeout
|
||||
}
|
||||
|
||||
// SetIdleTimeout sets the amount of time the connection may sit idle before
|
||||
// it is forcefully terminated.
|
||||
func (s *Connection) SetIdleTimeout(timeout time.Duration) {
|
||||
s.framer.setIdleTimeout(timeout)
|
||||
}
|
||||
|
||||
func (s *Connection) sendHeaders(headers http.Header, stream *Stream, fin bool) error {
|
||||
var flags spdy.ControlFlags
|
||||
if fin {
|
||||
flags = spdy.ControlFlagFin
|
||||
}
|
||||
|
||||
headerFrame := &spdy.HeadersFrame{
|
||||
StreamId: stream.streamId,
|
||||
Headers: headers,
|
||||
CFHeader: spdy.ControlFrameHeader{Flags: flags},
|
||||
}
|
||||
|
||||
return s.framer.WriteFrame(headerFrame)
|
||||
}
|
||||
|
||||
func (s *Connection) sendReply(headers http.Header, stream *Stream, fin bool) error {
|
||||
var flags spdy.ControlFlags
|
||||
if fin {
|
||||
flags = spdy.ControlFlagFin
|
||||
}
|
||||
|
||||
replyFrame := &spdy.SynReplyFrame{
|
||||
StreamId: stream.streamId,
|
||||
Headers: headers,
|
||||
CFHeader: spdy.ControlFrameHeader{Flags: flags},
|
||||
}
|
||||
|
||||
return s.framer.WriteFrame(replyFrame)
|
||||
}
|
||||
|
||||
func (s *Connection) sendResetFrame(status spdy.RstStreamStatus, streamId spdy.StreamId) error {
|
||||
resetFrame := &spdy.RstStreamFrame{
|
||||
StreamId: streamId,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
return s.framer.WriteFrame(resetFrame)
|
||||
}
|
||||
|
||||
func (s *Connection) sendReset(status spdy.RstStreamStatus, stream *Stream) error {
|
||||
return s.sendResetFrame(status, stream.streamId)
|
||||
}
|
||||
|
||||
func (s *Connection) sendStream(stream *Stream, fin bool) error {
|
||||
var flags spdy.ControlFlags
|
||||
if fin {
|
||||
flags = spdy.ControlFlagFin
|
||||
stream.finished = true
|
||||
}
|
||||
|
||||
var parentId spdy.StreamId
|
||||
if stream.parent != nil {
|
||||
parentId = stream.parent.streamId
|
||||
}
|
||||
|
||||
streamFrame := &spdy.SynStreamFrame{
|
||||
StreamId: spdy.StreamId(stream.streamId),
|
||||
AssociatedToStreamId: spdy.StreamId(parentId),
|
||||
Headers: stream.headers,
|
||||
CFHeader: spdy.ControlFrameHeader{Flags: flags},
|
||||
}
|
||||
|
||||
return s.framer.WriteFrame(streamFrame)
|
||||
}
|
||||
|
||||
// getNextStreamId returns the next sequential id
|
||||
// every call should produce a unique value or an error
|
||||
func (s *Connection) getNextStreamId() spdy.StreamId {
|
||||
sid := s.nextStreamId
|
||||
if sid > 0x7fffffff {
|
||||
return 0
|
||||
}
|
||||
s.nextStreamId = s.nextStreamId + 2
|
||||
return sid
|
||||
}
|
||||
|
||||
// PeekNextStreamId returns the next sequential id and keeps the next id untouched
|
||||
func (s *Connection) PeekNextStreamId() spdy.StreamId {
|
||||
sid := s.nextStreamId
|
||||
return sid
|
||||
}
|
||||
|
||||
func (s *Connection) validateStreamId(rid spdy.StreamId) error {
|
||||
if rid > 0x7fffffff || rid < s.receivedStreamId {
|
||||
return ErrInvalidStreamId
|
||||
}
|
||||
s.receivedStreamId = rid + 2
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Connection) addStream(stream *Stream) {
|
||||
s.streamCond.L.Lock()
|
||||
s.streams[stream.streamId] = stream
|
||||
debugMessage("(%p) (%p) Stream added, broadcasting: %d", s, stream, stream.streamId)
|
||||
s.streamCond.Broadcast()
|
||||
s.streamCond.L.Unlock()
|
||||
}
|
||||
|
||||
func (s *Connection) removeStream(stream *Stream) {
|
||||
s.streamCond.L.Lock()
|
||||
delete(s.streams, stream.streamId)
|
||||
debugMessage("(%p) (%p) Stream removed, broadcasting: %d", s, stream, stream.streamId)
|
||||
s.streamCond.Broadcast()
|
||||
s.streamCond.L.Unlock()
|
||||
}
|
||||
|
||||
func (s *Connection) getStream(streamId spdy.StreamId) (stream *Stream, ok bool) {
|
||||
s.streamLock.RLock()
|
||||
stream, ok = s.streams[streamId]
|
||||
s.streamLock.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// FindStream looks up the given stream id and either waits for the
|
||||
// stream to be found or returns nil if the stream id is no longer
|
||||
// valid.
|
||||
func (s *Connection) FindStream(streamId uint32) *Stream {
|
||||
var stream *Stream
|
||||
var ok bool
|
||||
s.streamCond.L.Lock()
|
||||
stream, ok = s.streams[spdy.StreamId(streamId)]
|
||||
debugMessage("(%p) Found stream %d? %t", s, spdy.StreamId(streamId), ok)
|
||||
for !ok && streamId >= uint32(s.receivedStreamId) {
|
||||
s.streamCond.Wait()
|
||||
stream, ok = s.streams[spdy.StreamId(streamId)]
|
||||
}
|
||||
s.streamCond.L.Unlock()
|
||||
return stream
|
||||
}
|
||||
|
||||
func (s *Connection) CloseChan() <-chan bool {
|
||||
return s.closeChan
|
||||
}
|
||||
38
vendor/github.com/docker/spdystream/handlers.go
generated
vendored
Normal file
38
vendor/github.com/docker/spdystream/handlers.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
package spdystream
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// MirrorStreamHandler mirrors all streams.
|
||||
func MirrorStreamHandler(stream *Stream) {
|
||||
replyErr := stream.SendReply(http.Header{}, false)
|
||||
if replyErr != nil {
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
io.Copy(stream, stream)
|
||||
stream.Close()
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
header, receiveErr := stream.ReceiveHeader()
|
||||
if receiveErr != nil {
|
||||
return
|
||||
}
|
||||
sendErr := stream.SendHeader(header, false)
|
||||
if sendErr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// NoopStreamHandler does nothing when stream connects, most
|
||||
// likely used with RejectAuthHandler which will not allow any
|
||||
// streams to make it to the stream handler.
|
||||
func NoOpStreamHandler(stream *Stream) {
|
||||
stream.SendReply(http.Header{}, false)
|
||||
}
|
||||
98
vendor/github.com/docker/spdystream/priority.go
generated
vendored
Normal file
98
vendor/github.com/docker/spdystream/priority.go
generated
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
package spdystream
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"sync"
|
||||
|
||||
"github.com/docker/spdystream/spdy"
|
||||
)
|
||||
|
||||
type prioritizedFrame struct {
|
||||
frame spdy.Frame
|
||||
priority uint8
|
||||
insertId uint64
|
||||
}
|
||||
|
||||
type frameQueue []*prioritizedFrame
|
||||
|
||||
func (fq frameQueue) Len() int {
|
||||
return len(fq)
|
||||
}
|
||||
|
||||
func (fq frameQueue) Less(i, j int) bool {
|
||||
if fq[i].priority == fq[j].priority {
|
||||
return fq[i].insertId < fq[j].insertId
|
||||
}
|
||||
return fq[i].priority < fq[j].priority
|
||||
}
|
||||
|
||||
func (fq frameQueue) Swap(i, j int) {
|
||||
fq[i], fq[j] = fq[j], fq[i]
|
||||
}
|
||||
|
||||
func (fq *frameQueue) Push(x interface{}) {
|
||||
*fq = append(*fq, x.(*prioritizedFrame))
|
||||
}
|
||||
|
||||
func (fq *frameQueue) Pop() interface{} {
|
||||
old := *fq
|
||||
n := len(old)
|
||||
*fq = old[0 : n-1]
|
||||
return old[n-1]
|
||||
}
|
||||
|
||||
type PriorityFrameQueue struct {
|
||||
queue *frameQueue
|
||||
c *sync.Cond
|
||||
size int
|
||||
nextInsertId uint64
|
||||
drain bool
|
||||
}
|
||||
|
||||
func NewPriorityFrameQueue(size int) *PriorityFrameQueue {
|
||||
queue := make(frameQueue, 0, size)
|
||||
heap.Init(&queue)
|
||||
|
||||
return &PriorityFrameQueue{
|
||||
queue: &queue,
|
||||
size: size,
|
||||
c: sync.NewCond(&sync.Mutex{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (q *PriorityFrameQueue) Push(frame spdy.Frame, priority uint8) {
|
||||
q.c.L.Lock()
|
||||
defer q.c.L.Unlock()
|
||||
for q.queue.Len() >= q.size {
|
||||
q.c.Wait()
|
||||
}
|
||||
pFrame := &prioritizedFrame{
|
||||
frame: frame,
|
||||
priority: priority,
|
||||
insertId: q.nextInsertId,
|
||||
}
|
||||
q.nextInsertId = q.nextInsertId + 1
|
||||
heap.Push(q.queue, pFrame)
|
||||
q.c.Signal()
|
||||
}
|
||||
|
||||
func (q *PriorityFrameQueue) Pop() spdy.Frame {
|
||||
q.c.L.Lock()
|
||||
defer q.c.L.Unlock()
|
||||
for q.queue.Len() == 0 {
|
||||
if q.drain {
|
||||
return nil
|
||||
}
|
||||
q.c.Wait()
|
||||
}
|
||||
frame := heap.Pop(q.queue).(*prioritizedFrame).frame
|
||||
q.c.Signal()
|
||||
return frame
|
||||
}
|
||||
|
||||
func (q *PriorityFrameQueue) Drain() {
|
||||
q.c.L.Lock()
|
||||
defer q.c.L.Unlock()
|
||||
q.drain = true
|
||||
q.c.Broadcast()
|
||||
}
|
||||
187
vendor/github.com/docker/spdystream/spdy/dictionary.go
generated
vendored
Normal file
187
vendor/github.com/docker/spdystream/spdy/dictionary.go
generated
vendored
Normal file
@@ -0,0 +1,187 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package spdy
|
||||
|
||||
// headerDictionary is the dictionary sent to the zlib compressor/decompressor.
|
||||
var headerDictionary = []byte{
|
||||
0x00, 0x00, 0x00, 0x07, 0x6f, 0x70, 0x74, 0x69,
|
||||
0x6f, 0x6e, 0x73, 0x00, 0x00, 0x00, 0x04, 0x68,
|
||||
0x65, 0x61, 0x64, 0x00, 0x00, 0x00, 0x04, 0x70,
|
||||
0x6f, 0x73, 0x74, 0x00, 0x00, 0x00, 0x03, 0x70,
|
||||
0x75, 0x74, 0x00, 0x00, 0x00, 0x06, 0x64, 0x65,
|
||||
0x6c, 0x65, 0x74, 0x65, 0x00, 0x00, 0x00, 0x05,
|
||||
0x74, 0x72, 0x61, 0x63, 0x65, 0x00, 0x00, 0x00,
|
||||
0x06, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x00,
|
||||
0x00, 0x00, 0x0e, 0x61, 0x63, 0x63, 0x65, 0x70,
|
||||
0x74, 0x2d, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65,
|
||||
0x74, 0x00, 0x00, 0x00, 0x0f, 0x61, 0x63, 0x63,
|
||||
0x65, 0x70, 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f,
|
||||
0x64, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x0f,
|
||||
0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x6c,
|
||||
0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x00,
|
||||
0x00, 0x00, 0x0d, 0x61, 0x63, 0x63, 0x65, 0x70,
|
||||
0x74, 0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x73,
|
||||
0x00, 0x00, 0x00, 0x03, 0x61, 0x67, 0x65, 0x00,
|
||||
0x00, 0x00, 0x05, 0x61, 0x6c, 0x6c, 0x6f, 0x77,
|
||||
0x00, 0x00, 0x00, 0x0d, 0x61, 0x75, 0x74, 0x68,
|
||||
0x6f, 0x72, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f,
|
||||
0x6e, 0x00, 0x00, 0x00, 0x0d, 0x63, 0x61, 0x63,
|
||||
0x68, 0x65, 0x2d, 0x63, 0x6f, 0x6e, 0x74, 0x72,
|
||||
0x6f, 0x6c, 0x00, 0x00, 0x00, 0x0a, 0x63, 0x6f,
|
||||
0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
|
||||
0x00, 0x00, 0x00, 0x0c, 0x63, 0x6f, 0x6e, 0x74,
|
||||
0x65, 0x6e, 0x74, 0x2d, 0x62, 0x61, 0x73, 0x65,
|
||||
0x00, 0x00, 0x00, 0x10, 0x63, 0x6f, 0x6e, 0x74,
|
||||
0x65, 0x6e, 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f,
|
||||
0x64, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x10,
|
||||
0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
|
||||
0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65,
|
||||
0x00, 0x00, 0x00, 0x0e, 0x63, 0x6f, 0x6e, 0x74,
|
||||
0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67,
|
||||
0x74, 0x68, 0x00, 0x00, 0x00, 0x10, 0x63, 0x6f,
|
||||
0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x6f,
|
||||
0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00,
|
||||
0x00, 0x0b, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
|
||||
0x74, 0x2d, 0x6d, 0x64, 0x35, 0x00, 0x00, 0x00,
|
||||
0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
|
||||
0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x00,
|
||||
0x00, 0x0c, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
|
||||
0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x00, 0x00,
|
||||
0x00, 0x04, 0x64, 0x61, 0x74, 0x65, 0x00, 0x00,
|
||||
0x00, 0x04, 0x65, 0x74, 0x61, 0x67, 0x00, 0x00,
|
||||
0x00, 0x06, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74,
|
||||
0x00, 0x00, 0x00, 0x07, 0x65, 0x78, 0x70, 0x69,
|
||||
0x72, 0x65, 0x73, 0x00, 0x00, 0x00, 0x04, 0x66,
|
||||
0x72, 0x6f, 0x6d, 0x00, 0x00, 0x00, 0x04, 0x68,
|
||||
0x6f, 0x73, 0x74, 0x00, 0x00, 0x00, 0x08, 0x69,
|
||||
0x66, 0x2d, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x00,
|
||||
0x00, 0x00, 0x11, 0x69, 0x66, 0x2d, 0x6d, 0x6f,
|
||||
0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x2d, 0x73,
|
||||
0x69, 0x6e, 0x63, 0x65, 0x00, 0x00, 0x00, 0x0d,
|
||||
0x69, 0x66, 0x2d, 0x6e, 0x6f, 0x6e, 0x65, 0x2d,
|
||||
0x6d, 0x61, 0x74, 0x63, 0x68, 0x00, 0x00, 0x00,
|
||||
0x08, 0x69, 0x66, 0x2d, 0x72, 0x61, 0x6e, 0x67,
|
||||
0x65, 0x00, 0x00, 0x00, 0x13, 0x69, 0x66, 0x2d,
|
||||
0x75, 0x6e, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69,
|
||||
0x65, 0x64, 0x2d, 0x73, 0x69, 0x6e, 0x63, 0x65,
|
||||
0x00, 0x00, 0x00, 0x0d, 0x6c, 0x61, 0x73, 0x74,
|
||||
0x2d, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65,
|
||||
0x64, 0x00, 0x00, 0x00, 0x08, 0x6c, 0x6f, 0x63,
|
||||
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00,
|
||||
0x0c, 0x6d, 0x61, 0x78, 0x2d, 0x66, 0x6f, 0x72,
|
||||
0x77, 0x61, 0x72, 0x64, 0x73, 0x00, 0x00, 0x00,
|
||||
0x06, 0x70, 0x72, 0x61, 0x67, 0x6d, 0x61, 0x00,
|
||||
0x00, 0x00, 0x12, 0x70, 0x72, 0x6f, 0x78, 0x79,
|
||||
0x2d, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,
|
||||
0x69, 0x63, 0x61, 0x74, 0x65, 0x00, 0x00, 0x00,
|
||||
0x13, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2d, 0x61,
|
||||
0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x61,
|
||||
0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x05,
|
||||
0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x00, 0x00,
|
||||
0x07, 0x72, 0x65, 0x66, 0x65, 0x72, 0x65, 0x72,
|
||||
0x00, 0x00, 0x00, 0x0b, 0x72, 0x65, 0x74, 0x72,
|
||||
0x79, 0x2d, 0x61, 0x66, 0x74, 0x65, 0x72, 0x00,
|
||||
0x00, 0x00, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65,
|
||||
0x72, 0x00, 0x00, 0x00, 0x02, 0x74, 0x65, 0x00,
|
||||
0x00, 0x00, 0x07, 0x74, 0x72, 0x61, 0x69, 0x6c,
|
||||
0x65, 0x72, 0x00, 0x00, 0x00, 0x11, 0x74, 0x72,
|
||||
0x61, 0x6e, 0x73, 0x66, 0x65, 0x72, 0x2d, 0x65,
|
||||
0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x00,
|
||||
0x00, 0x00, 0x07, 0x75, 0x70, 0x67, 0x72, 0x61,
|
||||
0x64, 0x65, 0x00, 0x00, 0x00, 0x0a, 0x75, 0x73,
|
||||
0x65, 0x72, 0x2d, 0x61, 0x67, 0x65, 0x6e, 0x74,
|
||||
0x00, 0x00, 0x00, 0x04, 0x76, 0x61, 0x72, 0x79,
|
||||
0x00, 0x00, 0x00, 0x03, 0x76, 0x69, 0x61, 0x00,
|
||||
0x00, 0x00, 0x07, 0x77, 0x61, 0x72, 0x6e, 0x69,
|
||||
0x6e, 0x67, 0x00, 0x00, 0x00, 0x10, 0x77, 0x77,
|
||||
0x77, 0x2d, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e,
|
||||
0x74, 0x69, 0x63, 0x61, 0x74, 0x65, 0x00, 0x00,
|
||||
0x00, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
|
||||
0x00, 0x00, 0x00, 0x03, 0x67, 0x65, 0x74, 0x00,
|
||||
0x00, 0x00, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75,
|
||||
0x73, 0x00, 0x00, 0x00, 0x06, 0x32, 0x30, 0x30,
|
||||
0x20, 0x4f, 0x4b, 0x00, 0x00, 0x00, 0x07, 0x76,
|
||||
0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x00, 0x00,
|
||||
0x00, 0x08, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31,
|
||||
0x2e, 0x31, 0x00, 0x00, 0x00, 0x03, 0x75, 0x72,
|
||||
0x6c, 0x00, 0x00, 0x00, 0x06, 0x70, 0x75, 0x62,
|
||||
0x6c, 0x69, 0x63, 0x00, 0x00, 0x00, 0x0a, 0x73,
|
||||
0x65, 0x74, 0x2d, 0x63, 0x6f, 0x6f, 0x6b, 0x69,
|
||||
0x65, 0x00, 0x00, 0x00, 0x0a, 0x6b, 0x65, 0x65,
|
||||
0x70, 0x2d, 0x61, 0x6c, 0x69, 0x76, 0x65, 0x00,
|
||||
0x00, 0x00, 0x06, 0x6f, 0x72, 0x69, 0x67, 0x69,
|
||||
0x6e, 0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x32,
|
||||
0x30, 0x31, 0x32, 0x30, 0x32, 0x32, 0x30, 0x35,
|
||||
0x32, 0x30, 0x36, 0x33, 0x30, 0x30, 0x33, 0x30,
|
||||
0x32, 0x33, 0x30, 0x33, 0x33, 0x30, 0x34, 0x33,
|
||||
0x30, 0x35, 0x33, 0x30, 0x36, 0x33, 0x30, 0x37,
|
||||
0x34, 0x30, 0x32, 0x34, 0x30, 0x35, 0x34, 0x30,
|
||||
0x36, 0x34, 0x30, 0x37, 0x34, 0x30, 0x38, 0x34,
|
||||
0x30, 0x39, 0x34, 0x31, 0x30, 0x34, 0x31, 0x31,
|
||||
0x34, 0x31, 0x32, 0x34, 0x31, 0x33, 0x34, 0x31,
|
||||
0x34, 0x34, 0x31, 0x35, 0x34, 0x31, 0x36, 0x34,
|
||||
0x31, 0x37, 0x35, 0x30, 0x32, 0x35, 0x30, 0x34,
|
||||
0x35, 0x30, 0x35, 0x32, 0x30, 0x33, 0x20, 0x4e,
|
||||
0x6f, 0x6e, 0x2d, 0x41, 0x75, 0x74, 0x68, 0x6f,
|
||||
0x72, 0x69, 0x74, 0x61, 0x74, 0x69, 0x76, 0x65,
|
||||
0x20, 0x49, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61,
|
||||
0x74, 0x69, 0x6f, 0x6e, 0x32, 0x30, 0x34, 0x20,
|
||||
0x4e, 0x6f, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65,
|
||||
0x6e, 0x74, 0x33, 0x30, 0x31, 0x20, 0x4d, 0x6f,
|
||||
0x76, 0x65, 0x64, 0x20, 0x50, 0x65, 0x72, 0x6d,
|
||||
0x61, 0x6e, 0x65, 0x6e, 0x74, 0x6c, 0x79, 0x34,
|
||||
0x30, 0x30, 0x20, 0x42, 0x61, 0x64, 0x20, 0x52,
|
||||
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x34, 0x30,
|
||||
0x31, 0x20, 0x55, 0x6e, 0x61, 0x75, 0x74, 0x68,
|
||||
0x6f, 0x72, 0x69, 0x7a, 0x65, 0x64, 0x34, 0x30,
|
||||
0x33, 0x20, 0x46, 0x6f, 0x72, 0x62, 0x69, 0x64,
|
||||
0x64, 0x65, 0x6e, 0x34, 0x30, 0x34, 0x20, 0x4e,
|
||||
0x6f, 0x74, 0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64,
|
||||
0x35, 0x30, 0x30, 0x20, 0x49, 0x6e, 0x74, 0x65,
|
||||
0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f,
|
||||
0x72, 0x35, 0x30, 0x31, 0x20, 0x4e, 0x6f, 0x74,
|
||||
0x20, 0x49, 0x6d, 0x70, 0x6c, 0x65, 0x6d, 0x65,
|
||||
0x6e, 0x74, 0x65, 0x64, 0x35, 0x30, 0x33, 0x20,
|
||||
0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20,
|
||||
0x55, 0x6e, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61,
|
||||
0x62, 0x6c, 0x65, 0x4a, 0x61, 0x6e, 0x20, 0x46,
|
||||
0x65, 0x62, 0x20, 0x4d, 0x61, 0x72, 0x20, 0x41,
|
||||
0x70, 0x72, 0x20, 0x4d, 0x61, 0x79, 0x20, 0x4a,
|
||||
0x75, 0x6e, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x41,
|
||||
0x75, 0x67, 0x20, 0x53, 0x65, 0x70, 0x74, 0x20,
|
||||
0x4f, 0x63, 0x74, 0x20, 0x4e, 0x6f, 0x76, 0x20,
|
||||
0x44, 0x65, 0x63, 0x20, 0x30, 0x30, 0x3a, 0x30,
|
||||
0x30, 0x3a, 0x30, 0x30, 0x20, 0x4d, 0x6f, 0x6e,
|
||||
0x2c, 0x20, 0x54, 0x75, 0x65, 0x2c, 0x20, 0x57,
|
||||
0x65, 0x64, 0x2c, 0x20, 0x54, 0x68, 0x75, 0x2c,
|
||||
0x20, 0x46, 0x72, 0x69, 0x2c, 0x20, 0x53, 0x61,
|
||||
0x74, 0x2c, 0x20, 0x53, 0x75, 0x6e, 0x2c, 0x20,
|
||||
0x47, 0x4d, 0x54, 0x63, 0x68, 0x75, 0x6e, 0x6b,
|
||||
0x65, 0x64, 0x2c, 0x74, 0x65, 0x78, 0x74, 0x2f,
|
||||
0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x69, 0x6d, 0x61,
|
||||
0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x2c, 0x69,
|
||||
0x6d, 0x61, 0x67, 0x65, 0x2f, 0x6a, 0x70, 0x67,
|
||||
0x2c, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x67,
|
||||
0x69, 0x66, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69,
|
||||
0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78,
|
||||
0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69,
|
||||
0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78,
|
||||
0x68, 0x74, 0x6d, 0x6c, 0x2b, 0x78, 0x6d, 0x6c,
|
||||
0x2c, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c,
|
||||
0x61, 0x69, 0x6e, 0x2c, 0x74, 0x65, 0x78, 0x74,
|
||||
0x2f, 0x6a, 0x61, 0x76, 0x61, 0x73, 0x63, 0x72,
|
||||
0x69, 0x70, 0x74, 0x2c, 0x70, 0x75, 0x62, 0x6c,
|
||||
0x69, 0x63, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74,
|
||||
0x65, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65,
|
||||
0x3d, 0x67, 0x7a, 0x69, 0x70, 0x2c, 0x64, 0x65,
|
||||
0x66, 0x6c, 0x61, 0x74, 0x65, 0x2c, 0x73, 0x64,
|
||||
0x63, 0x68, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65,
|
||||
0x74, 0x3d, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x63,
|
||||
0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x69,
|
||||
0x73, 0x6f, 0x2d, 0x38, 0x38, 0x35, 0x39, 0x2d,
|
||||
0x31, 0x2c, 0x75, 0x74, 0x66, 0x2d, 0x2c, 0x2a,
|
||||
0x2c, 0x65, 0x6e, 0x71, 0x3d, 0x30, 0x2e,
|
||||
}
|
||||
348
vendor/github.com/docker/spdystream/spdy/read.go
generated
vendored
Normal file
348
vendor/github.com/docker/spdystream/spdy/read.go
generated
vendored
Normal file
@@ -0,0 +1,348 @@
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package spdy
|
||||
|
||||
import (
|
||||
"compress/zlib"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (frame *SynStreamFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
return f.readSynStreamFrame(h, frame)
|
||||
}
|
||||
|
||||
func (frame *SynReplyFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
return f.readSynReplyFrame(h, frame)
|
||||
}
|
||||
|
||||
func (frame *RstStreamFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
frame.CFHeader = h
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.StreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.Status); err != nil {
|
||||
return err
|
||||
}
|
||||
if frame.Status == 0 {
|
||||
return &Error{InvalidControlFrame, frame.StreamId}
|
||||
}
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (frame *SettingsFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
frame.CFHeader = h
|
||||
var numSettings uint32
|
||||
if err := binary.Read(f.r, binary.BigEndian, &numSettings); err != nil {
|
||||
return err
|
||||
}
|
||||
frame.FlagIdValues = make([]SettingsFlagIdValue, numSettings)
|
||||
for i := uint32(0); i < numSettings; i++ {
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.FlagIdValues[i].Id); err != nil {
|
||||
return err
|
||||
}
|
||||
frame.FlagIdValues[i].Flag = SettingsFlag((frame.FlagIdValues[i].Id & 0xff000000) >> 24)
|
||||
frame.FlagIdValues[i].Id &= 0xffffff
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.FlagIdValues[i].Value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (frame *PingFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
frame.CFHeader = h
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
if frame.Id == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
if frame.CFHeader.Flags != 0 {
|
||||
return &Error{InvalidControlFrame, StreamId(frame.Id)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (frame *GoAwayFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
frame.CFHeader = h
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.LastGoodStreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
if frame.CFHeader.Flags != 0 {
|
||||
return &Error{InvalidControlFrame, frame.LastGoodStreamId}
|
||||
}
|
||||
if frame.CFHeader.length != 8 {
|
||||
return &Error{InvalidControlFrame, frame.LastGoodStreamId}
|
||||
}
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.Status); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (frame *HeadersFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
return f.readHeadersFrame(h, frame)
|
||||
}
|
||||
|
||||
func (frame *WindowUpdateFrame) read(h ControlFrameHeader, f *Framer) error {
|
||||
frame.CFHeader = h
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.StreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
if frame.CFHeader.Flags != 0 {
|
||||
return &Error{InvalidControlFrame, frame.StreamId}
|
||||
}
|
||||
if frame.CFHeader.length != 8 {
|
||||
return &Error{InvalidControlFrame, frame.StreamId}
|
||||
}
|
||||
if err := binary.Read(f.r, binary.BigEndian, &frame.DeltaWindowSize); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newControlFrame(frameType ControlFrameType) (controlFrame, error) {
|
||||
ctor, ok := cframeCtor[frameType]
|
||||
if !ok {
|
||||
return nil, &Error{Err: InvalidControlFrame}
|
||||
}
|
||||
return ctor(), nil
|
||||
}
|
||||
|
||||
var cframeCtor = map[ControlFrameType]func() controlFrame{
|
||||
TypeSynStream: func() controlFrame { return new(SynStreamFrame) },
|
||||
TypeSynReply: func() controlFrame { return new(SynReplyFrame) },
|
||||
TypeRstStream: func() controlFrame { return new(RstStreamFrame) },
|
||||
TypeSettings: func() controlFrame { return new(SettingsFrame) },
|
||||
TypePing: func() controlFrame { return new(PingFrame) },
|
||||
TypeGoAway: func() controlFrame { return new(GoAwayFrame) },
|
||||
TypeHeaders: func() controlFrame { return new(HeadersFrame) },
|
||||
TypeWindowUpdate: func() controlFrame { return new(WindowUpdateFrame) },
|
||||
}
|
||||
|
||||
func (f *Framer) uncorkHeaderDecompressor(payloadSize int64) error {
|
||||
if f.headerDecompressor != nil {
|
||||
f.headerReader.N = payloadSize
|
||||
return nil
|
||||
}
|
||||
f.headerReader = io.LimitedReader{R: f.r, N: payloadSize}
|
||||
decompressor, err := zlib.NewReaderDict(&f.headerReader, []byte(headerDictionary))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.headerDecompressor = decompressor
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadFrame reads SPDY encoded data and returns a decompressed Frame.
|
||||
func (f *Framer) ReadFrame() (Frame, error) {
|
||||
var firstWord uint32
|
||||
if err := binary.Read(f.r, binary.BigEndian, &firstWord); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if firstWord&0x80000000 != 0 {
|
||||
frameType := ControlFrameType(firstWord & 0xffff)
|
||||
version := uint16(firstWord >> 16 & 0x7fff)
|
||||
return f.parseControlFrame(version, frameType)
|
||||
}
|
||||
return f.parseDataFrame(StreamId(firstWord & 0x7fffffff))
|
||||
}
|
||||
|
||||
func (f *Framer) parseControlFrame(version uint16, frameType ControlFrameType) (Frame, error) {
|
||||
var length uint32
|
||||
if err := binary.Read(f.r, binary.BigEndian, &length); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flags := ControlFlags((length & 0xff000000) >> 24)
|
||||
length &= 0xffffff
|
||||
header := ControlFrameHeader{version, frameType, flags, length}
|
||||
cframe, err := newControlFrame(frameType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = cframe.read(header, f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cframe, nil
|
||||
}
|
||||
|
||||
func parseHeaderValueBlock(r io.Reader, streamId StreamId) (http.Header, error) {
|
||||
var numHeaders uint32
|
||||
if err := binary.Read(r, binary.BigEndian, &numHeaders); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var e error
|
||||
h := make(http.Header, int(numHeaders))
|
||||
for i := 0; i < int(numHeaders); i++ {
|
||||
var length uint32
|
||||
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nameBytes := make([]byte, length)
|
||||
if _, err := io.ReadFull(r, nameBytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := string(nameBytes)
|
||||
if name != strings.ToLower(name) {
|
||||
e = &Error{UnlowercasedHeaderName, streamId}
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
if h[name] != nil {
|
||||
e = &Error{DuplicateHeaders, streamId}
|
||||
}
|
||||
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := make([]byte, length)
|
||||
if _, err := io.ReadFull(r, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valueList := strings.Split(string(value), headerValueSeparator)
|
||||
for _, v := range valueList {
|
||||
h.Add(name, v)
|
||||
}
|
||||
}
|
||||
if e != nil {
|
||||
return h, e
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func (f *Framer) readSynStreamFrame(h ControlFrameHeader, frame *SynStreamFrame) error {
|
||||
frame.CFHeader = h
|
||||
var err error
|
||||
if err = binary.Read(f.r, binary.BigEndian, &frame.StreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = binary.Read(f.r, binary.BigEndian, &frame.AssociatedToStreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = binary.Read(f.r, binary.BigEndian, &frame.Priority); err != nil {
|
||||
return err
|
||||
}
|
||||
frame.Priority >>= 5
|
||||
if err = binary.Read(f.r, binary.BigEndian, &frame.Slot); err != nil {
|
||||
return err
|
||||
}
|
||||
reader := f.r
|
||||
if !f.headerCompressionDisabled {
|
||||
err := f.uncorkHeaderDecompressor(int64(h.length - 10))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reader = f.headerDecompressor
|
||||
}
|
||||
frame.Headers, err = parseHeaderValueBlock(reader, frame.StreamId)
|
||||
if !f.headerCompressionDisabled && (err == io.EOF && f.headerReader.N == 0 || f.headerReader.N != 0) {
|
||||
err = &Error{WrongCompressedPayloadSize, 0}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for h := range frame.Headers {
|
||||
if invalidReqHeaders[h] {
|
||||
return &Error{InvalidHeaderPresent, frame.StreamId}
|
||||
}
|
||||
}
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Framer) readSynReplyFrame(h ControlFrameHeader, frame *SynReplyFrame) error {
|
||||
frame.CFHeader = h
|
||||
var err error
|
||||
if err = binary.Read(f.r, binary.BigEndian, &frame.StreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
reader := f.r
|
||||
if !f.headerCompressionDisabled {
|
||||
err := f.uncorkHeaderDecompressor(int64(h.length - 4))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reader = f.headerDecompressor
|
||||
}
|
||||
frame.Headers, err = parseHeaderValueBlock(reader, frame.StreamId)
|
||||
if !f.headerCompressionDisabled && (err == io.EOF && f.headerReader.N == 0 || f.headerReader.N != 0) {
|
||||
err = &Error{WrongCompressedPayloadSize, 0}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for h := range frame.Headers {
|
||||
if invalidRespHeaders[h] {
|
||||
return &Error{InvalidHeaderPresent, frame.StreamId}
|
||||
}
|
||||
}
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Framer) readHeadersFrame(h ControlFrameHeader, frame *HeadersFrame) error {
|
||||
frame.CFHeader = h
|
||||
var err error
|
||||
if err = binary.Read(f.r, binary.BigEndian, &frame.StreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
reader := f.r
|
||||
if !f.headerCompressionDisabled {
|
||||
err := f.uncorkHeaderDecompressor(int64(h.length - 4))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reader = f.headerDecompressor
|
||||
}
|
||||
frame.Headers, err = parseHeaderValueBlock(reader, frame.StreamId)
|
||||
if !f.headerCompressionDisabled && (err == io.EOF && f.headerReader.N == 0 || f.headerReader.N != 0) {
|
||||
err = &Error{WrongCompressedPayloadSize, 0}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var invalidHeaders map[string]bool
|
||||
if frame.StreamId%2 == 0 {
|
||||
invalidHeaders = invalidReqHeaders
|
||||
} else {
|
||||
invalidHeaders = invalidRespHeaders
|
||||
}
|
||||
for h := range frame.Headers {
|
||||
if invalidHeaders[h] {
|
||||
return &Error{InvalidHeaderPresent, frame.StreamId}
|
||||
}
|
||||
}
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Framer) parseDataFrame(streamId StreamId) (*DataFrame, error) {
|
||||
var length uint32
|
||||
if err := binary.Read(f.r, binary.BigEndian, &length); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var frame DataFrame
|
||||
frame.StreamId = streamId
|
||||
frame.Flags = DataFlags(length >> 24)
|
||||
length &= 0xffffff
|
||||
frame.Data = make([]byte, length)
|
||||
if _, err := io.ReadFull(f.r, frame.Data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if frame.StreamId == 0 {
|
||||
return nil, &Error{ZeroStreamId, 0}
|
||||
}
|
||||
return &frame, nil
|
||||
}
|
||||
275
vendor/github.com/docker/spdystream/spdy/types.go
generated
vendored
Normal file
275
vendor/github.com/docker/spdystream/spdy/types.go
generated
vendored
Normal file
@@ -0,0 +1,275 @@
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package spdy implements the SPDY protocol (currently SPDY/3), described in
|
||||
// http://www.chromium.org/spdy/spdy-protocol/spdy-protocol-draft3.
|
||||
package spdy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Version is the protocol version number that this package implements.
|
||||
const Version = 3
|
||||
|
||||
// ControlFrameType stores the type field in a control frame header.
|
||||
type ControlFrameType uint16
|
||||
|
||||
const (
|
||||
TypeSynStream ControlFrameType = 0x0001
|
||||
TypeSynReply = 0x0002
|
||||
TypeRstStream = 0x0003
|
||||
TypeSettings = 0x0004
|
||||
TypePing = 0x0006
|
||||
TypeGoAway = 0x0007
|
||||
TypeHeaders = 0x0008
|
||||
TypeWindowUpdate = 0x0009
|
||||
)
|
||||
|
||||
// ControlFlags are the flags that can be set on a control frame.
|
||||
type ControlFlags uint8
|
||||
|
||||
const (
|
||||
ControlFlagFin ControlFlags = 0x01
|
||||
ControlFlagUnidirectional = 0x02
|
||||
ControlFlagSettingsClearSettings = 0x01
|
||||
)
|
||||
|
||||
// DataFlags are the flags that can be set on a data frame.
|
||||
type DataFlags uint8
|
||||
|
||||
const (
|
||||
DataFlagFin DataFlags = 0x01
|
||||
)
|
||||
|
||||
// MaxDataLength is the maximum number of bytes that can be stored in one frame.
|
||||
const MaxDataLength = 1<<24 - 1
|
||||
|
||||
// headerValueSepator separates multiple header values.
|
||||
const headerValueSeparator = "\x00"
|
||||
|
||||
// Frame is a single SPDY frame in its unpacked in-memory representation. Use
|
||||
// Framer to read and write it.
|
||||
type Frame interface {
|
||||
write(f *Framer) error
|
||||
}
|
||||
|
||||
// ControlFrameHeader contains all the fields in a control frame header,
|
||||
// in its unpacked in-memory representation.
|
||||
type ControlFrameHeader struct {
|
||||
// Note, high bit is the "Control" bit.
|
||||
version uint16 // spdy version number
|
||||
frameType ControlFrameType
|
||||
Flags ControlFlags
|
||||
length uint32 // length of data field
|
||||
}
|
||||
|
||||
type controlFrame interface {
|
||||
Frame
|
||||
read(h ControlFrameHeader, f *Framer) error
|
||||
}
|
||||
|
||||
// StreamId represents a 31-bit value identifying the stream.
|
||||
type StreamId uint32
|
||||
|
||||
// SynStreamFrame is the unpacked, in-memory representation of a SYN_STREAM
|
||||
// frame.
|
||||
type SynStreamFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
StreamId StreamId
|
||||
AssociatedToStreamId StreamId // stream id for a stream which this stream is associated to
|
||||
Priority uint8 // priority of this frame (3-bit)
|
||||
Slot uint8 // index in the server's credential vector of the client certificate
|
||||
Headers http.Header
|
||||
}
|
||||
|
||||
// SynReplyFrame is the unpacked, in-memory representation of a SYN_REPLY frame.
|
||||
type SynReplyFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
StreamId StreamId
|
||||
Headers http.Header
|
||||
}
|
||||
|
||||
// RstStreamStatus represents the status that led to a RST_STREAM.
|
||||
type RstStreamStatus uint32
|
||||
|
||||
const (
|
||||
ProtocolError RstStreamStatus = iota + 1
|
||||
InvalidStream
|
||||
RefusedStream
|
||||
UnsupportedVersion
|
||||
Cancel
|
||||
InternalError
|
||||
FlowControlError
|
||||
StreamInUse
|
||||
StreamAlreadyClosed
|
||||
InvalidCredentials
|
||||
FrameTooLarge
|
||||
)
|
||||
|
||||
// RstStreamFrame is the unpacked, in-memory representation of a RST_STREAM
|
||||
// frame.
|
||||
type RstStreamFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
StreamId StreamId
|
||||
Status RstStreamStatus
|
||||
}
|
||||
|
||||
// SettingsFlag represents a flag in a SETTINGS frame.
|
||||
type SettingsFlag uint8
|
||||
|
||||
const (
|
||||
FlagSettingsPersistValue SettingsFlag = 0x1
|
||||
FlagSettingsPersisted = 0x2
|
||||
)
|
||||
|
||||
// SettingsFlag represents the id of an id/value pair in a SETTINGS frame.
|
||||
type SettingsId uint32
|
||||
|
||||
const (
|
||||
SettingsUploadBandwidth SettingsId = iota + 1
|
||||
SettingsDownloadBandwidth
|
||||
SettingsRoundTripTime
|
||||
SettingsMaxConcurrentStreams
|
||||
SettingsCurrentCwnd
|
||||
SettingsDownloadRetransRate
|
||||
SettingsInitialWindowSize
|
||||
SettingsClientCretificateVectorSize
|
||||
)
|
||||
|
||||
// SettingsFlagIdValue is the unpacked, in-memory representation of the
|
||||
// combined flag/id/value for a setting in a SETTINGS frame.
|
||||
type SettingsFlagIdValue struct {
|
||||
Flag SettingsFlag
|
||||
Id SettingsId
|
||||
Value uint32
|
||||
}
|
||||
|
||||
// SettingsFrame is the unpacked, in-memory representation of a SPDY
|
||||
// SETTINGS frame.
|
||||
type SettingsFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
FlagIdValues []SettingsFlagIdValue
|
||||
}
|
||||
|
||||
// PingFrame is the unpacked, in-memory representation of a PING frame.
|
||||
type PingFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
Id uint32 // unique id for this ping, from server is even, from client is odd.
|
||||
}
|
||||
|
||||
// GoAwayStatus represents the status in a GoAwayFrame.
|
||||
type GoAwayStatus uint32
|
||||
|
||||
const (
|
||||
GoAwayOK GoAwayStatus = iota
|
||||
GoAwayProtocolError
|
||||
GoAwayInternalError
|
||||
)
|
||||
|
||||
// GoAwayFrame is the unpacked, in-memory representation of a GOAWAY frame.
|
||||
type GoAwayFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
LastGoodStreamId StreamId // last stream id which was accepted by sender
|
||||
Status GoAwayStatus
|
||||
}
|
||||
|
||||
// HeadersFrame is the unpacked, in-memory representation of a HEADERS frame.
|
||||
type HeadersFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
StreamId StreamId
|
||||
Headers http.Header
|
||||
}
|
||||
|
||||
// WindowUpdateFrame is the unpacked, in-memory representation of a
|
||||
// WINDOW_UPDATE frame.
|
||||
type WindowUpdateFrame struct {
|
||||
CFHeader ControlFrameHeader
|
||||
StreamId StreamId
|
||||
DeltaWindowSize uint32 // additional number of bytes to existing window size
|
||||
}
|
||||
|
||||
// TODO: Implement credential frame and related methods.
|
||||
|
||||
// DataFrame is the unpacked, in-memory representation of a DATA frame.
|
||||
type DataFrame struct {
|
||||
// Note, high bit is the "Control" bit. Should be 0 for data frames.
|
||||
StreamId StreamId
|
||||
Flags DataFlags
|
||||
Data []byte // payload data of this frame
|
||||
}
|
||||
|
||||
// A SPDY specific error.
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
UnlowercasedHeaderName ErrorCode = "header was not lowercased"
|
||||
DuplicateHeaders = "multiple headers with same name"
|
||||
WrongCompressedPayloadSize = "compressed payload size was incorrect"
|
||||
UnknownFrameType = "unknown frame type"
|
||||
InvalidControlFrame = "invalid control frame"
|
||||
InvalidDataFrame = "invalid data frame"
|
||||
InvalidHeaderPresent = "frame contained invalid header"
|
||||
ZeroStreamId = "stream id zero is disallowed"
|
||||
)
|
||||
|
||||
// Error contains both the type of error and additional values. StreamId is 0
|
||||
// if Error is not associated with a stream.
|
||||
type Error struct {
|
||||
Err ErrorCode
|
||||
StreamId StreamId
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
return string(e.Err)
|
||||
}
|
||||
|
||||
var invalidReqHeaders = map[string]bool{
|
||||
"Connection": true,
|
||||
"Host": true,
|
||||
"Keep-Alive": true,
|
||||
"Proxy-Connection": true,
|
||||
"Transfer-Encoding": true,
|
||||
}
|
||||
|
||||
var invalidRespHeaders = map[string]bool{
|
||||
"Connection": true,
|
||||
"Keep-Alive": true,
|
||||
"Proxy-Connection": true,
|
||||
"Transfer-Encoding": true,
|
||||
}
|
||||
|
||||
// Framer handles serializing/deserializing SPDY frames, including compressing/
|
||||
// decompressing payloads.
|
||||
type Framer struct {
|
||||
headerCompressionDisabled bool
|
||||
w io.Writer
|
||||
headerBuf *bytes.Buffer
|
||||
headerCompressor *zlib.Writer
|
||||
r io.Reader
|
||||
headerReader io.LimitedReader
|
||||
headerDecompressor io.ReadCloser
|
||||
}
|
||||
|
||||
// NewFramer allocates a new Framer for a given SPDY connection, represented by
|
||||
// a io.Writer and io.Reader. Note that Framer will read and write individual fields
|
||||
// from/to the Reader and Writer, so the caller should pass in an appropriately
|
||||
// buffered implementation to optimize performance.
|
||||
func NewFramer(w io.Writer, r io.Reader) (*Framer, error) {
|
||||
compressBuf := new(bytes.Buffer)
|
||||
compressor, err := zlib.NewWriterLevelDict(compressBuf, zlib.BestCompression, []byte(headerDictionary))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
framer := &Framer{
|
||||
w: w,
|
||||
headerBuf: compressBuf,
|
||||
headerCompressor: compressor,
|
||||
r: r,
|
||||
}
|
||||
return framer, nil
|
||||
}
|
||||
318
vendor/github.com/docker/spdystream/spdy/write.go
generated
vendored
Normal file
318
vendor/github.com/docker/spdystream/spdy/write.go
generated
vendored
Normal file
@@ -0,0 +1,318 @@
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package spdy
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (frame *SynStreamFrame) write(f *Framer) error {
|
||||
return f.writeSynStreamFrame(frame)
|
||||
}
|
||||
|
||||
func (frame *SynReplyFrame) write(f *Framer) error {
|
||||
return f.writeSynReplyFrame(frame)
|
||||
}
|
||||
|
||||
func (frame *RstStreamFrame) write(f *Framer) (err error) {
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypeRstStream
|
||||
frame.CFHeader.Flags = 0
|
||||
frame.CFHeader.length = 8
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.StreamId); err != nil {
|
||||
return
|
||||
}
|
||||
if frame.Status == 0 {
|
||||
return &Error{InvalidControlFrame, frame.StreamId}
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.Status); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (frame *SettingsFrame) write(f *Framer) (err error) {
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypeSettings
|
||||
frame.CFHeader.length = uint32(len(frame.FlagIdValues)*8 + 4)
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, uint32(len(frame.FlagIdValues))); err != nil {
|
||||
return
|
||||
}
|
||||
for _, flagIdValue := range frame.FlagIdValues {
|
||||
flagId := uint32(flagIdValue.Flag)<<24 | uint32(flagIdValue.Id)
|
||||
if err = binary.Write(f.w, binary.BigEndian, flagId); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, flagIdValue.Value); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (frame *PingFrame) write(f *Framer) (err error) {
|
||||
if frame.Id == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypePing
|
||||
frame.CFHeader.Flags = 0
|
||||
frame.CFHeader.length = 4
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.Id); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (frame *GoAwayFrame) write(f *Framer) (err error) {
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypeGoAway
|
||||
frame.CFHeader.Flags = 0
|
||||
frame.CFHeader.length = 8
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.LastGoodStreamId); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.Status); err != nil {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (frame *HeadersFrame) write(f *Framer) error {
|
||||
return f.writeHeadersFrame(frame)
|
||||
}
|
||||
|
||||
func (frame *WindowUpdateFrame) write(f *Framer) (err error) {
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypeWindowUpdate
|
||||
frame.CFHeader.Flags = 0
|
||||
frame.CFHeader.length = 8
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.StreamId); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.DeltaWindowSize); err != nil {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (frame *DataFrame) write(f *Framer) error {
|
||||
return f.writeDataFrame(frame)
|
||||
}
|
||||
|
||||
// WriteFrame writes a frame.
|
||||
func (f *Framer) WriteFrame(frame Frame) error {
|
||||
return frame.write(f)
|
||||
}
|
||||
|
||||
func writeControlFrameHeader(w io.Writer, h ControlFrameHeader) error {
|
||||
if err := binary.Write(w, binary.BigEndian, 0x8000|h.version); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := binary.Write(w, binary.BigEndian, h.frameType); err != nil {
|
||||
return err
|
||||
}
|
||||
flagsAndLength := uint32(h.Flags)<<24 | h.length
|
||||
if err := binary.Write(w, binary.BigEndian, flagsAndLength); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeHeaderValueBlock(w io.Writer, h http.Header) (n int, err error) {
|
||||
n = 0
|
||||
if err = binary.Write(w, binary.BigEndian, uint32(len(h))); err != nil {
|
||||
return
|
||||
}
|
||||
n += 2
|
||||
for name, values := range h {
|
||||
if err = binary.Write(w, binary.BigEndian, uint32(len(name))); err != nil {
|
||||
return
|
||||
}
|
||||
n += 2
|
||||
name = strings.ToLower(name)
|
||||
if _, err = io.WriteString(w, name); err != nil {
|
||||
return
|
||||
}
|
||||
n += len(name)
|
||||
v := strings.Join(values, headerValueSeparator)
|
||||
if err = binary.Write(w, binary.BigEndian, uint32(len(v))); err != nil {
|
||||
return
|
||||
}
|
||||
n += 2
|
||||
if _, err = io.WriteString(w, v); err != nil {
|
||||
return
|
||||
}
|
||||
n += len(v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Framer) writeSynStreamFrame(frame *SynStreamFrame) (err error) {
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
// Marshal the headers.
|
||||
var writer io.Writer = f.headerBuf
|
||||
if !f.headerCompressionDisabled {
|
||||
writer = f.headerCompressor
|
||||
}
|
||||
if _, err = writeHeaderValueBlock(writer, frame.Headers); err != nil {
|
||||
return
|
||||
}
|
||||
if !f.headerCompressionDisabled {
|
||||
f.headerCompressor.Flush()
|
||||
}
|
||||
|
||||
// Set ControlFrameHeader.
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypeSynStream
|
||||
frame.CFHeader.length = uint32(len(f.headerBuf.Bytes()) + 10)
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.StreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.AssociatedToStreamId); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.Priority<<5); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.Slot); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = f.w.Write(f.headerBuf.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
f.headerBuf.Reset()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Framer) writeSynReplyFrame(frame *SynReplyFrame) (err error) {
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
// Marshal the headers.
|
||||
var writer io.Writer = f.headerBuf
|
||||
if !f.headerCompressionDisabled {
|
||||
writer = f.headerCompressor
|
||||
}
|
||||
if _, err = writeHeaderValueBlock(writer, frame.Headers); err != nil {
|
||||
return
|
||||
}
|
||||
if !f.headerCompressionDisabled {
|
||||
f.headerCompressor.Flush()
|
||||
}
|
||||
|
||||
// Set ControlFrameHeader.
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypeSynReply
|
||||
frame.CFHeader.length = uint32(len(f.headerBuf.Bytes()) + 4)
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.StreamId); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = f.w.Write(f.headerBuf.Bytes()); err != nil {
|
||||
return
|
||||
}
|
||||
f.headerBuf.Reset()
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Framer) writeHeadersFrame(frame *HeadersFrame) (err error) {
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
// Marshal the headers.
|
||||
var writer io.Writer = f.headerBuf
|
||||
if !f.headerCompressionDisabled {
|
||||
writer = f.headerCompressor
|
||||
}
|
||||
if _, err = writeHeaderValueBlock(writer, frame.Headers); err != nil {
|
||||
return
|
||||
}
|
||||
if !f.headerCompressionDisabled {
|
||||
f.headerCompressor.Flush()
|
||||
}
|
||||
|
||||
// Set ControlFrameHeader.
|
||||
frame.CFHeader.version = Version
|
||||
frame.CFHeader.frameType = TypeHeaders
|
||||
frame.CFHeader.length = uint32(len(f.headerBuf.Bytes()) + 4)
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = writeControlFrameHeader(f.w, frame.CFHeader); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.StreamId); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = f.w.Write(f.headerBuf.Bytes()); err != nil {
|
||||
return
|
||||
}
|
||||
f.headerBuf.Reset()
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Framer) writeDataFrame(frame *DataFrame) (err error) {
|
||||
if frame.StreamId == 0 {
|
||||
return &Error{ZeroStreamId, 0}
|
||||
}
|
||||
if frame.StreamId&0x80000000 != 0 || len(frame.Data) > MaxDataLength {
|
||||
return &Error{InvalidDataFrame, frame.StreamId}
|
||||
}
|
||||
|
||||
// Serialize frame to Writer.
|
||||
if err = binary.Write(f.w, binary.BigEndian, frame.StreamId); err != nil {
|
||||
return
|
||||
}
|
||||
flagsAndLength := uint32(frame.Flags)<<24 | uint32(len(frame.Data))
|
||||
if err = binary.Write(f.w, binary.BigEndian, flagsAndLength); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = f.w.Write(frame.Data); err != nil {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
327
vendor/github.com/docker/spdystream/stream.go
generated
vendored
Normal file
327
vendor/github.com/docker/spdystream/stream.go
generated
vendored
Normal file
@@ -0,0 +1,327 @@
|
||||
package spdystream
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/docker/spdystream/spdy"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnreadPartialData = errors.New("unread partial data")
|
||||
)
|
||||
|
||||
type Stream struct {
|
||||
streamId spdy.StreamId
|
||||
parent *Stream
|
||||
conn *Connection
|
||||
startChan chan error
|
||||
|
||||
dataLock sync.RWMutex
|
||||
dataChan chan []byte
|
||||
unread []byte
|
||||
|
||||
priority uint8
|
||||
headers http.Header
|
||||
headerChan chan http.Header
|
||||
finishLock sync.Mutex
|
||||
finished bool
|
||||
replyCond *sync.Cond
|
||||
replied bool
|
||||
closeLock sync.Mutex
|
||||
closeChan chan bool
|
||||
}
|
||||
|
||||
// WriteData writes data to stream, sending a dataframe per call
|
||||
func (s *Stream) WriteData(data []byte, fin bool) error {
|
||||
s.waitWriteReply()
|
||||
var flags spdy.DataFlags
|
||||
|
||||
if fin {
|
||||
flags = spdy.DataFlagFin
|
||||
s.finishLock.Lock()
|
||||
if s.finished {
|
||||
s.finishLock.Unlock()
|
||||
return ErrWriteClosedStream
|
||||
}
|
||||
s.finished = true
|
||||
s.finishLock.Unlock()
|
||||
}
|
||||
|
||||
dataFrame := &spdy.DataFrame{
|
||||
StreamId: s.streamId,
|
||||
Flags: flags,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
debugMessage("(%p) (%d) Writing data frame", s, s.streamId)
|
||||
return s.conn.framer.WriteFrame(dataFrame)
|
||||
}
|
||||
|
||||
// Write writes bytes to a stream, calling write data for each call.
|
||||
func (s *Stream) Write(data []byte) (n int, err error) {
|
||||
err = s.WriteData(data, false)
|
||||
if err == nil {
|
||||
n = len(data)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Read reads bytes from a stream, a single read will never get more
|
||||
// than what is sent on a single data frame, but a multiple calls to
|
||||
// read may get data from the same data frame.
|
||||
func (s *Stream) Read(p []byte) (n int, err error) {
|
||||
if s.unread == nil {
|
||||
select {
|
||||
case <-s.closeChan:
|
||||
return 0, io.EOF
|
||||
case read, ok := <-s.dataChan:
|
||||
if !ok {
|
||||
return 0, io.EOF
|
||||
}
|
||||
s.unread = read
|
||||
}
|
||||
}
|
||||
n = copy(p, s.unread)
|
||||
if n < len(s.unread) {
|
||||
s.unread = s.unread[n:]
|
||||
} else {
|
||||
s.unread = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ReadData reads an entire data frame and returns the byte array
|
||||
// from the data frame. If there is unread data from the result
|
||||
// of a Read call, this function will return an ErrUnreadPartialData.
|
||||
func (s *Stream) ReadData() ([]byte, error) {
|
||||
debugMessage("(%p) Reading data from %d", s, s.streamId)
|
||||
if s.unread != nil {
|
||||
return nil, ErrUnreadPartialData
|
||||
}
|
||||
select {
|
||||
case <-s.closeChan:
|
||||
return nil, io.EOF
|
||||
case read, ok := <-s.dataChan:
|
||||
if !ok {
|
||||
return nil, io.EOF
|
||||
}
|
||||
return read, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stream) waitWriteReply() {
|
||||
if s.replyCond != nil {
|
||||
s.replyCond.L.Lock()
|
||||
for !s.replied {
|
||||
s.replyCond.Wait()
|
||||
}
|
||||
s.replyCond.L.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Wait waits for the stream to receive a reply.
|
||||
func (s *Stream) Wait() error {
|
||||
return s.WaitTimeout(time.Duration(0))
|
||||
}
|
||||
|
||||
// WaitTimeout waits for the stream to receive a reply or for timeout.
|
||||
// When the timeout is reached, ErrTimeout will be returned.
|
||||
func (s *Stream) WaitTimeout(timeout time.Duration) error {
|
||||
var timeoutChan <-chan time.Time
|
||||
if timeout > time.Duration(0) {
|
||||
timeoutChan = time.After(timeout)
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-s.startChan:
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
break
|
||||
case <-timeoutChan:
|
||||
return ErrTimeout
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the stream by sending an empty data frame with the
|
||||
// finish flag set, indicating this side is finished with the stream.
|
||||
func (s *Stream) Close() error {
|
||||
select {
|
||||
case <-s.closeChan:
|
||||
// Stream is now fully closed
|
||||
s.conn.removeStream(s)
|
||||
default:
|
||||
break
|
||||
}
|
||||
return s.WriteData([]byte{}, true)
|
||||
}
|
||||
|
||||
// Reset sends a reset frame, putting the stream into the fully closed state.
|
||||
func (s *Stream) Reset() error {
|
||||
s.conn.removeStream(s)
|
||||
return s.resetStream()
|
||||
}
|
||||
|
||||
func (s *Stream) resetStream() error {
|
||||
// Always call closeRemoteChannels, even if s.finished is already true.
|
||||
// This makes it so that stream.Close() followed by stream.Reset() allows
|
||||
// stream.Read() to unblock.
|
||||
s.closeRemoteChannels()
|
||||
|
||||
s.finishLock.Lock()
|
||||
if s.finished {
|
||||
s.finishLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
s.finished = true
|
||||
s.finishLock.Unlock()
|
||||
|
||||
resetFrame := &spdy.RstStreamFrame{
|
||||
StreamId: s.streamId,
|
||||
Status: spdy.Cancel,
|
||||
}
|
||||
return s.conn.framer.WriteFrame(resetFrame)
|
||||
}
|
||||
|
||||
// CreateSubStream creates a stream using the current as the parent
|
||||
func (s *Stream) CreateSubStream(headers http.Header, fin bool) (*Stream, error) {
|
||||
return s.conn.CreateStream(headers, s, fin)
|
||||
}
|
||||
|
||||
// SetPriority sets the stream priority, does not affect the
|
||||
// remote priority of this stream after Open has been called.
|
||||
// Valid values are 0 through 7, 0 being the highest priority
|
||||
// and 7 the lowest.
|
||||
func (s *Stream) SetPriority(priority uint8) {
|
||||
s.priority = priority
|
||||
}
|
||||
|
||||
// SendHeader sends a header frame across the stream
|
||||
func (s *Stream) SendHeader(headers http.Header, fin bool) error {
|
||||
return s.conn.sendHeaders(headers, s, fin)
|
||||
}
|
||||
|
||||
// SendReply sends a reply on a stream, only valid to be called once
|
||||
// when handling a new stream
|
||||
func (s *Stream) SendReply(headers http.Header, fin bool) error {
|
||||
if s.replyCond == nil {
|
||||
return errors.New("cannot reply on initiated stream")
|
||||
}
|
||||
s.replyCond.L.Lock()
|
||||
defer s.replyCond.L.Unlock()
|
||||
if s.replied {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := s.conn.sendReply(headers, s, fin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.replied = true
|
||||
s.replyCond.Broadcast()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Refuse sends a reset frame with the status refuse, only
|
||||
// valid to be called once when handling a new stream. This
|
||||
// may be used to indicate that a stream is not allowed
|
||||
// when http status codes are not being used.
|
||||
func (s *Stream) Refuse() error {
|
||||
if s.replied {
|
||||
return nil
|
||||
}
|
||||
s.replied = true
|
||||
return s.conn.sendReset(spdy.RefusedStream, s)
|
||||
}
|
||||
|
||||
// Cancel sends a reset frame with the status canceled. This
|
||||
// can be used at any time by the creator of the Stream to
|
||||
// indicate the stream is no longer needed.
|
||||
func (s *Stream) Cancel() error {
|
||||
return s.conn.sendReset(spdy.Cancel, s)
|
||||
}
|
||||
|
||||
// ReceiveHeader receives a header sent on the other side
|
||||
// of the stream. This function will block until a header
|
||||
// is received or stream is closed.
|
||||
func (s *Stream) ReceiveHeader() (http.Header, error) {
|
||||
select {
|
||||
case <-s.closeChan:
|
||||
break
|
||||
case header, ok := <-s.headerChan:
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("header chan closed")
|
||||
}
|
||||
return header, nil
|
||||
}
|
||||
return nil, fmt.Errorf("stream closed")
|
||||
}
|
||||
|
||||
// Parent returns the parent stream
|
||||
func (s *Stream) Parent() *Stream {
|
||||
return s.parent
|
||||
}
|
||||
|
||||
// Headers returns the headers used to create the stream
|
||||
func (s *Stream) Headers() http.Header {
|
||||
return s.headers
|
||||
}
|
||||
|
||||
// String returns the string version of stream using the
|
||||
// streamId to uniquely identify the stream
|
||||
func (s *Stream) String() string {
|
||||
return fmt.Sprintf("stream:%d", s.streamId)
|
||||
}
|
||||
|
||||
// Identifier returns a 32 bit identifier for the stream
|
||||
func (s *Stream) Identifier() uint32 {
|
||||
return uint32(s.streamId)
|
||||
}
|
||||
|
||||
// IsFinished returns whether the stream has finished
|
||||
// sending data
|
||||
func (s *Stream) IsFinished() bool {
|
||||
return s.finished
|
||||
}
|
||||
|
||||
// Implement net.Conn interface
|
||||
|
||||
func (s *Stream) LocalAddr() net.Addr {
|
||||
return s.conn.conn.LocalAddr()
|
||||
}
|
||||
|
||||
func (s *Stream) RemoteAddr() net.Addr {
|
||||
return s.conn.conn.RemoteAddr()
|
||||
}
|
||||
|
||||
// TODO set per stream values instead of connection-wide
|
||||
|
||||
func (s *Stream) SetDeadline(t time.Time) error {
|
||||
return s.conn.conn.SetDeadline(t)
|
||||
}
|
||||
|
||||
func (s *Stream) SetReadDeadline(t time.Time) error {
|
||||
return s.conn.conn.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
func (s *Stream) SetWriteDeadline(t time.Time) error {
|
||||
return s.conn.conn.SetWriteDeadline(t)
|
||||
}
|
||||
|
||||
func (s *Stream) closeRemoteChannels() {
|
||||
s.closeLock.Lock()
|
||||
defer s.closeLock.Unlock()
|
||||
select {
|
||||
case <-s.closeChan:
|
||||
default:
|
||||
close(s.closeChan)
|
||||
}
|
||||
}
|
||||
16
vendor/github.com/docker/spdystream/utils.go
generated
vendored
Normal file
16
vendor/github.com/docker/spdystream/utils.go
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
package spdystream
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
DEBUG = os.Getenv("DEBUG")
|
||||
)
|
||||
|
||||
func debugMessage(fmt string, args ...interface{}) {
|
||||
if DEBUG != "" {
|
||||
log.Printf(fmt, args...)
|
||||
}
|
||||
}
|
||||
22
vendor/github.com/gorilla/websocket/LICENSE
generated
vendored
Normal file
22
vendor/github.com/gorilla/websocket/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
Copyright (c) 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
392
vendor/github.com/gorilla/websocket/client.go
generated
vendored
Normal file
392
vendor/github.com/gorilla/websocket/client.go
generated
vendored
Normal file
@@ -0,0 +1,392 @@
|
||||
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrBadHandshake is returned when the server response to opening handshake is
|
||||
// invalid.
|
||||
var ErrBadHandshake = errors.New("websocket: bad handshake")
|
||||
|
||||
var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
|
||||
|
||||
// NewClient creates a new client connection using the given net connection.
|
||||
// The URL u specifies the host and request URI. Use requestHeader to specify
|
||||
// the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
|
||||
// (Cookie). Use the response.Header to get the selected subprotocol
|
||||
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
||||
//
|
||||
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
||||
// non-nil *http.Response so that callers can handle redirects, authentication,
|
||||
// etc.
|
||||
//
|
||||
// Deprecated: Use Dialer instead.
|
||||
func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
|
||||
d := Dialer{
|
||||
ReadBufferSize: readBufSize,
|
||||
WriteBufferSize: writeBufSize,
|
||||
NetDial: func(net, addr string) (net.Conn, error) {
|
||||
return netConn, nil
|
||||
},
|
||||
}
|
||||
return d.Dial(u.String(), requestHeader)
|
||||
}
|
||||
|
||||
// A Dialer contains options for connecting to WebSocket server.
|
||||
type Dialer struct {
|
||||
// NetDial specifies the dial function for creating TCP connections. If
|
||||
// NetDial is nil, net.Dial is used.
|
||||
NetDial func(network, addr string) (net.Conn, error)
|
||||
|
||||
// Proxy specifies a function to return a proxy for a given
|
||||
// Request. If the function returns a non-nil error, the
|
||||
// request is aborted with the provided error.
|
||||
// If Proxy is nil or returns a nil *URL, no proxy is used.
|
||||
Proxy func(*http.Request) (*url.URL, error)
|
||||
|
||||
// TLSClientConfig specifies the TLS configuration to use with tls.Client.
|
||||
// If nil, the default configuration is used.
|
||||
TLSClientConfig *tls.Config
|
||||
|
||||
// HandshakeTimeout specifies the duration for the handshake to complete.
|
||||
HandshakeTimeout time.Duration
|
||||
|
||||
// ReadBufferSize and WriteBufferSize specify I/O buffer sizes. If a buffer
|
||||
// size is zero, then a useful default size is used. The I/O buffer sizes
|
||||
// do not limit the size of the messages that can be sent or received.
|
||||
ReadBufferSize, WriteBufferSize int
|
||||
|
||||
// Subprotocols specifies the client's requested subprotocols.
|
||||
Subprotocols []string
|
||||
|
||||
// EnableCompression specifies if the client should attempt to negotiate
|
||||
// per message compression (RFC 7692). Setting this value to true does not
|
||||
// guarantee that compression will be supported. Currently only "no context
|
||||
// takeover" modes are supported.
|
||||
EnableCompression bool
|
||||
|
||||
// Jar specifies the cookie jar.
|
||||
// If Jar is nil, cookies are not sent in requests and ignored
|
||||
// in responses.
|
||||
Jar http.CookieJar
|
||||
}
|
||||
|
||||
var errMalformedURL = errors.New("malformed ws or wss URL")
|
||||
|
||||
// parseURL parses the URL.
|
||||
//
|
||||
// This function is a replacement for the standard library url.Parse function.
|
||||
// In Go 1.4 and earlier, url.Parse loses information from the path.
|
||||
func parseURL(s string) (*url.URL, error) {
|
||||
// From the RFC:
|
||||
//
|
||||
// ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
|
||||
// wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
|
||||
var u url.URL
|
||||
switch {
|
||||
case strings.HasPrefix(s, "ws://"):
|
||||
u.Scheme = "ws"
|
||||
s = s[len("ws://"):]
|
||||
case strings.HasPrefix(s, "wss://"):
|
||||
u.Scheme = "wss"
|
||||
s = s[len("wss://"):]
|
||||
default:
|
||||
return nil, errMalformedURL
|
||||
}
|
||||
|
||||
if i := strings.Index(s, "?"); i >= 0 {
|
||||
u.RawQuery = s[i+1:]
|
||||
s = s[:i]
|
||||
}
|
||||
|
||||
if i := strings.Index(s, "/"); i >= 0 {
|
||||
u.Opaque = s[i:]
|
||||
s = s[:i]
|
||||
} else {
|
||||
u.Opaque = "/"
|
||||
}
|
||||
|
||||
u.Host = s
|
||||
|
||||
if strings.Contains(u.Host, "@") {
|
||||
// Don't bother parsing user information because user information is
|
||||
// not allowed in websocket URIs.
|
||||
return nil, errMalformedURL
|
||||
}
|
||||
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
|
||||
hostPort = u.Host
|
||||
hostNoPort = u.Host
|
||||
if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
|
||||
hostNoPort = hostNoPort[:i]
|
||||
} else {
|
||||
switch u.Scheme {
|
||||
case "wss":
|
||||
hostPort += ":443"
|
||||
case "https":
|
||||
hostPort += ":443"
|
||||
default:
|
||||
hostPort += ":80"
|
||||
}
|
||||
}
|
||||
return hostPort, hostNoPort
|
||||
}
|
||||
|
||||
// DefaultDialer is a dialer with all fields set to the default zero values.
|
||||
var DefaultDialer = &Dialer{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
}
|
||||
|
||||
// Dial creates a new client connection. Use requestHeader to specify the
|
||||
// origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
|
||||
// Use the response.Header to get the selected subprotocol
|
||||
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
||||
//
|
||||
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
||||
// non-nil *http.Response so that callers can handle redirects, authentication,
|
||||
// etcetera. The response body may not contain the entire response and does not
|
||||
// need to be closed by the application.
|
||||
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
|
||||
|
||||
if d == nil {
|
||||
d = &Dialer{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
}
|
||||
}
|
||||
|
||||
challengeKey, err := generateChallengeKey()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
u, err := parseURL(urlStr)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
switch u.Scheme {
|
||||
case "ws":
|
||||
u.Scheme = "http"
|
||||
case "wss":
|
||||
u.Scheme = "https"
|
||||
default:
|
||||
return nil, nil, errMalformedURL
|
||||
}
|
||||
|
||||
if u.User != nil {
|
||||
// User name and password are not allowed in websocket URIs.
|
||||
return nil, nil, errMalformedURL
|
||||
}
|
||||
|
||||
req := &http.Request{
|
||||
Method: "GET",
|
||||
URL: u,
|
||||
Proto: "HTTP/1.1",
|
||||
ProtoMajor: 1,
|
||||
ProtoMinor: 1,
|
||||
Header: make(http.Header),
|
||||
Host: u.Host,
|
||||
}
|
||||
|
||||
// Set the cookies present in the cookie jar of the dialer
|
||||
if d.Jar != nil {
|
||||
for _, cookie := range d.Jar.Cookies(u) {
|
||||
req.AddCookie(cookie)
|
||||
}
|
||||
}
|
||||
|
||||
// Set the request headers using the capitalization for names and values in
|
||||
// RFC examples. Although the capitalization shouldn't matter, there are
|
||||
// servers that depend on it. The Header.Set method is not used because the
|
||||
// method canonicalizes the header names.
|
||||
req.Header["Upgrade"] = []string{"websocket"}
|
||||
req.Header["Connection"] = []string{"Upgrade"}
|
||||
req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
|
||||
req.Header["Sec-WebSocket-Version"] = []string{"13"}
|
||||
if len(d.Subprotocols) > 0 {
|
||||
req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
|
||||
}
|
||||
for k, vs := range requestHeader {
|
||||
switch {
|
||||
case k == "Host":
|
||||
if len(vs) > 0 {
|
||||
req.Host = vs[0]
|
||||
}
|
||||
case k == "Upgrade" ||
|
||||
k == "Connection" ||
|
||||
k == "Sec-Websocket-Key" ||
|
||||
k == "Sec-Websocket-Version" ||
|
||||
k == "Sec-Websocket-Extensions" ||
|
||||
(k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
|
||||
return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
|
||||
default:
|
||||
req.Header[k] = vs
|
||||
}
|
||||
}
|
||||
|
||||
if d.EnableCompression {
|
||||
req.Header.Set("Sec-Websocket-Extensions", "permessage-deflate; server_no_context_takeover; client_no_context_takeover")
|
||||
}
|
||||
|
||||
hostPort, hostNoPort := hostPortNoPort(u)
|
||||
|
||||
var proxyURL *url.URL
|
||||
// Check wether the proxy method has been configured
|
||||
if d.Proxy != nil {
|
||||
proxyURL, err = d.Proxy(req)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var targetHostPort string
|
||||
if proxyURL != nil {
|
||||
targetHostPort, _ = hostPortNoPort(proxyURL)
|
||||
} else {
|
||||
targetHostPort = hostPort
|
||||
}
|
||||
|
||||
var deadline time.Time
|
||||
if d.HandshakeTimeout != 0 {
|
||||
deadline = time.Now().Add(d.HandshakeTimeout)
|
||||
}
|
||||
|
||||
netDial := d.NetDial
|
||||
if netDial == nil {
|
||||
netDialer := &net.Dialer{Deadline: deadline}
|
||||
netDial = netDialer.Dial
|
||||
}
|
||||
|
||||
netConn, err := netDial("tcp", targetHostPort)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if netConn != nil {
|
||||
netConn.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := netConn.SetDeadline(deadline); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if proxyURL != nil {
|
||||
connectHeader := make(http.Header)
|
||||
if user := proxyURL.User; user != nil {
|
||||
proxyUser := user.Username()
|
||||
if proxyPassword, passwordSet := user.Password(); passwordSet {
|
||||
credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
|
||||
connectHeader.Set("Proxy-Authorization", "Basic "+credential)
|
||||
}
|
||||
}
|
||||
connectReq := &http.Request{
|
||||
Method: "CONNECT",
|
||||
URL: &url.URL{Opaque: hostPort},
|
||||
Host: hostPort,
|
||||
Header: connectHeader,
|
||||
}
|
||||
|
||||
connectReq.Write(netConn)
|
||||
|
||||
// Read response.
|
||||
// Okay to use and discard buffered reader here, because
|
||||
// TLS server will not speak until spoken to.
|
||||
br := bufio.NewReader(netConn)
|
||||
resp, err := http.ReadResponse(br, connectReq)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
f := strings.SplitN(resp.Status, " ", 2)
|
||||
return nil, nil, errors.New(f[1])
|
||||
}
|
||||
}
|
||||
|
||||
if u.Scheme == "https" {
|
||||
cfg := cloneTLSConfig(d.TLSClientConfig)
|
||||
if cfg.ServerName == "" {
|
||||
cfg.ServerName = hostNoPort
|
||||
}
|
||||
tlsConn := tls.Client(netConn, cfg)
|
||||
netConn = tlsConn
|
||||
if err := tlsConn.Handshake(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !cfg.InsecureSkipVerify {
|
||||
if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize)
|
||||
|
||||
if err := req.Write(netConn); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
resp, err := http.ReadResponse(conn.br, req)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if d.Jar != nil {
|
||||
if rc := resp.Cookies(); len(rc) > 0 {
|
||||
d.Jar.SetCookies(u, rc)
|
||||
}
|
||||
}
|
||||
|
||||
if resp.StatusCode != 101 ||
|
||||
!strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
|
||||
!strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
|
||||
resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
|
||||
// Before closing the network connection on return from this
|
||||
// function, slurp up some of the response to aid application
|
||||
// debugging.
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := io.ReadFull(resp.Body, buf)
|
||||
resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
|
||||
return nil, resp, ErrBadHandshake
|
||||
}
|
||||
|
||||
for _, ext := range parseExtensions(resp.Header) {
|
||||
if ext[""] != "permessage-deflate" {
|
||||
continue
|
||||
}
|
||||
_, snct := ext["server_no_context_takeover"]
|
||||
_, cnct := ext["client_no_context_takeover"]
|
||||
if !snct || !cnct {
|
||||
return nil, resp, errInvalidCompression
|
||||
}
|
||||
conn.newCompressionWriter = compressNoContextTakeover
|
||||
conn.newDecompressionReader = decompressNoContextTakeover
|
||||
break
|
||||
}
|
||||
|
||||
resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
|
||||
conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
|
||||
|
||||
netConn.SetDeadline(time.Time{})
|
||||
netConn = nil // to avoid close in defer.
|
||||
return conn, resp, nil
|
||||
}
|
||||
16
vendor/github.com/gorilla/websocket/client_clone.go
generated
vendored
Normal file
16
vendor/github.com/gorilla/websocket/client_clone.go
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build go1.8
|
||||
|
||||
package websocket
|
||||
|
||||
import "crypto/tls"
|
||||
|
||||
func cloneTLSConfig(cfg *tls.Config) *tls.Config {
|
||||
if cfg == nil {
|
||||
return &tls.Config{}
|
||||
}
|
||||
return cfg.Clone()
|
||||
}
|
||||
38
vendor/github.com/gorilla/websocket/client_clone_legacy.go
generated
vendored
Normal file
38
vendor/github.com/gorilla/websocket/client_clone_legacy.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !go1.8
|
||||
|
||||
package websocket
|
||||
|
||||
import "crypto/tls"
|
||||
|
||||
// cloneTLSConfig clones all public fields except the fields
|
||||
// SessionTicketsDisabled and SessionTicketKey. This avoids copying the
|
||||
// sync.Mutex in the sync.Once and makes it safe to call cloneTLSConfig on a
|
||||
// config in active use.
|
||||
func cloneTLSConfig(cfg *tls.Config) *tls.Config {
|
||||
if cfg == nil {
|
||||
return &tls.Config{}
|
||||
}
|
||||
return &tls.Config{
|
||||
Rand: cfg.Rand,
|
||||
Time: cfg.Time,
|
||||
Certificates: cfg.Certificates,
|
||||
NameToCertificate: cfg.NameToCertificate,
|
||||
GetCertificate: cfg.GetCertificate,
|
||||
RootCAs: cfg.RootCAs,
|
||||
NextProtos: cfg.NextProtos,
|
||||
ServerName: cfg.ServerName,
|
||||
ClientAuth: cfg.ClientAuth,
|
||||
ClientCAs: cfg.ClientCAs,
|
||||
InsecureSkipVerify: cfg.InsecureSkipVerify,
|
||||
CipherSuites: cfg.CipherSuites,
|
||||
PreferServerCipherSuites: cfg.PreferServerCipherSuites,
|
||||
ClientSessionCache: cfg.ClientSessionCache,
|
||||
MinVersion: cfg.MinVersion,
|
||||
MaxVersion: cfg.MaxVersion,
|
||||
CurvePreferences: cfg.CurvePreferences,
|
||||
}
|
||||
}
|
||||
148
vendor/github.com/gorilla/websocket/compression.go
generated
vendored
Normal file
148
vendor/github.com/gorilla/websocket/compression.go
generated
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
// Copyright 2017 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"compress/flate"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
minCompressionLevel = -2 // flate.HuffmanOnly not defined in Go < 1.6
|
||||
maxCompressionLevel = flate.BestCompression
|
||||
defaultCompressionLevel = 1
|
||||
)
|
||||
|
||||
var (
|
||||
flateWriterPools [maxCompressionLevel - minCompressionLevel + 1]sync.Pool
|
||||
flateReaderPool = sync.Pool{New: func() interface{} {
|
||||
return flate.NewReader(nil)
|
||||
}}
|
||||
)
|
||||
|
||||
func decompressNoContextTakeover(r io.Reader) io.ReadCloser {
|
||||
const tail =
|
||||
// Add four bytes as specified in RFC
|
||||
"\x00\x00\xff\xff" +
|
||||
// Add final block to squelch unexpected EOF error from flate reader.
|
||||
"\x01\x00\x00\xff\xff"
|
||||
|
||||
fr, _ := flateReaderPool.Get().(io.ReadCloser)
|
||||
fr.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil)
|
||||
return &flateReadWrapper{fr}
|
||||
}
|
||||
|
||||
func isValidCompressionLevel(level int) bool {
|
||||
return minCompressionLevel <= level && level <= maxCompressionLevel
|
||||
}
|
||||
|
||||
func compressNoContextTakeover(w io.WriteCloser, level int) io.WriteCloser {
|
||||
p := &flateWriterPools[level-minCompressionLevel]
|
||||
tw := &truncWriter{w: w}
|
||||
fw, _ := p.Get().(*flate.Writer)
|
||||
if fw == nil {
|
||||
fw, _ = flate.NewWriter(tw, level)
|
||||
} else {
|
||||
fw.Reset(tw)
|
||||
}
|
||||
return &flateWriteWrapper{fw: fw, tw: tw, p: p}
|
||||
}
|
||||
|
||||
// truncWriter is an io.Writer that writes all but the last four bytes of the
|
||||
// stream to another io.Writer.
|
||||
type truncWriter struct {
|
||||
w io.WriteCloser
|
||||
n int
|
||||
p [4]byte
|
||||
}
|
||||
|
||||
func (w *truncWriter) Write(p []byte) (int, error) {
|
||||
n := 0
|
||||
|
||||
// fill buffer first for simplicity.
|
||||
if w.n < len(w.p) {
|
||||
n = copy(w.p[w.n:], p)
|
||||
p = p[n:]
|
||||
w.n += n
|
||||
if len(p) == 0 {
|
||||
return n, nil
|
||||
}
|
||||
}
|
||||
|
||||
m := len(p)
|
||||
if m > len(w.p) {
|
||||
m = len(w.p)
|
||||
}
|
||||
|
||||
if nn, err := w.w.Write(w.p[:m]); err != nil {
|
||||
return n + nn, err
|
||||
}
|
||||
|
||||
copy(w.p[:], w.p[m:])
|
||||
copy(w.p[len(w.p)-m:], p[len(p)-m:])
|
||||
nn, err := w.w.Write(p[:len(p)-m])
|
||||
return n + nn, err
|
||||
}
|
||||
|
||||
type flateWriteWrapper struct {
|
||||
fw *flate.Writer
|
||||
tw *truncWriter
|
||||
p *sync.Pool
|
||||
}
|
||||
|
||||
func (w *flateWriteWrapper) Write(p []byte) (int, error) {
|
||||
if w.fw == nil {
|
||||
return 0, errWriteClosed
|
||||
}
|
||||
return w.fw.Write(p)
|
||||
}
|
||||
|
||||
func (w *flateWriteWrapper) Close() error {
|
||||
if w.fw == nil {
|
||||
return errWriteClosed
|
||||
}
|
||||
err1 := w.fw.Flush()
|
||||
w.p.Put(w.fw)
|
||||
w.fw = nil
|
||||
if w.tw.p != [4]byte{0, 0, 0xff, 0xff} {
|
||||
return errors.New("websocket: internal error, unexpected bytes at end of flate stream")
|
||||
}
|
||||
err2 := w.tw.w.Close()
|
||||
if err1 != nil {
|
||||
return err1
|
||||
}
|
||||
return err2
|
||||
}
|
||||
|
||||
type flateReadWrapper struct {
|
||||
fr io.ReadCloser
|
||||
}
|
||||
|
||||
func (r *flateReadWrapper) Read(p []byte) (int, error) {
|
||||
if r.fr == nil {
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
n, err := r.fr.Read(p)
|
||||
if err == io.EOF {
|
||||
// Preemptively place the reader back in the pool. This helps with
|
||||
// scenarios where the application does not call NextReader() soon after
|
||||
// this final read.
|
||||
r.Close()
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *flateReadWrapper) Close() error {
|
||||
if r.fr == nil {
|
||||
return io.ErrClosedPipe
|
||||
}
|
||||
err := r.fr.Close()
|
||||
flateReaderPool.Put(r.fr)
|
||||
r.fr = nil
|
||||
return err
|
||||
}
|
||||
1149
vendor/github.com/gorilla/websocket/conn.go
generated
vendored
Normal file
1149
vendor/github.com/gorilla/websocket/conn.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
18
vendor/github.com/gorilla/websocket/conn_read.go
generated
vendored
Normal file
18
vendor/github.com/gorilla/websocket/conn_read.go
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build go1.5
|
||||
|
||||
package websocket
|
||||
|
||||
import "io"
|
||||
|
||||
func (c *Conn) read(n int) ([]byte, error) {
|
||||
p, err := c.br.Peek(n)
|
||||
if err == io.EOF {
|
||||
err = errUnexpectedEOF
|
||||
}
|
||||
c.br.Discard(len(p))
|
||||
return p, err
|
||||
}
|
||||
21
vendor/github.com/gorilla/websocket/conn_read_legacy.go
generated
vendored
Normal file
21
vendor/github.com/gorilla/websocket/conn_read_legacy.go
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !go1.5
|
||||
|
||||
package websocket
|
||||
|
||||
import "io"
|
||||
|
||||
func (c *Conn) read(n int) ([]byte, error) {
|
||||
p, err := c.br.Peek(n)
|
||||
if err == io.EOF {
|
||||
err = errUnexpectedEOF
|
||||
}
|
||||
if len(p) > 0 {
|
||||
// advance over the bytes just read
|
||||
io.ReadFull(c.br, p)
|
||||
}
|
||||
return p, err
|
||||
}
|
||||
179
vendor/github.com/gorilla/websocket/doc.go
generated
vendored
Normal file
179
vendor/github.com/gorilla/websocket/doc.go
generated
vendored
Normal file
@@ -0,0 +1,179 @@
|
||||
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package websocket implements the WebSocket protocol defined in RFC 6455.
|
||||
//
|
||||
// Overview
|
||||
//
|
||||
// The Conn type represents a WebSocket connection. A server application calls
|
||||
// the Upgrader.Upgrade method from an HTTP request handler to get a *Conn:
|
||||
//
|
||||
// var upgrader = websocket.Upgrader{
|
||||
// ReadBufferSize: 1024,
|
||||
// WriteBufferSize: 1024,
|
||||
// }
|
||||
//
|
||||
// func handler(w http.ResponseWriter, r *http.Request) {
|
||||
// conn, err := upgrader.Upgrade(w, r, nil)
|
||||
// if err != nil {
|
||||
// log.Println(err)
|
||||
// return
|
||||
// }
|
||||
// ... Use conn to send and receive messages.
|
||||
// }
|
||||
//
|
||||
// Call the connection's WriteMessage and ReadMessage methods to send and
|
||||
// receive messages as a slice of bytes. This snippet of code shows how to echo
|
||||
// messages using these methods:
|
||||
//
|
||||
// for {
|
||||
// messageType, p, err := conn.ReadMessage()
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// if err := conn.WriteMessage(messageType, p); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// In above snippet of code, p is a []byte and messageType is an int with value
|
||||
// websocket.BinaryMessage or websocket.TextMessage.
|
||||
//
|
||||
// An application can also send and receive messages using the io.WriteCloser
|
||||
// and io.Reader interfaces. To send a message, call the connection NextWriter
|
||||
// method to get an io.WriteCloser, write the message to the writer and close
|
||||
// the writer when done. To receive a message, call the connection NextReader
|
||||
// method to get an io.Reader and read until io.EOF is returned. This snippet
|
||||
// shows how to echo messages using the NextWriter and NextReader methods:
|
||||
//
|
||||
// for {
|
||||
// messageType, r, err := conn.NextReader()
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// w, err := conn.NextWriter(messageType)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if _, err := io.Copy(w, r); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if err := w.Close(); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Data Messages
|
||||
//
|
||||
// The WebSocket protocol distinguishes between text and binary data messages.
|
||||
// Text messages are interpreted as UTF-8 encoded text. The interpretation of
|
||||
// binary messages is left to the application.
|
||||
//
|
||||
// This package uses the TextMessage and BinaryMessage integer constants to
|
||||
// identify the two data message types. The ReadMessage and NextReader methods
|
||||
// return the type of the received message. The messageType argument to the
|
||||
// WriteMessage and NextWriter methods specifies the type of a sent message.
|
||||
//
|
||||
// It is the application's responsibility to ensure that text messages are
|
||||
// valid UTF-8 encoded text.
|
||||
//
|
||||
// Control Messages
|
||||
//
|
||||
// The WebSocket protocol defines three types of control messages: close, ping
|
||||
// and pong. Call the connection WriteControl, WriteMessage or NextWriter
|
||||
// methods to send a control message to the peer.
|
||||
//
|
||||
// Connections handle received close messages by sending a close message to the
|
||||
// peer and returning a *CloseError from the the NextReader, ReadMessage or the
|
||||
// message Read method.
|
||||
//
|
||||
// Connections handle received ping and pong messages by invoking callback
|
||||
// functions set with SetPingHandler and SetPongHandler methods. The callback
|
||||
// functions are called from the NextReader, ReadMessage and the message Read
|
||||
// methods.
|
||||
//
|
||||
// The default ping handler sends a pong to the peer. The application's reading
|
||||
// goroutine can block for a short time while the handler writes the pong data
|
||||
// to the connection.
|
||||
//
|
||||
// The application must read the connection to process ping, pong and close
|
||||
// messages sent from the peer. If the application is not otherwise interested
|
||||
// in messages from the peer, then the application should start a goroutine to
|
||||
// read and discard messages from the peer. A simple example is:
|
||||
//
|
||||
// func readLoop(c *websocket.Conn) {
|
||||
// for {
|
||||
// if _, _, err := c.NextReader(); err != nil {
|
||||
// c.Close()
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Concurrency
|
||||
//
|
||||
// Connections support one concurrent reader and one concurrent writer.
|
||||
//
|
||||
// Applications are responsible for ensuring that no more than one goroutine
|
||||
// calls the write methods (NextWriter, SetWriteDeadline, WriteMessage,
|
||||
// WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and
|
||||
// that no more than one goroutine calls the read methods (NextReader,
|
||||
// SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler)
|
||||
// concurrently.
|
||||
//
|
||||
// The Close and WriteControl methods can be called concurrently with all other
|
||||
// methods.
|
||||
//
|
||||
// Origin Considerations
|
||||
//
|
||||
// Web browsers allow Javascript applications to open a WebSocket connection to
|
||||
// any host. It's up to the server to enforce an origin policy using the Origin
|
||||
// request header sent by the browser.
|
||||
//
|
||||
// The Upgrader calls the function specified in the CheckOrigin field to check
|
||||
// the origin. If the CheckOrigin function returns false, then the Upgrade
|
||||
// method fails the WebSocket handshake with HTTP status 403.
|
||||
//
|
||||
// If the CheckOrigin field is nil, then the Upgrader uses a safe default: fail
|
||||
// the handshake if the Origin request header is present and not equal to the
|
||||
// Host request header.
|
||||
//
|
||||
// An application can allow connections from any origin by specifying a
|
||||
// function that always returns true:
|
||||
//
|
||||
// var upgrader = websocket.Upgrader{
|
||||
// CheckOrigin: func(r *http.Request) bool { return true },
|
||||
// }
|
||||
//
|
||||
// The deprecated package-level Upgrade function does not perform origin
|
||||
// checking. The application is responsible for checking the Origin header
|
||||
// before calling the Upgrade function.
|
||||
//
|
||||
// Compression EXPERIMENTAL
|
||||
//
|
||||
// Per message compression extensions (RFC 7692) are experimentally supported
|
||||
// by this package in a limited capacity. Setting the EnableCompression option
|
||||
// to true in Dialer or Upgrader will attempt to negotiate per message deflate
|
||||
// support.
|
||||
//
|
||||
// var upgrader = websocket.Upgrader{
|
||||
// EnableCompression: true,
|
||||
// }
|
||||
//
|
||||
// If compression was successfully negotiated with the connection's peer, any
|
||||
// message received in compressed form will be automatically decompressed.
|
||||
// All Read methods will return uncompressed bytes.
|
||||
//
|
||||
// Per message compression of messages written to a connection can be enabled
|
||||
// or disabled by calling the corresponding Conn method:
|
||||
//
|
||||
// conn.EnableWriteCompression(false)
|
||||
//
|
||||
// Currently this package does not support compression with "context takeover".
|
||||
// This means that messages must be compressed and decompressed in isolation,
|
||||
// without retaining sliding window or dictionary state across messages. For
|
||||
// more details refer to RFC 7692.
|
||||
//
|
||||
// Use of compression is experimental and may result in decreased performance.
|
||||
package websocket
|
||||
60
vendor/github.com/gorilla/websocket/json.go
generated
vendored
Normal file
60
vendor/github.com/gorilla/websocket/json.go
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
)
|
||||
|
||||
// WriteJSON writes the JSON encoding of v as a message.
|
||||
//
|
||||
// Deprecated: Use c.WriteJSON instead.
|
||||
func WriteJSON(c *Conn, v interface{}) error {
|
||||
return c.WriteJSON(v)
|
||||
}
|
||||
|
||||
// WriteJSON writes the JSON encoding of v as a message.
|
||||
//
|
||||
// See the documentation for encoding/json Marshal for details about the
|
||||
// conversion of Go values to JSON.
|
||||
func (c *Conn) WriteJSON(v interface{}) error {
|
||||
w, err := c.NextWriter(TextMessage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err1 := json.NewEncoder(w).Encode(v)
|
||||
err2 := w.Close()
|
||||
if err1 != nil {
|
||||
return err1
|
||||
}
|
||||
return err2
|
||||
}
|
||||
|
||||
// ReadJSON reads the next JSON-encoded message from the connection and stores
|
||||
// it in the value pointed to by v.
|
||||
//
|
||||
// Deprecated: Use c.ReadJSON instead.
|
||||
func ReadJSON(c *Conn, v interface{}) error {
|
||||
return c.ReadJSON(v)
|
||||
}
|
||||
|
||||
// ReadJSON reads the next JSON-encoded message from the connection and stores
|
||||
// it in the value pointed to by v.
|
||||
//
|
||||
// See the documentation for the encoding/json Unmarshal function for details
|
||||
// about the conversion of JSON to a Go value.
|
||||
func (c *Conn) ReadJSON(v interface{}) error {
|
||||
_, r, err := c.NextReader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.NewDecoder(r).Decode(v)
|
||||
if err == io.EOF {
|
||||
// One value is expected in the message.
|
||||
err = io.ErrUnexpectedEOF
|
||||
}
|
||||
return err
|
||||
}
|
||||
55
vendor/github.com/gorilla/websocket/mask.go
generated
vendored
Normal file
55
vendor/github.com/gorilla/websocket/mask.go
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved. Use of
|
||||
// this source code is governed by a BSD-style license that can be found in the
|
||||
// LICENSE file.
|
||||
|
||||
// +build !appengine
|
||||
|
||||
package websocket
|
||||
|
||||
import "unsafe"
|
||||
|
||||
const wordSize = int(unsafe.Sizeof(uintptr(0)))
|
||||
|
||||
func maskBytes(key [4]byte, pos int, b []byte) int {
|
||||
|
||||
// Mask one byte at a time for small buffers.
|
||||
if len(b) < 2*wordSize {
|
||||
for i := range b {
|
||||
b[i] ^= key[pos&3]
|
||||
pos++
|
||||
}
|
||||
return pos & 3
|
||||
}
|
||||
|
||||
// Mask one byte at a time to word boundary.
|
||||
if n := int(uintptr(unsafe.Pointer(&b[0]))) % wordSize; n != 0 {
|
||||
n = wordSize - n
|
||||
for i := range b[:n] {
|
||||
b[i] ^= key[pos&3]
|
||||
pos++
|
||||
}
|
||||
b = b[n:]
|
||||
}
|
||||
|
||||
// Create aligned word size key.
|
||||
var k [wordSize]byte
|
||||
for i := range k {
|
||||
k[i] = key[(pos+i)&3]
|
||||
}
|
||||
kw := *(*uintptr)(unsafe.Pointer(&k))
|
||||
|
||||
// Mask one word at a time.
|
||||
n := (len(b) / wordSize) * wordSize
|
||||
for i := 0; i < n; i += wordSize {
|
||||
*(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) + uintptr(i))) ^= kw
|
||||
}
|
||||
|
||||
// Mask one byte at a time for remaining bytes.
|
||||
b = b[n:]
|
||||
for i := range b {
|
||||
b[i] ^= key[pos&3]
|
||||
pos++
|
||||
}
|
||||
|
||||
return pos & 3
|
||||
}
|
||||
15
vendor/github.com/gorilla/websocket/mask_safe.go
generated
vendored
Normal file
15
vendor/github.com/gorilla/websocket/mask_safe.go
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved. Use of
|
||||
// this source code is governed by a BSD-style license that can be found in the
|
||||
// LICENSE file.
|
||||
|
||||
// +build appengine
|
||||
|
||||
package websocket
|
||||
|
||||
func maskBytes(key [4]byte, pos int, b []byte) int {
|
||||
for i := range b {
|
||||
b[i] ^= key[pos&3]
|
||||
pos++
|
||||
}
|
||||
return pos & 3
|
||||
}
|
||||
103
vendor/github.com/gorilla/websocket/prepared.go
generated
vendored
Normal file
103
vendor/github.com/gorilla/websocket/prepared.go
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
// Copyright 2017 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PreparedMessage caches on the wire representations of a message payload.
|
||||
// Use PreparedMessage to efficiently send a message payload to multiple
|
||||
// connections. PreparedMessage is especially useful when compression is used
|
||||
// because the CPU and memory expensive compression operation can be executed
|
||||
// once for a given set of compression options.
|
||||
type PreparedMessage struct {
|
||||
messageType int
|
||||
data []byte
|
||||
err error
|
||||
mu sync.Mutex
|
||||
frames map[prepareKey]*preparedFrame
|
||||
}
|
||||
|
||||
// prepareKey defines a unique set of options to cache prepared frames in PreparedMessage.
|
||||
type prepareKey struct {
|
||||
isServer bool
|
||||
compress bool
|
||||
compressionLevel int
|
||||
}
|
||||
|
||||
// preparedFrame contains data in wire representation.
|
||||
type preparedFrame struct {
|
||||
once sync.Once
|
||||
data []byte
|
||||
}
|
||||
|
||||
// NewPreparedMessage returns an initialized PreparedMessage. You can then send
|
||||
// it to connection using WritePreparedMessage method. Valid wire
|
||||
// representation will be calculated lazily only once for a set of current
|
||||
// connection options.
|
||||
func NewPreparedMessage(messageType int, data []byte) (*PreparedMessage, error) {
|
||||
pm := &PreparedMessage{
|
||||
messageType: messageType,
|
||||
frames: make(map[prepareKey]*preparedFrame),
|
||||
data: data,
|
||||
}
|
||||
|
||||
// Prepare a plain server frame.
|
||||
_, frameData, err := pm.frame(prepareKey{isServer: true, compress: false})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// To protect against caller modifying the data argument, remember the data
|
||||
// copied to the plain server frame.
|
||||
pm.data = frameData[len(frameData)-len(data):]
|
||||
return pm, nil
|
||||
}
|
||||
|
||||
func (pm *PreparedMessage) frame(key prepareKey) (int, []byte, error) {
|
||||
pm.mu.Lock()
|
||||
frame, ok := pm.frames[key]
|
||||
if !ok {
|
||||
frame = &preparedFrame{}
|
||||
pm.frames[key] = frame
|
||||
}
|
||||
pm.mu.Unlock()
|
||||
|
||||
var err error
|
||||
frame.once.Do(func() {
|
||||
// Prepare a frame using a 'fake' connection.
|
||||
// TODO: Refactor code in conn.go to allow more direct construction of
|
||||
// the frame.
|
||||
mu := make(chan bool, 1)
|
||||
mu <- true
|
||||
var nc prepareConn
|
||||
c := &Conn{
|
||||
conn: &nc,
|
||||
mu: mu,
|
||||
isServer: key.isServer,
|
||||
compressionLevel: key.compressionLevel,
|
||||
enableWriteCompression: true,
|
||||
writeBuf: make([]byte, defaultWriteBufferSize+maxFrameHeaderSize),
|
||||
}
|
||||
if key.compress {
|
||||
c.newCompressionWriter = compressNoContextTakeover
|
||||
}
|
||||
err = c.WriteMessage(pm.messageType, pm.data)
|
||||
frame.data = nc.buf.Bytes()
|
||||
})
|
||||
return pm.messageType, frame.data, err
|
||||
}
|
||||
|
||||
type prepareConn struct {
|
||||
buf bytes.Buffer
|
||||
net.Conn
|
||||
}
|
||||
|
||||
func (pc *prepareConn) Write(p []byte) (int, error) { return pc.buf.Write(p) }
|
||||
func (pc *prepareConn) SetWriteDeadline(t time.Time) error { return nil }
|
||||
292
vendor/github.com/gorilla/websocket/server.go
generated
vendored
Normal file
292
vendor/github.com/gorilla/websocket/server.go
generated
vendored
Normal file
@@ -0,0 +1,292 @@
|
||||
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HandshakeError describes an error with the handshake from the peer.
|
||||
type HandshakeError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e HandshakeError) Error() string { return e.message }
|
||||
|
||||
// Upgrader specifies parameters for upgrading an HTTP connection to a
|
||||
// WebSocket connection.
|
||||
type Upgrader struct {
|
||||
// HandshakeTimeout specifies the duration for the handshake to complete.
|
||||
HandshakeTimeout time.Duration
|
||||
|
||||
// ReadBufferSize and WriteBufferSize specify I/O buffer sizes. If a buffer
|
||||
// size is zero, then buffers allocated by the HTTP server are used. The
|
||||
// I/O buffer sizes do not limit the size of the messages that can be sent
|
||||
// or received.
|
||||
ReadBufferSize, WriteBufferSize int
|
||||
|
||||
// Subprotocols specifies the server's supported protocols in order of
|
||||
// preference. If this field is set, then the Upgrade method negotiates a
|
||||
// subprotocol by selecting the first match in this list with a protocol
|
||||
// requested by the client.
|
||||
Subprotocols []string
|
||||
|
||||
// Error specifies the function for generating HTTP error responses. If Error
|
||||
// is nil, then http.Error is used to generate the HTTP response.
|
||||
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
|
||||
|
||||
// CheckOrigin returns true if the request Origin header is acceptable. If
|
||||
// CheckOrigin is nil, the host in the Origin header must not be set or
|
||||
// must match the host of the request.
|
||||
CheckOrigin func(r *http.Request) bool
|
||||
|
||||
// EnableCompression specify if the server should attempt to negotiate per
|
||||
// message compression (RFC 7692). Setting this value to true does not
|
||||
// guarantee that compression will be supported. Currently only "no context
|
||||
// takeover" modes are supported.
|
||||
EnableCompression bool
|
||||
}
|
||||
|
||||
func (u *Upgrader) returnError(w http.ResponseWriter, r *http.Request, status int, reason string) (*Conn, error) {
|
||||
err := HandshakeError{reason}
|
||||
if u.Error != nil {
|
||||
u.Error(w, r, status, err)
|
||||
} else {
|
||||
w.Header().Set("Sec-Websocket-Version", "13")
|
||||
http.Error(w, http.StatusText(status), status)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// checkSameOrigin returns true if the origin is not set or is equal to the request host.
|
||||
func checkSameOrigin(r *http.Request) bool {
|
||||
origin := r.Header["Origin"]
|
||||
if len(origin) == 0 {
|
||||
return true
|
||||
}
|
||||
u, err := url.Parse(origin[0])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return u.Host == r.Host
|
||||
}
|
||||
|
||||
func (u *Upgrader) selectSubprotocol(r *http.Request, responseHeader http.Header) string {
|
||||
if u.Subprotocols != nil {
|
||||
clientProtocols := Subprotocols(r)
|
||||
for _, serverProtocol := range u.Subprotocols {
|
||||
for _, clientProtocol := range clientProtocols {
|
||||
if clientProtocol == serverProtocol {
|
||||
return clientProtocol
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if responseHeader != nil {
|
||||
return responseHeader.Get("Sec-Websocket-Protocol")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Upgrade upgrades the HTTP server connection to the WebSocket protocol.
|
||||
//
|
||||
// The responseHeader is included in the response to the client's upgrade
|
||||
// request. Use the responseHeader to specify cookies (Set-Cookie) and the
|
||||
// application negotiated subprotocol (Sec-Websocket-Protocol).
|
||||
//
|
||||
// If the upgrade fails, then Upgrade replies to the client with an HTTP error
|
||||
// response.
|
||||
func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) {
|
||||
if r.Method != "GET" {
|
||||
return u.returnError(w, r, http.StatusMethodNotAllowed, "websocket: not a websocket handshake: request method is not GET")
|
||||
}
|
||||
|
||||
if _, ok := responseHeader["Sec-Websocket-Extensions"]; ok {
|
||||
return u.returnError(w, r, http.StatusInternalServerError, "websocket: application specific 'Sec-Websocket-Extensions' headers are unsupported")
|
||||
}
|
||||
|
||||
if !tokenListContainsValue(r.Header, "Connection", "upgrade") {
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: 'upgrade' token not found in 'Connection' header")
|
||||
}
|
||||
|
||||
if !tokenListContainsValue(r.Header, "Upgrade", "websocket") {
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: 'websocket' token not found in 'Upgrade' header")
|
||||
}
|
||||
|
||||
if !tokenListContainsValue(r.Header, "Sec-Websocket-Version", "13") {
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: unsupported version: 13 not found in 'Sec-Websocket-Version' header")
|
||||
}
|
||||
|
||||
checkOrigin := u.CheckOrigin
|
||||
if checkOrigin == nil {
|
||||
checkOrigin = checkSameOrigin
|
||||
}
|
||||
if !checkOrigin(r) {
|
||||
return u.returnError(w, r, http.StatusForbidden, "websocket: 'Origin' header value not allowed")
|
||||
}
|
||||
|
||||
challengeKey := r.Header.Get("Sec-Websocket-Key")
|
||||
if challengeKey == "" {
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: `Sec-Websocket-Key' header is missing or blank")
|
||||
}
|
||||
|
||||
subprotocol := u.selectSubprotocol(r, responseHeader)
|
||||
|
||||
// Negotiate PMCE
|
||||
var compress bool
|
||||
if u.EnableCompression {
|
||||
for _, ext := range parseExtensions(r.Header) {
|
||||
if ext[""] != "permessage-deflate" {
|
||||
continue
|
||||
}
|
||||
compress = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
netConn net.Conn
|
||||
err error
|
||||
)
|
||||
|
||||
h, ok := w.(http.Hijacker)
|
||||
if !ok {
|
||||
return u.returnError(w, r, http.StatusInternalServerError, "websocket: response does not implement http.Hijacker")
|
||||
}
|
||||
var brw *bufio.ReadWriter
|
||||
netConn, brw, err = h.Hijack()
|
||||
if err != nil {
|
||||
return u.returnError(w, r, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if brw.Reader.Buffered() > 0 {
|
||||
netConn.Close()
|
||||
return nil, errors.New("websocket: client sent data before handshake is complete")
|
||||
}
|
||||
|
||||
c := newConnBRW(netConn, true, u.ReadBufferSize, u.WriteBufferSize, brw)
|
||||
c.subprotocol = subprotocol
|
||||
|
||||
if compress {
|
||||
c.newCompressionWriter = compressNoContextTakeover
|
||||
c.newDecompressionReader = decompressNoContextTakeover
|
||||
}
|
||||
|
||||
p := c.writeBuf[:0]
|
||||
p = append(p, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "...)
|
||||
p = append(p, computeAcceptKey(challengeKey)...)
|
||||
p = append(p, "\r\n"...)
|
||||
if c.subprotocol != "" {
|
||||
p = append(p, "Sec-Websocket-Protocol: "...)
|
||||
p = append(p, c.subprotocol...)
|
||||
p = append(p, "\r\n"...)
|
||||
}
|
||||
if compress {
|
||||
p = append(p, "Sec-Websocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover\r\n"...)
|
||||
}
|
||||
for k, vs := range responseHeader {
|
||||
if k == "Sec-Websocket-Protocol" {
|
||||
continue
|
||||
}
|
||||
for _, v := range vs {
|
||||
p = append(p, k...)
|
||||
p = append(p, ": "...)
|
||||
for i := 0; i < len(v); i++ {
|
||||
b := v[i]
|
||||
if b <= 31 {
|
||||
// prevent response splitting.
|
||||
b = ' '
|
||||
}
|
||||
p = append(p, b)
|
||||
}
|
||||
p = append(p, "\r\n"...)
|
||||
}
|
||||
}
|
||||
p = append(p, "\r\n"...)
|
||||
|
||||
// Clear deadlines set by HTTP server.
|
||||
netConn.SetDeadline(time.Time{})
|
||||
|
||||
if u.HandshakeTimeout > 0 {
|
||||
netConn.SetWriteDeadline(time.Now().Add(u.HandshakeTimeout))
|
||||
}
|
||||
if _, err = netConn.Write(p); err != nil {
|
||||
netConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
if u.HandshakeTimeout > 0 {
|
||||
netConn.SetWriteDeadline(time.Time{})
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Upgrade upgrades the HTTP server connection to the WebSocket protocol.
|
||||
//
|
||||
// Deprecated: Use websocket.Upgrader instead.
|
||||
//
|
||||
// Upgrade does not perform origin checking. The application is responsible for
|
||||
// checking the Origin header before calling Upgrade. An example implementation
|
||||
// of the same origin policy check is:
|
||||
//
|
||||
// if req.Header.Get("Origin") != "http://"+req.Host {
|
||||
// http.Error(w, "Origin not allowed", 403)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// If the endpoint supports subprotocols, then the application is responsible
|
||||
// for negotiating the protocol used on the connection. Use the Subprotocols()
|
||||
// function to get the subprotocols requested by the client. Use the
|
||||
// Sec-Websocket-Protocol response header to specify the subprotocol selected
|
||||
// by the application.
|
||||
//
|
||||
// The responseHeader is included in the response to the client's upgrade
|
||||
// request. Use the responseHeader to specify cookies (Set-Cookie) and the
|
||||
// negotiated subprotocol (Sec-Websocket-Protocol).
|
||||
//
|
||||
// The connection buffers IO to the underlying network connection. The
|
||||
// readBufSize and writeBufSize parameters specify the size of the buffers to
|
||||
// use. Messages can be larger than the buffers.
|
||||
//
|
||||
// If the request is not a valid WebSocket handshake, then Upgrade returns an
|
||||
// error of type HandshakeError. Applications should handle this error by
|
||||
// replying to the client with an HTTP error response.
|
||||
func Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, readBufSize, writeBufSize int) (*Conn, error) {
|
||||
u := Upgrader{ReadBufferSize: readBufSize, WriteBufferSize: writeBufSize}
|
||||
u.Error = func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
||||
// don't return errors to maintain backwards compatibility
|
||||
}
|
||||
u.CheckOrigin = func(r *http.Request) bool {
|
||||
// allow all connections by default
|
||||
return true
|
||||
}
|
||||
return u.Upgrade(w, r, responseHeader)
|
||||
}
|
||||
|
||||
// Subprotocols returns the subprotocols requested by the client in the
|
||||
// Sec-Websocket-Protocol header.
|
||||
func Subprotocols(r *http.Request) []string {
|
||||
h := strings.TrimSpace(r.Header.Get("Sec-Websocket-Protocol"))
|
||||
if h == "" {
|
||||
return nil
|
||||
}
|
||||
protocols := strings.Split(h, ",")
|
||||
for i := range protocols {
|
||||
protocols[i] = strings.TrimSpace(protocols[i])
|
||||
}
|
||||
return protocols
|
||||
}
|
||||
|
||||
// IsWebSocketUpgrade returns true if the client requested upgrade to the
|
||||
// WebSocket protocol.
|
||||
func IsWebSocketUpgrade(r *http.Request) bool {
|
||||
return tokenListContainsValue(r.Header, "Connection", "upgrade") &&
|
||||
tokenListContainsValue(r.Header, "Upgrade", "websocket")
|
||||
}
|
||||
214
vendor/github.com/gorilla/websocket/util.go
generated
vendored
Normal file
214
vendor/github.com/gorilla/websocket/util.go
generated
vendored
Normal file
@@ -0,0 +1,214 @@
|
||||
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
|
||||
|
||||
func computeAcceptKey(challengeKey string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(challengeKey))
|
||||
h.Write(keyGUID)
|
||||
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func generateChallengeKey() (string, error) {
|
||||
p := make([]byte, 16)
|
||||
if _, err := io.ReadFull(rand.Reader, p); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(p), nil
|
||||
}
|
||||
|
||||
// Octet types from RFC 2616.
|
||||
var octetTypes [256]byte
|
||||
|
||||
const (
|
||||
isTokenOctet = 1 << iota
|
||||
isSpaceOctet
|
||||
)
|
||||
|
||||
func init() {
|
||||
// From RFC 2616
|
||||
//
|
||||
// OCTET = <any 8-bit sequence of data>
|
||||
// CHAR = <any US-ASCII character (octets 0 - 127)>
|
||||
// CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
|
||||
// CR = <US-ASCII CR, carriage return (13)>
|
||||
// LF = <US-ASCII LF, linefeed (10)>
|
||||
// SP = <US-ASCII SP, space (32)>
|
||||
// HT = <US-ASCII HT, horizontal-tab (9)>
|
||||
// <"> = <US-ASCII double-quote mark (34)>
|
||||
// CRLF = CR LF
|
||||
// LWS = [CRLF] 1*( SP | HT )
|
||||
// TEXT = <any OCTET except CTLs, but including LWS>
|
||||
// separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <">
|
||||
// | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT
|
||||
// token = 1*<any CHAR except CTLs or separators>
|
||||
// qdtext = <any TEXT except <">>
|
||||
|
||||
for c := 0; c < 256; c++ {
|
||||
var t byte
|
||||
isCtl := c <= 31 || c == 127
|
||||
isChar := 0 <= c && c <= 127
|
||||
isSeparator := strings.IndexRune(" \t\"(),/:;<=>?@[]\\{}", rune(c)) >= 0
|
||||
if strings.IndexRune(" \t\r\n", rune(c)) >= 0 {
|
||||
t |= isSpaceOctet
|
||||
}
|
||||
if isChar && !isCtl && !isSeparator {
|
||||
t |= isTokenOctet
|
||||
}
|
||||
octetTypes[c] = t
|
||||
}
|
||||
}
|
||||
|
||||
func skipSpace(s string) (rest string) {
|
||||
i := 0
|
||||
for ; i < len(s); i++ {
|
||||
if octetTypes[s[i]]&isSpaceOctet == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return s[i:]
|
||||
}
|
||||
|
||||
func nextToken(s string) (token, rest string) {
|
||||
i := 0
|
||||
for ; i < len(s); i++ {
|
||||
if octetTypes[s[i]]&isTokenOctet == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return s[:i], s[i:]
|
||||
}
|
||||
|
||||
func nextTokenOrQuoted(s string) (value string, rest string) {
|
||||
if !strings.HasPrefix(s, "\"") {
|
||||
return nextToken(s)
|
||||
}
|
||||
s = s[1:]
|
||||
for i := 0; i < len(s); i++ {
|
||||
switch s[i] {
|
||||
case '"':
|
||||
return s[:i], s[i+1:]
|
||||
case '\\':
|
||||
p := make([]byte, len(s)-1)
|
||||
j := copy(p, s[:i])
|
||||
escape := true
|
||||
for i = i + 1; i < len(s); i++ {
|
||||
b := s[i]
|
||||
switch {
|
||||
case escape:
|
||||
escape = false
|
||||
p[j] = b
|
||||
j++
|
||||
case b == '\\':
|
||||
escape = true
|
||||
case b == '"':
|
||||
return string(p[:j]), s[i+1:]
|
||||
default:
|
||||
p[j] = b
|
||||
j++
|
||||
}
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// tokenListContainsValue returns true if the 1#token header with the given
|
||||
// name contains token.
|
||||
func tokenListContainsValue(header http.Header, name string, value string) bool {
|
||||
headers:
|
||||
for _, s := range header[name] {
|
||||
for {
|
||||
var t string
|
||||
t, s = nextToken(skipSpace(s))
|
||||
if t == "" {
|
||||
continue headers
|
||||
}
|
||||
s = skipSpace(s)
|
||||
if s != "" && s[0] != ',' {
|
||||
continue headers
|
||||
}
|
||||
if strings.EqualFold(t, value) {
|
||||
return true
|
||||
}
|
||||
if s == "" {
|
||||
continue headers
|
||||
}
|
||||
s = s[1:]
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// parseExtensiosn parses WebSocket extensions from a header.
|
||||
func parseExtensions(header http.Header) []map[string]string {
|
||||
|
||||
// From RFC 6455:
|
||||
//
|
||||
// Sec-WebSocket-Extensions = extension-list
|
||||
// extension-list = 1#extension
|
||||
// extension = extension-token *( ";" extension-param )
|
||||
// extension-token = registered-token
|
||||
// registered-token = token
|
||||
// extension-param = token [ "=" (token | quoted-string) ]
|
||||
// ;When using the quoted-string syntax variant, the value
|
||||
// ;after quoted-string unescaping MUST conform to the
|
||||
// ;'token' ABNF.
|
||||
|
||||
var result []map[string]string
|
||||
headers:
|
||||
for _, s := range header["Sec-Websocket-Extensions"] {
|
||||
for {
|
||||
var t string
|
||||
t, s = nextToken(skipSpace(s))
|
||||
if t == "" {
|
||||
continue headers
|
||||
}
|
||||
ext := map[string]string{"": t}
|
||||
for {
|
||||
s = skipSpace(s)
|
||||
if !strings.HasPrefix(s, ";") {
|
||||
break
|
||||
}
|
||||
var k string
|
||||
k, s = nextToken(skipSpace(s[1:]))
|
||||
if k == "" {
|
||||
continue headers
|
||||
}
|
||||
s = skipSpace(s)
|
||||
var v string
|
||||
if strings.HasPrefix(s, "=") {
|
||||
v, s = nextTokenOrQuoted(skipSpace(s[1:]))
|
||||
s = skipSpace(s)
|
||||
}
|
||||
if s != "" && s[0] != ',' && s[0] != ';' {
|
||||
continue headers
|
||||
}
|
||||
ext[k] = v
|
||||
}
|
||||
if s != "" && s[0] != ',' {
|
||||
continue headers
|
||||
}
|
||||
result = append(result, ext)
|
||||
if s == "" {
|
||||
continue headers
|
||||
}
|
||||
s = s[1:]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user