17
"github.com/containers/common/libnetwork/types"
18
"github.com/containers/common/pkg/machine"
19
"github.com/sirupsen/logrus"
22
const machineGvproxyEndpoint = "gateway.containers.internal"
24
// machineExpose is the struct for the gvproxy port forwarding api send via json
25
type machineExpose struct {
26
// Local is the local address on the vm host, format is ip:port
27
Local string `json:"local"`
28
// Remote is used to specify the vm ip:port
29
Remote string `json:"remote,omitempty"`
30
// Protocol to forward, tcp or udp
31
Protocol string `json:"protocol"`
34
func requestMachinePorts(expose bool, ports []types.PortMapping) error {
35
url := "http://" + machineGvproxyEndpoint + "/services/forwarder/"
41
ctx := context.Background()
42
client := &http.Client{
43
Transport: &http.Transport{
44
// make sure to not set a proxy here so explicitly ignore the proxy
45
// since we want to talk directly to gvproxy
46
// https://github.com/containers/podman/issues/13628
49
IdleConnTimeout: 30 * time.Second,
50
TLSHandshakeTimeout: 10 * time.Second,
51
ExpectContinueTimeout: 1 * time.Second,
54
buf := new(bytes.Buffer)
55
for num, port := range ports {
56
protocols := strings.Split(port.Protocol, ",")
57
for _, protocol := range protocols {
58
for i := uint16(0); i < port.Range; i++ {
59
machinePort := machineExpose{
60
Local: net.JoinHostPort(port.HostIP, strconv.FormatInt(int64(port.HostPort+i), 10)),
64
// only set the remote port the ip will be automatically be set by gvproxy
65
machinePort.Remote = ":" + strconv.FormatInt(int64(port.HostPort+i), 10)
69
if err := json.NewEncoder(buf).Encode(machinePort); err != nil {
71
// in case of an error make sure to unexpose the other ports
72
if cerr := requestMachinePorts(false, ports[:num]); cerr != nil {
73
logrus.Errorf("failed to free gvproxy machine ports: %v", cerr)
78
if err := makeMachineRequest(ctx, client, url, buf); err != nil {
80
// in case of an error make sure to unexpose the other ports
81
if cerr := requestMachinePorts(false, ports[:num]); cerr != nil {
82
logrus.Errorf("failed to free gvproxy machine ports: %v", cerr)
94
func makeMachineRequest(ctx context.Context, client *http.Client, url string, buf io.Reader) error {
95
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
99
req.Header.Add("Accept", "application/json")
100
req.Header.Add("Content-Type", "application/json")
101
resp, err := client.Do(req)
105
defer resp.Body.Close()
106
if resp.StatusCode != http.StatusOK {
107
return annotateGvproxyResponseError(resp.Body)
112
func annotateGvproxyResponseError(r io.Reader) error {
113
b, err := io.ReadAll(r)
114
if err == nil && len(b) > 0 {
115
return fmt.Errorf("something went wrong with the request: %q", string(b))
117
return errors.New("something went wrong with the request, could not read response")
120
// exposeMachinePorts exposes the ports for podman machine via gvproxy
121
func (r *Runtime) exposeMachinePorts(ports []types.PortMapping) error {
122
if !machine.IsGvProxyBased() {
125
return requestMachinePorts(true, ports)
128
// unexposeMachinePorts closes the ports for podman machine via gvproxy
129
func (r *Runtime) unexposeMachinePorts(ports []types.PortMapping) error {
130
if !machine.IsGvProxyBased() {
133
return requestMachinePorts(false, ports)