|
|
|
@ -1,7 +1,8 @@
|
|
|
|
|
package tools
|
|
|
|
|
package httpserver
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
@ -11,90 +12,50 @@ import (
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"b612.me/starainrt"
|
|
|
|
|
"b612.me/starcrypto"
|
|
|
|
|
|
|
|
|
|
"b612.me/starlog"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"b612.me/staros"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var port, ip, path string
|
|
|
|
|
var up bool
|
|
|
|
|
var basicAuth, certKey string
|
|
|
|
|
|
|
|
|
|
type TraceHandler struct {
|
|
|
|
|
h http.Handler
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
type VtqeHttpServer struct {
|
|
|
|
|
basicAuth string
|
|
|
|
|
path string
|
|
|
|
|
upload bool
|
|
|
|
|
version string
|
|
|
|
|
indexFile string
|
|
|
|
|
stopMime bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// httpCmd represents the http command
|
|
|
|
|
var httpcmd = &cobra.Command{
|
|
|
|
|
Use: "http",
|
|
|
|
|
Short: "HTTP文件服务器",
|
|
|
|
|
Long: `HTTP文件服务器`,
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
func RunHttpServer(listenIp, port, auth, folderPath, certKey, version, indexFile string, doUpload, stopMime bool) error {
|
|
|
|
|
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)
|
|
|
|
|
var vtqe VtqeHttpServer = VtqeHttpServer{auth, folderPath, doUpload, version, indexFile, stopMime}
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
if certKey == "" {
|
|
|
|
|
err = http.ListenAndServe(ip+":"+port, nil)
|
|
|
|
|
err = http.ListenAndServe(listenIp+":"+port, nil)
|
|
|
|
|
} else {
|
|
|
|
|
certs := strings.Split(certKey, ":")
|
|
|
|
|
if len(certs) != 2 {
|
|
|
|
|
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 {
|
|
|
|
|
starlog.Criticalln("Error:" + err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
func (v VtqeHttpServer) httpListen(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
log := starlog.Std.NewFlag()
|
|
|
|
|
w.Header().Set("Server", "Vicorique")
|
|
|
|
|
w.Header().Set("Server", "Victorique")
|
|
|
|
|
w.Header().Set("Powered", "B612.ME")
|
|
|
|
|
write401 := func() {
|
|
|
|
|
w.Header().Set("WWW-Authenticate", ` Basic realm="Please Enter Passwd"`)
|
|
|
|
@ -108,14 +69,14 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
</body>
|
|
|
|
|
</html>`))
|
|
|
|
|
}
|
|
|
|
|
if basicAuth != "" {
|
|
|
|
|
if v.basicAuth != "" {
|
|
|
|
|
authHeader := strings.TrimSpace(r.Header.Get("Authorization"))
|
|
|
|
|
if len(authHeader) == 0 {
|
|
|
|
|
log.Noticeln("No Authed! Get Path is", r.URL.Path, r.RemoteAddr)
|
|
|
|
|
write401()
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
userAuth := base64.StdEncoding.EncodeToString([]byte(basicAuth))
|
|
|
|
|
userAuth := base64.StdEncoding.EncodeToString([]byte(v.basicAuth))
|
|
|
|
|
authStr := strings.Split(authHeader, " ")
|
|
|
|
|
if strings.TrimSpace(authStr[1]) != userAuth {
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
fullpath, _ := filepath.Abs(path + p)
|
|
|
|
|
|
|
|
|
|
if p == "/" {
|
|
|
|
|
ReadFolder(w, r, fullpath, true)
|
|
|
|
|
v.readFolder(w, r, fullpath, true)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if up {
|
|
|
|
|
if v.upload {
|
|
|
|
|
if p == "/vtqeupload1127/web" {
|
|
|
|
|
w.Write([]byte(`<html><body><form id= "uploadForm" action= "../vtqeupload1127" method= "post" enctype ="multipart/form-data">
|
|
|
|
|
<h1 >B612 File Upload Page </h1>
|
|
|
|
@ -144,7 +111,7 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !starainrt.Exists(fullpath) {
|
|
|
|
|
if !staros.Exists(fullpath) {
|
|
|
|
|
w.WriteHeader(404)
|
|
|
|
|
if len(cmd) != 0 {
|
|
|
|
|
if cmd[0] == "header" {
|
|
|
|
@ -158,8 +125,8 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Write([]byte("<h1>404 NOT FOUND</h1>"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if starainrt.IsFolder(fullpath) {
|
|
|
|
|
ReadFolder(w, r, fullpath, false)
|
|
|
|
|
if staros.IsFolder(fullpath) {
|
|
|
|
|
v.readFolder(w, r, fullpath, false)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fpdst, err := os.Open(fullpath)
|
|
|
|
@ -171,26 +138,16 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
fpinfo, _ := os.Stat(fullpath)
|
|
|
|
|
name := filepath.Base(fullpath)
|
|
|
|
|
tmp := strings.Split(name, ".")
|
|
|
|
|
ext := ""
|
|
|
|
|
if len(tmp) >= 2 {
|
|
|
|
|
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:
|
|
|
|
|
ext := filepath.Ext(name)
|
|
|
|
|
mime := GetMIME(ext)
|
|
|
|
|
if mime == "" || v.stopMime {
|
|
|
|
|
w.Header().Set("Content-Type", "application/download")
|
|
|
|
|
w.Header().Set("Content-Disposition", "attachment;filename="+name)
|
|
|
|
|
}
|
|
|
|
|
etag := new(starainrt.StarCrypto).MD5([]byte(fpinfo.ModTime().String()))
|
|
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
|
} else {
|
|
|
|
|
w.Header().Set("Content-Type", mime)
|
|
|
|
|
}
|
|
|
|
|
etag := starcrypto.Md5Str([]byte(fpinfo.ModTime().String()))
|
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
|
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"))
|
|
|
|
@ -297,15 +254,15 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
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)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorln(err)
|
|
|
|
|
w.WriteHeader(403)
|
|
|
|
|
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>"))
|
|
|
|
|
if up {
|
|
|
|
|
w.Write([]byte("<html>\n<style>\np{margin: 2px auto}\n</style>\n<h1>B612 Http Server - " + v.version + "</h1>"))
|
|
|
|
|
if v.upload {
|
|
|
|
|
w.Write([]byte("<a href=/vtqeupload1127/web>Upload Web Page Is Openned!</a><br /><br />"))
|
|
|
|
|
}
|
|
|
|
|
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>"))
|
|
|
|
|
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)
|
|
|
|
|
}
|