-
Notifications
You must be signed in to change notification settings - Fork 143
/
session.go
85 lines (75 loc) · 1.79 KB
/
session.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
package main
import (
"log"
"os"
"github.com/maxmcd/webtty/pkg/sd"
"github.com/pion/webrtc/v3"
"golang.org/x/crypto/ssh/terminal"
)
type session struct {
// mutex?
oldTerminalState *terminal.State
stunServers []string
errChan chan error
isTerminal bool
pc *webrtc.PeerConnection
offer sd.SessionDescription
answer sd.SessionDescription
dc *webrtc.DataChannel
}
func (s *session) init() (err error) {
s.errChan = make(chan error, 1)
s.isTerminal = terminal.IsTerminal(int(os.Stdin.Fd()))
if err = s.createPeerConnection(); err != nil {
log.Println(err)
return
}
return
}
func (s *session) cleanup() {
if s.dc != nil {
// TODO: check dc state?
if err := s.dc.SendText("quit"); err != nil {
log.Println(err)
}
}
if s.isTerminal {
if err := s.restoreTerminalState(); err != nil {
log.Println(err)
}
}
}
func (s *session) restoreTerminalState() error {
if s.oldTerminalState != nil {
return terminal.Restore(int(os.Stdin.Fd()), s.oldTerminalState)
}
return nil
}
func (s *session) makeRawTerminal() error {
var err error
s.oldTerminalState, err = terminal.MakeRaw(int(os.Stdin.Fd()))
return err
}
func (s *session) createPeerConnection() (err error) {
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: s.stunServers,
},
},
}
s.pc, err = webrtc.NewPeerConnection(config)
if err != nil {
return
}
// fmt.Println(s.pc)
// fmt.Println(s.pc.SignalingState)
// fmt.Println(s.pc.ConnectionState)
// if s.pc.OnDataChannel == nil {
// return errors.New("Couldn't create a peerConnection")
// }
s.pc.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
log.Printf("ICE Connection State has changed: %s\n", connectionState.String())
})
return
}