github.com/erda-project/erda-infra@v1.0.9/providers/remote-forward/protocol.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package forward
    16  
    17  import (
    18  	"encoding/gob"
    19  	"io"
    20  	"time"
    21  )
    22  
    23  const (
    24  	// ProtocolVersion .
    25  	ProtocolVersion = 1
    26  	// HandshakeTimeout .
    27  	HandshakeTimeout = 60 * time.Second
    28  )
    29  
    30  func init() {
    31  	gob.Register(&RequestHeader{})
    32  	gob.Register(&ResponseHeader{})
    33  }
    34  
    35  // RequestHeader .
    36  type RequestHeader struct {
    37  	Version    uint32
    38  	Name       string
    39  	Token      string
    40  	ShadowAddr string
    41  }
    42  
    43  // DecodeRequestHeader .
    44  func DecodeRequestHeader(r io.Reader) (*RequestHeader, error) {
    45  	dec := gob.NewDecoder(r)
    46  	h := &RequestHeader{}
    47  	err := dec.Decode(h)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return h, nil
    52  }
    53  
    54  // EncodeRequestHeader .
    55  func EncodeRequestHeader(w io.Writer, h *RequestHeader) error {
    56  	enc := gob.NewEncoder(w)
    57  	return enc.Encode(h)
    58  }
    59  
    60  // ResponseHeader .
    61  type ResponseHeader struct {
    62  	ShadowAddr string
    63  	Error      string
    64  	Values     map[string]interface{}
    65  }
    66  
    67  // DecodeResponseHeader .
    68  func DecodeResponseHeader(r io.Reader) (*ResponseHeader, error) {
    69  	dec := gob.NewDecoder(r)
    70  	h := &ResponseHeader{}
    71  	err := dec.Decode(h)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	return h, nil
    76  }
    77  
    78  // EncodeResponseHeader .
    79  func EncodeResponseHeader(w io.Writer, h *ResponseHeader) error {
    80  	enc := gob.NewEncoder(w)
    81  	return enc.Encode(h)
    82  }