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 b15227c6
authored
Aug 19, 2015
by
Shoji Ihara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Random URL generation(Close #17)
1 parent
2aaa155a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
11 deletions
app/app.go
main.go
app/app.go
View file @
b15227c
package
app
package
app
import
(
import
(
"crypto/rand"
"encoding/base64"
"encoding/base64"
"encoding/json"
"encoding/json"
"log"
"log"
"math/big"
"net/http"
"net/http"
"os/exec"
"os/exec"
"strconv"
"strings"
"strings"
"syscall"
"syscall"
"unsafe"
"unsafe"
...
@@ -20,16 +23,18 @@ type App struct {
...
@@ -20,16 +23,18 @@ type App struct {
Address
string
Address
string
Port
string
Port
string
PermitWrite
bool
PermitWrite
bool
RandomUrl
bool
Credential
string
Credential
string
Command
[]
string
Command
[]
string
}
}
func
New
(
address
string
,
port
string
,
permitWrite
bool
,
cred
string
,
command
[]
string
)
*
App
{
func
New
(
address
string
,
port
string
,
permitWrite
bool
,
cred
string
,
randomUrl
bool
,
command
[]
string
)
*
App
{
return
&
App
{
return
&
App
{
Address
:
address
,
Address
:
address
,
Port
:
port
,
Port
:
port
,
PermitWrite
:
permitWrite
,
PermitWrite
:
permitWrite
,
Credential
:
cred
,
Credential
:
cred
,
RandomUrl
:
randomUrl
,
Command
:
command
,
Command
:
command
,
}
}
}
}
...
@@ -65,21 +70,24 @@ func basicAuthHandler(h http.Handler, cred string) http.Handler {
...
@@ -65,21 +70,24 @@ func basicAuthHandler(h http.Handler, cred string) http.Handler {
}
}
func
(
app
*
App
)
Run
()
error
{
func
(
app
*
App
)
Run
()
error
{
http
.
Handle
(
"/"
,
path
:=
"/"
http
.
FileServer
(
if
app
.
RandomUrl
{
&
assetfs
.
AssetFS
{
Asset
:
Asset
,
AssetDir
:
AssetDir
,
Prefix
:
"bindata"
},
randomPath
:=
generateRandomString
(
8
)
),
path
=
"/"
+
randomPath
+
"/"
)
}
http
.
HandleFunc
(
"/ws"
,
app
.
generateHandler
())
fs
:=
http
.
StripPrefix
(
path
,
http
.
FileServer
(
&
assetfs
.
AssetFS
{
Asset
:
Asset
,
AssetDir
:
AssetDir
,
Prefix
:
"bindata"
}))
http
.
Handle
(
path
,
fs
)
http
.
HandleFunc
(
path
+
"ws"
,
app
.
generateHandler
())
url
:=
app
.
Address
+
":"
+
app
.
Port
endpoint
:=
app
.
Address
+
":"
+
app
.
Port
log
.
Printf
(
"Server is running at %s, command: %s"
,
url
,
strings
.
Join
(
app
.
Command
,
" "
))
log
.
Printf
(
"Server is running at %s, command: %s"
,
endpoint
+
path
,
strings
.
Join
(
app
.
Command
,
" "
))
handler
:=
http
.
Handler
(
http
.
DefaultServeMux
)
handler
:=
http
.
Handler
(
http
.
DefaultServeMux
)
handler
=
loggerHandler
(
handler
)
handler
=
loggerHandler
(
handler
)
if
app
.
Credential
!=
""
{
if
app
.
Credential
!=
""
{
handler
=
basicAuthHandler
(
handler
,
app
.
Credential
)
handler
=
basicAuthHandler
(
handler
,
app
.
Credential
)
}
}
err
:=
http
.
ListenAndServe
(
url
,
handler
)
err
:=
http
.
ListenAndServe
(
endpoint
,
handler
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -227,3 +235,14 @@ type command struct {
...
@@ -227,3 +235,14 @@ type command struct {
Name
string
`json:"name"`
Name
string
`json:"name"`
Arguments
map
[
string
]
interface
{}
`json:"arguments"`
Arguments
map
[
string
]
interface
{}
`json:"arguments"`
}
}
func
generateRandomString
(
length
int
)
string
{
const
base
=
36
size
:=
big
.
NewInt
(
base
)
n
:=
make
([]
byte
,
length
)
for
i
,
_
:=
range
n
{
c
,
_
:=
rand
.
Int
(
rand
.
Reader
,
size
)
n
[
i
]
=
strconv
.
FormatInt
(
c
.
Int64
(),
base
)[
0
]
}
return
string
(
n
)
}
main.go
View file @
b15227c
...
@@ -37,6 +37,11 @@ func main() {
...
@@ -37,6 +37,11 @@ func main() {
Usage
:
"Credential for Basic Authentication (ex: user:pass)"
,
Usage
:
"Credential for Basic Authentication (ex: user:pass)"
,
EnvVar
:
"GOTTY_CREDENTIAL"
,
EnvVar
:
"GOTTY_CREDENTIAL"
,
},
},
cli
.
BoolFlag
{
Name
:
"random-url, r"
,
Usage
:
"Add a random string to the URL"
,
EnvVar
:
"GOTTY_RANDOM_URL"
,
},
}
}
cmd
.
Action
=
func
(
c
*
cli
.
Context
)
{
cmd
.
Action
=
func
(
c
*
cli
.
Context
)
{
if
len
(
c
.
Args
())
==
0
{
if
len
(
c
.
Args
())
==
0
{
...
@@ -44,7 +49,7 @@ func main() {
...
@@ -44,7 +49,7 @@ func main() {
cli
.
ShowAppHelp
(
c
)
cli
.
ShowAppHelp
(
c
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
}
}
app
:=
app
.
New
(
c
.
String
(
"addr"
),
c
.
String
(
"port"
),
c
.
Bool
(
"permit-write"
),
c
.
String
(
"credential"
),
c
.
Args
())
app
:=
app
.
New
(
c
.
String
(
"addr"
),
c
.
String
(
"port"
),
c
.
Bool
(
"permit-write"
),
c
.
String
(
"credential"
),
c
.
Bool
(
"random-url"
),
c
.
Args
())
err
:=
app
.
Run
()
err
:=
app
.
Run
()
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
err
)
fmt
.
Println
(
err
)
...
...
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