Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -107,7 +109,28 @@ func (cs *clientSession) run() (err error) {
cs.dc.OnOpen(cs.dataChannelOnOpen())
cs.dc.OnMessage(cs.dataChannelOnMessage())

if cs.offer, err = sd.Decode(cs.offerString); err != nil {
offerText := cs.offerString
if len(cs.offerString) == 32 {
url := fmt.Sprintf("https://www.10kb.site/%s", cs.offerString)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}

client := &http.Client{}
req.Header.Set("Content-Type", "text/plain;charset=UTF-8")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
offerText = fmt.Sprintf("%s", bodyText)
}

if cs.offer, err = sd.Decode(fmt.Sprintf("%s", offerText)); err != nil {
log.Println(err)
return
}
Expand Down Expand Up @@ -164,5 +187,6 @@ func (cs *clientSession) run() (err error) {
}
err = <-cs.errChan
cs.cleanup()
os.Exit(1)
return err
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/btcsuite/btcutil v0.0.0-20190316010144-3ac1210f4b38
github.com/google/uuid v1.1.2
github.com/kr/pty v1.1.4
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/pion/webrtc/v2 v2.1.18
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down
52 changes: 44 additions & 8 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"time"

"github.com/google/uuid"
"github.com/kr/pty"
"github.com/maxmcd/webtty/pkg/sd"
"github.com/mitchellh/colorstring"
Expand Down Expand Up @@ -208,22 +212,54 @@ func (hs *hostSession) run() (err error) {
if err = hs.init(); err != nil {
return
}

var connection_uuid string = ""
if err = hs.createOffer(); err != nil {
return
}

colorstring.Printf("[bold]Setting up a WebTTY connection.\n\n")
if hs.oneWay {
colorstring.Printf(
"Warning: One-way connections rely on a third party to connect. " +
"More info here: https://github.com/maxmcd/webtty#one-way-connections\n\n")
}

if err = hs.createOffer(); err != nil {
return
// send SDP to 10kb.site
var data = strings.NewReader(sd.Encode(hs.offer))
connection_uuid = strings.Replace(uuid.New().String(), "-", "", -1)
url := fmt.Sprintf("https://up.10kb.site/%s", connection_uuid)
req, err := http.NewRequest("POST", url, data)
if err != nil {
log.Fatal(err)
}

client := &http.Client{}
req.Header.Set("Content-Type", "text/plain;charset=UTF-8")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}

_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

// Output the offer in base64 so we can paste it in browser
colorstring.Printf("[bold]Connection ready. Here is your connection data:\n\n")

fmt.Printf("%s\n\n", connection_uuid)
colorstring.Printf(`[bold]Paste it in the terminal after the webtty command` +
"\n")
}

// Output the offer in base64 so we can paste it in browser
colorstring.Printf("[bold]Connection ready. Here is your connection data:\n\n")
fmt.Printf("%s\n\n", sd.Encode(hs.offer))
colorstring.Printf(`[bold]Paste it in the terminal after the webtty command` +
"\n[bold]Or in a browser: [reset]https://maxmcd.github.io/webtty/\n\n")
if connection_uuid == "" {
// Output the offer in base64 so we can paste it in browser
colorstring.Printf("[bold]Connection ready. Here is your connection data:\n\n")
fmt.Printf("%s\n\n", sd.Encode(hs.offer))
colorstring.Printf(`[bold]Paste it in the terminal after the webtty command` +
"\n[bold]Or in a browser: [reset]https://maxmcd.github.io/webtty/\n\n")
}

if hs.oneWay == false {
colorstring.Println("[bold]When you have the answer, paste it below and hit enter:")
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/ioutil"
"log"
"os"
"os/signal"
"syscall"
)

func main() {
Expand All @@ -19,6 +21,17 @@ func main() {
"eg: webtty -o -v -ni -cmd docker run -it --rm alpine:latest sh")
stunServer := flag.String("s", "stun:stun.l.google.com:19302", "The stun server to use")

sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
_ = <-sigc
os.Exit(0)
}()

cmd := []string{"bash", "-l"}
for i, arg := range os.Args {
if arg == "-cmd" {
Expand All @@ -38,7 +51,6 @@ func main() {
if len(args) > 0 {
offerString = args[len(args)-1]
}

var err error
if len(offerString) == 0 {
hc := hostSession{
Expand Down