Tests are working again
This commit is contained in:
@@ -6,9 +6,10 @@ import (
|
||||
|
||||
"github.com/googollee/go-socket.io"
|
||||
"github.com/play-with-docker/play-with-docker/config"
|
||||
"github.com/play-with-docker/play-with-docker/docker"
|
||||
"github.com/play-with-docker/play-with-docker/event"
|
||||
"github.com/play-with-docker/play-with-docker/provider"
|
||||
"github.com/play-with-docker/play-with-docker/pwd"
|
||||
"github.com/play-with-docker/play-with-docker/scheduler"
|
||||
"github.com/play-with-docker/play-with-docker/storage"
|
||||
)
|
||||
|
||||
@@ -17,19 +18,18 @@ var e event.EventApi
|
||||
var ws *socketio.Server
|
||||
|
||||
func Bootstrap() {
|
||||
sp := provider.NewLocalSessionProvider()
|
||||
|
||||
e = event.NewLocalBroker()
|
||||
|
||||
t := pwd.NewScheduler(e, sp)
|
||||
|
||||
s, err := storage.NewFileStorage(config.SessionsFile)
|
||||
e = event.NewLocalBroker()
|
||||
|
||||
f := docker.NewLocalCachedFactory(s)
|
||||
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
log.Fatal("Error initializing StorageAPI: ", err)
|
||||
}
|
||||
core = pwd.NewPWD(sp, t, e, s)
|
||||
core = pwd.NewPWD(f, e, s)
|
||||
|
||||
scheduler.NewScheduler(s, e, core)
|
||||
}
|
||||
|
||||
func RegisterEvents(s *socketio.Server) {
|
||||
|
||||
@@ -1,206 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
var sshConfig = &ssh.ServerConfig{
|
||||
PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
|
||||
user := c.User()
|
||||
chunks := strings.Split(user, "-")
|
||||
ip := strings.Join(chunks[:4], ".")
|
||||
sessionPrefix := chunks[4]
|
||||
|
||||
log.Println(ip, sessionPrefix)
|
||||
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
|
||||
func ListenSSHProxy(laddr string) {
|
||||
privateBytes, err := ioutil.ReadFile("/etc/ssh/ssh_host_rsa_key")
|
||||
if err != nil {
|
||||
log.Fatal("Failed to load private key: ", err)
|
||||
}
|
||||
|
||||
private, err := ssh.ParsePrivateKey(privateBytes)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to parse private key: ", err)
|
||||
}
|
||||
|
||||
sshConfig.AddHostKey(private)
|
||||
|
||||
listener, err := net.Listen("tcp", laddr)
|
||||
if err != nil {
|
||||
log.Fatal("failed to listen for connection: ", err)
|
||||
}
|
||||
for {
|
||||
nConn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Fatal("failed to accept incoming connection: ", err)
|
||||
}
|
||||
|
||||
go handle(nConn)
|
||||
}
|
||||
}
|
||||
|
||||
func handle(c net.Conn) {
|
||||
sshCon, chans, reqs, err := ssh.NewServerConn(c, sshConfig)
|
||||
if err != nil {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
user := sshCon.User()
|
||||
chunks := strings.Split(user, "-")
|
||||
ip := strings.Join(chunks[:4], ".")
|
||||
sessionPrefix := chunks[4]
|
||||
|
||||
i := core.InstanceFindByIPAndSession(sessionPrefix, ip)
|
||||
if i == nil {
|
||||
log.Printf("Couldn't find instance with ip [%s] in session [%s]\n", ip, sessionPrefix)
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// The incoming Request channel must be serviced.
|
||||
go ssh.DiscardRequests(reqs)
|
||||
|
||||
newChannel := <-chans
|
||||
if newChannel == nil {
|
||||
sshCon.Close()
|
||||
return
|
||||
}
|
||||
|
||||
if newChannel.ChannelType() != "session" {
|
||||
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
|
||||
return
|
||||
}
|
||||
|
||||
channel, requests, err := newChannel.Accept()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not accept channel: %v", err)
|
||||
}
|
||||
|
||||
stderr := channel.Stderr()
|
||||
|
||||
fmt.Fprintf(stderr, "Connecting to %s\r\n", ip)
|
||||
|
||||
clientConfig := &ssh.ClientConfig{
|
||||
User: "root",
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password("root"),
|
||||
},
|
||||
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:22", ip), clientConfig)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "Connect failed: %v\r\n", err)
|
||||
channel.Close()
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
for newChannel = range chans {
|
||||
if newChannel == nil {
|
||||
return
|
||||
}
|
||||
|
||||
channel2, reqs2, err := client.OpenChannel(newChannel.ChannelType(), newChannel.ExtraData())
|
||||
if err != nil {
|
||||
x, ok := err.(*ssh.OpenChannelError)
|
||||
if ok {
|
||||
newChannel.Reject(x.Reason, x.Message)
|
||||
} else {
|
||||
newChannel.Reject(ssh.Prohibited, "remote server denied channel request")
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
channel, reqs, err := newChannel.Accept()
|
||||
if err != nil {
|
||||
channel2.Close()
|
||||
continue
|
||||
}
|
||||
go proxy(reqs, reqs2, channel, channel2)
|
||||
}
|
||||
}()
|
||||
|
||||
// Forward the session channel
|
||||
channel2, reqs2, err := client.OpenChannel("session", []byte{})
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "Remote session setup failed: %v\r\n", err)
|
||||
channel.Close()
|
||||
return
|
||||
}
|
||||
|
||||
maskedReqs := make(chan *ssh.Request, 1)
|
||||
go func() {
|
||||
for req := range requests {
|
||||
if req.Type == "auth-agent-req@openssh.com" {
|
||||
continue
|
||||
}
|
||||
maskedReqs <- req
|
||||
}
|
||||
}()
|
||||
proxy(maskedReqs, reqs2, channel, channel2)
|
||||
}
|
||||
|
||||
func proxy(reqs1, reqs2 <-chan *ssh.Request, channel1, channel2 ssh.Channel) {
|
||||
var closer sync.Once
|
||||
closeFunc := func() {
|
||||
channel1.Close()
|
||||
channel2.Close()
|
||||
}
|
||||
|
||||
defer closer.Do(closeFunc)
|
||||
|
||||
closerChan := make(chan bool, 1)
|
||||
|
||||
go func() {
|
||||
io.Copy(channel1, channel2)
|
||||
closerChan <- true
|
||||
}()
|
||||
|
||||
go func() {
|
||||
io.Copy(channel2, channel1)
|
||||
closerChan <- true
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case req := <-reqs1:
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
b, err := channel2.SendRequest(req.Type, req.WantReply, req.Payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Reply(b, nil)
|
||||
|
||||
case req := <-reqs2:
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
b, err := channel1.SendRequest(req.Type, req.WantReply, req.Payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Reply(b, nil)
|
||||
case <-closerChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,15 @@ package handlers
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/text/encoding"
|
||||
|
||||
"github.com/googollee/go-socket.io"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/event"
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
)
|
||||
|
||||
func WS(so socketio.Socket) {
|
||||
@@ -26,6 +32,61 @@ func WS(so socketio.Socket) {
|
||||
|
||||
so.Join(session.Id)
|
||||
|
||||
var rw sync.Mutex
|
||||
trackedTerminals := make(map[string]net.Conn, len(session.Instances))
|
||||
|
||||
attachTerminalToSocket := func(instance *types.Instance, ws socketio.Socket) {
|
||||
rw.Lock()
|
||||
defer rw.Unlock()
|
||||
if _, found := trackedTerminals[instance.Name]; found {
|
||||
return
|
||||
}
|
||||
conn, err := core.InstanceGetTerminal(instance)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
trackedTerminals[instance.Name] = conn
|
||||
|
||||
go func(instanceName string, c net.Conn, ws socketio.Socket) {
|
||||
defer c.Close()
|
||||
encoder := encoding.Replacement.NewEncoder()
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n, err := c.Read(buf)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
b, err := encoder.Bytes(buf[:n])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
ws.Emit("instance terminal out", instanceName, b)
|
||||
}
|
||||
}(instance.Name, conn, ws)
|
||||
}
|
||||
// since this is a new connection, get all terminals of the session and attach
|
||||
for _, instance := range session.Instances {
|
||||
attachTerminalToSocket(instance, so)
|
||||
}
|
||||
|
||||
e.On(event.INSTANCE_NEW, func(sessionId string, args ...interface{}) {
|
||||
if sessionId != session.Id {
|
||||
return
|
||||
}
|
||||
|
||||
// There is a new instance in a session we are tracking. We should track it's terminal
|
||||
instanceName := args[0].(string)
|
||||
instance := core.InstanceGet(session, instanceName)
|
||||
if instance == nil {
|
||||
log.Printf("Instance [%s] was not found in session [%s]\n", instanceName, sessionId)
|
||||
return
|
||||
}
|
||||
attachTerminalToSocket(instance, so)
|
||||
})
|
||||
|
||||
client := core.ClientNew(so.Id(), session)
|
||||
|
||||
so.On("session close", func() {
|
||||
@@ -33,8 +94,14 @@ func WS(so socketio.Socket) {
|
||||
})
|
||||
|
||||
so.On("instance terminal in", func(name, data string) {
|
||||
// User wrote something on the terminal. Need to write it to the instance terminal
|
||||
core.InstanceWriteToTerminal(session.Id, name, data)
|
||||
rw.Lock()
|
||||
defer rw.Unlock()
|
||||
conn, found := trackedTerminals[name]
|
||||
if !found {
|
||||
log.Printf("Could not find instance [%s] in session [%s]\n", name, sessionId)
|
||||
return
|
||||
}
|
||||
go conn.Write([]byte(data))
|
||||
})
|
||||
|
||||
so.On("instance viewport resize", func(cols, rows uint) {
|
||||
|
||||
Reference in New Issue
Block a user