Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
bitmask-vpn
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
anna petry
bitmask-vpn
Commits
33b9ba9a
Unverified
Commit
33b9ba9a
authored
4 years ago
by
Kali Kaneko
Browse files
Options
Downloads
Patches
Plain Diff
[feat] authentication token for webapi
parent
0f8eab4e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/backend/webapi.go
+34
-4
34 additions, 4 deletions
pkg/backend/webapi.go
pkg/bitmask/auth.go
+52
-0
52 additions, 0 deletions
pkg/bitmask/auth.go
with
86 additions
and
4 deletions
pkg/backend/webapi.go
+
34
−
4
View file @
33b9ba9a
...
...
@@ -4,8 +4,35 @@ import (
"fmt"
"log"
"net/http"
"os"
"0xacab.org/leap/bitmask-vpn/pkg/bitmask"
)
func
Adapt
(
h
http
.
Handler
,
adapters
...
Adapter
)
http
.
Handler
{
for
_
,
adapter
:=
range
adapters
{
h
=
adapter
(
h
)
}
return
h
}
type
Adapter
func
(
http
.
Handler
)
http
.
Handler
func
CheckAuth
(
token
string
)
Adapter
{
return
func
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
:=
r
.
Header
.
Get
(
"X-Auth-Token"
)
if
t
==
token
{
h
.
ServeHTTP
(
w
,
r
)
}
else
{
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
w
.
Write
([]
byte
(
"401 - Unauthorized"
))
}
})
}
}
func
webOn
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Println
(
"Web UI: on"
)
SwitchOn
()
...
...
@@ -24,12 +51,15 @@ func webStatus(w http.ResponseWriter, r *http.Request) {
func
webQuit
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Println
(
"Web UI: quit"
)
Quit
()
os
.
Exit
(
0
)
}
func
enableWebAPI
()
{
http
.
HandleFunc
(
"/vpn/start"
,
webOn
)
http
.
HandleFunc
(
"/vpn/stop"
,
webOff
)
http
.
HandleFunc
(
"/vpn/status"
,
webStatus
)
http
.
HandleFunc
(
"/vpn/quit"
,
webQuit
)
bitmask
.
GenerateAuthToken
()
auth
:=
CheckAuth
(
bitmask
.
ReadAuthToken
())
http
.
Handle
(
"/vpn/start"
,
Adapt
(
http
.
HandlerFunc
(
webOn
),
auth
))
http
.
Handle
(
"/vpn/stop"
,
Adapt
(
http
.
HandlerFunc
(
webOff
),
auth
))
http
.
Handle
(
"/vpn/status"
,
Adapt
(
http
.
HandlerFunc
(
webStatus
),
auth
))
http
.
Handle
(
"/vpn/quit"
,
Adapt
(
http
.
HandlerFunc
(
webQuit
),
auth
))
http
.
ListenAndServe
(
":8080"
,
nil
)
}
This diff is collapsed.
Click to expand it.
pkg/bitmask/auth.go
0 → 100644
+
52
−
0
View file @
33b9ba9a
package
bitmask
import
(
"io/ioutil"
"log"
"math/rand"
"os"
"runtime"
"strings"
"time"
)
/* functions for local authentication of control endpoints */
const
tokenPath
=
"/dev/shm/bitmask-token"
func
GenerateAuthToken
()
{
if
runtime
.
GOOS
!=
"linux"
{
log
.
Println
(
"Authentication token only implemented in linux at the moment."
)
return
}
t
:=
getRandomString
()
err
:=
ioutil
.
WriteFile
(
tokenPath
,
[]
byte
(
t
),
os
.
FileMode
(
int
(
0600
)))
if
err
!=
nil
{
log
.
Println
(
"Could not write authentication token."
)
}
}
func
ReadAuthToken
()
string
{
if
runtime
.
GOOS
!=
"linux"
{
log
.
Println
(
"Authentication token only implemented in linux at the moment."
)
return
""
}
token
,
err
:=
ioutil
.
ReadFile
(
tokenPath
)
if
err
!=
nil
{
log
.
Println
(
"Error reading token:"
,
err
)
}
return
string
(
token
)
}
func
getRandomString
()
string
{
rand
.
Seed
(
time
.
Now
()
.
UnixNano
())
chars
:=
[]
rune
(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
"abcdefghijklmnopqrstuvwxyz"
+
"0123456789"
)
length
:=
40
var
b
strings
.
Builder
for
i
:=
0
;
i
<
length
;
i
++
{
b
.
WriteRune
(
chars
[
rand
.
Intn
(
len
(
chars
))])
}
return
b
.
String
()
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment