> For the complete documentation index, see [llms.txt](https://thorstenkramm.gitbook.io/saltstack/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thorstenkramm.gitbook.io/saltstack/salt-cloud/dynamische-portforwarding.md).

# Dynamische Portforwarding

### Gobetween auf dem Host (Wirt) installieren

```
curl -L -O https://github.com/yyyar/gobetween/releases/download/0.6.1/gobetween_0.6.1_linux_amd64.tar.gz
tar xzf gobetween_0.6.1_linux_amd64.tar.gz 
mv gobetween /usr/local/sbin/
mkdir /etc/gobetween
mv config/gobetween.toml /etc/gobetween/config.example.toml
```

{% code title="/etc/systemd/system/gobetween.service " %}

```
[Unit]
Description=TCP/UDP Forwarding and loadbalancing service
ConditionFileIsExecutable=/usr/local/sbin/gobetween

[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/local/sbin/gobetween "-c" "/etc/gobetween/gobetween.toml"

User=root

Restart=always
RestartSec=120
EnvironmentFile=-/etc/sysconfig/gobetween

[Install]
WantedBy=multi-user.target


```

{% endcode %}

Konfigurieren Sie die gobetween HTTP API, so dass Salt-Minions dynamische Portforwardings per API anlegen können.

{% code title="/etc/gobetween/gobetween.toml" %}

```
[logging]
level = "info"
output = "/var/log/gobetween.log"

[defaults]
max_connections = 0              # Maximum simultaneous connections to the server
client_idle_timeout = "0"        # Client inactivity duration before forced connection drop
backend_idle_timeout = "0"       # Backend inactivity duration before forced connection drop
backend_connection_timeout = "0" # Backend connection timeout (ignored in udp)[servers]

[api]
enabled = true  # true | false
bind = "192.168.122.1:1500"  # "host:port"
cors = false    # cross-origin resource sharing
```

{% endcode %}

Starten Sie gobetween mit `systemctl start gobetween`.

### Firewall anpassen

Falls eine Firewall aktiv ist, erlauben Sie gobetween die Nutzung einer Portrange und öffnen Sie den API port

```bash
ufw allow 30000:30300/tcp 
ufw allow from 192.168.122.0/24 to any port 1500
ufw reload
```

### Portforwarding per Salt-State ausrollen

{% code title="/srv/salt/vminit/portforwarding.sls" %}

```yaml
#
# Create a port forwarding to a vm using gobetween on the default gw
#


# Copy the gobetween client
/usr/local/bin/gobetween-cli:
  file.managed:
    - source: salt://vminit/gobetween-cli.py
    - user: root
    - mode: 755

# Call the client and create a portforwarding on the default gw
forward:
  cmd.run:
    - name: /usr/local/bin/gobetween-cli {{ grains['ip4_gw'] }} 22
    - stateful: True
    - requires:
      - file: /usr/local/bin/gobetween-cli

```

{% endcode %}

{% file src="/files/-Laq4cD25VXHgwXkvTKH" %}
gobetween-cli.py
{% endfile %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://thorstenkramm.gitbook.io/saltstack/salt-cloud/dynamische-portforwarding.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
