This commit is contained in:
chenwenxiao
2019-02-21 15:41:09 +08:00
parent 9442e00997
commit f9f3142543
6 changed files with 123 additions and 31 deletions

View File

@@ -111,6 +111,12 @@ docker run -it --rm -p 8000:8000 -v $PWD:/app/public --name gohttpserver \
$ gohttpserver --auth-type openid --auth-openid https://login.example-hostname.com/openid/
```
- Use oauth2 with
```sh
$ gohttpserver --auth-type oauth2-proxy
```
- Enable upload
```sh

View File

@@ -49,6 +49,19 @@
</a>
</template>
[[end]]
[[if eq .AuthType "oauth2-proxy"]]
<template v-if="!user.email">
<a href="#" class="btn btn-sm btn-default navbar-btn">
Guest <span class="glyphicon glyphicon-user"></span>
</a>
</template>
<template v-else>
<a href="/-/logout" class="btn btn-sm btn-default navbar-btn">
<span v-text="user.name"></span>
<i class="fa fa-sign-out"></i>
</a>
</template>
[[end]]
</ul>
<form class="navbar-form navbar-right">
<div class="input-group">
@@ -132,6 +145,9 @@
<span class="hidden-xs">Archive</span> Zip
<span class="glyphicon glyphicon-download-alt"></span>
</a>
<button class="btn btn-default btn-xs" v-on:click="showInfo(f)">
<span class="glyphicon glyphicon-info-sign"></span>
</button>
<button class="btn btn-default btn-xs" v-if="auth.delete" v-on:click="deletePathConfirm(f, $event)">
<span style="color:#CC3300" class="glyphicon glyphicon-trash"></span>
</button>

View File

