-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
encoding.go
53 lines (43 loc) · 1008 Bytes
/
encoding.go
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
package api
import (
"encoding/base64"
"strings"
"github.com/owenthereal/upterm/upterm"
)
func EncodeIdentifierSession(session *GetSessionResponse) (string, error) {
id := &Identifier{
Id: session.SessionId,
Type: Identifier_CLIENT,
NodeAddr: session.NodeAddr,
}
return EncodeIdentifier(id)
}
func EncodeIdentifier(id *Identifier) (string, error) {
result := id.Id
if id.Type == Identifier_CLIENT {
result += ":" + base64.URLEncoding.EncodeToString([]byte(id.NodeAddr))
}
return result, nil
}
func DecodeIdentifier(id, clientVersion string) (*Identifier, error) {
t := Identifier_HOST
if clientVersion != upterm.HostSSHClientVersion {
t = Identifier_CLIENT
}
split := strings.SplitN(id, ":", 2)
var (
nodeAddr []byte
err error
)
if len(split) == 2 {
nodeAddr, err = base64.URLEncoding.DecodeString(split[1])
if err != nil {
return nil, err
}
}
return &Identifier{
Id: split[0],
Type: t,
NodeAddr: string(nodeAddr),
}, nil
}