|
|
@ -1,7 +1,8 @@
|
|
|
|
package tools
|
|
|
|
package httpserver
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/base64"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
@ -11,90 +12,50 @@ import (
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"b612.me/starainrt"
|
|
|
|
"b612.me/starcrypto"
|
|
|
|
|
|
|
|
|
|
|
|
"b612.me/starlog"
|
|
|
|
"b612.me/starlog"
|
|
|
|
|
|
|
|
"b612.me/staros"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var port, ip, path string
|
|
|
|
type VtqeHttpServer struct {
|
|
|
|
var up bool
|
|
|
|
basicAuth string
|
|
|
|
var basicAuth, certKey string
|
|
|
|
path string
|
|
|
|
|
|
|
|
upload bool
|
|
|
|
type TraceHandler struct {
|
|
|
|
version string
|
|
|
|
h http.Handler
|
|
|
|
indexFile string
|
|
|
|
}
|
|
|
|
stopMime bool
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
httpcmd.Flags().StringVarP(&port, "port", "p", "80", "监听端口")
|
|
|
|
|
|
|
|
httpcmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "监听ip")
|
|
|
|
|
|
|
|
httpcmd.Flags().StringVarP(&path, "folder", "f", "./", "本地文件地址")
|
|
|
|
|
|
|
|
httpcmd.Flags().BoolVarP(&up, "upload", "u", false, "是否开启文件上传")
|
|
|
|
|
|
|
|
httpcmd.Flags().StringVarP(&basicAuth, "auth", "a", "", "HTTP BASIC AUTH认证(用户名:密码)")
|
|
|
|
|
|
|
|
httpcmd.Flags().StringVarP(&certKey, "cert", "c", "", "TLS证书路径,用:分割证书与密钥")
|
|
|
|
|
|
|
|
Maincmd.AddCommand(httpcmd)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// httpCmd represents the http command
|
|
|
|
|
|
|
|
var httpcmd = &cobra.Command{
|
|
|
|
|
|
|
|
Use: "http",
|
|
|
|
|
|
|
|
Short: "HTTP文件服务器",
|
|
|
|
|
|
|
|
Long: `HTTP文件服务器`,
|
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
http.HandleFunc("/", httplisten)
|
|
|
|
|
|
|
|
path, _ = filepath.Abs(path)
|
|
|
|
|
|
|
|
starlog.Infoln("Listening On Port:" + port)
|
|
|
|
|
|
|
|
if up {
|
|
|
|
|
|
|
|
starlog.Infoln("upload is openned,path is /vtqeupload1127")
|
|
|
|
|
|
|
|
http.HandleFunc("/vtqeupload1127", uploadfile)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if certKey == "" {
|
|
|
|
|
|
|
|
err = http.ListenAndServe(ip+":"+port, nil)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
certs := strings.Split(certKey, ":")
|
|
|
|
|
|
|
|
if len(certs) != 2 {
|
|
|
|
|
|
|
|
starlog.Criticalln("证书不正确!")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = http.ListenAndServeTLS(ip+":"+port, certs[0], certs[1], nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Criticalln("Error:" + err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func uploadfile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func RunHttpServer(listenIp, port, auth, folderPath, certKey, version, indexFile string, doUpload, stopMime bool) error {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
var err error
|
|
|
|
w.Write([]byte("USE POST METHOD!"))
|
|
|
|
var vtqe VtqeHttpServer = VtqeHttpServer{auth, folderPath, doUpload, version, indexFile, stopMime}
|
|
|
|
return
|
|
|
|
http.HandleFunc("/", vtqe.httpListen)
|
|
|
|
|
|
|
|
starlog.Noticeln("Listening On " + listenIp + ":" + port)
|
|
|
|
|
|
|
|
if doUpload {
|
|
|
|
|
|
|
|
starlog.Noticeln("upload is openned,path is /vtqeupload1127")
|
|
|
|
|
|
|
|
http.HandleFunc("/vtqeupload1127", vtqe.uploadFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.ParseMultipartForm(10485760)
|
|
|
|
if certKey == "" {
|
|
|
|
file, handler, err := r.FormFile("victorique")
|
|
|
|
err = http.ListenAndServe(listenIp+":"+port, nil)
|
|
|
|
if err != nil {
|
|
|
|
} else {
|
|
|
|
starlog.Errorln(err)
|
|
|
|
certs := strings.Split(certKey, ":")
|
|
|
|
w.WriteHeader(502)
|
|
|
|
if len(certs) != 2 {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
starlog.Criticalln("证书不正确!")
|
|
|
|
return
|
|
|
|
return errors.New("ZSBZQ")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = http.ListenAndServeTLS(listenIp+":"+port, certs[0], certs[1], nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
starlog.Noticef("Upload %s From %s\n", handler.Filename, r.RemoteAddr)
|
|
|
|
|
|
|
|
fmt.Fprintf(w, `<html><body><p>%v</p><h2><a href="./vtqeupload1127/web">Return To Web Page</a></h2></body></html>`, handler.Header)
|
|
|
|
|
|
|
|
os.Mkdir("./vtqeupload1127", 0755)
|
|
|
|
|
|
|
|
f, err := os.OpenFile("./vtqeupload1127/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0755)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
starlog.Errorln(err)
|
|
|
|
starlog.Criticalln("Error:" + err.Error())
|
|
|
|
return
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
return nil
|
|
|
|
io.Copy(f, file)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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", "Victorique")
|
|
|
|
w.Header().Set("Powered", "B612.ME")
|
|
|
|
w.Header().Set("Powered", "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"`)
|
|
|
@ -108,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)
|
|
|
@ -126,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>
|
|
|
@ -144,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" {
|
|
|
@ -158,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)
|
|
|
@ -171,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)
|
|
|
|
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"))
|
|
|
@ -297,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"))
|
|
|
@ -329,3 +286,29 @@ func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot
|
|
|
|
w.Write([]byte("</pre>\n</html>"))
|
|
|
|
w.Write([]byte("</pre>\n</html>"))
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (v VtqeHttpServer) uploadFile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
if r.Method != "POST" {
|
|
|
|
|
|
|
|
w.Write([]byte("USE POST METHOD!"))
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ParseMultipartForm(10485760)
|
|
|
|
|
|
|
|
file, handler, err := r.FormFile("victorique")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Errorln(err)
|
|
|
|
|
|
|
|
w.WriteHeader(502)
|
|
|
|
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
starlog.Noticef("Upload %s From %s\n", handler.Filename, r.RemoteAddr)
|
|
|
|
|
|
|
|
fmt.Fprintf(w, `<html><body><p>%v</p><h2><a href="./vtqeupload1127/web">Return To Web Page</a></h2></body></html>`, handler.Header)
|
|
|
|
|
|
|
|
os.Mkdir("./vtqeupload1127", 0755)
|
|
|
|
|
|
|
|
f, err := os.OpenFile("./vtqeupload1127/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0755)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Errorln(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
io.Copy(f, file)
|
|
|
|
|
|
|
|
}
|