-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
142 additions
and
54 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package httpgo | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// ServeDirectoryWithAuth 启动一个带有基本身份验证的文件服务器 | ||
func ServeDirectoryWithAuth(dir, username, password string, port int) error { | ||
// 检查目录是否存在 | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
return err | ||
} | ||
|
||
// 创建一个文件服务器处理程序 | ||
fs := http.FileServer(http.Dir(dir)) | ||
|
||
// 使用 BasicAuth 中间件保护文件服务器 | ||
protectedFS := BasicAuth(fs, username, password) | ||
|
||
// 启动 Web 服务器 | ||
addr := ":" + strconv.Itoa(port) | ||
log.Printf("Serving %s on HTTP port %d\n", dir, port) | ||
return http.ListenAndServe(addr, protectedFS) | ||
} | ||
|
||
// BasicAuth 是一个中间件函数,用于实现基本身份验证 | ||
func BasicAuth(next http.Handler, username, password string) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
user, pass, ok := r.BasicAuth() | ||
if !ok || user != username || pass != password { | ||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
return | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
var Charset1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
|
||
func GenerateRandomString(length int) string { | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
// 创建一个字节数组来存储生成的随机字符 | ||
b := make([]byte, length) | ||
|
||
// 随机选择字符集中的字符并填充到字节数组中 | ||
for i := range b { | ||
b[i] = Charset1[rand.Intn(len(Charset1))] | ||
} | ||
|
||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters