This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
dockersh.go
141 lines (132 loc) · 3.67 KB
/
dockersh.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"github.com/docker/libcontainer/user"
"os"
"os/signal"
"strings"
"syscall"
)
func main() {
if os.Args[0] == "/init" {
os.Exit(initMain())
} else {
os.Exit(realMain())
}
}
func tmplConfigVar(template string, v *configInterpolation) string {
shell := "/bin/bash"
r := strings.NewReplacer("%h", v.Home, "%u", v.User, "%s", shell) // Arguments are old, new ...
return r.Replace(template)
}
func getInterpolatedConfig(config *Configuration, configInterpolations configInterpolation) error {
config.ContainerUsername = tmplConfigVar(config.ContainerUsername, &configInterpolations)
config.MountHomeTo = tmplConfigVar(config.MountHomeTo, &configInterpolations)
config.MountHomeFrom = tmplConfigVar(config.MountHomeFrom, &configInterpolations)
config.ImageName = tmplConfigVar(config.ImageName, &configInterpolations)
config.Shell = tmplConfigVar(config.Shell, &configInterpolations)
config.UserCwd = tmplConfigVar(config.UserCwd, &configInterpolations)
config.ContainerName = tmplConfigVar(config.ContainerName, &configInterpolations)
return nil
}
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func gatewayIP() (string, error) {
file, err := os.Open("/proc/net/route")
if err != nil {
return "", errors.New("Could not open /proc/net/route")
}
defer file.Close()
r := bufio.NewReader(file)
s, err := Readln(r)
ip := ""
for err == nil {
f := strings.Fields(s)
if f[1] == "00000000" {
a, _ := hex.DecodeString(f[2])
ip = fmt.Sprintf("%v.%v.%v.%v", a[3], a[2], a[1], a[0])
err = nil
break
}
s, err = Readln(r)
}
return ip, err
}
func initMain() int {
fmt.Fprintf(os.Stdout, "started dockersh persistent container\n")
pfString := os.Getenv("DOCKERSH_PORTFORWARD")
if pfString != "" {
fmt.Printf("DOCKERSH_PORTFORWARD file exists; processing...")
pfs := strings.Split(pfString, ",")
gw, err := gatewayIP()
if err != nil {
panic(err)
}
for _, element := range pfs {
err := validatePortforwardString(element)
if err != nil {
panic(err)
}
fmt.Println(element)
parts := strings.Split(element, ":") // Parts is hostport:containerport
localAddr := "127.0.0.1:" + parts[1]
remoteAddr := gw + ":" + parts[0]
go proxyMain(localAddr, remoteAddr)
}
}
// Wait for terminating signal
sc := make(chan os.Signal, 2)
signal.Notify(sc, syscall.SIGTERM, syscall.SIGINT)
<-sc
return 0
}
func realMain() int {
err := dockerVersionCheck()
if err != nil {
fmt.Fprintf(os.Stderr, "Docker version error: %v", err)
return 1
}
username, homedir, uid, gid, err := getCurrentUser()
if err != nil {
fmt.Fprintf(os.Stderr, "could not get current user: %v", err)
return 1
}
config, err := loadAllConfig(username, homedir)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not load config: %v\n", err)
return 1
}
configInterpolations := configInterpolation{homedir, username}
err = getInterpolatedConfig(&config, configInterpolations)
if err != nil {
panic(fmt.Sprintf("Cannot interpolate config: %v", err))
}
_, err = dockerpid(config.ContainerName)
if err != nil {
_, err = dockerstart(config)
if err != nil {
fmt.Fprintf(os.Stderr, "could not start container: %s\n", err)
return 1
}
}
_, _, groups, _, err := user.GetUserGroupSupplementaryHome(username, 65536, 65536, "/")
err = nsenterexec(config.ContainerName, uid, gid, groups, config.UserCwd, config.Shell)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting shell in new container: %v\n", err)
return 1
}
return 0
}