-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
72 lines (59 loc) · 1.76 KB
/
config.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
62
63
64
65
66
67
68
69
70
71
72
// mkonion: create a Tor onion service for existing Docker containers
// Copyright (C) 2016 Aleksa Sarai <cyphar@cyphar.com>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"bytes"
"text/template"
"github.com/docker/engine-api/client"
)
const (
// Describes the entire torrc template.
// TODO: Make the hidden_service path customisable.
torTemplate = `
# Disable SOCKS, we're only running as a hidden service.
SocksPort 0
# Set up hidden service.
HiddenServiceDir /var/lib/tor/hidden_service
{{range .Targets}}
HiddenServicePort {{.ExternalPort}} {{.}}
{{end}}
`
)
type TargetIP struct {
Addr string
InternalPort string
ExternalPort string
}
func (t TargetIP) String() string {
return t.Addr + ":" + t.InternalPort
}
// XXX: This is probably very horrible.
var confTemplate = template.Must(template.New("tor").Parse(torTemplate))
func GenerateTargetMappings(ip string, mappings map[string]string) []TargetIP {
var targets []TargetIP
for external, internal := range mappings {
targets = append(targets, TargetIP{
Addr: ip,
InternalPort: internal,
ExternalPort: external,
})
}
return targets
}
// GenerateConfig generates a configuraton file for a target container for a
// given network. This is returned as a string, and warnings are logged if there
// are any non-TCP ports exposed on the container.
func GenerateConfig(cli *client.Client, targets []TargetIP) ([]byte, error) {
config := new(bytes.Buffer)
if err := confTemplate.Execute(config, struct {
Targets []TargetIP
}{
Targets: targets,
}); err != nil {
return nil, err
}
return config.Bytes(), nil
}