File: example.go

package info (click to toggle)
golang-github-microsoft-dev-tunnels 0.0.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,988 kB
  • sloc: cs: 9,969; java: 2,767; javascript: 328; xml: 186; makefile: 5
file content (109 lines) | stat: -rw-r--r-- 2,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package main

import (
	"context"
	"errors"
	"fmt"
	"log"
	"net/url"
	"os"

	tunnels "github.com/microsoft/dev-tunnels/go/tunnels"
)

// Set the tunnelId and cluster Id for the tunnels you want to connect to
const (
	tunnelId  = ""
	clusterId = "usw2"
)

var (
	uri       = tunnels.ServiceProperties.ServiceURI
	userAgent = []tunnels.UserAgent{{Name: "Tunnels-Go-SDK-Example", Version: "0.0.1"}}
	ctx       = context.Background()
)

// Put your tunnels access token in the return statement or set the TUNNELS_TOKEN env variable
func getAccessToken() string {
	if token := os.Getenv("TUNNELS_TOKEN"); token != "" {
		return token
	}
	return ""
}

func main() {
	logger := log.New(os.Stdout, "", log.LstdFlags)

	url, err := url.Parse(uri)
	if err != nil {
		fmt.Println(fmt.Errorf(err.Error()))
		return
	}

	// create manager to get tunnel
	managementClient, err := tunnels.NewManager(userAgent, getAccessToken, url, nil)
	if err != nil {
		fmt.Println(fmt.Errorf(err.Error()))
		return
	}

	limits, err := managementClient.ListUserLimits(ctx)
	if err != nil {
		fmt.Println(fmt.Errorf(err.Error()))
		return
	}

	logger.Printf("Successfully retrieved %d user limit(s)", len(limits))

	// set up options to request a connect token
	options := &tunnels.TunnelRequestOptions{IncludePorts: true, TokenScopes: []tunnels.TunnelAccessScope{"connect"}}

	newTunnel := &tunnels.Tunnel{
		TunnelID:  tunnelId,
		ClusterID: clusterId,
	}

	// get tunnel for connection
	getTunnel, err := managementClient.GetTunnel(ctx, newTunnel, options)
	if err != nil {
		fmt.Println(fmt.Errorf(err.Error()))
		return
	}
	if getTunnel.TunnelID == "" {
		fmt.Println(fmt.Errorf(err.Error()))
		return
	} else {
		logger.Printf(fmt.Sprintf("Got tunnel with id %s", getTunnel.TunnelID))
	}

	// create channels for errors and listeners
	done := make(chan error)

	go func() {
		// start client connection to tunnel
		c, err := tunnels.NewClient(logger, getTunnel, true)
		c.Connect(ctx, "")
		if err != nil {
			done <- fmt.Errorf("connect failed: %v", err)
			return
		}
		if c == nil {
			done <- errors.New("nil connection")
			return
		}
	}()
	for {
		select {
		case err := <-done:
			if err != nil {
				fmt.Println(fmt.Errorf(err.Error()))
			}
			return
		case <-ctx.Done():
			return
		}
	}
}