repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

pico / pkg / shared
Antonio Mika  ·  2025-03-20

tuns.go

 1package shared
 2
 3import (
 4	"fmt"
 5	"strings"
 6)
 7
 8// ParseTunsTCP parses the tunnelID and tunsInstance to return a user friendly address.
 9func ParseTunsTCP(tunnelID string, tunsInstance string) (string, error) {
10	// example string:
11	// tcp://10.0.0.89:33652,tcp6://[2603:c020:400a:d000:bd71:c59f:720c:484b]:33652
12	splitData := strings.Split(tunnelID, ":")
13	if len(splitData) < 3 {
14		return "", fmt.Errorf("invalid tunnelID: %s", tunnelID)
15	}
16
17	port := splitData[len(splitData)-1]
18	if port == "" {
19		return "", fmt.Errorf("invalid tunnelID: %s", tunnelID)
20	}
21
22	return fmt.Sprintf("%s:%s", tunsInstance, port), nil
23}