/
backend.go
61 lines (49 loc) · 1.49 KB
/
backend.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
54
55
56
57
58
59
60
61
package backends
import (
"fmt"
"strings"
)
type ConnectionConfig struct {
Name string
LocalAddress string "local_address"
RemoteAddress string "remote_address"
Url string
}
type ReadWrite interface {
CreateProxyConfiguration(proxy_configuration string) error
DeleteProxyConfiguration(proxy_configuration string) error
GetProxyConfigurations() ([]ConnectionConfig, error)
}
type ReadOnly interface {
GetProxyConfigurations() ([]ConnectionConfig, error)
IsPollable() bool
}
func ParseConnectionsParameter(connectionsArg string) ([]ConnectionConfig, error) {
if len(connectionsArg) == 0 {
return nil, fmt.Errorf("Connection must not be empty")
}
connections := strings.Split(connectionsArg, ",");
connectionsConfig := make([]ConnectionConfig, len(connections))
for i := range connections {
config, err := ParseConnection(connections[i])
if err != nil {
fmt.Errorf("Error parsing connection %s", connections[i])
} else {
connectionsConfig[i] = *config
}
}
return connectionsConfig, nil
}
func ParseConnection(connection string) (*ConnectionConfig, error) {
connectionParts := strings.Split(connection, ":")
if len(connectionParts) != 3 {
return nil, fmt.Errorf("A connection must have three parts: srcPort:destHost:destPort '", connection, "'")
}
config := ConnectionConfig{
Name: connectionParts[1],
LocalAddress: ":" + connectionParts[0],
RemoteAddress: connectionParts[1] + ":" + connectionParts[2],
Url: connection,
}
return &config, nil
}