Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
haoqu.ma
/
gotty
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit a765d6c6
authored
Aug 23, 2015
by
Iwasaki Yudai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set window title by control message
1 parent
67b54b7f
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
76 additions
and
48 deletions
Makefile
app/app.go
app/client_context.go
app/resource.go
main.go
resources/gotty.js
resources/index.html
Makefile
View file @
a765d6c
...
...
@@ -3,7 +3,7 @@ gotty: app/resource.go main.go app/*.go
resource
:
app/resource.go
app/resource.go
:
bindata/static/hterm.js bindata/static/gotty.js bindata/
templates
/index.html
app/resource.go
:
bindata/static/hterm.js bindata/static/gotty.js bindata/
static
/index.html
go-bindata -prefix bindata -pkg app -ignore
=
\\
.gitkeep -o app/resource.go bindata/...
gofmt -w app/resource.go
...
...
@@ -23,5 +23,5 @@ bindata/static/hterm.js: bindata/static libapps/hterm/js/*.js
bindata/static/gotty.js
:
bindata/static resources/gotty.js
cp resources/gotty.js bindata/static/gotty.js
bindata/
templates/index.html
:
bindata/templates
resources/index.html
cp resources/index.html bindata/
templates
/index.html
bindata/
static/index.html
:
bindata/static
resources/index.html
cp resources/index.html bindata/
static
/index.html
app/app.go
View file @
a765d6c
package
app
import
(
"bytes"
"crypto/rand"
"encoding/base64"
htemplate
"html/template
"
"errors
"
"log"
"math/big"
"net"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
ttemplate
"text/template"
"text/template"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/websocket"
...
...
@@ -24,6 +22,7 @@ type App struct {
options
Options
upgrader
*
websocket
.
Upgrader
titleTemplate
*
template
.
Template
}
type
Options
struct
{
...
...
@@ -36,7 +35,12 @@ type Options struct {
Command
[]
string
}
func
New
(
options
Options
)
*
App
{
func
New
(
options
Options
)
(
*
App
,
error
)
{
titleTemplate
,
err
:=
template
.
New
(
"title"
)
.
Parse
(
options
.
TitleFormat
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Title format string syntax error"
)
}
return
&
App
{
options
:
options
,
...
...
@@ -45,7 +49,8 @@ func New(options Options) *App {
WriteBufferSize
:
1024
,
Subprotocols
:
[]
string
{
"gotty"
},
},
}
titleTemplate
:
titleTemplate
,
},
nil
}
func
(
app
*
App
)
Run
()
error
{
...
...
@@ -56,15 +61,13 @@ func (app *App) Run() error {
endpoint
:=
app
.
options
.
Address
+
":"
+
app
.
options
.
Port
indexHandler
:=
http
.
HandlerFunc
(
app
.
handleIndex
)
wsHandler
:=
http
.
HandlerFunc
(
app
.
handleWS
)
staticHandler
:=
http
.
FileServer
(
&
assetfs
.
AssetFS
{
Asset
:
Asset
,
AssetDir
:
AssetDir
,
Prefix
:
"static"
},
)
var
siteMux
=
http
.
NewServeMux
()
siteMux
.
Handle
(
path
+
"/"
,
indexHandler
)
siteMux
.
Handle
(
path
+
"/static/"
,
http
.
StripPrefix
(
path
+
"/static/"
,
staticHandler
))
siteMux
.
Handle
(
path
+
"/"
,
http
.
StripPrefix
(
path
+
"/"
,
staticHandler
))
siteMux
.
Handle
(
path
+
"/ws"
,
wsHandler
)
siteHandler
:=
http
.
Handler
(
siteMux
)
...
...
@@ -94,36 +97,6 @@ func (app *App) Run() error {
return
nil
}
type
TitleVars
struct
{
Command
string
Hostname
string
}
type
IndexVars
struct
{
Title
string
}
func
(
app
*
App
)
handleIndex
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
title
:=
make
([]
byte
,
0
)
titleBuf
:=
bytes
.
NewBuffer
(
title
)
hostname
,
_
:=
os
.
Hostname
()
titleVars
:=
TitleVars
{
Command
:
strings
.
Join
(
app
.
options
.
Command
,
" "
),
Hostname
:
hostname
,
}
titleTmpl
,
_
:=
ttemplate
.
New
(
"title"
)
.
Parse
(
app
.
options
.
TitleFormat
)
titleTmpl
.
Execute
(
titleBuf
,
titleVars
)
data
,
_
:=
Asset
(
"templates/index.html"
)
tmpl
,
_
:=
htemplate
.
New
(
"index"
)
.
Parse
(
string
(
data
))
vars
:=
IndexVars
{
Title
:
titleBuf
.
String
(),
}
tmpl
.
Execute
(
w
,
vars
)
}
func
(
app
*
App
)
handleWS
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Printf
(
"New client connected: %s"
,
r
.
RemoteAddr
)
...
...
app/client_context.go
View file @
a765d6c
...
...
@@ -6,6 +6,7 @@ import (
"net/http"
"os"
"os/exec"
"strings"
"syscall"
"unsafe"
...
...
@@ -26,11 +27,23 @@ const (
ResizeTerminal
=
'1'
)
const
(
Output
=
'0'
SetWindowTitle
=
'1'
)
type
argResizeTerminal
struct
{
Columns
float64
Rows
float64
}
type
ContextVars
struct
{
Command
string
Pid
int
Hostname
string
RemoteAddr
string
}
func
(
context
*
clientContext
)
goHandleClient
()
{
exit
:=
make
(
chan
bool
,
2
)
...
...
@@ -56,6 +69,11 @@ func (context *clientContext) goHandleClient() {
}
func
(
context
*
clientContext
)
processSend
()
{
if
err
:=
context
.
sendInitialize
();
err
!=
nil
{
log
.
Printf
(
err
.
Error
())
return
}
buf
:=
make
([]
byte
,
1024
)
utf8f
:=
utf8reader
.
New
(
context
.
pty
)
...
...
@@ -71,11 +89,34 @@ func (context *clientContext) processSend() {
return
}
writer
.
Write
([]
byte
{
Output
})
writer
.
Write
(
buf
[
:
size
])
writer
.
Close
()
}
}
func
(
context
*
clientContext
)
sendInitialize
()
error
{
hostname
,
_
:=
os
.
Hostname
()
titleVars
:=
ContextVars
{
Command
:
strings
.
Join
(
context
.
app
.
options
.
Command
,
" "
),
Pid
:
context
.
command
.
Process
.
Pid
,
Hostname
:
hostname
,
RemoteAddr
:
context
.
request
.
RemoteAddr
,
}
writer
,
err
:=
context
.
connection
.
NextWriter
(
websocket
.
TextMessage
)
if
err
!=
nil
{
return
err
}
writer
.
Write
([]
byte
{
SetWindowTitle
})
if
err
=
context
.
app
.
titleTemplate
.
Execute
(
writer
,
titleVars
);
err
!=
nil
{
return
err
}
writer
.
Close
()
return
nil
}
func
(
context
*
clientContext
)
processReceive
()
{
for
{
_
,
data
,
err
:=
context
.
connection
.
ReadMessage
()
...
...
app/resource.go
View file @
a765d6c
This diff could not be displayed because it is too large.
main.go
View file @
a765d6c
...
...
@@ -56,7 +56,8 @@ func main() {
cli
.
ShowAppHelp
(
c
)
os
.
Exit
(
1
)
}
app
:=
app
.
New
(
app
,
err
:=
app
.
New
(
app
.
Options
{
c
.
String
(
"addr"
),
c
.
String
(
"port"
),
...
...
@@ -67,11 +68,16 @@ func main() {
c
.
Args
(),
},
)
err
:=
app
.
Run
()
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
Exit
(
2
)
}
err
=
app
.
Run
()
if
err
!=
nil
{
fmt
.
Println
(
err
)
os
.
Exit
(
3
)
}
}
cli
.
AppHelpTemplate
=
helpTemplate
...
...
resources/gotty.js
View file @
a765d6c
...
...
@@ -38,7 +38,15 @@
};
ws
.
onmessage
=
function
(
event
)
{
term
.
io
.
writeUTF16
(
event
.
data
);
data
=
event
.
data
.
slice
(
1
);
switch
(
event
.
data
[
0
])
{
case
'0'
:
term
.
io
.
writeUTF16
(
data
);
break
;
case
'1'
:
term
.
setWindowTitle
(
data
);
break
;
}
}
ws
.
onclose
=
function
(
event
)
{
...
...
resources/index.html
View file @
a765d6c
<!doctype html>
<html>
<head>
<title>
{{ .Title }}
</title>
<title>
GoTTY
</title>
<style>body
{
position
:
absolute
;
height
:
100%
;
width
:
100%
;
margin
:
0px
;}
</style>
</head>
<body>
<script
src=
"
static/
hterm.js"
></script>
<script
src=
"
static/
gotty.js"
></script>
<script
src=
"hterm.js"
></script>
<script
src=
"gotty.js"
></script>
</body>
</html>
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment