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 791e1e22
authored
Aug 20, 2015
by
mattn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
HTTP Basic Authentication support. Close #8
1 parent
937c5700
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
3 deletions
app/app.go
main.go
app/app.go
View file @
791e1e2
package
app
package
app
import
(
import
(
"encoding/base64"
"encoding/json"
"encoding/json"
"log"
"log"
"net/http"
"net/http"
...
@@ -19,18 +20,50 @@ type App struct {
...
@@ -19,18 +20,50 @@ type App struct {
Address
string
Address
string
Port
string
Port
string
PermitWrite
bool
PermitWrite
bool
Credential
string
Command
[]
string
Command
[]
string
}
}
func
New
(
address
string
,
port
string
,
permitWrite
bool
,
command
[]
string
)
*
App
{
func
New
(
address
string
,
port
string
,
permitWrite
bool
,
c
red
string
,
c
ommand
[]
string
)
*
App
{
return
&
App
{
return
&
App
{
Address
:
address
,
Address
:
address
,
Port
:
port
,
Port
:
port
,
PermitWrite
:
permitWrite
,
PermitWrite
:
permitWrite
,
Credential
:
cred
,
Command
:
command
,
Command
:
command
,
}
}
}
}
func
loggerHandler
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Printf
(
"%s %s"
,
r
.
Method
,
r
.
URL
.
Path
)
h
.
ServeHTTP
(
w
,
r
)
})
}
func
basicAuthHandler
(
h
http
.
Handler
,
cred
string
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
token
:=
strings
.
SplitN
(
r
.
Header
.
Get
(
"Authorization"
),
" "
,
2
)
if
len
(
token
)
!=
2
||
strings
.
ToLower
(
token
[
0
])
!=
"basic"
{
w
.
Header
()
.
Set
(
"WWW-Authenticate"
,
`Basic realm="GoTTY"`
)
http
.
Error
(
w
,
"Bad Request"
,
http
.
StatusUnauthorized
)
return
}
payload
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
token
[
1
])
if
err
!=
nil
{
http
.
Error
(
w
,
"Internal Server Error"
,
http
.
StatusInternalServerError
)
return
}
if
cred
!=
string
(
payload
)
{
w
.
Header
()
.
Set
(
"WWW-Authenticate"
,
`Basic realm="GoTTY"`
)
http
.
Error
(
w
,
"authorization failed"
,
http
.
StatusUnauthorized
)
return
}
h
.
ServeHTTP
(
w
,
r
)
})
}
func
(
app
*
App
)
Run
()
error
{
func
(
app
*
App
)
Run
()
error
{
http
.
Handle
(
"/"
,
http
.
Handle
(
"/"
,
http
.
FileServer
(
http
.
FileServer
(
...
@@ -41,7 +74,12 @@ func (app *App) Run() error {
...
@@ -41,7 +74,12 @@ func (app *App) Run() error {
url
:=
app
.
Address
+
":"
+
app
.
Port
url
:=
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"
,
url
,
strings
.
Join
(
app
.
Command
,
" "
))
err
:=
http
.
ListenAndServe
(
url
,
nil
)
handler
:=
http
.
Handler
(
http
.
DefaultServeMux
)
handler
=
loggerHandler
(
handler
)
if
app
.
Credential
!=
""
{
handler
=
basicAuthHandler
(
handler
,
app
.
Credential
)
}
err
:=
http
.
ListenAndServe
(
url
,
handler
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
main.go
View file @
791e1e2
...
@@ -32,6 +32,11 @@ func main() {
...
@@ -32,6 +32,11 @@ func main() {
Usage
:
"Permit clients to write to the TTY (BE CAREFUL)"
,
Usage
:
"Permit clients to write to the TTY (BE CAREFUL)"
,
EnvVar
:
"GOTTY_PERMIT_WRITE"
,
EnvVar
:
"GOTTY_PERMIT_WRITE"
,
},
},
cli
.
StringFlag
{
Name
:
"credential, c"
,
Usage
:
"Credential for Basic Authentication (ex: user:pass)"
,
EnvVar
:
"GOTTY_CREDENTIAL"
,
},
}
}
cmd
.
Action
=
func
(
c
*
cli
.
Context
)
{
cmd
.
Action
=
func
(
c
*
cli
.
Context
)
{
if
len
(
c
.
Args
())
==
0
{
if
len
(
c
.
Args
())
==
0
{
...
@@ -39,7 +44,7 @@ func main() {
...
@@ -39,7 +44,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
.
Args
())
app
:=
app
.
New
(
c
.
String
(
"addr"
),
c
.
String
(
"port"
),
c
.
Bool
(
"permit-write"
),
c
.
String
(
"credential"
),
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