@@ -233,7 +233,10 @@ var vm = new Vue({
showInfo: function (f) {
console.log(f);
$.ajax({
url: pathJoin(["/-/info", location.pathname, f.name]),
url: pathJoin(["/", location.pathname, f.name]),
data: {
op: "info",
},
method: "GET",
success: function (res) {
$("#file-info-title").text(f.name);
@@ -250,9 +253,9 @@ var vm = new Vue({
return
}
$.ajax({
url: pathJoin(["/-/mkdir", location.pathname]),
url: pathJoin(["/", location.pathname, "/", name]),
data: {
name: name,
op: "mkdir",
},
method: "POST",
success: function (res) {
@@ -282,8 +285,9 @@ var vm = new Vue({
}
});
},
updateBreadcrumb: function () {
var pathname = decodeURI(location.pathname || "/");
updateBreadcrumb: function (pathname) {
var pathname = decodeURI(pathname || location.pathname || "/");
pathname = pathname.split('?')[0]
var parts = pathname.split('/');
this.breadcrumb = [];
if (pathname == "/") {
@@ -343,16 +347,21 @@ window.onpopstate = function (event) {
function loadFileOrDir(reqPath) {
let requestUri = reqPath + location.search
window.history.pushState({}, "", requestUri);
loadFileList(requestUri)
var retObj = loadFileList(requestUri)
if(retObj !== null) {
retObj.done(function (value) {
window.history.pushState({}, "", requestUri);
});
}
}
function loadFileList(pathname) {
var pathname = pathname || location.pathname + location.search;
// console.log("load filelist:", pathname)
var retObj = null
if (getQueryString("raw") !== "false") { // not a file preview
let sep = pathname.indexOf("?") === -1 ? "?" : "&"
$.ajax({
var sep = pathname.indexOf("?") === -1 ? "?" : "&"
retObj = $.ajax({
url: pathname + sep + "json=true",
dataType: "json",
cache: false,
@@ -361,22 +370,27 @@ function loadFileList(pathname) {
var weight = f.type == 'dir' ? 1000 : 1;
return -weight * f.mtime;
})
vm.files = res.files;
vm.auth = res.auth;
},
error: function (err) {
console.error(err)
error: function (jqXHR, textStatus, errorThrown) {
let errMsg = jqXHR.getResponseHeader("x-auth-authentication-message")
if(errMsg==null){
errMsg = jqXHR.statusText
}
alert(String(jqXHR.status).concat(":", errMsg));
console.error(errMsg)
},
});
}
vm.updateBreadcrumb();
vm.updateBreadcrumb(pathname);
vm.previewMode = getQueryString("raw") == "false";
if (vm.previewMode) {
vm.loadPreviewFile();
}
return retObj
}
Vue.filter('fromNow', function (value) {

View File

@@ -83,21 +83,21 @@ func NewHTTPStaticServer(root string) *HTTPStaticServer {
}
}()
m.HandleFunc("/-/status", s.hStatus)
m.HandleFunc("/-/zip/{path:.*}", s.hZip)
m.HandleFunc("/-/unzip/{zip_path:.*}/-/{path:.*}", s.hUnzip)
m.HandleFunc("/-/json/{path:.*}", s.hJSONList)
m.HandleFunc("/-/status", s.hStatus) //unused
m.HandleFunc("/-/zip/{path:.*}", s.hZip) //unused
m.HandleFunc("/-/unzip/{zip_path:.*}/-/{path:.*}", s.hUnzip) //unused
m.HandleFunc("/-/json/{path:.*}", s.hJSONList) //unused
// routers for Apple *.ipa
m.HandleFunc("/-/ipa/plist/{path:.*}", s.hPlist)
m.HandleFunc("/-/ipa/link/{path:.*}", s.hIpaLink)
m.HandleFunc("/-/ipa/plist/{path:.*}", s.hPlist) //unused
m.HandleFunc("/-/ipa/link/{path:.*}", s.hIpaLink) //unused
// TODO: /ipa/info
m.HandleFunc("/-/info/{path:.*}", s.hInfo)
m.HandleFunc("/-/mkdir/{path:.*}", s.hMkdir)
m.HandleFunc("/-/info/{path:.*}", s.hInfo) //unused
m.HandleFunc("/-/mkdir/{path:.*}", s.hMkdir) //unused
m.HandleFunc("/{path:.*}", s.hIndex).Methods("GET", "HEAD")
m.HandleFunc("/{path:.*}", s.hUpload).Methods("POST")
m.HandleFunc("/{path:.*}", s.hDelete).Methods("DELETE")
m.HandleFunc("/{path:.*}", s.hGet).Methods("GET", "HEAD")
m.HandleFunc("/{path:.*}", s.hPOST).Methods("POST")
m.HandleFunc("/{path:.*}", s.hDELETE).Methods("DELETE")
return s
}
@@ -105,6 +105,31 @@ func (s *HTTPStaticServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.m.ServeHTTP(w, r)
}
func (s *HTTPStaticServer) hGet(w http.ResponseWriter, r *http.Request) {
if r.FormValue("op") == "info" {
fmt.Println("hGet op=info")
s.hInfo(w, r)
}else {
fmt.Println("hGet op not equal info")
s.hIndex(w, r)
}
return
}
func (s *HTTPStaticServer) hPOST(w http.ResponseWriter, r *http.Request) {
if r.FormValue("op") == "mkdir" {
s.hMkdir(w, r)
}else {
s.hUpload(w, r)
}
return
}
func (s *HTTPStaticServer) hDELETE(w http.ResponseWriter, r *http.Request) {
s.hDelete(w, r)
return
}
func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) {
path := mux.Vars(r)["path"]
relPath := filepath.Join(s.Root, path)
@@ -141,13 +166,14 @@ func (s *HTTPStaticServer) hStatus(w http.ResponseWriter, r *http.Request) {
}
func (s *HTTPStaticServer) hMkdir(w http.ResponseWriter, req *http.Request) {
path := mux.Vars(req)["path"]
path := filepath.Dir(mux.Vars(req)["path"])
auth := s.readAccessConf(path)
if !auth.canDelete(req) {
http.Error(w, "Mkdir forbidden", http.StatusForbidden)
return
}
name := req.FormValue("name")
name := filepath.Base(mux.Vars(req)["path"])
if err := checkFilename(name); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
@@ -293,10 +319,7 @@ func parseApkInfo(path string) (ai *ApkInfo) {
func (s *HTTPStaticServer) hInfo(w http.ResponseWriter, r *http.Request) {
path := mux.Vars(r)["path"]
relPath := filepath.Join(s.Root, path)
if !isFile(relPath) {
http.Error(w, "Not a file", 403)
return
}
fi, err := os.Stat(relPath)
if err != nil {
http.Error(w, err.Error(), 500)
@@ -315,6 +338,8 @@ func (s *HTTPStaticServer) hInfo(w http.ResponseWriter, r *http.Request) {
case ".apk":
fji.Type = "apk"
fji.Extra = parseApkInfo(relPath)
case "":
fji.Type = "Dir"
default:
fji.Type = "text"
}

View File

@@ -180,6 +180,8 @@ func main() {
handleOpenID(gcfg.Auth.OpenID, false) // FIXME(ssx): set secure default to false
// case "github":
// handleOAuth2ID(gcfg.Auth.Type, gcfg.Auth.ID, gcfg.Auth.Secret) // FIXME(ssx): set secure default to false
case "oauth2-proxy":
handleOauth2()
}
// CORS

29
oauth2-proxy.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import (
"net/http"
"encoding/json"
"net/url"
)
func handleOauth2() {
http.HandleFunc("/-/user", func(w http.ResponseWriter, r *http.Request) {
fullNameMap, _:= url.ParseQuery(r.Header.Get("X-Auth-Request-Fullname"))
var fullName string
for k := range fullNameMap {
fullName = k
break
}
user := &UserInfo{
Email: r.Header.Get("X-Auth-Request-Email"),
Name: fullName,
NickName: r.Header.Get("X-Auth-Request-User"),
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
data, _ := json.Marshal(user)
w.Write(data)
})
}