Skip to content

Commit

Permalink
Add referer feature to config and update request handling
Browse files Browse the repository at this point in the history
  • Loading branch information
arloor committed Dec 27, 2023
1 parent 1e47a61 commit 37fe016
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"program": "${workspaceFolder}/cmd/${workspaceFolderBasename}",
"args": [
"--addr=localhost:7788",
"--refer=arloor"
// "--tls=true"
]
}
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
LogPath string `yaml:"log"`
WebPath string `yaml:"content"`
BasicAuth string `yaml:"auth"`
Refer string `yaml:"refer"`
}

var Instance Config
Expand All @@ -29,6 +30,7 @@ func init() {
flag.StringVar(&Instance.LogPath, "log", "/tmp/proxy.log", "日志文件路径")
flag.StringVar(&Instance.WebPath, "content", ".", "文件服务器目录")
flag.StringVar(&Instance.BasicAuth, "auth", "", "Basic Auth Header")
flag.StringVar(&Instance.Refer, "refer", "", "本站的referer特征")
flag.Parse()
initLog()
out, err := yaml.Marshal(Instance)
Expand Down
4 changes: 4 additions & 0 deletions internal/server/httpHandlerFunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func writeIp(w http.ResponseWriter, r *http.Request) {
func fileHandlerFunc() http.HandlerFunc {

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if config.Instance.Refer != "" && r.Header.Get("referer") != "" && !strings.Contains(r.Header.Get("referer"),config.Instance.Refer){
HttpRequst.WithLabelValues(r.Header.Get("referer"), r.URL.Path).Inc()
HttpRequst.WithLabelValues("all", "all").Inc()
}

if containsDotDot(r.URL.Path) {
// Too many programs use r.URL.Path to construct the argument to
Expand Down
8 changes: 4 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ var ssl_last_cert_update time.Time = time.Now()
const ssl_cert_update_interval = 5 * time.Hour

var (
// HttpRequst = promauto.NewCounterVec(prometheus.CounterOpts{
// Name: "req_from_out",
// Help: "Number of HTTP requests received",
// }, []string{"referer", "path"})
HttpRequst = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "req_from_out",
Help: "Number of HTTP requests received",
}, []string{"referer", "path"})
ProxyAccess = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "proxy_access",
Help: "num proxy_access",
Expand Down

0 comments on commit 37fe016

Please sign in to comment.