|
|
@ -2,6 +2,7 @@ package httpserver
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/base64"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
@ -11,38 +12,51 @@ import (
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"b612.me/starainrt"
|
|
|
|
"b612.me/starcrypto"
|
|
|
|
|
|
|
|
|
|
|
|
"b612.me/starlog"
|
|
|
|
"b612.me/starlog"
|
|
|
|
|
|
|
|
"b612.me/staros"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func RunHttpServer(listenIp, port, basicAuth, path, cert string, doUpload bool) {
|
|
|
|
type VtqeHttpServer struct {
|
|
|
|
|
|
|
|
basicAuth string
|
|
|
|
|
|
|
|
path string
|
|
|
|
|
|
|
|
upload bool
|
|
|
|
|
|
|
|
version string
|
|
|
|
|
|
|
|
indexFile string
|
|
|
|
|
|
|
|
stopMime bool
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func RunHttpServer(listenIp, port, auth, folderPath, certKey, version, indexFile string, doUpload, stopMime bool) error {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
http.HandleFunc("/", httplisten)
|
|
|
|
var vtqe VtqeHttpServer = VtqeHttpServer{auth, folderPath, doUpload, version, indexFile, stopMime}
|
|
|
|
path, _ = filepath.Abs(path)
|
|
|
|
http.HandleFunc("/", vtqe.httpListen)
|
|
|
|
starlog.Infoln("Listening On Port:" + port)
|
|
|
|
starlog.Noticeln("Listening On " + listenIp + ":" + port)
|
|
|
|
if up {
|
|
|
|
if doUpload {
|
|
|
|
starlog.Infoln("upload is openned,path is /vtqeupload1127")
|
|
|
|
starlog.Noticeln("upload is openned,path is /vtqeupload1127")
|
|
|
|
http.HandleFunc("/vtqeupload1127", uploadfile)
|
|
|
|
http.HandleFunc("/vtqeupload1127", vtqe.uploadFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if certKey == "" {
|
|
|
|
if certKey == "" {
|
|
|
|
err = http.ListenAndServe(ip+":"+port, nil)
|
|
|
|
err = http.ListenAndServe(listenIp+":"+port, nil)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
certs := strings.Split(certKey, ":")
|
|
|
|
certs := strings.Split(certKey, ":")
|
|
|
|
if len(certs) != 2 {
|
|
|
|
if len(certs) != 2 {
|
|
|
|
starlog.Criticalln("证书不正确!")
|
|
|
|
starlog.Criticalln("证书不正确!")
|
|
|
|
return
|
|
|
|
return errors.New("ZSBZQ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = http.ListenAndServeTLS(ip+":"+port, certs[0], certs[1], nil)
|
|
|
|
err = http.ListenAndServeTLS(listenIp+":"+port, certs[0], certs[1], nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
starlog.Criticalln("Error:" + err.Error())
|
|
|
|
starlog.Criticalln("Error:" + err.Error())
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (v VtqeHttpServer) httpListen(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log := starlog.Std.NewFlag()
|
|
|
|
log := starlog.Std.NewFlag()
|
|
|
|
w.Header().Set("Server", "Vicorique")
|
|
|
|
w.Header().Set("Server", "Vicorique")
|
|
|
|
w.Header().Set("Powered", "B612.ME")
|
|
|
|
w.Header().Set("Powered By", "B612.ME")
|
|
|
|
write401 := func() {
|
|
|
|
write401 := func() {
|
|
|
|
w.Header().Set("WWW-Authenticate", ` Basic realm="Please Enter Passwd"`)
|
|
|
|
w.Header().Set("WWW-Authenticate", ` Basic realm="Please Enter Passwd"`)
|
|
|
|
w.WriteHeader(401)
|
|
|
|
w.WriteHeader(401)
|
|
|
@ -55,14 +69,14 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
</body>
|
|
|
|
</body>
|
|
|
|
</html>`))
|
|
|
|
</html>`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if basicAuth != "" {
|
|
|
|
if v.basicAuth != "" {
|
|
|
|
authHeader := strings.TrimSpace(r.Header.Get("Authorization"))
|
|
|
|
authHeader := strings.TrimSpace(r.Header.Get("Authorization"))
|
|
|
|
if len(authHeader) == 0 {
|
|
|
|
if len(authHeader) == 0 {
|
|
|
|
log.Noticeln("No Authed! Get Path is", r.URL.Path, r.RemoteAddr)
|
|
|
|
log.Noticeln("No Authed! Get Path is", r.URL.Path, r.RemoteAddr)
|
|
|
|
write401()
|
|
|
|
write401()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
userAuth := base64.StdEncoding.EncodeToString([]byte(basicAuth))
|
|
|
|
userAuth := base64.StdEncoding.EncodeToString([]byte(v.basicAuth))
|
|
|
|
authStr := strings.Split(authHeader, " ")
|
|
|
|
authStr := strings.Split(authHeader, " ")
|
|
|
|
if strings.TrimSpace(authStr[1]) != userAuth {
|
|
|
|
if strings.TrimSpace(authStr[1]) != userAuth {
|
|
|
|
log.Noticeln("Auth Failed! Get Path is", r.URL.Path, r.RemoteAddr, "pwd enter is", authHeader)
|
|
|
|
log.Noticeln("Auth Failed! Get Path is", r.URL.Path, r.RemoteAddr, "pwd enter is", authHeader)
|
|
|
@ -73,14 +87,20 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p := r.URL.Path
|
|
|
|
p := r.URL.Path
|
|
|
|
cmd := r.URL.Query()["cmd"]
|
|
|
|
cmd := r.URL.Query()["cmd"]
|
|
|
|
|
|
|
|
fullpath, _ := filepath.Abs(v.path + p)
|
|
|
|
|
|
|
|
if p == "/" && v.indexFile != "" {
|
|
|
|
|
|
|
|
tmppath, _ := filepath.Abs(v.path + "/" + v.indexFile)
|
|
|
|
|
|
|
|
if staros.Exists(tmppath) {
|
|
|
|
|
|
|
|
fullpath = tmppath
|
|
|
|
|
|
|
|
p = "/" + v.indexFile
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
log.Noticeln("Get " + p + " " + r.RemoteAddr)
|
|
|
|
log.Noticeln("Get " + p + " " + r.RemoteAddr)
|
|
|
|
fullpath, _ := filepath.Abs(path + p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if p == "/" {
|
|
|
|
if p == "/" {
|
|
|
|
ReadFolder(w, r, fullpath, true)
|
|
|
|
v.readFolder(w, r, fullpath, true)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if up {
|
|
|
|
if v.upload {
|
|
|
|
if p == "/vtqeupload1127/web" {
|
|
|
|
if p == "/vtqeupload1127/web" {
|
|
|
|
w.Write([]byte(`<html><body><form id= "uploadForm" action= "../vtqeupload1127" method= "post" enctype ="multipart/form-data">
|
|
|
|
w.Write([]byte(`<html><body><form id= "uploadForm" action= "../vtqeupload1127" method= "post" enctype ="multipart/form-data">
|
|
|
|
<h1 >B612 File Upload Page </h1>
|
|
|
|
<h1 >B612 File Upload Page </h1>
|
|
|
@ -91,7 +111,7 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !starainrt.Exists(fullpath) {
|
|
|
|
if !staros.Exists(fullpath) {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.WriteHeader(404)
|
|
|
|
if len(cmd) != 0 {
|
|
|
|
if len(cmd) != 0 {
|
|
|
|
if cmd[0] == "header" {
|
|
|
|
if cmd[0] == "header" {
|
|
|
@ -105,8 +125,8 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("<h1>404 NOT FOUND</h1>"))
|
|
|
|
w.Write([]byte("<h1>404 NOT FOUND</h1>"))
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if starainrt.IsFolder(fullpath) {
|
|
|
|
if staros.IsFolder(fullpath) {
|
|
|
|
ReadFolder(w, r, fullpath, false)
|
|
|
|
v.readFolder(w, r, fullpath, false)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fpdst, err := os.Open(fullpath)
|
|
|
|
fpdst, err := os.Open(fullpath)
|
|
|
@ -118,26 +138,16 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fpinfo, _ := os.Stat(fullpath)
|
|
|
|
fpinfo, _ := os.Stat(fullpath)
|
|
|
|
name := filepath.Base(fullpath)
|
|
|
|
name := filepath.Base(fullpath)
|
|
|
|
tmp := strings.Split(name, ".")
|
|
|
|
ext := filepath.Ext(name)[1:]
|
|
|
|
ext := ""
|
|
|
|
mime := GetMIME(ext)
|
|
|
|
if len(tmp) >= 2 {
|
|
|
|
if mime == "" || v.stopMime {
|
|
|
|
ext = strings.ToLower(tmp[len(tmp)-1])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ext {
|
|
|
|
|
|
|
|
case "jpeg", "jpg", "jpe":
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
|
|
|
|
|
|
case "mpeg", "mpg", "mkv":
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "video/mpeg")
|
|
|
|
|
|
|
|
case "mp4":
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "video/mp4")
|
|
|
|
|
|
|
|
case "pdf":
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/pdf")
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/download")
|
|
|
|
w.Header().Set("Content-Type", "application/download")
|
|
|
|
w.Header().Set("Content-Disposition", "attachment;filename="+name)
|
|
|
|
w.Header().Set("Content-Disposition", "attachment;filename="+name)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", mime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
etag := new(starainrt.StarCrypto).MD5([]byte(fpinfo.ModTime().String()))
|
|
|
|
etag := starcrypto.Md5Str([]byte(fpinfo.ModTime().String()))
|
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
w.Header().Set("ETag", etag)
|
|
|
|
w.Header().Set("ETag", etag)
|
|
|
|
w.Header().Set("Last-Modified", strings.ReplaceAll(fpinfo.ModTime().UTC().Format("Mon, 2 Jan 2006 15:04:05 MST"), "UTC", "GMT"))
|
|
|
|
w.Header().Set("Last-Modified", strings.ReplaceAll(fpinfo.ModTime().UTC().Format("Mon, 2 Jan 2006 15:04:05 MST"), "UTC", "GMT"))
|
|
|
@ -244,15 +254,15 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Infoln(fpinfo.Name(), "客户端下载已结束,共传输大小:"+tani)
|
|
|
|
log.Infoln(fpinfo.Name(), "客户端下载已结束,共传输大小:"+tani)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot bool) {
|
|
|
|
func (v VtqeHttpServer) readFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot bool) {
|
|
|
|
dir, err := ioutil.ReadDir(fullpath)
|
|
|
|
dir, err := ioutil.ReadDir(fullpath)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
starlog.Errorln(err)
|
|
|
|
starlog.Errorln(err)
|
|
|
|
w.WriteHeader(403)
|
|
|
|
w.WriteHeader(403)
|
|
|
|
w.Write([]byte("<h1>May Cannot Access!</h1>"))
|
|
|
|
w.Write([]byte("<h1>May Cannot Access!</h1>"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte("<html>\n<style>\np{margin: 2px auto}\n</style>\n<h1>B612 Http Server - " + Version + "</h1>"))
|
|
|
|
w.Write([]byte("<html>\n<style>\np{margin: 2px auto}\n</style>\n<h1>B612 Http Server - " + v.version + "</h1>"))
|
|
|
|
if up {
|
|
|
|
if v.upload {
|
|
|
|
w.Write([]byte("<a href=/vtqeupload1127/web>Upload Web Page Is Openned!</a><br /><br />"))
|
|
|
|
w.Write([]byte("<a href=/vtqeupload1127/web>Upload Web Page Is Openned!</a><br /><br />"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte("<hr /><pre>\n"))
|
|
|
|
w.Write([]byte("<hr /><pre>\n"))
|
|
|
@ -277,7 +287,7 @@ func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func uploadfile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (v VtqeHttpServer) uploadFile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
w.Write([]byte("USE POST METHOD!"))
|
|
|
|
w.Write([]byte("USE POST METHOD!"))
|
|
|
|
return
|
|
|
|
return
|
|
|
|