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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
12 deletions
app/app.go
main.go
app/app.go
View file @
b15227c
package
app
import
(
"crypto/rand"
"encoding/base64"
"encoding/json"
"log"
"math/big"
"net/http"
"os/exec"
"strconv"
"strings"
"syscall"
"unsafe"
...
...
@@ -20,16 +23,18 @@ type App struct {
Address
string
Port
string
PermitWrite
bool
RandomUrl
bool
Credential
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
{
Address
:
address
,
Port
:
port
,
PermitWrite
:
permitWrite
,
Credential
:
cred
,
RandomUrl
:
randomUrl
,
Command
:
command
,
}
}
...
...
@@ -65,21 +70,24 @@ func basicAuthHandler(h http.Handler, cred string) http.Handler {
}
func
(
app
*
App
)
Run
()
error
{
http
.
Handle
(
"/"
,
http
.
FileServer
(
&
assetfs
.
AssetFS
{
Asset
:
Asset
,
AssetDir
:
AssetDir
,
Prefix
:
"bindata"
},
),
)
http
.
HandleFunc
(
"/ws"
,
app
.
generateHandler
())
url
:=
app
.
Address
+
":"
+
app
.
Port
log
.
Printf
(
"Server is running at %s, command: %s"
,
url
,
strings
.
Join
(
app
.
Command
,
" "
))
path
:=
"/"
if
app
.
RandomUrl
{
randomPath
:=
generateRandomString
(
8
)
path
=
"/"
+
randomPath
+
"/"
}
fs
:=
http
.
StripPrefix
(
path
,
http
.
FileServer
(
&
assetfs
.
AssetFS
{
Asset
:
Asset
,
AssetDir
:
AssetDir
,
Prefix
:
"bindata"
}))
http
.
Handle
(
path
,
fs
)
http
.
HandleFunc
(
path
+
"ws"
,
app
.
generateHandler
())
endpoint
:=
app
.
Address
+
":"
+
app
.
Port
log
.
Printf
(
"Server is running at %s, command: %s"
,
endpoint
+
path
,
strings
.
Join
(
app
.
Command
,
" "
))
handler
:=
http
.
Handler
(
http
.
DefaultServeMux
)
handler
=
loggerHandler
(
handler
)
if
app
.
Credential
!=
""
{
handler
=
basicAuthHandler
(
handler
,
app
.
Credential
)
}
err
:=
http
.
ListenAndServe
(
url
,
handler
)
err
:=
http
.
ListenAndServe
(
endpoint
,
handler
)
if
err
!=
nil
{
return
err
}
...
...
@@ -227,3 +235,14 @@ type command struct {
Name
string
`json:"name"`
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() {
Usage
:
"Credential for Basic Authentication (ex: user:pass)"
,
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
)
{
if
len
(
c
.
Args
())
==
0
{
...
...
@@ -44,7 +49,7 @@ func main() {
cli
.
ShowAppHelp
(
c
)
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
()
if
err
!=
nil
{
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