github.com/secure-build/gitlab-runner@v12.5.0+incompatible/session/proxy/proxy.go (about)

     1  package proxy
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  	"strconv"
     7  )
     8  
     9  type Pool map[string]*Proxy
    10  
    11  type Pooler interface {
    12  	Pool() Pool
    13  }
    14  
    15  type Proxy struct {
    16  	Settings          *Settings
    17  	ConnectionHandler Requester
    18  }
    19  
    20  type Settings struct {
    21  	ServiceName string
    22  	Ports       []Port
    23  }
    24  
    25  type Port struct {
    26  	Number   int
    27  	Protocol string
    28  	Name     string
    29  }
    30  
    31  type Requester interface {
    32  	ProxyRequest(w http.ResponseWriter, r *http.Request, requestedURI, port string, settings *Settings)
    33  }
    34  
    35  func NewPool() Pool {
    36  	return Pool{}
    37  }
    38  
    39  func NewProxySettings(serviceName string, ports []Port) *Settings {
    40  	return &Settings{
    41  		ServiceName: serviceName,
    42  		Ports:       ports,
    43  	}
    44  }
    45  
    46  // PortByNameOrNumber accepts both a port number or a port name.
    47  // It will try to convert the method into an integer and then
    48  // search if there is any port number with that value or any
    49  // port name by the param value.
    50  func (p *Settings) PortByNameOrNumber(portNameOrNumber string) (Port, error) {
    51  	intPort, _ := strconv.Atoi(portNameOrNumber)
    52  
    53  	for _, port := range p.Ports {
    54  		if port.Number == intPort || port.Name == portNameOrNumber {
    55  			return port, nil
    56  		}
    57  	}
    58  
    59  	return Port{}, errors.New("invalid port")
    60  }
    61  
    62  func (p *Port) Scheme() (string, error) {
    63  	if p.Protocol == "http" || p.Protocol == "https" {
    64  		return p.Protocol, nil
    65  	}
    66  
    67  	return "", errors.New("invalid port scheme")
    68  }
    69  
    70  // WebsocketProtocolFor returns the proper Websocket protocol
    71  // based on the HTTP protocol
    72  func WebsocketProtocolFor(httpProtocol string) string {
    73  	if httpProtocol == "https" {
    74  		return "wss"
    75  	}
    76  
    77  	return "ws"
    78  }